/src/mysql-server/mysys/strmake.cc
Line | Count | Source |
1 | | /* Copyright (c) 2000, 2025, Oracle and/or its affiliates. |
2 | | |
3 | | This program is free software; you can redistribute it and/or modify |
4 | | it under the terms of the GNU General Public License, version 2.0, |
5 | | as published by the Free Software Foundation. |
6 | | |
7 | | This program is designed to work with certain software (including |
8 | | but not limited to OpenSSL) that is licensed under separate terms, |
9 | | as designated in a particular file or component or in included license |
10 | | documentation. The authors of MySQL hereby grant you an additional |
11 | | permission to link the program and your derivative works with the |
12 | | separately licensed software that they have either included with |
13 | | the program or referenced in the documentation. |
14 | | |
15 | | Without limiting anything contained in the foregoing, this file, |
16 | | which is part of C Driver for MySQL (Connector/C), is also subject to the |
17 | | Universal FOSS Exception, version 1.0, a copy of which can be found at |
18 | | http://oss.oracle.com/licenses/universal-foss-exception. |
19 | | |
20 | | This program is distributed in the hope that it will be useful, |
21 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | | GNU General Public License, version 2.0, for more details. |
24 | | |
25 | | You should have received a copy of the GNU General Public License |
26 | | along with this program; if not, write to the Free Software |
27 | | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
28 | | |
29 | | /* File : strmake.c |
30 | | Author : Michael Widenius |
31 | | Updated: 20 Jul 1984 |
32 | | Defines: strmake() |
33 | | |
34 | | strmake(dst,src,length) moves length characters, or until end, of src to |
35 | | dst and appends a closing NUL to dst. |
36 | | Note that if strlen(src) >= length then dst[length] will be set to \0 |
37 | | strmake() returns pointer to closing null |
38 | | */ |
39 | | |
40 | | #include "strmake.h" |
41 | | |
42 | 2 | char *strmake(char *dst, const char *src, size_t length) { |
43 | | #ifdef EXTRA_DEBUG |
44 | | /* |
45 | | 'length' is the maximum length of the string; the buffer needs |
46 | | to be one character larger to accommodate the terminating '\0'. |
47 | | This is easy to get wrong, so we make sure we write to the |
48 | | entire length of the buffer to identify incorrect buffer-sizes. |
49 | | We only initialise the "unused" part of the buffer here, a) for |
50 | | efficiency, and b) because dst==src is allowed, so initialising |
51 | | the entire buffer would overwrite the source-string. Also, we |
52 | | write a character rather than '\0' as this makes spotting these |
53 | | problems in the results easier. |
54 | | */ |
55 | | unsigned n = 0; |
56 | | while (n < length && src[n++]) |
57 | | ; |
58 | | memset(dst + n, (int)'Z', length - n + 1); |
59 | | #endif |
60 | | |
61 | 65 | while (length--) |
62 | 64 | if (!(*dst++ = *src++)) return dst - 1; |
63 | 1 | *dst = 0; |
64 | 1 | return dst; |
65 | 2 | } |