1 |
/* |
2 |
* Copyright (c) 2002 Andy Smith |
3 |
* Copyright (c) 2014-2021 ircd-hybrid development team |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* it under the terms of the GNU General Public License as published by |
7 |
* the Free Software Foundation; either version 2 of the License, or |
8 |
* (at your option) any later version. |
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 Street, Fifth Floor, Boston, MA 02110-1301 |
18 |
* USA |
19 |
*/ |
20 |
|
21 |
#include "setup.h" |
22 |
|
23 |
#include <string.h> |
24 |
|
25 |
#include "compat.h" |
26 |
|
27 |
/* $OpenBSD: strlcat.c,v 1.18 2016/10/16 17:37:39 dtucker Exp $ */ |
28 |
/* $OpenBSD: strlcpy.c,v 1.15 2016/10/16 17:37:39 dtucker Exp $ */ |
29 |
/* |
30 |
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> |
31 |
* |
32 |
* Permission to use, copy, modify, and distribute this software for any |
33 |
* purpose with or without fee is hereby granted, provided that the above |
34 |
* copyright notice and this permission notice appear in all copies. |
35 |
* |
36 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
37 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
38 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
39 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
40 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
41 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
42 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
43 |
*/ |
44 |
|
45 |
#ifndef HAVE_STRLCAT |
46 |
/* |
47 |
* Appends src to string dst of size dsize (unlike strncat, dsize is the |
48 |
* full size of dst, not space left). At most dsize-1 characters |
49 |
* will be copied. Always NUL terminates (unless dsize <= strlen(dst)). |
50 |
* Returns strlen(src) + MIN(dsize, strlen(initial dst)). |
51 |
* If retval >= dsize, truncation occurred. |
52 |
*/ |
53 |
size_t |
54 |
strlcat(char *dst, const char *src, size_t dsize) |
55 |
{ |
56 |
const char *odst = dst; |
57 |
const char *osrc = src; |
58 |
size_t n = dsize; |
59 |
size_t dlen; |
60 |
|
61 |
/* Find the end of dst and adjust bytes left but don't go past end. */ |
62 |
while (n-- != 0 && *dst != '\0') |
63 |
dst++; |
64 |
|
65 |
dlen = dst - odst; |
66 |
n = dsize - dlen; |
67 |
|
68 |
if (n-- == 0) |
69 |
return dlen + strlen(src); |
70 |
|
71 |
while (*src != '\0') |
72 |
{ |
73 |
if (n != 0) |
74 |
{ |
75 |
*dst++ = *src; |
76 |
n--; |
77 |
} |
78 |
|
79 |
src++; |
80 |
} |
81 |
|
82 |
*dst = '\0'; |
83 |
|
84 |
return dlen + (src - osrc); /* count does not include NUL */ |
85 |
} |
86 |
#endif |
87 |
|
88 |
#ifndef HAVE_STRLCPY |
89 |
/* |
90 |
* Copy string src to buffer dst of size dsize. At most dsize-1 |
91 |
* chars will be copied. Always NUL terminates (unless dsize == 0). |
92 |
* Returns strlen(src); if retval >= dsize, truncation occurred. |
93 |
*/ |
94 |
size_t |
95 |
strlcpy(char *dst, const char *src, size_t dsize) |
96 |
{ |
97 |
const char *osrc = src; |
98 |
size_t nleft = dsize; |
99 |
|
100 |
/* Copy as many bytes as will fit. */ |
101 |
if (nleft != 0) |
102 |
{ |
103 |
while (--nleft != 0) |
104 |
{ |
105 |
if ((*dst++ = *src++) == '\0') |
106 |
break; |
107 |
} |
108 |
} |
109 |
|
110 |
/* Not enough room in dst, add NUL and traverse rest of src. */ |
111 |
if (nleft == 0) |
112 |
{ |
113 |
if (dsize != 0) |
114 |
*dst = '\0'; /* NUL-terminate dst */ |
115 |
|
116 |
while (*src++) |
117 |
; |
118 |
} |
119 |
|
120 |
return src - osrc - 1; /* count does not include NUL */ |
121 |
} |
122 |
#endif |