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