diff options
Diffstat (limited to 'openbsd-compat')
-rw-r--r-- | openbsd-compat/.cvsignore | 1 | ||||
-rw-r--r-- | openbsd-compat/Makefile.in | 2 | ||||
-rw-r--r-- | openbsd-compat/arc4random.c | 4 | ||||
-rw-r--r-- | openbsd-compat/bindresvport.c | 1 | ||||
-rw-r--r-- | openbsd-compat/bsd-asprintf.c | 12 | ||||
-rw-r--r-- | openbsd-compat/bsd-err.c | 71 | ||||
-rw-r--r-- | openbsd-compat/bsd-misc.c | 17 | ||||
-rw-r--r-- | openbsd-compat/bsd-misc.h | 11 | ||||
-rw-r--r-- | openbsd-compat/bsd-snprintf.c | 12 | ||||
-rw-r--r-- | openbsd-compat/inet_aton.c | 10 | ||||
-rw-r--r-- | openbsd-compat/openbsd-compat.h | 35 | ||||
-rw-r--r-- | openbsd-compat/port-solaris.h | 1 | ||||
-rw-r--r-- | openbsd-compat/regress/.cvsignore | 6 | ||||
-rw-r--r-- | openbsd-compat/vis.c | 60 | ||||
-rw-r--r-- | openbsd-compat/vis.h | 5 |
15 files changed, 194 insertions, 54 deletions
diff --git a/openbsd-compat/.cvsignore b/openbsd-compat/.cvsignore deleted file mode 100644 index f3c7a7c5d..000000000 --- a/openbsd-compat/.cvsignore +++ /dev/null | |||
@@ -1 +0,0 @@ | |||
1 | Makefile | ||
diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in index 3c5e3b7f7..aca9eba75 100644 --- a/openbsd-compat/Makefile.in +++ b/openbsd-compat/Makefile.in | |||
@@ -18,7 +18,7 @@ LDFLAGS=-L. @LDFLAGS@ | |||
18 | 18 | ||
19 | OPENBSD=base64.o basename.o bcrypt_pbkdf.o bindresvport.o blowfish.o daemon.o dirname.o fmt_scaled.o getcwd.o getgrouplist.o getopt_long.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o pwcache.o readpassphrase.o reallocarray.o realpath.o rresvport.o setenv.o setproctitle.o sha1.o sha2.o rmd160.o md5.o sigact.o strlcat.o strlcpy.o strmode.o strnlen.o strptime.o strsep.o strtonum.o strtoll.o strtoul.o strtoull.o timingsafe_bcmp.o vis.o blowfish.o bcrypt_pbkdf.o explicit_bzero.o | 19 | OPENBSD=base64.o basename.o bcrypt_pbkdf.o bindresvport.o blowfish.o daemon.o dirname.o fmt_scaled.o getcwd.o getgrouplist.o getopt_long.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o pwcache.o readpassphrase.o reallocarray.o realpath.o rresvport.o setenv.o setproctitle.o sha1.o sha2.o rmd160.o md5.o sigact.o strlcat.o strlcpy.o strmode.o strnlen.o strptime.o strsep.o strtonum.o strtoll.o strtoul.o strtoull.o timingsafe_bcmp.o vis.o blowfish.o bcrypt_pbkdf.o explicit_bzero.o |
20 | 20 | ||
21 | COMPAT=arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o getrrsetbyname-ldns.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-setres_id.o bsd-snprintf.o bsd-statvfs.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xmmap.o xcrypt.o kludge-fd_set.o | 21 | COMPAT=arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o getrrsetbyname-ldns.o bsd-err.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-setres_id.o bsd-snprintf.o bsd-statvfs.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xmmap.o xcrypt.o kludge-fd_set.o |
22 | 22 | ||
23 | PORTS=port-aix.o port-irix.o port-linux.o port-solaris.o port-tun.o port-uw.o | 23 | PORTS=port-aix.o port-irix.o port-linux.o port-solaris.o port-tun.o port-uw.o |
24 | 24 | ||
diff --git a/openbsd-compat/arc4random.c b/openbsd-compat/arc4random.c index 046f57e61..b6256b4f8 100644 --- a/openbsd-compat/arc4random.c +++ b/openbsd-compat/arc4random.c | |||
@@ -110,8 +110,8 @@ _rs_stir(void) | |||
110 | 110 | ||
111 | #ifdef WITH_OPENSSL | 111 | #ifdef WITH_OPENSSL |
112 | if (RAND_bytes(rnd, sizeof(rnd)) <= 0) | 112 | if (RAND_bytes(rnd, sizeof(rnd)) <= 0) |
113 | fatal("Couldn't obtain random bytes (error %ld)", | 113 | fatal("Couldn't obtain random bytes (error 0x%lx)", |
114 | ERR_get_error()); | 114 | (unsigned long)ERR_get_error()); |
115 | #else | 115 | #else |
116 | getrnd(rnd, sizeof(rnd)); | 116 | getrnd(rnd, sizeof(rnd)); |
117 | #endif | 117 | #endif |
diff --git a/openbsd-compat/bindresvport.c b/openbsd-compat/bindresvport.c index c89f21403..eeb269d59 100644 --- a/openbsd-compat/bindresvport.c +++ b/openbsd-compat/bindresvport.c | |||
@@ -64,6 +64,7 @@ bindresvport_sa(int sd, struct sockaddr *sa) | |||
64 | if (sa == NULL) { | 64 | if (sa == NULL) { |
65 | memset(&myaddr, 0, sizeof(myaddr)); | 65 | memset(&myaddr, 0, sizeof(myaddr)); |
66 | sa = (struct sockaddr *)&myaddr; | 66 | sa = (struct sockaddr *)&myaddr; |
67 | salen = sizeof(myaddr); | ||
67 | 68 | ||
68 | if (getsockname(sd, sa, &salen) == -1) | 69 | if (getsockname(sd, sa, &salen) == -1) |
69 | return -1; /* errno is correctly set */ | 70 | return -1; /* errno is correctly set */ |
diff --git a/openbsd-compat/bsd-asprintf.c b/openbsd-compat/bsd-asprintf.c index 3368195d4..7b83448ca 100644 --- a/openbsd-compat/bsd-asprintf.c +++ b/openbsd-compat/bsd-asprintf.c | |||
@@ -25,18 +25,6 @@ | |||
25 | #include <stdarg.h> | 25 | #include <stdarg.h> |
26 | #include <stdlib.h> | 26 | #include <stdlib.h> |
27 | 27 | ||
28 | #ifndef VA_COPY | ||
29 | # ifdef HAVE_VA_COPY | ||
30 | # define VA_COPY(dest, src) va_copy(dest, src) | ||
31 | # else | ||
32 | # ifdef HAVE___VA_COPY | ||
33 | # define VA_COPY(dest, src) __va_copy(dest, src) | ||
34 | # else | ||
35 | # define VA_COPY(dest, src) (dest) = (src) | ||
36 | # endif | ||
37 | # endif | ||
38 | #endif | ||
39 | |||
40 | #define INIT_SZ 128 | 28 | #define INIT_SZ 128 |
41 | 29 | ||
42 | int | 30 | int |
diff --git a/openbsd-compat/bsd-err.c b/openbsd-compat/bsd-err.c new file mode 100644 index 000000000..ab10646f0 --- /dev/null +++ b/openbsd-compat/bsd-err.c | |||
@@ -0,0 +1,71 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2015 Tim Rice <tim@multitalents.net> | ||
3 | * | ||
4 | * Redistribution and use in source and binary forms, with or without | ||
5 | * modification, are permitted provided that the following conditions | ||
6 | * are met: | ||
7 | * | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. The name of the author may not be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | #include "includes.h" | ||
29 | |||
30 | #ifndef HAVE_ERR | ||
31 | void | ||
32 | err(int r, const char *fmt, ...) | ||
33 | { | ||
34 | va_list args; | ||
35 | |||
36 | va_start(args, fmt); | ||
37 | fprintf(stderr, "%s: ", strerror(errno)); | ||
38 | vfprintf(stderr, fmt, args); | ||
39 | fputc('\n', stderr); | ||
40 | va_end(args); | ||
41 | exit(r); | ||
42 | } | ||
43 | #endif | ||
44 | |||
45 | #ifndef HAVE_ERRX | ||
46 | void | ||
47 | errx(int r, const char *fmt, ...) | ||
48 | { | ||
49 | va_list args; | ||
50 | |||
51 | va_start(args, fmt); | ||
52 | vfprintf(stderr, fmt, args); | ||
53 | fputc('\n', stderr); | ||
54 | va_end(args); | ||
55 | exit(r); | ||
56 | } | ||
57 | #endif | ||
58 | |||
59 | #ifndef HAVE_WARN | ||
60 | void | ||
61 | warn(const char *fmt, ...) | ||
62 | { | ||
63 | va_list args; | ||
64 | |||
65 | va_start(args, fmt); | ||
66 | fprintf(stderr, "%s: ", strerror(errno)); | ||
67 | vfprintf(stderr, fmt, args); | ||
68 | fputc('\n', stderr); | ||
69 | va_end(args); | ||
70 | } | ||
71 | #endif | ||
diff --git a/openbsd-compat/bsd-misc.c b/openbsd-compat/bsd-misc.c index 2a788e47f..18bf62dd8 100644 --- a/openbsd-compat/bsd-misc.c +++ b/openbsd-compat/bsd-misc.c | |||
@@ -284,3 +284,20 @@ pledge(const char *promises, const char *paths[]) | |||
284 | return 0; | 284 | return 0; |
285 | } | 285 | } |
286 | #endif | 286 | #endif |
287 | |||
288 | #ifndef HAVE_MBTOWC | ||
289 | /* a mbtowc that only supports ASCII */ | ||
290 | int | ||
291 | mbtowc(wchar_t *pwc, const char *s, size_t n) | ||
292 | { | ||
293 | if (s == NULL || *s == '\0') | ||
294 | return 0; /* ASCII is not state-dependent */ | ||
295 | if (*s < 0 || *s > 0x7f || n < 1) { | ||
296 | errno = EOPNOTSUPP; | ||
297 | return -1; | ||
298 | } | ||
299 | if (pwc != NULL) | ||
300 | *pwc = *s; | ||
301 | return 1; | ||
302 | } | ||
303 | #endif | ||
diff --git a/openbsd-compat/bsd-misc.h b/openbsd-compat/bsd-misc.h index 0d81d1735..27abb2e92 100644 --- a/openbsd-compat/bsd-misc.h +++ b/openbsd-compat/bsd-misc.h | |||
@@ -126,4 +126,15 @@ pid_t getpgid(pid_t); | |||
126 | int pledge(const char *promises, const char *paths[]); | 126 | int pledge(const char *promises, const char *paths[]); |
127 | #endif | 127 | #endif |
128 | 128 | ||
129 | /* bsd-err.h */ | ||
130 | #ifndef HAVE_ERR | ||
131 | void err(int, const char *, ...) __attribute__((format(printf, 2, 3))); | ||
132 | #endif | ||
133 | #ifndef HAVE_ERRX | ||
134 | void errx(int, const char *, ...) __attribute__((format(printf, 2, 3))); | ||
135 | #endif | ||
136 | #ifndef HAVE_WARN | ||
137 | void warn(const char *, ...) __attribute__((format(printf, 1, 2))); | ||
138 | #endif | ||
139 | |||
129 | #endif /* _BSD_MISC_H */ | 140 | #endif /* _BSD_MISC_H */ |
diff --git a/openbsd-compat/bsd-snprintf.c b/openbsd-compat/bsd-snprintf.c index 23a635989..d95b6a401 100644 --- a/openbsd-compat/bsd-snprintf.c +++ b/openbsd-compat/bsd-snprintf.c | |||
@@ -99,18 +99,6 @@ | |||
99 | # undef HAVE_VSNPRINTF | 99 | # undef HAVE_VSNPRINTF |
100 | #endif | 100 | #endif |
101 | 101 | ||
102 | #ifndef VA_COPY | ||
103 | # ifdef HAVE_VA_COPY | ||
104 | # define VA_COPY(dest, src) va_copy(dest, src) | ||
105 | # else | ||
106 | # ifdef HAVE___VA_COPY | ||
107 | # define VA_COPY(dest, src) __va_copy(dest, src) | ||
108 | # else | ||
109 | # define VA_COPY(dest, src) (dest) = (src) | ||
110 | # endif | ||
111 | # endif | ||
112 | #endif | ||
113 | |||
114 | #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) | 102 | #if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) |
115 | 103 | ||
116 | #include <ctype.h> | 104 | #include <ctype.h> |
diff --git a/openbsd-compat/inet_aton.c b/openbsd-compat/inet_aton.c index 130597e14..093a17203 100644 --- a/openbsd-compat/inet_aton.c +++ b/openbsd-compat/inet_aton.c | |||
@@ -3,7 +3,7 @@ | |||
3 | /* | 3 | /* |
4 | * Copyright (c) 1983, 1990, 1993 | 4 | * Copyright (c) 1983, 1990, 1993 |
5 | * The Regents of the University of California. All rights reserved. | 5 | * The Regents of the University of California. All rights reserved. |
6 | * | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | 9 | * are met: |
@@ -15,7 +15,7 @@ | |||
15 | * 3. Neither the name of the University nor the names of its contributors | 15 | * 3. Neither the name of the University nor the names of its contributors |
16 | * may be used to endorse or promote products derived from this software | 16 | * may be used to endorse or promote products derived from this software |
17 | * without specific prior written permission. | 17 | * without specific prior written permission. |
18 | * | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
@@ -29,14 +29,14 @@ | |||
29 | * SUCH DAMAGE. | 29 | * SUCH DAMAGE. |
30 | * - | 30 | * - |
31 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | 31 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
32 | * | 32 | * |
33 | * Permission to use, copy, modify, and distribute this software for any | 33 | * Permission to use, copy, modify, and distribute this software for any |
34 | * purpose with or without fee is hereby granted, provided that the above | 34 | * purpose with or without fee is hereby granted, provided that the above |
35 | * copyright notice and this permission notice appear in all copies, and that | 35 | * copyright notice and this permission notice appear in all copies, and that |
36 | * the name of Digital Equipment Corporation not be used in advertising or | 36 | * the name of Digital Equipment Corporation not be used in advertising or |
37 | * publicity pertaining to distribution of the document or software without | 37 | * publicity pertaining to distribution of the document or software without |
38 | * specific, written prior permission. | 38 | * specific, written prior permission. |
39 | * | 39 | * |
40 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | 40 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
41 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | 41 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES |
42 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | 42 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT |
@@ -77,7 +77,7 @@ inet_addr(const char *cp) | |||
77 | } | 77 | } |
78 | #endif | 78 | #endif |
79 | 79 | ||
80 | /* | 80 | /* |
81 | * Check whether "cp" is a valid ascii representation | 81 | * Check whether "cp" is a valid ascii representation |
82 | * of an Internet address and convert to a binary address. | 82 | * of an Internet address and convert to a binary address. |
83 | * Returns 1 if the address is valid, 0 if not. | 83 | * Returns 1 if the address is valid, 0 if not. |
diff --git a/openbsd-compat/openbsd-compat.h b/openbsd-compat/openbsd-compat.h index 8cc8a11b7..37d2064cd 100644 --- a/openbsd-compat/openbsd-compat.h +++ b/openbsd-compat/openbsd-compat.h | |||
@@ -36,6 +36,8 @@ | |||
36 | 36 | ||
37 | #include <sys/socket.h> | 37 | #include <sys/socket.h> |
38 | 38 | ||
39 | #include <stddef.h> /* for wchar_t */ | ||
40 | |||
39 | /* OpenBSD function replacements */ | 41 | /* OpenBSD function replacements */ |
40 | #include "base64.h" | 42 | #include "base64.h" |
41 | #include "sigact.h" | 43 | #include "sigact.h" |
@@ -231,10 +233,43 @@ long long strtonum(const char *, long long, long long, const char **); | |||
231 | # define mblen(x, y) (1) | 233 | # define mblen(x, y) (1) |
232 | #endif | 234 | #endif |
233 | 235 | ||
236 | #ifndef HAVE_WCWIDTH | ||
237 | # define wcwidth(x) (((x) >= 0x20 && (x) <= 0x7e) ? 1 : -1) | ||
238 | /* force our no-op nl_langinfo and mbtowc */ | ||
239 | # undef HAVE_NL_LANGINFO | ||
240 | # undef HAVE_MBTOWC | ||
241 | # undef HAVE_LANGINFO_H | ||
242 | #endif | ||
243 | |||
244 | #ifndef HAVE_NL_LANGINFO | ||
245 | # define nl_langinfo(x) "" | ||
246 | #endif | ||
247 | |||
248 | #ifndef HAVE_MBTOWC | ||
249 | int mbtowc(wchar_t *, const char*, size_t); | ||
250 | #endif | ||
251 | |||
234 | #if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF) | 252 | #if !defined(HAVE_VASPRINTF) || !defined(HAVE_VSNPRINTF) |
235 | # include <stdarg.h> | 253 | # include <stdarg.h> |
236 | #endif | 254 | #endif |
237 | 255 | ||
256 | /* | ||
257 | * Some platforms unconditionally undefine va_copy() so we define VA_COPY() | ||
258 | * instead. This is known to be the case on at least some configurations of | ||
259 | * AIX with the xlc compiler. | ||
260 | */ | ||
261 | #ifndef VA_COPY | ||
262 | # ifdef HAVE_VA_COPY | ||
263 | # define VA_COPY(dest, src) va_copy(dest, src) | ||
264 | # else | ||
265 | # ifdef HAVE___VA_COPY | ||
266 | # define VA_COPY(dest, src) __va_copy(dest, src) | ||
267 | # else | ||
268 | # define VA_COPY(dest, src) (dest) = (src) | ||
269 | # endif | ||
270 | # endif | ||
271 | #endif | ||
272 | |||
238 | #ifndef HAVE_VASPRINTF | 273 | #ifndef HAVE_VASPRINTF |
239 | int vasprintf(char **, const char *, va_list); | 274 | int vasprintf(char **, const char *, va_list); |
240 | #endif | 275 | #endif |
diff --git a/openbsd-compat/port-solaris.h b/openbsd-compat/port-solaris.h index 3a41ea8cd..a7cb5eb30 100644 --- a/openbsd-compat/port-solaris.h +++ b/openbsd-compat/port-solaris.h | |||
@@ -27,6 +27,7 @@ void solaris_contract_post_fork_child(void); | |||
27 | void solaris_contract_post_fork_parent(pid_t pid); | 27 | void solaris_contract_post_fork_parent(pid_t pid); |
28 | void solaris_set_default_project(struct passwd *); | 28 | void solaris_set_default_project(struct passwd *); |
29 | # ifdef USE_SOLARIS_PRIVS | 29 | # ifdef USE_SOLARIS_PRIVS |
30 | #include <priv.h> | ||
30 | priv_set_t *solaris_basic_privset(void); | 31 | priv_set_t *solaris_basic_privset(void); |
31 | void solaris_drop_privs_pinfo_net_fork_exec(void); | 32 | void solaris_drop_privs_pinfo_net_fork_exec(void); |
32 | void solaris_drop_privs_root_pinfo_net(void); | 33 | void solaris_drop_privs_root_pinfo_net(void); |
diff --git a/openbsd-compat/regress/.cvsignore b/openbsd-compat/regress/.cvsignore deleted file mode 100644 index 33074f4a3..000000000 --- a/openbsd-compat/regress/.cvsignore +++ /dev/null | |||
@@ -1,6 +0,0 @@ | |||
1 | Makefile | ||
2 | snprintftest | ||
3 | strduptest | ||
4 | strtonumtest | ||
5 | closefromtest | ||
6 | opensslvertest | ||
diff --git a/openbsd-compat/vis.c b/openbsd-compat/vis.c index f6f5665c1..3cef6bafd 100644 --- a/openbsd-compat/vis.c +++ b/openbsd-compat/vis.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: vis.c,v 1.19 2005/09/01 17:15:49 millert Exp $ */ | 1 | /* $OpenBSD: vis.c,v 1.25 2015/09/13 11:32:51 guenther Exp $ */ |
2 | /*- | 2 | /*- |
3 | * Copyright (c) 1989, 1993 | 3 | * Copyright (c) 1989, 1993 |
4 | * The Regents of the University of California. All rights reserved. | 4 | * The Regents of the University of California. All rights reserved. |
@@ -33,13 +33,24 @@ | |||
33 | #include "includes.h" | 33 | #include "includes.h" |
34 | #if !defined(HAVE_STRNVIS) || defined(BROKEN_STRNVIS) | 34 | #if !defined(HAVE_STRNVIS) || defined(BROKEN_STRNVIS) |
35 | 35 | ||
36 | /* | ||
37 | * We want these to override in the BROKEN_STRNVIS case. TO avoid future sync | ||
38 | * problems no-op out the weak symbol definition rather than remove it. | ||
39 | */ | ||
40 | #define DEF_WEAK(x) | ||
41 | |||
42 | #include <sys/types.h> | ||
43 | #include <errno.h> | ||
36 | #include <ctype.h> | 44 | #include <ctype.h> |
45 | #include <limits.h> | ||
37 | #include <string.h> | 46 | #include <string.h> |
47 | #include <stdlib.h> | ||
38 | 48 | ||
39 | #include "vis.h" | 49 | #include "vis.h" |
40 | 50 | ||
41 | #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') | 51 | #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') |
42 | #define isvisible(c) \ | 52 | #define isvisible(c,flag) \ |
53 | (((c) == '\\' || (flag & VIS_ALL) == 0) && \ | ||
43 | (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \ | 54 | (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \ |
44 | (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \ | 55 | (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \ |
45 | (flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) || \ | 56 | (flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) || \ |
@@ -48,7 +59,7 @@ | |||
48 | ((flag & VIS_NL) == 0 && (c) == '\n') || \ | 59 | ((flag & VIS_NL) == 0 && (c) == '\n') || \ |
49 | ((flag & VIS_SAFE) && ((c) == '\b' || \ | 60 | ((flag & VIS_SAFE) && ((c) == '\b' || \ |
50 | (c) == '\007' || (c) == '\r' || \ | 61 | (c) == '\007' || (c) == '\r' || \ |
51 | isgraph((u_char)(c))))) | 62 | isgraph((u_char)(c)))))) |
52 | 63 | ||
53 | /* | 64 | /* |
54 | * vis - visually encode characters | 65 | * vis - visually encode characters |
@@ -56,10 +67,11 @@ | |||
56 | char * | 67 | char * |
57 | vis(char *dst, int c, int flag, int nextc) | 68 | vis(char *dst, int c, int flag, int nextc) |
58 | { | 69 | { |
59 | if (isvisible(c)) { | 70 | if (isvisible(c, flag)) { |
60 | *dst++ = c; | 71 | if ((c == '"' && (flag & VIS_DQ) != 0) || |
61 | if (c == '\\' && (flag & VIS_NOSLASH) == 0) | 72 | (c == '\\' && (flag & VIS_NOSLASH) == 0)) |
62 | *dst++ = '\\'; | 73 | *dst++ = '\\'; |
74 | *dst++ = c; | ||
63 | *dst = '\0'; | 75 | *dst = '\0'; |
64 | return (dst); | 76 | return (dst); |
65 | } | 77 | } |
@@ -136,6 +148,7 @@ done: | |||
136 | *dst = '\0'; | 148 | *dst = '\0'; |
137 | return (dst); | 149 | return (dst); |
138 | } | 150 | } |
151 | DEF_WEAK(vis); | ||
139 | 152 | ||
140 | /* | 153 | /* |
141 | * strvis, strnvis, strvisx - visually encode characters from src into dst | 154 | * strvis, strnvis, strvisx - visually encode characters from src into dst |
@@ -161,6 +174,7 @@ strvis(char *dst, const char *src, int flag) | |||
161 | *dst = '\0'; | 174 | *dst = '\0'; |
162 | return (dst - start); | 175 | return (dst - start); |
163 | } | 176 | } |
177 | DEF_WEAK(strvis); | ||
164 | 178 | ||
165 | int | 179 | int |
166 | strnvis(char *dst, const char *src, size_t siz, int flag) | 180 | strnvis(char *dst, const char *src, size_t siz, int flag) |
@@ -171,19 +185,18 @@ strnvis(char *dst, const char *src, size_t siz, int flag) | |||
171 | 185 | ||
172 | i = 0; | 186 | i = 0; |
173 | for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) { | 187 | for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) { |
174 | if (isvisible(c)) { | 188 | if (isvisible(c, flag)) { |
175 | i = 1; | 189 | if ((c == '"' && (flag & VIS_DQ) != 0) || |
176 | *dst++ = c; | 190 | (c == '\\' && (flag & VIS_NOSLASH) == 0)) { |
177 | if (c == '\\' && (flag & VIS_NOSLASH) == 0) { | ||
178 | /* need space for the extra '\\' */ | 191 | /* need space for the extra '\\' */ |
179 | if (dst < end) | 192 | if (dst + 1 >= end) { |
180 | *dst++ = '\\'; | ||
181 | else { | ||
182 | dst--; | ||
183 | i = 2; | 193 | i = 2; |
184 | break; | 194 | break; |
185 | } | 195 | } |
196 | *dst++ = '\\'; | ||
186 | } | 197 | } |
198 | i = 1; | ||
199 | *dst++ = c; | ||
187 | src++; | 200 | src++; |
188 | } else { | 201 | } else { |
189 | i = vis(tbuf, c, flag, *++src) - tbuf; | 202 | i = vis(tbuf, c, flag, *++src) - tbuf; |
@@ -207,6 +220,25 @@ strnvis(char *dst, const char *src, size_t siz, int flag) | |||
207 | } | 220 | } |
208 | 221 | ||
209 | int | 222 | int |
223 | stravis(char **outp, const char *src, int flag) | ||
224 | { | ||
225 | char *buf; | ||
226 | int len, serrno; | ||
227 | |||
228 | buf = reallocarray(NULL, 4, strlen(src) + 1); | ||
229 | if (buf == NULL) | ||
230 | return -1; | ||
231 | len = strvis(buf, src, flag); | ||
232 | serrno = errno; | ||
233 | *outp = realloc(buf, len + 1); | ||
234 | if (*outp == NULL) { | ||
235 | *outp = buf; | ||
236 | errno = serrno; | ||
237 | } | ||
238 | return (len); | ||
239 | } | ||
240 | |||
241 | int | ||
210 | strvisx(char *dst, const char *src, size_t len, int flag) | 242 | strvisx(char *dst, const char *src, size_t len, int flag) |
211 | { | 243 | { |
212 | char c; | 244 | char c; |
diff --git a/openbsd-compat/vis.h b/openbsd-compat/vis.h index d1286c99d..2cdfd364b 100644 --- a/openbsd-compat/vis.h +++ b/openbsd-compat/vis.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: vis.h,v 1.11 2005/08/09 19:38:31 millert Exp $ */ | 1 | /* $OpenBSD: vis.h,v 1.15 2015/07/20 01:52:27 millert Exp $ */ |
2 | /* $NetBSD: vis.h,v 1.4 1994/10/26 00:56:41 cgd Exp $ */ | 2 | /* $NetBSD: vis.h,v 1.4 1994/10/26 00:56:41 cgd Exp $ */ |
3 | 3 | ||
4 | /*- | 4 | /*- |
@@ -58,6 +58,8 @@ | |||
58 | #define VIS_NL 0x10 /* also encode newline */ | 58 | #define VIS_NL 0x10 /* also encode newline */ |
59 | #define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL) | 59 | #define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL) |
60 | #define VIS_SAFE 0x20 /* only encode "unsafe" characters */ | 60 | #define VIS_SAFE 0x20 /* only encode "unsafe" characters */ |
61 | #define VIS_DQ 0x200 /* backslash-escape double quotes */ | ||
62 | #define VIS_ALL 0x400 /* encode all characters */ | ||
61 | 63 | ||
62 | /* | 64 | /* |
63 | * other | 65 | * other |
@@ -81,6 +83,7 @@ | |||
81 | 83 | ||
82 | char *vis(char *, int, int, int); | 84 | char *vis(char *, int, int, int); |
83 | int strvis(char *, const char *, int); | 85 | int strvis(char *, const char *, int); |
86 | int stravis(char **, const char *, int); | ||
84 | int strnvis(char *, const char *, size_t, int) | 87 | int strnvis(char *, const char *, size_t, int) |
85 | __attribute__ ((__bounded__(__string__,1,3))); | 88 | __attribute__ ((__bounded__(__string__,1,3))); |
86 | int strvisx(char *, const char *, size_t, int) | 89 | int strvisx(char *, const char *, size_t, int) |