"Initial commit to Gerrit"
[profile/ivi/gpsd.git] / strl.c
1 /*
2  * This file is Copyright (c) 2010 by the GPSD project
3  * BSD terms apply: see the file COPYING in the distribution root for details.
4  */
5 #include <stdio.h>
6 #ifndef S_SPLINT_S
7 #include <unistd.h>
8 #endif /* S_SPLINT_S */
9 #include <stdlib.h>
10 #include <string.h>
11 #include "gpsd_config.h"
12
13 #ifndef HAVE_STRLCAT
14 /*      $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $      */
15
16 /*
17  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
18  *
19  * Permission to use, copy, modify, and distribute this software for any
20  * purpose with or without fee is hereby granted, provided that the above
21  * copyright notice and this permission notice appear in all copies.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
24  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
26  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
28  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
29  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30  */
31
32 /*
33  * Appends src to string dst of size siz (unlike strncat, siz is the
34  * full size of dst, not space left).  At most siz-1 characters
35  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
36  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
37  * If retval >= siz, truncation occurred.
38  */
39 /*@ -usedef -mustdefine @*/
40 size_t strlcat(char *dst, const char *src, size_t siz)
41 {
42     char *d = dst;
43     const char *s = src;
44     size_t n = siz;
45     size_t dlen;
46
47     /* Find the end of dst and adjust bytes left but don't go past end */
48     while (n-- != 0 && *d != '\0')
49         d++;
50     dlen = (size_t) (d - dst);
51     n = siz - dlen;
52
53     if (n == 0)
54         return (dlen + strlen(s));
55     while (*s != '\0') {
56         if (n != 1) {
57             *d++ = *s;
58             n--;
59         }
60         s++;
61     }
62     *d = '\0';
63
64     return (dlen + (s - src));  /* count does not include NUL */
65 }
66
67 /*@ +usedef +mustdefine @*/
68 #endif /* HAVE_STRLCAT */
69
70 #ifndef HAVE_STRLCPY
71 /*      $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $    */
72
73 /*
74  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
75  *
76  * Permission to use, copy, modify, and distribute this software for any
77  * purpose with or without fee is hereby granted, provided that the above
78  * copyright notice and this permission notice appear in all copies.
79  *
80  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
81  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
82  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
83  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
84  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
85  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
86  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
87  */
88
89 /*
90  * Copy src to string dst of size siz.  At most siz-1 characters
91  * will be copied.  Always NUL terminates (unless siz == 0).
92  * Returns strlen(src); if retval >= siz, truncation occurred.
93  */
94 size_t strlcpy(char *dst, const char *src, size_t siz)
95 {
96     char *d = dst;
97     const char *s = src;
98     size_t n = siz;
99
100     /* Copy as many bytes as will fit */
101     if (n != 0) {
102         while (--n != 0) {
103             if ((*d++ = *s++) == '\0')
104                 break;
105         }
106     }
107
108     /* Not enough room in dst, add NUL and traverse rest of src */
109     if (n == 0) {
110         if (siz != 0)
111             *d = '\0';          /* NUL-terminate dst */
112         while (*s++ != '\0')
113             continue;
114     }
115
116     return ((size_t) (s - src - 1));    /* count does not include NUL */
117 }
118 #endif /* HAVE_STRLCPY */