MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_user.c
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_user.h>
18 #include <m_string.h>
19 #include <mysql_com.h>
20 
21 /*
22  Parse user value to user name and host name parts.
23 
24  SYNOPSIS
25  user_id_str [IN] User value string (the source).
26  user_id_len [IN] Length of the user value.
27  user_name_str [OUT] Buffer to store user name part.
28  Must be not less than USERNAME_LENGTH + 1.
29  user_name_len [OUT] A place to store length of the user name part.
30  host_name_str [OUT] Buffer to store host name part.
31  Must be not less than HOSTNAME_LENGTH + 1.
32  host_name_len [OUT] A place to store length of the host name part.
33 */
34 
35 void parse_user(const char *user_id_str, size_t user_id_len,
36  char *user_name_str, size_t *user_name_len,
37  char *host_name_str, size_t *host_name_len)
38 {
39  char *p= strrchr(user_id_str, '@');
40 
41  if (!p)
42  {
43  *user_name_len= 0;
44  *host_name_len= 0;
45  }
46  else
47  {
48  *user_name_len= (uint) (p - user_id_str);
49  *host_name_len= (uint) (user_id_len - *user_name_len - 1);
50 
51  if (*user_name_len > USERNAME_LENGTH)
52  *user_name_len= USERNAME_LENGTH;
53 
54  if (*host_name_len > HOSTNAME_LENGTH)
55  *host_name_len= HOSTNAME_LENGTH;
56 
57  memcpy(user_name_str, user_id_str, *user_name_len);
58  memcpy(host_name_str, p + 1, *host_name_len);
59  }
60 
61  user_name_str[*user_name_len]= 0;
62  host_name_str[*host_name_len]= 0;
63 }