diff options
| author | christos <christos@NetBSD.org> | 2016-05-02 19:18:29 +0000 |
|---|---|---|
| committer | christos <christos@NetBSD.org> | 2016-05-02 19:18:29 +0000 |
| commit | d591b4e8741370bd42e79d22ed4da3f966edb669 (patch) | |
| tree | 569fca075f4a14393f3a043c914a43f8ba82b500 /sys/lib | |
| parent | 0da5af96a0e50d94b3caf3b0f7da1563aeb673af (diff) | |
move scsipi_strvis -> libkern:strnvisx()
change the prototype to match userland
fix sizes of strings passed to it
Diffstat (limited to 'sys/lib')
| -rw-r--r-- | sys/lib/libkern/Makefile.libkern | 3 | ||||
| -rw-r--r-- | sys/lib/libkern/libkern.h | 6 | ||||
| -rw-r--r-- | sys/lib/libkern/strnvisx.c | 87 |
3 files changed, 94 insertions, 2 deletions
diff --git a/sys/lib/libkern/Makefile.libkern b/sys/lib/libkern/Makefile.libkern index 2234890c817..70f1084e671 100644 --- a/sys/lib/libkern/Makefile.libkern +++ b/sys/lib/libkern/Makefile.libkern @@ -1,4 +1,4 @@ -# $NetBSD: Makefile.libkern,v 1.38 2015/04/15 19:13:47 mrg Exp $ +# $NetBSD: Makefile.libkern,v 1.39 2016/05/02 19:18:29 christos Exp $ # # Variable definitions for libkern. @@ -74,6 +74,7 @@ SRCS+= memset.c SRCS+= popcount32.c popcount64.c SRCS+= strtoul.c strtoll.c strtoull.c strtoimax.c strtoumax.c SRCS+= strtoi.c strtou.c +SRCS+= strnvisx.c SRCS+= scanc.c skpc.c SRCS+= random.c diff --git a/sys/lib/libkern/libkern.h b/sys/lib/libkern/libkern.h index 5c14e452f77..5863818950c 100644 --- a/sys/lib/libkern/libkern.h +++ b/sys/lib/libkern/libkern.h @@ -1,4 +1,4 @@ -/* $NetBSD: libkern.h,v 1.121 2015/08/30 07:55:45 uebayasi Exp $ */ +/* $NetBSD: libkern.h,v 1.122 2016/05/02 19:18:29 christos Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -464,6 +464,10 @@ unsigned int popcount64(uint64_t) __constfunc; void *explicit_memset(void *, int, size_t); int consttime_memequal(const void *, const void *, size_t); +int strnvisx(char *, size_t, const char *, size_t, int); +#define VIS_OCTAL 0x01 +#define VIS_SAFE 0x20 +#define VIS_TRIM 0x40 #ifdef notyet /* diff --git a/sys/lib/libkern/strnvisx.c b/sys/lib/libkern/strnvisx.c new file mode 100644 index 00000000000..819b942d45d --- /dev/null +++ b/sys/lib/libkern/strnvisx.c @@ -0,0 +1,87 @@ +/* $NetBSD: strnvisx.c,v 1.1 2016/05/02 19:18:29 christos Exp $ */ + +/*- + * Copyright (c) 1998, 1999, 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Charles M. Hannum; by Jason R. Thorpe of the Numerical Aerospace + * Simulation Facility, NASA Ames Research Center. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * from scsipiconf.c... + */ +#include <lib/libkern/libkern.h> + +#define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == '\377') + +int +strnvisx(char *dst, size_t dlen, const char *src, size_t slen, int flags) +{ + if (dlen == 0) + return -1; + + if (flags & VIS_TRIM) { + /* Trim leading and trailing blanks and NULs. */ + while (slen > 0 && STRVIS_ISWHITE(src[0])) + ++src, --slen; + while (slen > 0 && STRVIS_ISWHITE(src[slen - 1])) + --slen; + } + + while (slen > 0) { + if ((flags & VIS_SAFE) && (*src < 0x20 || (*src & 0x80))) { + /* non-printable characters */ + if (dlen < 4) + goto out; + dlen -= 4; + *dst++ = '\\'; + *dst++ = ((*src & 0300) >> 6) + '0'; + *dst++ = ((*src & 0070) >> 3) + '0'; + *dst++ = ((*src & 0007) >> 0) + '0'; + } else if (*src == '\\') { + /* quote characters */ + if (dlen < 2) + goto out; + dlen -= 2; + *dst++ = '\\'; + *dst++ = '\\'; + } else { + /* normal characters */ + if (dlen < 1) + goto out; + *dst++ = *src; + } + ++src, --slen; + } +out: + if (dlen > 0) { + *dst++ = '\0'; + return slen ? -1 : 0; + } else { + *--dst = '\0'; + return -1; + } +} |
