1/* Copyright (c) 2006-2008 MySQL AB, 2009 Sun Microsystems, Inc.
2 Use is subject to license terms.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
17#include <my_global.h>
18#include <my_user.h>
19#include <m_string.h>
20#include <mysql_com.h>
21
22/*
23 Parse user value to user name and host name parts.
24
25 SYNOPSIS
26 user_id_str [IN] User value string (the source).
27 user_id_len [IN] Length of the user value.
28 user_name_str [OUT] Buffer to store user name part.
29 Must be not less than USERNAME_LENGTH + 1.
30 user_name_len [OUT] A place to store length of the user name part.
31 host_name_str [OUT] Buffer to store host name part.
32 Must be not less than HOSTNAME_LENGTH + 1.
33 host_name_len [OUT] A place to store length of the host name part.
34
35 RETURN
36 0 - if only a user was set, no '@' was found
37 1 - if both user and host were set
38*/
39
40int parse_user(const char *user_id_str, size_t user_id_len,
41 char *user_name_str, size_t *user_name_len,
42 char *host_name_str, size_t *host_name_len)
43{
44 char *p= strrchr(user_id_str, '@');
45
46 if (!p)
47 {
48 *user_name_len= user_id_len;
49 *host_name_len= 0;
50 }
51 else
52 {
53 *user_name_len= (uint) (p - user_id_str);
54 *host_name_len= (uint) (user_id_len - *user_name_len - 1);
55 }
56
57 if (*user_name_len > USERNAME_LENGTH)
58 *user_name_len= USERNAME_LENGTH;
59
60 if (*host_name_len > HOSTNAME_LENGTH)
61 *host_name_len= HOSTNAME_LENGTH;
62
63 memcpy(user_name_str, user_id_str, *user_name_len);
64 memcpy(host_name_str, p + 1, *host_name_len);
65
66 user_name_str[*user_name_len]= 0;
67 host_name_str[*host_name_len]= 0;
68
69 return p != NULL;
70}
71