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