2 * Copyright (c) 1999-2002 Proofpoint, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
12 SM_RCSID("@(#)$Id: strl.c,v 1.32 2013-11-22 20:51:43 ca Exp $")
13 #include <sm/config.h>
14 #include <sm/string.h>
17 ** Notice: this file is used by libmilter. Please try to avoid
18 ** using libsm specific functions.
22 ** XXX the type of the length parameter has been changed
23 ** from size_t to ssize_t to avoid theoretical problems with negative
24 ** numbers passed into these functions.
25 ** The real solution to this problem is to make sure that this doesn't
26 ** happen, but for now we'll use this workaround.
30 ** SM_STRLCPY -- size bounded string copy
32 ** This is a bounds-checking variant of strcpy.
33 ** If size > 0, copy up to size-1 characters from the nul terminated
34 ** string src to dst, nul terminating the result. If size == 0,
35 ** the dst buffer is not modified.
36 ** Additional note: this function has been "tuned" to run fast and tested
37 ** as such (versus versions in some OS's libc).
39 ** The result is strlen(src). You can detect truncation (not all
40 ** of the characters in the source string were copied) using the
43 ** char *s, buf[BUFSIZ];
45 ** if (sm_strlcpy(buf, s, sizeof(buf)) >= sizeof(buf))
49 ** dst -- destination buffer
50 ** src -- source string
51 ** size -- size of destination buffer
58 sm_strlcpy(dst, src, size)
60 register const char *src;
67 for (i = 0; i < size && (dst[i] = src[i]) != 0; i++)
73 return i + strlen(src + i);
77 ** SM_STRLCAT -- size bounded string concatenation
79 ** This is a bounds-checking variant of strcat.
80 ** If strlen(dst) < size, then append at most size - strlen(dst) - 1
81 ** characters from the source string to the destination string,
82 ** nul terminating the result. Otherwise, dst is not modified.
84 ** The result is the initial length of dst + the length of src.
85 ** You can detect overflow (not all of the characters in the
86 ** source string were copied) using the following idiom:
88 ** char *s, buf[BUFSIZ];
90 ** if (sm_strlcat(buf, s, sizeof(buf)) >= sizeof(buf))
94 ** dst -- nul-terminated destination string buffer
95 ** src -- nul-terminated source string
96 ** size -- size of destination buffer
99 ** total length of the string tried to create
100 ** (= initial length of dst + length of src)
104 sm_strlcat(dst, src, size)
106 register const char *src;
109 register ssize_t i, j, o;
113 return o + strlen(src);
115 for (i = 0, j = o; i < size && (dst[j] = src[i]) != 0; i++, j++)
121 return j + strlen(src + i);
124 ** SM_STRLCAT2 -- append two strings to dst obeying length and
127 ** strlcat2 will append at most len - strlen(dst) - 1 chars.
128 ** terminates with '\0' if len > 0
129 ** dst = dst "+" src1 "+" src2
130 ** use this instead of sm_strlcat(dst,src1); sm_strlcat(dst,src2);
134 ** dst -- "destination" string.
135 ** src1 -- "from" string 1.
136 ** src2 -- "from" string 2.
137 ** len -- max. length of "destination" string.
140 ** total length of the string tried to create
141 ** (= initial length of dst + length of src)
142 ** if this is greater than len then an overflow would have
148 sm_strlcat2(dst, src1, src2, len)
150 register const char *src1;
151 register const char *src2;
154 register ssize_t i, j, o;
156 /* current size of dst */
159 /* max. size is less than current? */
161 return o + strlen(src1) + strlen(src2);
163 len -= o + 1; /* space left in dst */
165 /* copy the first string; i: index in src1; j: index in dst */
166 for (i = 0, j = o; i < len && (dst[j] = src1[i]) != 0; i++, j++)
169 /* src1: end reached? */
172 /* no: terminate dst; there is space since i < len */
174 return j + strlen(src1 + i) + strlen(src2);
177 len -= i; /* space left in dst */
179 /* copy the second string; i: index in src2; j: index in dst */
180 for (i = 0; i < len && (dst[j] = src2[i]) != 0; i++, j++)
182 dst[j] = '\0'; /* terminate dst; there is space since i < len */
186 return j + strlen(src2 + i);
190 ** SM_STRLCPYN -- concatenate n strings and assign the result to dst
191 ** while obeying length and '\0' terminate it
193 ** dst = src1 "+" src2 "+" ...
194 ** use this instead of sm_snprintf() for string values
195 ** and repeated sm_strlc*() calls for better speed.
198 ** dst -- "destination" string.
199 ** len -- max. length of "destination" string.
200 ** n -- number of strings
204 ** total length of the string tried to create
205 ** (= initial length of dst + length of src)
206 ** if this is greater than len then an overflow would have
212 sm_strlcpyn(char *dst, ssize_t len, int n, ...)
214 sm_strlcpyn(dst, len, n, va_alist)
219 #endif /* __STDC__ */
221 register ssize_t i, j;
227 if (len-- <= 0) /* This allows space for the terminating '\0' */
231 i += strlen(SM_VA_ARG(ap, char *));
236 j = 0; /* index in dst */
238 /* loop through all source strings */
241 str = SM_VA_ARG(ap, char *);
243 /* copy string; i: index in str; j: index in dst */
244 for (i = 0; j < len && (dst[j] = str[i]) != 0; i++, j++)
247 /* str: end reached? */
250 /* no: terminate dst; there is space since j < len */
252 j += strlen(str + i);
254 j += strlen(SM_VA_ARG(ap, char *));
261 dst[j] = '\0'; /* terminate dst; there is space since j < len */
267 ** SM_STRLAPP -- append string if it fits into buffer.
269 ** If size > 0, copy up to size-1 characters from the nul terminated
270 ** string src to dst, nul terminating the result. If size == 0,
271 ** the dst buffer is not modified.
273 ** This routine is useful for appending strings in a loop, e.g, instead of
275 ** for (ptr, ptr != NULL, ptr = next->ptr)
277 ** (void) sm_strlcpy(s, ptr->string, sizeof buf - (s - buf));
280 ** replace the loop body with:
281 ** if (!sm_strlapp(*s, ptr->string, sizeof buf - (s - buf)))
285 ** XXX interface isn't completely clear (yet), hence this code is
290 ** dst -- (pointer to) destination buffer
291 ** src -- source string
292 ** size -- size of destination buffer
295 ** true if strlen(src) < size
298 ** modifies dst if append succeeds (enough space).
302 sm_strlapp(dst, src, size)
304 register const char *src;
311 for (i = 0; i < size && ((*dst)[i] = src[i]) != '\0'; i++)