MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
strmake.c
1 /* Copyright (c) 2000, 2001, 2003, 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 /* File : strmake.c
18  Author : Michael Widenius
19  Updated: 20 Jul 1984
20  Defines: strmake()
21 
22  strmake(dst,src,length) moves length characters, or until end, of src to
23  dst and appends a closing NUL to dst.
24  Note that if strlen(src) >= length then dst[length] will be set to \0
25  strmake() returns pointer to closing null
26 */
27 
28 #include <my_global.h>
29 #include "m_string.h"
30 
31 char *strmake(register char *dst, register const char *src, size_t length)
32 {
33 #ifdef EXTRA_DEBUG
34  /*
35  'length' is the maximum length of the string; the buffer needs
36  to be one character larger to accomodate the terminating '\0'.
37  This is easy to get wrong, so we make sure we write to the
38  entire length of the buffer to identify incorrect buffer-sizes.
39  We only initialise the "unused" part of the buffer here, a) for
40  efficiency, and b) because dst==src is allowed, so initialising
41  the entire buffer would overwrite the source-string. Also, we
42  write a character rather than '\0' as this makes spotting these
43  problems in the results easier.
44  */
45  uint n= 0;
46  while (n < length && src[n++]);
47  memset(dst + n, (int) 'Z', length - n + 1);
48 #endif
49 
50  while (length--)
51  if (! (*dst++ = *src++))
52  return dst-1;
53  *dst=0;
54  return dst;
55 }