/src/server/strings/strmake.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. |
2 | | Copyright (c) 2009-2011, Monty Program Ab |
3 | | Use is subject to license terms. |
4 | | Copyright (c) 2009-2011, Monty Program Ab |
5 | | |
6 | | This program is free software; you can redistribute it and/or modify |
7 | | it under the terms of the GNU General Public License as published by |
8 | | the Free Software Foundation; version 2 of the License. |
9 | | |
10 | | This program is distributed in the hope that it will be useful, |
11 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | GNU General Public License for more details. |
14 | | |
15 | | You should have received a copy of the GNU General Public License |
16 | | along with this program; if not, write to the Free Software |
17 | | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */ |
18 | | |
19 | | /* File : strmake.c |
20 | | Author : Michael Widenius |
21 | | Updated: 20 Jul 1984 |
22 | | Defines: strmake() |
23 | | |
24 | | strmake(dst,src,length) moves length characters, or until end, of src to |
25 | | dst and appends a closing NUL to dst. |
26 | | Note that if strlen(src) >= length then dst[length] will be set to \0 |
27 | | strmake() returns pointer to closing null |
28 | | */ |
29 | | |
30 | | #include "strings_def.h" |
31 | | |
32 | | char *strmake(register char *dst, register const char *src, size_t length) |
33 | 0 | { |
34 | 0 | while (length--) |
35 | 0 | { |
36 | 0 | if (! (*dst++ = *src++)) |
37 | 0 | { |
38 | | #ifdef EXTRA_DEBUG |
39 | | /* |
40 | | 'length' is the maximum length of the string; the buffer needs |
41 | | to be one character larger to accommodate the terminating |
42 | | '\0'. This is easy to get wrong, so we make sure we write to |
43 | | the entire length of the buffer to identify incorrect |
44 | | buffer-sizes. We only initialism the "unused" part of the |
45 | | buffer here, a) for efficiency, and b) because dst==src is |
46 | | allowed, so initializing the entire buffer would overwrite the |
47 | | source-string. Also, we write a character rather than '\0' as |
48 | | this makes spotting these problems in the results easier. |
49 | | |
50 | | If we are using purify/valgrind, we only set one character at |
51 | | end to be able to detect also wrong accesses after the end of |
52 | | dst. |
53 | | */ |
54 | | if (length) |
55 | | { |
56 | | #ifdef HAVE_valgrind |
57 | | dst[length-1]= 'Z'; |
58 | | #else |
59 | | bfill(dst, length-1, (int) 'Z'); |
60 | | #endif /* HAVE_valgrind */ |
61 | | } |
62 | | #endif /* EXTRA_DEBUG */ |
63 | 0 | return dst-1; |
64 | 0 | } |
65 | 0 | } |
66 | 0 | *dst=0; |
67 | 0 | return dst; |
68 | 0 | } |