Annotation of parser3/src/lib/ltdl/lt__strl.c, revision 1.2
1.1 moko 1: /* lt__strl.c -- size-bounded string copying and concatenation
2:
1.2 ! moko 3: Copyright (C) 2004, 2011-2019, 2021-2024 Free Software Foundation,
! 4: Inc.
1.1 moko 5: Written by Bob Friesenhahn, 2004
6:
7: NOTE: The canonical source of this file is maintained with the
8: GNU Libtool package. Report bugs to bug-libtool@gnu.org.
9:
10: GNU Libltdl is free software; you can redistribute it and/or
11: modify it under the terms of the GNU Lesser General Public
12: License as published by the Free Software Foundation; either
13: version 2 of the License, or (at your option) any later version.
14:
15: As a special exception to the GNU Lesser General Public License,
16: if you distribute this file as part of a program or library that
17: is built using GNU Libtool, you may include this file under the
18: same distribution terms that you use for the rest of that program.
19:
20: GNU Libltdl 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 Lesser General Public License for more details.
24:
25: You should have received a copy of the GNU Lesser General Public
1.2 ! moko 26: License along with GNU Libltdl. If not, see <https://www.gnu.org/licenses/>.
1.1 moko 27: */
28:
29: #include <assert.h>
30: #include <string.h>
31:
32: #include "lt__strl.h"
33:
34: /*
35: lt_strlcat appends the NULL-terminated string src to the end of dst.
36: It will append at most dstsize - strlen(dst) - 1 bytes,
1.2 ! moko 37: NULL-terminating the result. The total length of the string that
1.1 moko 38: would have been created given sufficient buffer size (may be longer
1.2 ! moko 39: than dstsize) is returned. This function substitutes for strlcat(),
1.1 moko 40: which is available under NetBSD, FreeBSD and Solaris 9.
41:
42: Buffer overflow can be checked as follows:
43:
44: if (lt_strlcat(dst, src, dstsize) >= dstsize)
45: return -1;
46: */
1.2 ! moko 47: #if !defined HAVE_STRLCAT
1.1 moko 48: size_t
49: lt_strlcat(char *dst, const char *src, const size_t dstsize)
50: {
51: size_t length;
52: char *p;
53: const char *q;
54:
55: assert(dst != NULL);
56: assert(src != (const char *) NULL);
57: assert(dstsize >= 1);
58:
59: length=strlen(dst);
60:
61: /*
62: Copy remaining characters from src while constraining length to
63: size - 1.
64: */
65: for ( p = dst + length, q = src;
1.2 ! moko 66: (*q != 0) && (length < dstsize - 1);
1.1 moko 67: length++, p++, q++ )
68: *p = *q;
69:
70: dst[length]='\0';
71:
72: /*
73: Add remaining length of src to length.
74: */
75: while (*q++)
76: length++;
77:
78: return length;
79: }
1.2 ! moko 80: #endif /* !defined HAVE_STRLCAT */
1.1 moko 81:
82: /*
83: lt_strlcpy copies up to dstsize - 1 characters from the NULL-terminated
84: string src to dst, NULL-terminating the result. The total length of
1.2 ! moko 85: the string that would have been created given sufficient buffer
1.1 moko 86: size (may be longer than dstsize) is returned. This function
1.2 ! moko 87: substitutes for strlcpy(), which is available under OpenBSD, FreeBSD
1.1 moko 88: and Solaris 9.
89:
90: Buffer overflow can be checked as follows:
91:
92: if (lt_strlcpy(dst, src, dstsize) >= dstsize)
93: return -1;
94: */
1.2 ! moko 95: #if !defined HAVE_STRLCPY
1.1 moko 96: size_t
97: lt_strlcpy(char *dst, const char *src, const size_t dstsize)
98: {
99: size_t length=0;
100: char *p;
101: const char *q;
102:
103: assert(dst != NULL);
104: assert(src != (const char *) NULL);
105: assert(dstsize >= 1);
106:
107: /*
108: Copy src to dst within bounds of size-1.
109: */
1.2 ! moko 110: for ( p=dst, q=src, length=0;
! 111: (*q != 0) && (length < dstsize-1);
1.1 moko 112: length++, p++, q++ )
113: *p = *q;
114:
115: dst[length]='\0';
116:
117: /*
118: Add remaining length of src to length.
119: */
120: while (*q++)
121: length++;
122:
123: return length;
124: }
1.2 ! moko 125: #endif /* !defined HAVE_STRLCPY */
E-mail: