Blame


1 0efd7c9a 2022-07-30 op #include "config.h"
2 0efd7c9a 2022-07-30 op #if !HAVE_GETPROGNAME
3 0efd7c9a 2022-07-30 op /*
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>
7 0efd7c9a 2022-07-30 op *
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.
11 0efd7c9a 2022-07-30 op *
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.
19 0efd7c9a 2022-07-30 op */
20 0efd7c9a 2022-07-30 op
21 0efd7c9a 2022-07-30 op #include <sys/types.h>
22 0efd7c9a 2022-07-30 op
23 0efd7c9a 2022-07-30 op #include <errno.h>
24 0efd7c9a 2022-07-30 op
25 0efd7c9a 2022-07-30 op #if HAVE_GETEXECNAME
26 0efd7c9a 2022-07-30 op #include <stdlib.h>
27 0efd7c9a 2022-07-30 op const char *
28 0efd7c9a 2022-07-30 op getprogname(void)
29 0efd7c9a 2022-07-30 op {
30 0efd7c9a 2022-07-30 op return getexecname();
31 0efd7c9a 2022-07-30 op }
32 0efd7c9a 2022-07-30 op #elif HAVE_PROGRAM_INVOCATION_SHORT_NAME
33 0efd7c9a 2022-07-30 op const char *
34 0efd7c9a 2022-07-30 op getprogname(void)
35 0efd7c9a 2022-07-30 op {
36 0efd7c9a 2022-07-30 op return (program_invocation_short_name);
37 0efd7c9a 2022-07-30 op }
38 0efd7c9a 2022-07-30 op #elif HAVE___PROGNAME
39 0efd7c9a 2022-07-30 op const char *
40 0efd7c9a 2022-07-30 op getprogname(void)
41 0efd7c9a 2022-07-30 op {
42 0efd7c9a 2022-07-30 op extern char *__progname;
43 0efd7c9a 2022-07-30 op
44 0efd7c9a 2022-07-30 op return (__progname);
45 0efd7c9a 2022-07-30 op }
46 0efd7c9a 2022-07-30 op #else
47 0efd7c9a 2022-07-30 op #warning No getprogname available.
48 0efd7c9a 2022-07-30 op const char *
49 0efd7c9a 2022-07-30 op getprogname(void)
50 0efd7c9a 2022-07-30 op {
51 0efd7c9a 2022-07-30 op return ("lstun");
52 0efd7c9a 2022-07-30 op }
53 0efd7c9a 2022-07-30 op #endif
54 0efd7c9a 2022-07-30 op #endif /* !HAVE_GETPROGNAME */
55 0efd7c9a 2022-07-30 op #if !HAVE_STRLCAT
56 0efd7c9a 2022-07-30 op /*
57 0efd7c9a 2022-07-30 op * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
58 0efd7c9a 2022-07-30 op *
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.
62 0efd7c9a 2022-07-30 op *
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.
70 0efd7c9a 2022-07-30 op */
71 0efd7c9a 2022-07-30 op
72 0efd7c9a 2022-07-30 op #include <sys/types.h>
73 0efd7c9a 2022-07-30 op #include <string.h>
74 0efd7c9a 2022-07-30 op
75 0efd7c9a 2022-07-30 op /*
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.
81 0efd7c9a 2022-07-30 op */
82 0efd7c9a 2022-07-30 op size_t
83 0efd7c9a 2022-07-30 op strlcat(char *dst, const char *src, size_t siz)
84 0efd7c9a 2022-07-30 op {
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;
88 0efd7c9a 2022-07-30 op size_t dlen;
89 0efd7c9a 2022-07-30 op
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')
92 0efd7c9a 2022-07-30 op d++;
93 0efd7c9a 2022-07-30 op dlen = d - dst;
94 0efd7c9a 2022-07-30 op n = siz - dlen;
95 0efd7c9a 2022-07-30 op
96 0efd7c9a 2022-07-30 op if (n == 0)
97 0efd7c9a 2022-07-30 op return(dlen + strlen(s));
98 0efd7c9a 2022-07-30 op while (*s != '\0') {
99 0efd7c9a 2022-07-30 op if (n != 1) {
100 0efd7c9a 2022-07-30 op *d++ = *s;
101 0efd7c9a 2022-07-30 op n--;
102 0efd7c9a 2022-07-30 op }
103 0efd7c9a 2022-07-30 op s++;
104 0efd7c9a 2022-07-30 op }
105 0efd7c9a 2022-07-30 op *d = '\0';
106 0efd7c9a 2022-07-30 op
107 0efd7c9a 2022-07-30 op return(dlen + (s - src)); /* count does not include NUL */
108 0efd7c9a 2022-07-30 op }
109 0efd7c9a 2022-07-30 op #endif /* !HAVE_STRLCAT */
110 0efd7c9a 2022-07-30 op #if !HAVE_STRLCPY
111 0efd7c9a 2022-07-30 op /*
112 0efd7c9a 2022-07-30 op * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
113 0efd7c9a 2022-07-30 op *
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.
117 0efd7c9a 2022-07-30 op *
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.
125 0efd7c9a 2022-07-30 op */
126 0efd7c9a 2022-07-30 op
127 0efd7c9a 2022-07-30 op #include <sys/types.h>
128 0efd7c9a 2022-07-30 op #include <string.h>
129 0efd7c9a 2022-07-30 op
130 0efd7c9a 2022-07-30 op /*
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.
134 0efd7c9a 2022-07-30 op */
135 0efd7c9a 2022-07-30 op size_t
136 0efd7c9a 2022-07-30 op strlcpy(char *dst, const char *src, size_t siz)
137 0efd7c9a 2022-07-30 op {
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;
141 0efd7c9a 2022-07-30 op
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')
146 0efd7c9a 2022-07-30 op break;
147 0efd7c9a 2022-07-30 op }
148 0efd7c9a 2022-07-30 op }
149 0efd7c9a 2022-07-30 op
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 */
154 0efd7c9a 2022-07-30 op while (*s++)
155 0efd7c9a 2022-07-30 op ;
156 0efd7c9a 2022-07-30 op }
157 0efd7c9a 2022-07-30 op
158 0efd7c9a 2022-07-30 op return(s - src - 1); /* count does not include NUL */
159 0efd7c9a 2022-07-30 op }
160 0efd7c9a 2022-07-30 op #endif /* !HAVE_STRLCPY */
161 0efd7c9a 2022-07-30 op #if !HAVE_STRTONUM
162 0efd7c9a 2022-07-30 op /*
163 0efd7c9a 2022-07-30 op * Copyright (c) 2004 Ted Unangst and Todd Miller
164 0efd7c9a 2022-07-30 op * All rights reserved.
165 0efd7c9a 2022-07-30 op *
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.
169 0efd7c9a 2022-07-30 op *
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.
177 0efd7c9a 2022-07-30 op */
178 0efd7c9a 2022-07-30 op
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>
182 0efd7c9a 2022-07-30 op
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
186 0efd7c9a 2022-07-30 op
187 0efd7c9a 2022-07-30 op long long
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)
190 0efd7c9a 2022-07-30 op {
191 0efd7c9a 2022-07-30 op long long ll = 0;
192 0efd7c9a 2022-07-30 op int error = 0;
193 0efd7c9a 2022-07-30 op char *ep;
194 0efd7c9a 2022-07-30 op struct errval {
195 0efd7c9a 2022-07-30 op const char *errstr;
196 0efd7c9a 2022-07-30 op int err;
197 0efd7c9a 2022-07-30 op } ev[4] = {
198 0efd7c9a 2022-07-30 op { NULL, 0 },
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 },
202 0efd7c9a 2022-07-30 op };
203 0efd7c9a 2022-07-30 op
204 0efd7c9a 2022-07-30 op ev[0].err = errno;
205 0efd7c9a 2022-07-30 op errno = 0;
206 0efd7c9a 2022-07-30 op if (minval > maxval) {
207 0efd7c9a 2022-07-30 op error = INVALID;
208 0efd7c9a 2022-07-30 op } else {
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;
216 0efd7c9a 2022-07-30 op }
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;
220 0efd7c9a 2022-07-30 op if (error)
221 0efd7c9a 2022-07-30 op ll = 0;
222 0efd7c9a 2022-07-30 op
223 0efd7c9a 2022-07-30 op return (ll);
224 0efd7c9a 2022-07-30 op }
225 0efd7c9a 2022-07-30 op #endif /* !HAVE_STRTONUM */