]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/libntp/strl_obsd.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / ntp / libntp / strl_obsd.c
1 /* 
2  * Why use strlcpy()/strlcat() instead of standard strncpy()/strncat()?
3  * To reduce likelihood of bugs and avoid wasteful zero fills.  See:
4  * http://www.gratisoft.us/todd/papers/strlcpy.html
5  */
6
7 /*      $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $    */
8
9 /*
10  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
11  *
12  * Permission to use, copy, modify, and distribute this software for any
13  * purpose with or without fee is hereby granted, provided that the above
14  * copyright notice and this permission notice appear in all copies.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  */
24
25 #include <config.h>             /* + marks local changes */
26 #ifdef HAVE_SYS_TYPES_H         /* + */
27 #include <sys/types.h>
28 #endif                          /* + */
29 #include <string.h>
30
31 #include "ntp_stdlib.h"         /* + strlcpy, strlcat prototypes */
32
33 #ifndef HAVE_STRLCPY            /* + */
34 /*
35  * Copy src to string dst of size siz.  At most siz-1 characters
36  * will be copied.  Always NUL terminates (unless siz == 0).
37  * Returns strlen(src); if retval >= siz, truncation occurred.
38  */
39 size_t
40 strlcpy(char *dst, const char *src, size_t siz)
41 {
42         char *d = dst;
43         const char *s = src;
44         size_t n = siz;
45
46         /* Copy as many bytes as will fit */
47         if (n != 0) {
48                 while (--n != 0) {
49                         if ((*d++ = *s++) == '\0')
50                                 break;
51                 }
52         }
53
54         /* Not enough room in dst, add NUL and traverse rest of src */
55         if (n == 0) {
56                 if (siz != 0)
57                         *d = '\0';              /* NUL-terminate dst */
58                 while (*s++)
59                         ;
60         }
61
62         return(s - src - 1);    /* count does not include NUL */
63 }
64 #endif                          /* + */
65
66
67 /*      $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $      */
68
69 /*
70  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
71  *
72  * Permission to use, copy, modify, and distribute this software for any
73  * purpose with or without fee is hereby granted, provided that the above
74  * copyright notice and this permission notice appear in all copies.
75  *
76  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
77  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
78  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
79  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
80  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
81  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
82  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
83  */
84
85 /* #include <sys/types.h> */    /* + */
86 /* #include <string.h> */       /* + */
87
88 #ifndef HAVE_STRLCAT            /* + */
89 /*
90  * Appends src to string dst of size siz (unlike strncat, siz is the
91  * full size of dst, not space left).  At most siz-1 characters
92  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
93  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
94  * If retval >= siz, truncation occurred.
95  */
96 size_t
97 strlcat(char *dst, const char *src, size_t siz)
98 {
99         char *d = dst;
100         const char *s = src;
101         size_t n = siz;
102         size_t dlen;
103
104         /* Find the end of dst and adjust bytes left but don't go past end */
105         while (n-- != 0 && *d != '\0')
106                 d++;
107         dlen = d - dst;
108         n = siz - dlen;
109
110         if (n == 0)
111                 return(dlen + strlen(s));
112         while (*s != '\0') {
113                 if (n != 1) {
114                         *d++ = *s;
115                         n--;
116                 }
117                 s++;
118         }
119         *d = '\0';
120
121         return(dlen + (s - src));       /* count does not include NUL */
122 }
123 #endif                          /* + */