ze-filter  (ze-filter-0.8.0-develop-180218)
zeString.c
Go to the documentation of this file.
1 /*
2  *
3  * ze-filter - Mail Server Filter for sendmail
4  *
5  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
6  *
7  * Auteur : Jose Marcio Martins da Cruz
8  * jose.marcio.mc@gmail.com
9  *
10  * Historique :
11  * Creation : Wed Nov 29 10:19:07 CET 2017
12  *
13  * This program is free software - GPL v2.,
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  */
20 
21 /*
22  * Copyright (c) 1999-2002, 2004, 2005 Sendmail, Inc. and its suppliers.
23  * All rights reserved.
24  *
25  * By using this file, you agree to the terms and conditions set
26  * forth in the LICENSE file which can be found at the top level of
27  * the sendmail distribution.
28  *
29  */
30 
31 #include <ze-sys.h>
32 #include <libze.h>
33 #include <zeString.h>
34 
35 /* ****************************************************************************
36  * *
37  * *
38  **************************************************************************** */
39 
40 /*
41 ** STRLCAT -- size bounded string concatenation
42 **
43 ** This is a bounds-checking variant of strcat.
44 ** If strlen(dst) < size, then append at most size - strlen(dst) - 1
45 ** characters from the source string to the destination string,
46 ** nul terminating the result. Otherwise, dst is not modified.
47 **
48 ** The result is the initial length of dst + the length of src.
49 ** You can detect overflow (not all of the characters in the
50 ** source string were copied) using the following idiom:
51 **
52 ** char *s, buf[BUFSIZ];
53 ** ...
54 ** if (strlcat(buf, s, sizeof(buf)) >= sizeof(buf))
55 ** goto overflow;
56 **
57 ** Parameters:
58 ** dst -- nul-terminated destination string buffer
59 ** src -- nul-terminated source string
60 ** size -- size of destination buffer
61 **
62 ** Returns:
63 ** total length of the string tried to create
64 ** (= initial length of dst + length of src)
65 */
66 
67 size_t
68 zeStrlCat(char *dst, const char *src, size_t size)
69 {
70  size_t i, j, o;
71 
72  o = strlen(dst);
73  if (size < o + 1)
74  return o + strlen(src);
75  size -= o + 1;
76  for (i = 0, j = o; i < size && (dst[j] = src[i]) != 0; i++, j++)
77  continue;
78  dst[j] = '\0';
79  if (src[i] == '\0')
80  return j;
81  else
82  return j + strlen(src + i);
83 }
84 
85 /*
86 ** STRLCPY -- size bounded string copy
87 **
88 ** This is a bounds-checking variant of strcpy.
89 ** If size > 0, copy up to size-1 characters from the nul terminated
90 ** string src to dst, nul terminating the result. If size == 0,
91 ** the dst buffer is not modified.
92 ** Additional note: this function has been "tuned" to run fast and tested
93 ** as such (versus versions in some OS's libc).
94 **
95 ** The result is strlen(src). You can detect truncation (not all
96 ** of the characters in the source string were copied) using the
97 ** following idiom:
98 **
99 ** char *s, buf[BUFSIZ];
100 ** ...
101 ** if (strlcpy(buf, s, sizeof(buf)) >= sizeof(buf))
102 ** goto overflow;
103 **
104 ** Parameters:
105 ** dst -- destination buffer
106 ** src -- source string
107 ** size -- size of destination buffer
108 **
109 ** Returns:
110 ** strlen(src)
111 */
112 
113 size_t
114 zeStrlCpy(char *dst, const char *src, size_t size)
115 {
116  size_t i;
117 
118  if (size-- <= 0)
119  return strlen(src);
120  for (i = 0; i < size && (dst[i] = src[i]) != 0; i++)
121  continue;
122  dst[i] = '\0';
123  if (src[i] == '\0')
124  return i;
125  else
126  return i + strlen(src + i);
127 }
size_t zeStrlCpy(char *dst, const char *src, size_t size)
Definition: zeString.c:114
size_t zeStrlCat(char *dst, const char *src, size_t size)
Definition: zeString.c:68