2 * Copyright (c) 1999-2002 Sendmail, 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.
11 #pragma ident "%Z%%M% %I% %E% SMI"
14 SM_RCSID("@(#)$Id: strl.c,v 1.31 2002/01/20 01:41:25 gshapiro Exp $")
15 #include <sm/config.h>
16 #include <sm/string.h>
19 ** Notice: this file is used by libmilter. Please try to avoid
20 ** using libsm specific functions.
24 ** XXX the type of the length parameter has been changed
25 ** from size_t to ssize_t to avoid theoretical problems with negative
26 ** numbers passed into these functions.
27 ** The real solution to this problem is to make sure that this doesn't
28 ** happen, but for now we'll use this workaround.
32 ** SM_STRLCPY -- size bounded string copy
34 ** This is a bounds-checking variant of strcpy.
35 ** If size > 0, copy up to size-1 characters from the nul terminated
36 ** string src to dst, nul terminating the result. If size == 0,
37 ** the dst buffer is not modified.
38 ** Additional note: this function has been "tuned" to run fast and tested
39 ** as such (versus versions in some OS's libc).
41 ** The result is strlen(src). You can detect truncation (not all
42 ** of the characters in the source string were copied) using the
45 ** char *s, buf[BUFSIZ];
47 ** if (sm_strlcpy(buf, s, sizeof(buf)) >= sizeof(buf))
51 ** dst -- destination buffer
52 ** src -- source string
53 ** size -- size of destination buffer
60 sm_strlcpy(dst
, src
, size
)
62 register const char *src
;
69 for (i
= 0; i
< size
&& (dst
[i
] = src
[i
]) != 0; i
++)
75 return i
+ strlen(src
+ i
);
79 ** SM_STRLCAT -- size bounded string concatenation
81 ** This is a bounds-checking variant of strcat.
82 ** If strlen(dst) < size, then append at most size - strlen(dst) - 1
83 ** characters from the source string to the destination string,
84 ** nul terminating the result. Otherwise, dst is not modified.
86 ** The result is the initial length of dst + the length of src.
87 ** You can detect overflow (not all of the characters in the
88 ** source string were copied) using the following idiom:
90 ** char *s, buf[BUFSIZ];
92 ** if (sm_strlcat(buf, s, sizeof(buf)) >= sizeof(buf))
96 ** dst -- nul-terminated destination string buffer
97 ** src -- nul-terminated source string
98 ** size -- size of destination buffer
101 ** total length of the string tried to create
102 ** (= initial length of dst + length of src)
106 sm_strlcat(dst
, src
, size
)
108 register const char *src
;
111 register ssize_t i
, j
, o
;
115 return o
+ strlen(src
);
117 for (i
= 0, j
= o
; i
< size
&& (dst
[j
] = src
[i
]) != 0; i
++, j
++)
123 return j
+ strlen(src
+ i
);
126 ** SM_STRLCAT2 -- append two strings to dst obeying length and
129 ** strlcat2 will append at most len - strlen(dst) - 1 chars.
130 ** terminates with '\0' if len > 0
131 ** dst = dst "+" src1 "+" src2
132 ** use this instead of sm_strlcat(dst,src1); sm_strlcat(dst,src2);
136 ** dst -- "destination" string.
137 ** src1 -- "from" string 1.
138 ** src2 -- "from" string 2.
139 ** len -- max. length of "destination" string.
142 ** total length of the string tried to create
143 ** (= initial length of dst + length of src)
144 ** if this is greater than len then an overflow would have
150 sm_strlcat2(dst
, src1
, src2
, len
)
152 register const char *src1
;
153 register const char *src2
;
156 register ssize_t i
, j
, o
;
158 /* current size of dst */
161 /* max. size is less than current? */
163 return o
+ strlen(src1
) + strlen(src2
);
165 len
-= o
+ 1; /* space left in dst */
167 /* copy the first string; i: index in src1; j: index in dst */
168 for (i
= 0, j
= o
; i
< len
&& (dst
[j
] = src1
[i
]) != 0; i
++, j
++)
171 /* src1: end reached? */
174 /* no: terminate dst; there is space since i < len */
176 return j
+ strlen(src1
+ i
) + strlen(src2
);
179 len
-= i
; /* space left in dst */
181 /* copy the second string; i: index in src2; j: index in dst */
182 for (i
= 0; i
< len
&& (dst
[j
] = src2
[i
]) != 0; i
++, j
++)
184 dst
[j
] = '\0'; /* terminate dst; there is space since i < len */
188 return j
+ strlen(src2
+ i
);
192 ** SM_STRLCPYN -- concatenate n strings and assign the result to dst
193 ** while obeying length and '\0' terminate it
195 ** dst = src1 "+" src2 "+" ...
196 ** use this instead of sm_snprintf() for string values
197 ** and repeated sm_strlc*() calls for better speed.
200 ** dst -- "destination" string.
201 ** len -- max. length of "destination" string.
202 ** n -- number of strings
206 ** total length of the string tried to create
207 ** (= initial length of dst + length of src)
208 ** if this is greater than len then an overflow would have
214 sm_strlcpyn(char *dst
, ssize_t len
, int n
, ...)
216 sm_strlcpyn(dst
, len
, n
, va_alist
)
221 #endif /* __STDC__ */
223 register ssize_t i
, j
;
229 if (len
-- <= 0) /* This allows space for the terminating '\0' */
233 i
+= strlen(SM_VA_ARG(ap
, char *));
238 j
= 0; /* index in dst */
240 /* loop through all source strings */
243 str
= SM_VA_ARG(ap
, char *);
245 /* copy string; i: index in str; j: index in dst */
246 for (i
= 0; j
< len
&& (dst
[j
] = str
[i
]) != 0; i
++, j
++)
249 /* str: end reached? */
252 /* no: terminate dst; there is space since j < len */
254 j
+= strlen(str
+ i
);
256 j
+= strlen(SM_VA_ARG(ap
, char *));
263 dst
[j
] = '\0'; /* terminate dst; there is space since j < len */
269 ** SM_STRLAPP -- append string if it fits into buffer.
271 ** If size > 0, copy up to size-1 characters from the nul terminated
272 ** string src to dst, nul terminating the result. If size == 0,
273 ** the dst buffer is not modified.
275 ** This routine is useful for appending strings in a loop, e.g, instead of
277 ** for (ptr, ptr != NULL, ptr = next->ptr)
279 ** (void) sm_strlcpy(s, ptr->string, sizeof buf - (s - buf));
282 ** replace the loop body with:
283 ** if (!sm_strlapp(*s, ptr->string, sizeof buf - (s - buf)))
287 ** XXX interface isn't completely clear (yet), hence this code is
292 ** dst -- (pointer to) destination buffer
293 ** src -- source string
294 ** size -- size of destination buffer
297 ** true if strlen(src) < size
300 ** modifies dst if append succeeds (enough space).
304 sm_strlapp(dst
, src
, size
)
306 register const char *src
;
313 for (i
= 0; i
< size
&& ((*dst
)[i
] = src
[i
]) != '\0'; i
++)