Merge tag 'upstream/3.0.717'
[darkstat-debian] / bsd.c
1 /* darkstat 3
2 * copyright (c) 2001-2011 Emil Mikulic.
3 *
4 * bsd.c: *BSD compatibility.
5 *
6 * Permission to use, copy, modify, and distribute this file for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include "bsd.h"
20 #include "cdefs.h"
21 #include "config.h"
22
23 #include <string.h> /* for strlen */
24
25 /* strlcpy() and strlcat() are derived from:
26 *
27 * $OpenBSD: strlcpy.c,v 1.4
28 * $FreeBSD: src/lib/libc/string/strlcpy.c,v 1.8
29 *
30 * $OpenBSD: strlcat.c,v 1.2
31 * $FreeBSD: src/lib/libc/string/strlcat.c,v 1.10
32 *
33 * under the following license:
34 *
35 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. The name of the author may not be used to endorse or promote products
47 * derived from this software without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
50 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
51 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
52 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
55 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
56 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
57 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
58 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 #ifndef HAVE_STRLCPY
62 /*
63 * Copy src to string dst of size siz. At most siz-1 characters
64 * will be copied. Always NUL terminates (unless siz == 0).
65 * Returns strlen(src); if retval >= siz, truncation occurred.
66 */
67 size_t
68 strlcpy(char * restrict dst, const char * restrict src, const size_t siz)
69 {
70 char *d = dst;
71 const char *s = src;
72 size_t n = siz;
73
74 /* Copy as many bytes as will fit */
75 if (n != 0 && --n != 0) {
76 do {
77 if ((*d++ = *s++) == 0)
78 break;
79 } while (--n != 0);
80 }
81
82 /* Not enough room in dst, add NUL and traverse rest of src */
83 if (n == 0) {
84 if (siz != 0)
85 *d = '\0'; /* NUL-terminate dst */
86 while (*s++)
87 ;
88 }
89
90 return (size_t)(s - src - 1); /* count does not include NUL */
91 }
92 #endif
93
94 #ifndef HAVE_STRLCAT
95 /*
96 * Appends src to string dst of size siz (unlike strncat, siz is the
97 * full size of dst, not space left). At most siz-1 characters
98 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
99 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
100 * If retval >= siz, truncation occurred.
101 */
102 size_t
103 strlcat(char * restrict dst, const char * restrict src, const size_t siz)
104 {
105 char *d = dst;
106 const char *s = src;
107 size_t n = siz;
108 size_t dlen;
109
110 /* Find the end of dst and adjust bytes left but don't go past end */
111 while (n-- != 0 && *d != '\0')
112 d++;
113 dlen = (size_t)(d - dst);
114 n = siz - dlen;
115
116 if (n == 0)
117 return(dlen + strlen(s));
118 while (*s != '\0') {
119 if (n != 1) {
120 *d++ = *s;
121 n--;
122 }
123 s++;
124 }
125 *d = '\0';
126
127 return (dlen + (size_t)(s - src)); /* count does not include NUL */
128 }
129 #endif
130
131 /* vim:set ts=3 sw=3 tw=78 expandtab: */