Blob


1 #include "config.h"
2 #if !HAVE_GETPROGNAME
3 /*
4 * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
5 * Copyright (c) 2017 Kristaps Dzonsons <kristaps@bsd.lv>
6 * Copyright (c) 2020 Stephen Gregoratto <dev@sgregoratto.me>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
17 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
18 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
21 #include <sys/types.h>
23 #include <errno.h>
25 #if HAVE_GETEXECNAME
26 #include <stdlib.h>
27 const char *
28 getprogname(void)
29 {
30 return getexecname();
31 }
32 #elif HAVE_PROGRAM_INVOCATION_SHORT_NAME
33 const char *
34 getprogname(void)
35 {
36 return (program_invocation_short_name);
37 }
38 #elif HAVE___PROGNAME
39 const char *
40 getprogname(void)
41 {
42 extern char *__progname;
44 return (__progname);
45 }
46 #else
47 #warning No getprogname available.
48 const char *
49 getprogname(void)
50 {
51 return ("lstun");
52 }
53 #endif
54 #endif /* !HAVE_GETPROGNAME */
55 #if !HAVE_STRLCAT
56 /*
57 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
58 *
59 * Permission to use, copy, modify, and distribute this software for any
60 * purpose with or without fee is hereby granted, provided that the above
61 * copyright notice and this permission notice appear in all copies.
62 *
63 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
64 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
65 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
66 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
67 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
68 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
69 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
70 */
72 #include <sys/types.h>
73 #include <string.h>
75 /*
76 * Appends src to string dst of size siz (unlike strncat, siz is the
77 * full size of dst, not space left). At most siz-1 characters
78 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
79 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
80 * If retval >= siz, truncation occurred.
81 */
82 size_t
83 strlcat(char *dst, const char *src, size_t siz)
84 {
85 char *d = dst;
86 const char *s = src;
87 size_t n = siz;
88 size_t dlen;
90 /* Find the end of dst and adjust bytes left but don't go past end */
91 while (n-- != 0 && *d != '\0')
92 d++;
93 dlen = d - dst;
94 n = siz - dlen;
96 if (n == 0)
97 return(dlen + strlen(s));
98 while (*s != '\0') {
99 if (n != 1) {
100 *d++ = *s;
101 n--;
103 s++;
105 *d = '\0';
107 return(dlen + (s - src)); /* count does not include NUL */
109 #endif /* !HAVE_STRLCAT */
110 #if !HAVE_STRLCPY
111 /*
112 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
114 * Permission to use, copy, modify, and distribute this software for any
115 * purpose with or without fee is hereby granted, provided that the above
116 * copyright notice and this permission notice appear in all copies.
118 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
119 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
120 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
121 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
122 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
123 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
124 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
125 */
127 #include <sys/types.h>
128 #include <string.h>
130 /*
131 * Copy src to string dst of size siz. At most siz-1 characters
132 * will be copied. Always NUL terminates (unless siz == 0).
133 * Returns strlen(src); if retval >= siz, truncation occurred.
134 */
135 size_t
136 strlcpy(char *dst, const char *src, size_t siz)
138 char *d = dst;
139 const char *s = src;
140 size_t n = siz;
142 /* Copy as many bytes as will fit */
143 if (n != 0) {
144 while (--n != 0) {
145 if ((*d++ = *s++) == '\0')
146 break;
150 /* Not enough room in dst, add NUL and traverse rest of src */
151 if (n == 0) {
152 if (siz != 0)
153 *d = '\0'; /* NUL-terminate dst */
154 while (*s++)
158 return(s - src - 1); /* count does not include NUL */
160 #endif /* !HAVE_STRLCPY */
161 #if !HAVE_STRTONUM
162 /*
163 * Copyright (c) 2004 Ted Unangst and Todd Miller
164 * All rights reserved.
166 * Permission to use, copy, modify, and distribute this software for any
167 * purpose with or without fee is hereby granted, provided that the above
168 * copyright notice and this permission notice appear in all copies.
170 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
171 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
172 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
173 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
174 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
175 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
176 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
177 */
179 #include <errno.h>
180 #include <limits.h>
181 #include <stdlib.h>
183 #define INVALID 1
184 #define TOOSMALL 2
185 #define TOOLARGE 3
187 long long
188 strtonum(const char *numstr, long long minval, long long maxval,
189 const char **errstrp)
191 long long ll = 0;
192 int error = 0;
193 char *ep;
194 struct errval {
195 const char *errstr;
196 int err;
197 } ev[4] = {
198 { NULL, 0 },
199 { "invalid", EINVAL },
200 { "too small", ERANGE },
201 { "too large", ERANGE },
202 };
204 ev[0].err = errno;
205 errno = 0;
206 if (minval > maxval) {
207 error = INVALID;
208 } else {
209 ll = strtoll(numstr, &ep, 10);
210 if (numstr == ep || *ep != '\0')
211 error = INVALID;
212 else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
213 error = TOOSMALL;
214 else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
215 error = TOOLARGE;
217 if (errstrp != NULL)
218 *errstrp = ev[error].errstr;
219 errno = ev[error].err;
220 if (error)
221 ll = 0;
223 return (ll);
225 #endif /* !HAVE_STRTONUM */