summaryrefslogtreecommitdiff
path: root/src/lib/libc/string
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/string')
-rw-r--r--src/lib/libc/string/Makefile.inc152
-rw-r--r--src/lib/libc/string/__strerror.c110
-rw-r--r--src/lib/libc/string/__strsignal.c98
-rw-r--r--src/lib/libc/string/bcmp.366
-rw-r--r--src/lib/libc/string/bcmp.c59
-rw-r--r--src/lib/libc/string/bcopy.367
-rw-r--r--src/lib/libc/string/bcopy.c134
-rw-r--r--src/lib/libc/string/bm.3111
-rw-r--r--src/lib/libc/string/bm.c215
-rw-r--r--src/lib/libc/string/bstring.3106
-rw-r--r--src/lib/libc/string/bzero.361
-rw-r--r--src/lib/libc/string/bzero.c50
-rw-r--r--src/lib/libc/string/ffs.356
-rw-r--r--src/lib/libc/string/ffs.c48
-rw-r--r--src/lib/libc/string/index.c50
-rw-r--r--src/lib/libc/string/memccpy.373
-rw-r--r--src/lib/libc/string/memccpy.c60
-rw-r--r--src/lib/libc/string/memchr.374
-rw-r--r--src/lib/libc/string/memchr.c51
-rw-r--r--src/lib/libc/string/memcmp.377
-rw-r--r--src/lib/libc/string/memcmp.c54
-rw-r--r--src/lib/libc/string/memcpy.377
-rw-r--r--src/lib/libc/string/memmove.370
-rw-r--r--src/lib/libc/string/memset.367
-rw-r--r--src/lib/libc/string/memset.c51
-rw-r--r--src/lib/libc/string/rindex.c52
-rw-r--r--src/lib/libc/string/strcasecmp.384
-rw-r--r--src/lib/libc/string/strcasecmp.c113
-rw-r--r--src/lib/libc/string/strcat.3131
-rw-r--r--src/lib/libc/string/strcat.c53
-rw-r--r--src/lib/libc/string/strchr.3102
-rw-r--r--src/lib/libc/string/strcmp.387
-rw-r--r--src/lib/libc/string/strcmp.c53
-rw-r--r--src/lib/libc/string/strcoll.368
-rw-r--r--src/lib/libc/string/strcoll.c47
-rw-r--r--src/lib/libc/string/strcpy.3150
-rw-r--r--src/lib/libc/string/strcpy.c52
-rw-r--r--src/lib/libc/string/strcspn.388
-rw-r--r--src/lib/libc/string/strcspn.c61
-rw-r--r--src/lib/libc/string/strdup.384
-rw-r--r--src/lib/libc/string/strdup.c57
-rw-r--r--src/lib/libc/string/strerror.385
-rw-r--r--src/lib/libc/string/strerror.c50
-rw-r--r--src/lib/libc/string/strerror_r.c30
-rw-r--r--src/lib/libc/string/string.3157
-rw-r--r--src/lib/libc/string/strlcat.c59
-rw-r--r--src/lib/libc/string/strlcpy.3179
-rw-r--r--src/lib/libc/string/strlcpy.c55
-rw-r--r--src/lib/libc/string/strlen.361
-rw-r--r--src/lib/libc/string/strlen.c51
-rw-r--r--src/lib/libc/string/strmode.3159
-rw-r--r--src/lib/libc/string/strmode.c148
-rw-r--r--src/lib/libc/string/strncat.c60
-rw-r--r--src/lib/libc/string/strncmp.c53
-rw-r--r--src/lib/libc/string/strncpy.c60
-rw-r--r--src/lib/libc/string/strpbrk.372
-rw-r--r--src/lib/libc/string/strpbrk.c51
-rw-r--r--src/lib/libc/string/strrchr.3104
-rw-r--r--src/lib/libc/string/strsep.3112
-rw-r--r--src/lib/libc/string/strsep.c79
-rw-r--r--src/lib/libc/string/strsignal.356
-rw-r--r--src/lib/libc/string/strsignal.c45
-rw-r--r--src/lib/libc/string/strspn.384
-rw-r--r--src/lib/libc/string/strspn.c54
-rw-r--r--src/lib/libc/string/strstr.381
-rw-r--r--src/lib/libc/string/strstr.c59
-rw-r--r--src/lib/libc/string/strtok.3152
-rw-r--r--src/lib/libc/string/strtok.c90
-rw-r--r--src/lib/libc/string/strxfrm.362
-rw-r--r--src/lib/libc/string/strxfrm.c54
-rw-r--r--src/lib/libc/string/swab.361
-rw-r--r--src/lib/libc/string/swab.c58
72 files changed, 5800 insertions, 0 deletions
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc
new file mode 100644
index 0000000000..26e2bc10f2
--- /dev/null
+++ b/src/lib/libc/string/Makefile.inc
@@ -0,0 +1,152 @@
1# $OpenBSD: Makefile.inc,v 1.12 2003/02/20 03:20:09 deraadt Exp $
2
3# string sources
4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string ${LIBCSRCDIR}/string
5
6SRCS+= bm.c memccpy.c strcasecmp.c strcoll.c strdup.c strerror.c \
7 strerror_r.c strlcat.c strmode.c strsignal.c strtok.c strxfrm.c \
8 __strerror.c __strsignal.c
9
10# machine-dependent net sources
11# m-d Makefile.inc must include sources for:
12# bcmp() bcopy() bzero() ffs() index() memchr() memcmp() memset()
13# rindex() strcat() strcmp() strcpy() strcspn() strlen() strlcpy()
14# strncat() strncmp() strncpy() strpbrk() strsep()
15# strspn() strstr() swav()
16# m-d Makefile.inc may include sources for:
17# memcpy() memmove() strchr() strrchr()
18
19.include "${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string/Makefile.inc"
20
21# if no machine specific memmove(3), build one out of bcopy(3).
22.if empty(SRCS:Mmemmove.S)
23OBJS+= memmove.o
24memmove.o: bcopy.c
25 ${CC} -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET}
26 @${LD} -x -r ${.TARGET}
27 @mv a.out ${.TARGET}
28
29memmove.go: bcopy.c
30 ${CC} -g -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET}
31 @${LD} -x -r ${.TARGET}
32 @mv a.out ${.TARGET}
33
34memmove.po: bcopy.c
35 ${CC} -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET}
36 @${LD} -X -r ${.TARGET}
37 @mv a.out ${.TARGET}
38
39memmove.so: bcopy.c
40 ${CC} ${PICFLAG} -DPIC -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \
41 -o ${.TARGET}
42.endif
43
44# if no machine specific memcpy(3), build one out of bcopy(3).
45# if there is a machine specific memmove(3), we'll assume it aliases
46# memcpy(3).
47.if empty(SRCS:Mmemcpy.S)
48.if empty(SRCS:Mmemmove.S)
49OBJS+= memcpy.o
50memcpy.o: bcopy.c
51 ${CC} -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET}
52 @${LD} -x -r ${.TARGET}
53 @mv a.out ${.TARGET}
54
55memcpy.go: bcopy.c
56 ${CC} -g -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET}
57 @${LD} -x -r ${.TARGET}
58 @mv a.out ${.TARGET}
59
60memcpy.po: bcopy.c
61 ${CC} -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET}
62 @${LD} -X -r ${.TARGET}
63 @mv a.out ${.TARGET}
64
65memcpy.so: bcopy.c
66 ${CC} ${PICFLAG} -DPIC -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \
67 -o ${.TARGET}
68.endif
69.endif
70
71# if no machine specific strchr(3), build one out of index(3).
72.if empty(SRCS:Mstrchr.S)
73OBJS+= strchr.o
74strchr.o: index.c
75 ${CC} -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET}
76 @${LD} -x -r ${.TARGET}
77 @mv a.out ${.TARGET}
78
79strchr.go: index.c
80 ${CC} -g -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET}
81 @${LD} -x -r ${.TARGET}
82 @mv a.out ${.TARGET}
83
84strchr.po: index.c
85 ${CC} -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET}
86 @${LD} -X -r ${.TARGET}
87 @mv a.out ${.TARGET}
88
89strchr.so: index.c
90 ${CC} ${PICFLAG} -DPIC -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \
91 -o ${.TARGET}
92.endif
93
94# if no machine specific strrchr(3), build one out of rindex(3).
95.if empty(SRCS:Mstrrchr.S)
96OBJS+= strrchr.o
97strrchr.o: rindex.c
98 ${CC} -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET}
99 @${LD} -x -r ${.TARGET}
100 @mv a.out ${.TARGET}
101
102strrchr.go: rindex.c
103 ${CC} -g -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET}
104 @${LD} -x -r ${.TARGET}
105 @mv a.out ${.TARGET}
106
107strrchr.po: rindex.c
108 ${CC} -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET}
109 @${LD} -X -r ${.TARGET}
110 @mv a.out ${.TARGET}
111
112strrchr.so: rindex.c
113 ${CC} ${PICFLAG} -DPIC -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \
114 -o ${.TARGET}
115.endif
116
117# build .ln files for memmove, memcpy, strchr and strrchr always from
118# bcopy, index, and rindex
119LOBJS+= memmove.ln memcpy.ln strchr.ln strrchr.ln
120
121memmove.ln: bcopy.c
122 lint ${LINTFLAGS} -DMEMMOVE ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \
123 ${LIBCSRCDIR}/string/bcopy.c
124
125memcpy.ln: bcopy.c
126 lint ${LINTFLAGS} -DMEMCOPY ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \
127 ${LIBCSRCDIR}/string/bcopy.c
128
129strchr.ln: index.c
130 lint ${LINTFLAGS} -DSTRCHR ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \
131 ${LIBCSRCDIR}/string/index.c
132
133strrchr.ln: rindex.c
134 lint ${LINTFLAGS} -DSTRRCHR ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \
135 ${LIBCSRCDIR}/string/rindex.c
136
137MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \
138 memcmp.3 memcpy.3 memmove.3 memset.3 strcasecmp.3 strcat.3 \
139 strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strerror.3 \
140 string.3 strlen.3 strmode.3 strdup.3 strpbrk.3 strrchr.3 strsep.3 \
141 strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 strlcpy.3
142
143MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3
144MLINKS+=strchr.3 index.3
145MLINKS+=strrchr.3 rindex.3
146MLINKS+=strcasecmp.3 strncasecmp.3
147MLINKS+=strcat.3 strncat.3
148MLINKS+=strcmp.3 strncmp.3
149MLINKS+=strcpy.3 strncpy.3
150MLINKS+=strlcpy.3 strlcat.3
151MLINKS+=strtok.3 strtok_r.3
152MLINKS+=strerror.3 strerror_r.3
diff --git a/src/lib/libc/string/__strerror.c b/src/lib/libc/string/__strerror.c
new file mode 100644
index 0000000000..15436eaab2
--- /dev/null
+++ b/src/lib/libc/string/__strerror.c
@@ -0,0 +1,110 @@
1/*
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: __strerror.c,v 1.10 2003/06/02 20:18:38 millert Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#ifdef NLS
35#define catclose _catclose
36#define catgets _catgets
37#define catopen _catopen
38#include <nl_types.h>
39#endif
40
41#define sys_errlist _sys_errlist
42#define sys_nerr _sys_nerr
43
44#include <errno.h>
45#include <limits.h>
46#include <stdio.h>
47#include <string.h>
48
49static char *
50itoa(num)
51 int num;
52{
53 static char buffer[11];
54 char *p;
55
56 p = buffer + 4;
57 while (num >= 10) {
58 *--p = (num % 10) + '0';
59 num /= 10;
60 }
61 *p = (num % 10) + '0';
62 return p;
63}
64
65/*
66 * Since perror() is not allowed to change the contents of strerror()'s
67 * static buffer, both functions supply their own buffers to the
68 * internal function __strerror().
69 */
70
71char *
72__strerror(num, buf)
73 int num;
74 char *buf;
75{
76#define UPREFIX "Unknown error: "
77 register unsigned int errnum;
78#ifdef NLS
79 int save_errno;
80 nl_catd catd;
81
82 catd = catopen("libc", 0);
83#endif
84
85 errnum = num; /* convert to unsigned */
86 if (errnum < sys_nerr) {
87#ifdef NLS
88 strlcpy(buf, catgets(catd, 1, errnum,
89 (char *)sys_errlist[errnum]), NL_TEXTMAX);
90#else
91 return(sys_errlist[errnum]);
92#endif
93 } else {
94#ifdef NLS
95 strlcpy(buf, catgets(catd, 1, 0xffff, UPREFIX), NL_TEXTMAX);
96#else
97 strlcpy(buf, UPREFIX, NL_TEXTMAX);
98#endif
99 strlcat(buf, itoa(errnum), NL_TEXTMAX);
100 errno = EINVAL;
101 }
102
103#ifdef NLS
104 save_errno = errno;
105 catclose(catd);
106 errno = save_errno;
107#endif
108
109 return buf;
110}
diff --git a/src/lib/libc/string/__strsignal.c b/src/lib/libc/string/__strsignal.c
new file mode 100644
index 0000000000..5bab3cd0af
--- /dev/null
+++ b/src/lib/libc/string/__strsignal.c
@@ -0,0 +1,98 @@
1/*
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: __strsignal.c,v 1.8 2003/06/02 20:18:38 millert Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#ifdef NLS
35#define catclose _catclose
36#define catgets _catgets
37#define catopen _catopen
38#include <nl_types.h>
39#endif
40
41#define sys_siglist _sys_siglist
42
43#include <stdio.h>
44#include <limits.h>
45#include <signal.h>
46#include <string.h>
47
48static char *itoa(num)
49 int num;
50{
51 static char buffer[11];
52 char *p;
53
54 p = buffer + 4;
55 while (num >= 10) {
56 *--p = (num % 10) + '0';
57 num /= 10;
58 }
59 *p = (num % 10) + '0';
60 return p;
61}
62
63char *
64__strsignal(num, buf)
65 int num;
66 char *buf;
67{
68#define UPREFIX "Unknown signal: "
69 register unsigned int signum;
70
71#ifdef NLS
72 nl_catd catd ;
73 catd = catopen("libc", 0);
74#endif
75
76 signum = num; /* convert to unsigned */
77 if (signum < NSIG) {
78#ifdef NLS
79 strlcpy(buf, catgets(catd, 2, signum,
80 (char *)sys_siglist[signum]), NL_TEXTMAX);
81#else
82 return((char *)sys_siglist[signum]);
83#endif
84 } else {
85#ifdef NLS
86 strlcpy(buf, catgets(catd, 1, 0xffff, UPREFIX), NL_TEXTMAX);
87#else
88 strlcpy(buf, UPREFIX, NL_TEXTMAX);
89#endif
90 strlcat(buf, itoa(signum), NL_TEXTMAX);
91 }
92
93#ifdef NLS
94 catclose(catd);
95#endif
96
97 return buf;
98}
diff --git a/src/lib/libc/string/bcmp.3 b/src/lib/libc/string/bcmp.3
new file mode 100644
index 0000000000..953476caa7
--- /dev/null
+++ b/src/lib/libc/string/bcmp.3
@@ -0,0 +1,66 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek.
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" $OpenBSD: bcmp.3,v 1.6 2003/06/02 20:18:38 millert Exp $
31.\"
32.Dd April 19, 1991
33.Dt BCMP 3
34.Os
35.Sh NAME
36.Nm bcmp
37.Nd compare byte string
38.Sh SYNOPSIS
39.Fd #include <string.h>
40.Ft int
41.Fn bcmp "const void *b1" "const void *b2" "size_t len"
42.Sh DESCRIPTION
43The
44.Fn bcmp
45function compares byte string
46.Fa b1
47against byte string
48.Fa b2 ,
49returning zero if they are identical, non-zero otherwise.
50Both strings are assumed to be
51.Fa len
52bytes long.
53Zero-length strings are always identical.
54.Pp
55The strings may overlap.
56.Sh SEE ALSO
57.Xr memcmp 3 ,
58.Xr strcasecmp 3 ,
59.Xr strcmp 3 ,
60.Xr strcoll 3 ,
61.Xr strxfrm 3
62.Sh HISTORY
63A
64.Fn bcmp
65function first appeared in
66.Bx 4.2 .
diff --git a/src/lib/libc/string/bcmp.c b/src/lib/libc/string/bcmp.c
new file mode 100644
index 0000000000..2beeef8298
--- /dev/null
+++ b/src/lib/libc/string/bcmp.c
@@ -0,0 +1,59 @@
1/*
2 * Copyright (c) 1987 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: bcmp.c,v 1.5 2003/06/02 20:18:38 millert Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#ifndef _KERNEL
35#include <string.h>
36#else
37#include <lib/libkern/libkern.h>
38#endif
39
40/*
41 * bcmp -- vax cmpc3 instruction
42 */
43int
44bcmp(b1, b2, length)
45 const void *b1, *b2;
46 register size_t length;
47{
48 register char *p1, *p2;
49
50 if (length == 0)
51 return(0);
52 p1 = (char *)b1;
53 p2 = (char *)b2;
54 do
55 if (*p1++ != *p2++)
56 break;
57 while (--length);
58 return(length);
59}
diff --git a/src/lib/libc/string/bcopy.3 b/src/lib/libc/string/bcopy.3
new file mode 100644
index 0000000000..7b55bcbf43
--- /dev/null
+++ b/src/lib/libc/string/bcopy.3
@@ -0,0 +1,67 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\" notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\" notice, this list of conditions and the following disclaimer in the
14.\" documentation and/or other materials provided with the distribution.
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
17.\" without specific prior written permission.
18.\"
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
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\" $OpenBSD: bcopy.3,v 1.8 2003/06/21 12:04:01 avsm Exp $
32.\"
33.Dd April 19, 1991
34.Dt BCOPY 3
35.Os
36.Sh NAME
37.Nm bcopy
38.Nd copy bytes
39.Sh SYNOPSIS
40.Fd #include <string.h>
41.Ft void
42.Fn bcopy "const void *src" "void *dst" "size_t len"
43.Sh DESCRIPTION
44The
45.Fn bcopy
46function copies
47.Fa len
48bytes from buffer
49.Fa src
50to buffer
51.Fa dst .
52The two buffers may overlap.
53If
54.Fa len
55is zero, no bytes are copied.
56.Sh SEE ALSO
57.Xr memccpy 3 ,
58.Xr memcpy 3 ,
59.Xr memmove 3 ,
60.Xr strcpy 3 ,
61.Xr strlcpy 3 ,
62.Xr strncpy 3
63.Sh HISTORY
64A
65.Fn bcopy
66function appeared in
67.Bx 4.2 .
diff --git a/src/lib/libc/string/bcopy.c b/src/lib/libc/string/bcopy.c
new file mode 100644
index 0000000000..c48faa145e
--- /dev/null
+++ b/src/lib/libc/string/bcopy.c
@@ -0,0 +1,134 @@
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char *rcsid = "$OpenBSD: bcopy.c,v 1.3 2003/06/02 20:18:38 millert Exp $";
35#endif /* LIBC_SCCS and not lint */
36
37#include <string.h>
38
39/*
40 * sizeof(word) MUST BE A POWER OF TWO
41 * SO THAT wmask BELOW IS ALL ONES
42 */
43typedef long word; /* "word" used for optimal copy speed */
44
45#define wsize sizeof(word)
46#define wmask (wsize - 1)
47
48/*
49 * Copy a block of memory, handling overlap.
50 * This is the routine that actually implements
51 * (the portable versions of) bcopy, memcpy, and memmove.
52 */
53#ifdef MEMCOPY
54void *
55memcpy(dst0, src0, length)
56#else
57#ifdef MEMMOVE
58void *
59memmove(dst0, src0, length)
60#else
61void
62bcopy(src0, dst0, length)
63#endif
64#endif
65 void *dst0;
66 const void *src0;
67 register size_t length;
68{
69 register char *dst = dst0;
70 register const char *src = src0;
71 register size_t t;
72
73 if (length == 0 || dst == src) /* nothing to do */
74 goto done;
75
76 /*
77 * Macros: loop-t-times; and loop-t-times, t>0
78 */
79#define TLOOP(s) if (t) TLOOP1(s)
80#define TLOOP1(s) do { s; } while (--t)
81
82 if ((unsigned long)dst < (unsigned long)src) {
83 /*
84 * Copy forward.
85 */
86 t = (long)src; /* only need low bits */
87 if ((t | (long)dst) & wmask) {
88 /*
89 * Try to align operands. This cannot be done
90 * unless the low bits match.
91 */
92 if ((t ^ (long)dst) & wmask || length < wsize)
93 t = length;
94 else
95 t = wsize - (t & wmask);
96 length -= t;
97 TLOOP1(*dst++ = *src++);
98 }
99 /*
100 * Copy whole words, then mop up any trailing bytes.
101 */
102 t = length / wsize;
103 TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
104 t = length & wmask;
105 TLOOP(*dst++ = *src++);
106 } else {
107 /*
108 * Copy backwards. Otherwise essentially the same.
109 * Alignment works as before, except that it takes
110 * (t&wmask) bytes to align, not wsize-(t&wmask).
111 */
112 src += length;
113 dst += length;
114 t = (long)src;
115 if ((t | (long)dst) & wmask) {
116 if ((t ^ (long)dst) & wmask || length <= wsize)
117 t = length;
118 else
119 t &= wmask;
120 length -= t;
121 TLOOP1(*--dst = *--src);
122 }
123 t = length / wsize;
124 TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
125 t = length & wmask;
126 TLOOP(*--dst = *--src);
127 }
128done:
129#if defined(MEMCOPY) || defined(MEMMOVE)
130 return (dst0);
131#else
132 return;
133#endif
134}
diff --git a/src/lib/libc/string/bm.3 b/src/lib/libc/string/bm.3
new file mode 100644
index 0000000000..772b0d372e
--- /dev/null
+++ b/src/lib/libc/string/bm.3
@@ -0,0 +1,111 @@
1.\" Copyright (c) 1994
2.\" The Regents of the University of California. All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Andrew Hume of AT&T Bell Laboratories.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\" notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\" notice, this list of conditions and the following disclaimer in the
14.\" documentation and/or other materials provided with the distribution.
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
17.\" without specific prior written permission.
18.\"
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
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\" $OpenBSD: bm.3,v 1.7 2003/06/02 20:18:38 millert Exp $
32.\"
33.Dd June 29, 1999
34.Dt BM 3
35.Os
36.Sh NAME
37.Nm bm_comp ,
38.Nm bm_exec ,
39.Nm bm_free
40.Nd Boyer-Moore string search
41.Sh SYNOPSIS
42.Fd #include <sys/types.h>
43.Fd #include <bm.h>
44.Ft bm_pat *
45.Fn bm_comp "u_char *pattern" "size_t patlen" "u_char freq[256]"
46.Ft u_char *
47.Fn bm_exec "bm_pat *pdesc" "u_char *text" "size_t len"
48.Ft void
49.Fn bm_free "bm_pat *pdesc"
50.Sh DESCRIPTION
51These routines implement an efficient mechanism to find an
52occurrence of a byte string within another byte string.
53.Pp
54.Fn bm_comp
55evaluates
56.Fa patlen
57bytes starting at
58.Fa pattern
59and returns a pointer to a structure describing them.
60The bytes referenced by
61.Fa pattern
62may be of any value.
63.Pp
64The search takes advantage of the frequency distribution of the
65bytes in the text to be searched.
66If specified,
67.Ar freq
68should be an array of 256 values,
69with higher values indicating that the corresponding character occurs
70more frequently.
71(A less than optimal frequency distribution can only result in less
72than optimal performance, not incorrect results.)
73If
74.Ar freq
75is
76.Dv NULL ,
77a system default table is used.
78.Pp
79.Fn bm_exec
80returns a pointer to the leftmost occurrence of the string given to
81.Fn bm_comp
82within
83.Ar text ,
84or
85.Dv NULL
86if none occurs.
87The number of bytes in
88.Ar text
89must be specified by
90.Ar len .
91.Pp
92Space allocated for the returned description is discarded
93by calling
94.Fn bm_free
95with the returned description as an argument.
96.Pp
97The asymptotic speed of
98.Fn bm_exec
99is
100.Pf O Ns Pq len / patlen .
101.Sh SEE ALSO
102.Xr regexp 3 ,
103.Xr strstr 3
104.Rs
105.%R "Fast String Searching"
106.%A Andrew Hume
107.%A Daniel Sunday
108.%J "Software Practice and Experience"
109.%V Volume 21, 11 (November 1991)
110.%P 1221-48
111.Re
diff --git a/src/lib/libc/string/bm.c b/src/lib/libc/string/bm.c
new file mode 100644
index 0000000000..2c7f8fac32
--- /dev/null
+++ b/src/lib/libc/string/bm.c
@@ -0,0 +1,215 @@
1/*-
2 * Copyright (c) 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Andrew Hume of AT&T Bell Laboratories.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char *rcsid = "$OpenBSD: bm.c,v 1.4 2003/06/02 20:18:38 millert Exp $";
35#endif /* LIBC_SCCS and not lint */
36
37#include <sys/types.h>
38
39#include <bm.h>
40#include <errno.h>
41#include <stdlib.h>
42#include <string.h>
43
44/*
45 * XXX
46 * The default frequency table starts at 99 and counts down. The default
47 * table should probably be oriented toward text, and will necessarily be
48 * locale specific. This one is for English. It was derived from the
49 * OSF/1 and 4.4BSD formatted and unformatted manual pages, and about 100Mb
50 * of email and random text. Change it if you can find something better.
51 */
52static u_char const freq_def[256] = {
53 0, 0, 0, 0, 0, 0, 0, 0,
54 0, 77, 90, 0, 0, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0,
56 0, 0, 0, 0, 0, 0, 0, 0,
57 99, 28, 42, 27, 16, 14, 20, 51,
58 66, 65, 59, 24, 75, 76, 84, 56,
59 72, 74, 64, 55, 54, 47, 41, 37,
60 44, 61, 70, 43, 23, 53, 49, 22,
61 33, 58, 40, 46, 45, 57, 60, 26,
62 30, 63, 21, 12, 32, 50, 38, 39,
63 34, 11, 48, 67, 62, 35, 15, 29,
64 71, 18, 9, 17, 25, 13, 10, 52,
65 36, 95, 78, 86, 87, 98, 82, 80,
66 88, 94, 19, 68, 89, 83, 93, 96,
67 81, 7, 91, 92, 97, 85, 69, 73,
68 31, 79, 8, 5, 4, 6, 3, 0,
69 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0, 0, 0, 0,
79 0, 0, 0, 0, 0, 0, 0, 0,
80 0, 0, 0, 0, 0, 0, 0, 0,
81 0, 0, 0, 0, 0, 0, 0, 0,
82 0, 0, 0, 0, 0, 0, 0, 0,
83 0, 0, 0, 0, 0, 0, 0, 0,
84 0, 0, 0, 0, 0, 0, 0, 0,
85};
86
87bm_pat *
88bm_comp(pb, len, freq)
89 u_char const *pb;
90 size_t len;
91 u_char const *freq;
92{
93 register u_char const *pe, *p;
94 register size_t *d, r;
95 register int j;
96 int sv_errno;
97 bm_pat *pat;
98
99 if (len == 0) {
100 errno = EINVAL;
101 return (NULL);
102 }
103 if ((pat = malloc(sizeof(*pat))) == NULL)
104 return (NULL);
105 pat->pat = NULL;
106 pat->delta = NULL;
107
108 pat->patlen = len; /* copy pattern */
109 if ((pat->pat = malloc(pat->patlen)) == NULL)
110 goto mem;
111 memcpy(pat->pat, pb, pat->patlen);
112 /* get skip delta */
113 if ((pat->delta = malloc(256 * sizeof(*d))) == NULL)
114 goto mem;
115 for (j = 0, d = pat->delta; j < 256; j++)
116 d[j] = pat->patlen;
117 for (pe = pb + pat->patlen - 1; pb <= pe; pb++)
118 d[*pb] = pe - pb;
119
120 if (freq == NULL) /* default freq table */
121 freq = freq_def;
122 r = 0; /* get guard */
123 for (pb = pat->pat, pe = pb + pat->patlen - 1; pb < pe; pb++)
124 if (freq[*pb] < freq[pat->pat[r]])
125 r = pb - pat->pat;
126 pat->rarec = pat->pat[r];
127 pat->rareoff = r - (pat->patlen - 1);
128
129 /* get md2 shift */
130 for (pe = pat->pat + pat->patlen - 1, p = pe - 1; p >= pat->pat; p--)
131 if (*p == *pe)
132 break;
133
134 /* *p is first leftward reoccurrence of *pe */
135 pat->md2 = pe - p;
136 return (pat);
137
138mem: sv_errno = errno;
139 bm_free(pat);
140 errno = sv_errno;
141 return (NULL);
142}
143
144void
145bm_free(pat)
146 bm_pat *pat;
147{
148 if (pat->pat != NULL)
149 free(pat->pat);
150 if (pat->delta != NULL)
151 free(pat->delta);
152 free(pat);
153}
154
155u_char *
156bm_exec(pat, base, n)
157 bm_pat *pat;
158 u_char *base;
159 size_t n;
160{
161 register u_char *e, *ep, *p, *q, *s;
162 register size_t *d0, k, md2, n1, ro;
163 register int rc;
164
165 if (n == 0)
166 return (NULL);
167
168 d0 = pat->delta;
169 n1 = pat->patlen - 1;
170 md2 = pat->md2;
171 ro = pat->rareoff;
172 rc = pat->rarec;
173 ep = pat->pat + pat->patlen - 1;
174 s = base + (pat->patlen - 1);
175
176 /* fast loop up to n - 3 * patlen */
177 e = base + n - 3 * pat->patlen;
178 while (s < e) {
179 k = d0[*s]; /* ufast skip loop */
180 while (k) {
181 k = d0[*(s += k)];
182 k = d0[*(s += k)];
183 }
184 if (s >= e)
185 break;
186 if (s[ro] != rc) /* guard test */
187 goto mismatch1;
188 /* fwd match */
189 for (p = pat->pat, q = s - n1; p < ep;)
190 if (*q++ != *p++)
191 goto mismatch1;
192 return (s - n1);
193
194mismatch1: s += md2; /* md2 shift */
195 }
196
197 /* slow loop up to end */
198 e = base + n;
199 while (s < e) {
200 s += d0[*s]; /* step */
201 if (s >= e)
202 break;
203 if (s[ro] != rc) /* guard test */
204 goto mismatch2;
205 /* fwd match */
206 for (p = pat->pat, q = s - n1; p <= ep;)
207 if (*q++ != *p++)
208 goto mismatch2;
209 return (s - n1);
210
211mismatch2: s += md2; /* md2 shift */
212 }
213
214 return (NULL);
215}
diff --git a/src/lib/libc/string/bstring.3 b/src/lib/libc/string/bstring.3
new file mode 100644
index 0000000000..54d8302ff2
--- /dev/null
+++ b/src/lib/libc/string/bstring.3
@@ -0,0 +1,106 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek.
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" $OpenBSD: bstring.3,v 1.5 2003/06/02 20:18:38 millert Exp $
31.\"
32.Dd April 19, 1991
33.Dt BSTRING 3
34.Os
35.Sh NAME
36.Nm bcmp ,
37.Nm bcopy ,
38.Nm bzero ,
39.Nm memccpy ,
40.Nm memchr ,
41.Nm memcmp ,
42.Nm memcpy ,
43.Nm memmove ,
44.Nm memset
45.Nd byte string operations
46.Sh SYNOPSIS
47.Fd #include <string.h>
48.Ft int
49.Fn bcmp "const void *b1" "const void *b2" "size_t len"
50.Ft void
51.Fn bcopy "const void *src" "void *dst" "size_t len"
52.Ft void
53.Fn bzero "void *b" "size_t len"
54.Ft void *
55.Fn memchr "const void *b" "int c" "size_t len"
56.Ft int
57.Fn memcmp "const void *b1" "const void *b2" "size_t len"
58.Ft void *
59.Fn memccpy "void *dst" "const void *src" "int c" "size_t len"
60.Ft void *
61.Fn memcpy "void *dst" "const void *src" "size_t len"
62.Ft void *
63.Fn memmove "void *dst" "const void *src" "size_t len"
64.Ft void *
65.Fn memset "void *b" "int c" "size_t len"
66.Sh DESCRIPTION
67These functions operate on variable length strings of bytes.
68They do not check for terminating null bytes as the routines
69listed in
70.Xr string 3
71do.
72.Pp
73See the specific manual pages for more information.
74.Sh SEE ALSO
75.Xr bcmp 3 ,
76.Xr bcopy 3 ,
77.Xr bzero 3 ,
78.Xr memccpy 3 ,
79.Xr memchr 3 ,
80.Xr memcmp 3 ,
81.Xr memcpy 3 ,
82.Xr memmove 3 ,
83.Xr memset 3
84.Sh STANDARDS
85The functions
86.Fn memchr ,
87.Fn memcmp ,
88.Fn memcpy ,
89.Fn memmove ,
90and
91.Fn memset
92conform to
93.St -ansiC .
94.Sh HISTORY
95The
96.Fn bzero
97and
98.Fn memccpy
99functions appeared in
100.Bx 4.3 .
101The
102.Fn bcmp
103and
104.Fn bcopy
105functions appeared in
106.Bx 4.2 .
diff --git a/src/lib/libc/string/bzero.3 b/src/lib/libc/string/bzero.3
new file mode 100644
index 0000000000..592d9f2f08
--- /dev/null
+++ b/src/lib/libc/string/bzero.3
@@ -0,0 +1,61 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek.
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" $OpenBSD: bzero.3,v 1.6 2003/06/02 20:18:38 millert Exp $
31.\"
32.Dd April 19, 1991
33.Dt BZERO 3
34.Os
35.Sh NAME
36.Nm bzero
37.Nd write zeroes to a byte string
38.Sh SYNOPSIS
39.Fd #include <string.h>
40.Ft void
41.Fn bzero "void *b" "size_t len"
42.Sh DESCRIPTION
43The
44.Fn bzero
45function writes
46.Fa len
47zero bytes to the string
48.Fa b .
49If
50.Fa len
51is zero,
52.Fn bzero
53does nothing.
54.Sh SEE ALSO
55.Xr memset 3 ,
56.Xr swab 3
57.Sh HISTORY
58A
59.Fn bzero
60function appeared in
61.Bx 4.3 .
diff --git a/src/lib/libc/string/bzero.c b/src/lib/libc/string/bzero.c
new file mode 100644
index 0000000000..7e557ecd4a
--- /dev/null
+++ b/src/lib/libc/string/bzero.c
@@ -0,0 +1,50 @@
1/*
2 * Copyright (c) 1987 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: bzero.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#ifndef _KERNEL
35#include <string.h>
36#else
37#include <lib/libkern/libkern.h>
38#endif
39
40/*
41 * bzero -- vax movc5 instruction
42 */
43void
44bzero(void *b, size_t length)
45{
46 char *p;
47
48 for (p = b; length--;)
49 *p++ = '\0';
50}
diff --git a/src/lib/libc/string/ffs.3 b/src/lib/libc/string/ffs.3
new file mode 100644
index 0000000000..a2f0cd336d
--- /dev/null
+++ b/src/lib/libc/string/ffs.3
@@ -0,0 +1,56 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek.
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" $OpenBSD: ffs.3,v 1.4 2003/06/02 20:18:38 millert Exp $
31.\"
32.Dd April 19, 1991
33.Dt FFS 3
34.Os
35.Sh NAME
36.Nm ffs
37.Nd find first bit set in a bit string
38.Sh SYNOPSIS
39.Fd #include <string.h>
40.Ft int
41.Fn ffs "int value"
42.Sh DESCRIPTION
43The
44.Fn ffs
45function finds the first bit set in
46.Fa value
47and returns the index of that bit.
48Bits are numbered starting from 1, starting at the rightmost bit.
49A return value of 0 means that the argument was zero.
50.Sh SEE ALSO
51.Xr bitstring 3
52.Sh HISTORY
53The
54.Fn ffs
55function appeared in
56.Bx 4.3 .
diff --git a/src/lib/libc/string/ffs.c b/src/lib/libc/string/ffs.c
new file mode 100644
index 0000000000..f1d14096a2
--- /dev/null
+++ b/src/lib/libc/string/ffs.c
@@ -0,0 +1,48 @@
1/* $OpenBSD: ffs.c,v 1.6 2003/06/11 21:08:16 deraadt Exp $ */
2
3/*
4 * Public domain.
5 * Written by Dale Rahn.
6 */
7
8#if defined(LIBC_SCCS) && !defined(lint)
9static char *rcsid = "$OpenBSD: ffs.c,v 1.6 2003/06/11 21:08:16 deraadt Exp $";
10#endif /* LIBC_SCCS and not lint */
11
12#if !defined(_KERNEL) && !defined(_STANDALONE)
13#include <string.h>
14#else
15#include <lib/libkern/libkern.h>
16#endif
17
18/*
19 * ffs -- vax ffs instruction
20 */
21int
22ffs(int mask)
23{
24 int bit;
25 unsigned int r = mask;
26 static const signed char t[16] = {
27 -28, 1, 2, 1,
28 3, 1, 2, 1,
29 4, 1, 2, 1,
30 3, 1, 2, 1
31 };
32
33 bit = 0;
34 if (!(r & 0xffff)) {
35 bit += 16;
36 r >>= 16;
37 }
38 if (!(r & 0xff)) {
39 bit += 8;
40 r >>= 8;
41 }
42 if (!(r & 0xf)) {
43 bit += 4;
44 r >>= 4;
45 }
46
47 return (bit + t[ r & 0xf ]);
48}
diff --git a/src/lib/libc/string/index.c b/src/lib/libc/string/index.c
new file mode 100644
index 0000000000..52d0fd46fa
--- /dev/null
+++ b/src/lib/libc/string/index.c
@@ -0,0 +1,50 @@
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: index.c,v 1.4 2003/06/02 20:18:38 millert Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#include <string.h>
35
36char *
37#ifdef STRCHR
38strchr(const char *p, int ch)
39#else
40index(const char *p, int ch)
41#endif
42{
43 for (;; ++p) {
44 if (*p == ch)
45 return((char *)p);
46 if (!*p)
47 return((char *)NULL);
48 }
49 /* NOTREACHED */
50}
diff --git a/src/lib/libc/string/memccpy.3 b/src/lib/libc/string/memccpy.3
new file mode 100644
index 0000000000..8c159cb8de
--- /dev/null
+++ b/src/lib/libc/string/memccpy.3
@@ -0,0 +1,73 @@
1.\" $OpenBSD: memccpy.3,v 1.8 2003/06/02 20:18:38 millert Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" @(#)memccpy.3 8.1 (Berkeley) 6/9/93
31.\"
32.Dd June 9, 1993
33.Dt MEMCCPY 3
34.Os
35.Sh NAME
36.Nm memccpy
37.Nd copy string until character found
38.Sh SYNOPSIS
39.Fd #include <string.h>
40.Ft void *
41.Fn memccpy "void *dst" "const void *src" "int c" "size_t len"
42.Sh DESCRIPTION
43The
44.Fn memccpy
45function copies bytes from string
46.Fa src
47to string
48.Fa dst .
49If the character
50.Fa c
51(as converted to an
52.Li unsigned char )
53occurs in the string
54.Fa src ,
55the copy stops and a pointer to the byte after the copy of
56.Fa c
57in the string
58.Fa dst
59is returned.
60Otherwise,
61.Fa len
62bytes are copied, and a null pointer is returned.
63.Sh SEE ALSO
64.Xr bcopy 3 ,
65.Xr memcpy 3 ,
66.Xr memmove 3 ,
67.Xr strcpy 3 ,
68.Xr strlcpy 3
69.Sh HISTORY
70The
71.Fn memccpy
72function first appeared in
73.Bx 4.4 .
diff --git a/src/lib/libc/string/memccpy.c b/src/lib/libc/string/memccpy.c
new file mode 100644
index 0000000000..37496d1c26
--- /dev/null
+++ b/src/lib/libc/string/memccpy.c
@@ -0,0 +1,60 @@
1/* $OpenBSD: memccpy.c,v 1.4 2003/06/02 20:18:38 millert Exp $ */
2
3/*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
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
17 * without specific prior written permission.
18 *
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
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#if defined(LIBC_SCCS) && !defined(lint)
33#if 0
34static char sccsid[] = "@(#)memccpy.c 8.1 (Berkeley) 6/4/93";
35#else
36static char *rcsid = "$OpenBSD: memccpy.c,v 1.4 2003/06/02 20:18:38 millert Exp $";
37#endif
38#endif /* LIBC_SCCS and not lint */
39
40#include <string.h>
41
42void *
43memccpy(t, f, c, n)
44 void *t;
45 const void *f;
46 int c;
47 register size_t n;
48{
49
50 if (n) {
51 register unsigned char *tp = t;
52 register const unsigned char *fp = f;
53 register unsigned char uc = c;
54 do {
55 if ((*tp++ = *fp++) == uc)
56 return (tp);
57 } while (--n != 0);
58 }
59 return (0);
60}
diff --git a/src/lib/libc/string/memchr.3 b/src/lib/libc/string/memchr.3
new file mode 100644
index 0000000000..ddca58bc5e
--- /dev/null
+++ b/src/lib/libc/string/memchr.3
@@ -0,0 +1,74 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: memchr.3,v 1.6 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt MEMCHR 3
36.Os
37.Sh NAME
38.Nm memchr
39.Nd locate byte in byte string
40.Sh SYNOPSIS
41.Fd #include <string.h>
42.Ft void *
43.Fn memchr "const void *b" "int c" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn memchr
47function locates the first occurrence of
48.Fa c
49(converted to an
50.Li unsigned char )
51in string
52.Fa b .
53.Sh RETURN VALUES
54The
55.Fn memchr
56function returns a pointer to the byte located, or
57.Dv NULL
58if no such byte exists within
59.Fa len
60bytes.
61.Sh SEE ALSO
62.Xr strchr 3 ,
63.Xr strcspn 3 ,
64.Xr strpbrk 3 ,
65.Xr strrchr 3 ,
66.Xr strsep 3 ,
67.Xr strspn 3 ,
68.Xr strstr 3 ,
69.Xr strtok 3
70.Sh STANDARDS
71The
72.Fn memchr
73function conforms to
74.St -ansiC .
diff --git a/src/lib/libc/string/memchr.c b/src/lib/libc/string/memchr.c
new file mode 100644
index 0000000000..454abcbc17
--- /dev/null
+++ b/src/lib/libc/string/memchr.c
@@ -0,0 +1,51 @@
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char *rcsid = "$OpenBSD: memchr.c,v 1.6 2003/06/11 21:08:16 deraadt Exp $";
35#endif /* LIBC_SCCS and not lint */
36
37#include <string.h>
38
39void *
40memchr(const void *s, int c, size_t n)
41{
42 if (n != 0) {
43 const unsigned char *p = s;
44
45 do {
46 if (*p++ == (unsigned char)c)
47 return ((void *)(p - 1));
48 } while (--n != 0);
49 }
50 return (NULL);
51}
diff --git a/src/lib/libc/string/memcmp.3 b/src/lib/libc/string/memcmp.3
new file mode 100644
index 0000000000..bd46e6898e
--- /dev/null
+++ b/src/lib/libc/string/memcmp.3
@@ -0,0 +1,77 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: memcmp.3,v 1.5 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt MEMCMP 3
36.Os
37.Sh NAME
38.Nm memcmp
39.Nd compare byte string
40.Sh SYNOPSIS
41.Fd #include <string.h>
42.Ft int
43.Fn memcmp "const void *b1" "const void *b2" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn memcmp
47function compares byte string
48.Fa b1
49against byte string
50.Fa b2 .
51Both strings are assumed to be
52.Fa len
53bytes long.
54.Sh RETURN VALUES
55The
56.Fn memcmp
57function returns zero if the two strings are identical,
58otherwise returns the difference between the first two differing bytes
59(treated as
60.Li unsigned char
61values, so that
62.Sq Li \e200
63is greater than
64.Sq Li \&\e0 ,
65for example).
66Zero-length strings are always identical.
67.Sh SEE ALSO
68.Xr bcmp 3 ,
69.Xr strcasecmp 3 ,
70.Xr strcmp 3 ,
71.Xr strcoll 3 ,
72.Xr strxfrm 3
73.Sh STANDARDS
74The
75.Fn memcmp
76function conforms to
77.St -ansiC .
diff --git a/src/lib/libc/string/memcmp.c b/src/lib/libc/string/memcmp.c
new file mode 100644
index 0000000000..6c83208a7b
--- /dev/null
+++ b/src/lib/libc/string/memcmp.c
@@ -0,0 +1,54 @@
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char *rcsid = "$OpenBSD: memcmp.c,v 1.4 2003/06/11 21:08:16 deraadt Exp $";
35#endif /* LIBC_SCCS and not lint */
36
37#include <string.h>
38
39/*
40 * Compare memory regions.
41 */
42int
43memcmp(const void *s1, const void *s2, size_t n)
44{
45 if (n != 0) {
46 const unsigned char *p1 = s1, *p2 = s2;
47
48 do {
49 if (*p1++ != *p2++)
50 return (*--p1 - *--p2);
51 } while (--n != 0);
52 }
53 return (0);
54}
diff --git a/src/lib/libc/string/memcpy.3 b/src/lib/libc/string/memcpy.3
new file mode 100644
index 0000000000..c8265d94a6
--- /dev/null
+++ b/src/lib/libc/string/memcpy.3
@@ -0,0 +1,77 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: memcpy.3,v 1.6 2003/06/21 12:04:01 avsm Exp $
33.\"
34.Dd June 29, 1991
35.Dt MEMCPY 3
36.Os
37.Sh NAME
38.Nm memcpy
39.Nd copy bytes
40.Sh SYNOPSIS
41.Fd #include <string.h>
42.Ft void *
43.Fn memcpy "void *dst" "const void *src" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn memcpy
47function copies
48.Fa len
49bytes from buffer
50.Fa src
51to buffer
52.Fa dst .
53.Sh RETURN VALUES
54The
55.Fn memcpy
56function returns the original value of
57.Fa dst .
58.Sh SEE ALSO
59.Xr bcopy 3 ,
60.Xr memccpy 3 ,
61.Xr memmove 3 ,
62.Xr strcpy 3 ,
63.Xr strlcpy 3
64.Sh STANDARDS
65The
66.Fn memcpy
67function conforms to
68.St -ansiC .
69.Sh BUGS
70In this implementation
71.Fn memcpy
72is implemented using
73.Xr bcopy 3 ,
74and therefore the buffers may overlap.
75On other systems, copying overlapping buffers may produce surprises.
76A simpler solution is to not use
77.Fn memcpy .
diff --git a/src/lib/libc/string/memmove.3 b/src/lib/libc/string/memmove.3
new file mode 100644
index 0000000000..d4a364eb36
--- /dev/null
+++ b/src/lib/libc/string/memmove.3
@@ -0,0 +1,70 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: memmove.3,v 1.6 2003/06/21 12:04:01 avsm Exp $
33.\"
34.Dd June 29, 1991
35.Dt MEMMOVE 3
36.Os
37.Sh NAME
38.Nm memmove
39.Nd copy bytes
40.Sh SYNOPSIS
41.Fd #include <string.h>
42.Ft void *
43.Fn memmove "void *dst" "const void *src" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn memmove
47function copies
48.Fa len
49bytes from buffer
50.Fa src
51to buffer
52.Fa dst .
53The two buffers may overlap;
54the copy is always done in a non-destructive manner.
55.Sh RETURN VALUES
56The
57.Fn memmove
58function returns the original value of
59.Fa dst .
60.Sh SEE ALSO
61.Xr bcopy 3 ,
62.Xr memccpy 3 ,
63.Xr memcpy 3 ,
64.Xr strcpy 3 ,
65.Xr strlcpy 3
66.Sh STANDARDS
67The
68.Fn memmove
69function conforms to
70.St -ansiC .
diff --git a/src/lib/libc/string/memset.3 b/src/lib/libc/string/memset.3
new file mode 100644
index 0000000000..291b1427dc
--- /dev/null
+++ b/src/lib/libc/string/memset.3
@@ -0,0 +1,67 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: memset.3,v 1.5 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt MEMSET 3
36.Os
37.Sh NAME
38.Nm memset
39.Nd write a byte to byte string
40.Sh SYNOPSIS
41.Fd #include <string.h>
42.Ft void *
43.Fn memset "void *b" "int c" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn memset
47function writes
48.Fa len
49bytes of value
50.Fa c
51(converted to an
52.Li unsigned char )
53to the string
54.Fa b .
55.Sh RETURN VALUES
56The
57.Fn memset
58function returns the original value of
59.Fa b .
60.Sh SEE ALSO
61.Xr bzero 3 ,
62.Xr swab 3
63.Sh STANDARDS
64The
65.Fn memset
66function conforms to
67.St -ansiC .
diff --git a/src/lib/libc/string/memset.c b/src/lib/libc/string/memset.c
new file mode 100644
index 0000000000..9bc0556faa
--- /dev/null
+++ b/src/lib/libc/string/memset.c
@@ -0,0 +1,51 @@
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char *rcsid = "$OpenBSD: memset.c,v 1.4 2003/06/11 21:08:16 deraadt Exp $";
35#endif /* LIBC_SCCS and not lint */
36
37#include <string.h>
38
39void *
40memset(void *dst, int c, size_t n)
41{
42
43 if (n != 0) {
44 char *d = dst;
45
46 do
47 *d++ = c;
48 while (--n != 0);
49 }
50 return (dst);
51}
diff --git a/src/lib/libc/string/rindex.c b/src/lib/libc/string/rindex.c
new file mode 100644
index 0000000000..b880d0eaeb
--- /dev/null
+++ b/src/lib/libc/string/rindex.c
@@ -0,0 +1,52 @@
1/*
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: rindex.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#include <string.h>
35
36char *
37#ifdef STRRCHR
38strrchr(const char *p, int ch)
39#else
40rindex(const char *p, int ch)
41#endif
42{
43 char *save;
44
45 for (save = NULL;; ++p) {
46 if (*p == ch)
47 save = (char *)p;
48 if (!*p)
49 return(save);
50 }
51 /* NOTREACHED */
52}
diff --git a/src/lib/libc/string/strcasecmp.3 b/src/lib/libc/string/strcasecmp.3
new file mode 100644
index 0000000000..e1f5d416d1
--- /dev/null
+++ b/src/lib/libc/string/strcasecmp.3
@@ -0,0 +1,84 @@
1.\" $OpenBSD: strcasecmp.3,v 1.8 2003/06/02 20:18:38 millert Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek.
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)strcasecmp.3 8.1 (Berkeley) 6/9/93
33.\"
34.Dd June 9, 1993
35.Dt STRCASECMP 3
36.Os
37.Sh NAME
38.Nm strcasecmp ,
39.Nm strncasecmp
40.Nd compare strings, ignoring case
41.Sh SYNOPSIS
42.Fd #include <string.h>
43.Ft int
44.Fn strcasecmp "const char *s1" "const char *s2"
45.Ft int
46.Fn strncasecmp "const char *s1" "const char *s2" "size_t len"
47.Sh DESCRIPTION
48The
49.Fn strcasecmp
50and
51.Fn strncasecmp
52functions compare the null-terminated strings
53.Fa s1
54and
55.Fa s2
56and return an integer greater than, equal to, or less than 0,
57according to whether
58.Fa s1
59is lexicographically greater than, equal to, or less than
60.Fa s2
61after translation of each corresponding character to lower-case.
62The strings themselves are not modified.
63The comparison is done using unsigned characters, so that
64.Sq Li \e200
65is greater than
66.Ql \e0 .
67.Pp
68.Fn strncasecmp
69compares at most
70.Fa len
71characters.
72.Sh SEE ALSO
73.Xr bcmp 3 ,
74.Xr memcmp 3 ,
75.Xr strcmp 3 ,
76.Xr strcoll 3 ,
77.Xr strxfrm 3
78.Sh HISTORY
79The
80.Fn strcasecmp
81and
82.Fn strncasecmp
83functions first appeared in
84.Bx 4.4 .
diff --git a/src/lib/libc/string/strcasecmp.c b/src/lib/libc/string/strcasecmp.c
new file mode 100644
index 0000000000..62da063444
--- /dev/null
+++ b/src/lib/libc/string/strcasecmp.c
@@ -0,0 +1,113 @@
1/* $OpenBSD: strcasecmp.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $ */
2
3/*
4 * Copyright (c) 1987, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
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
17 * without specific prior written permission.
18 *
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
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <string.h>
33
34#if defined(LIBC_SCCS) && !defined(lint)
35#if 0
36static char sccsid[] = "@(#)strcasecmp.c 8.1 (Berkeley) 6/4/93";
37#else
38static char *rcsid = "$OpenBSD: strcasecmp.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $";
39#endif
40#endif /* LIBC_SCCS and not lint */
41
42typedef unsigned char u_char;
43
44/*
45 * This array is designed for mapping upper and lower case letter
46 * together for a case independent comparison. The mappings are
47 * based upon ascii character sequences.
48 */
49static const u_char charmap[] = {
50 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
51 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
52 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
53 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
54 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
55 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
56 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
57 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
58 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
59 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
60 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
61 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
62 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
63 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
64 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
65 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
66 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
67 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
68 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
69 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
70 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
71 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
72 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
73 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
74 '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
75 '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
76 '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
77 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
78 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
79 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
80 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
81 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
82};
83
84int
85strcasecmp(const char *s1, const char *s2)
86{
87 const u_char *cm = charmap;
88 const u_char *us1 = (const u_char *)s1;
89 const u_char *us2 = (const u_char *)s2;
90
91 while (cm[*us1] == cm[*us2++])
92 if (*us1++ == '\0')
93 return (0);
94 return (cm[*us1] - cm[*--us2]);
95}
96
97int
98strncasecmp(const char *s1, const char *s2, size_t n)
99{
100 if (n != 0) {
101 const u_char *cm = charmap;
102 const u_char *us1 = (const u_char *)s1;
103 const u_char *us2 = (const u_char *)s2;
104
105 do {
106 if (cm[*us1] != cm[*us2++])
107 return (cm[*us1] - cm[*--us2]);
108 if (*us1++ == '\0')
109 break;
110 } while (--n != 0);
111 }
112 return (0);
113}
diff --git a/src/lib/libc/string/strcat.3 b/src/lib/libc/string/strcat.3
new file mode 100644
index 0000000000..5b878c3e8c
--- /dev/null
+++ b/src/lib/libc/string/strcat.3
@@ -0,0 +1,131 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strcat.3,v 1.9 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd July 8, 1997
35.Dt STRCAT 3
36.Os
37.Sh NAME
38.Nm strcat ,
39.Nm strncat
40.Nd concatenate strings
41.Sh SYNOPSIS
42.Fd #include <string.h>
43.Ft char *
44.Fn strcat "char *s" "const char *append"
45.Ft char *
46.Fn strncat "char *s" "const char *append" "size_t count"
47.Sh DESCRIPTION
48The
49.Fn strcat
50and
51.Fn strncat
52functions append a copy of the null-terminated string
53.Fa append
54to the end of the null-terminated string
55.Fa s ,
56then add a terminating
57.Ql \e0 .
58The string
59.Fa s
60must have sufficient space to hold the result.
61.Pp
62The
63.Fn strncat
64function appends not more than
65.Fa count
66characters where space for the terminating
67.Ql \e0
68should not be included in
69.Fa count .
70.Sh RETURN VALUES
71The
72.Fn strcat
73and
74.Fn strncat
75functions return the pointer
76.Fa s .
77.Sh EXAMPLES
78The following appends
79.Dq Li abc
80to
81.Dq Li chararray :
82.Bd -literal -offset indent
83char *letters = "abcdefghi";
84
85(void)strncat(chararray, letters, 3);
86.Ed
87.Pp
88The following example shows how to use
89.Fn strncat
90safely in conjunction with
91.Xr strncpy 3 .
92.Bd -literal -offset indent
93char buf[BUFSIZ];
94char *input, *suffix;
95
96(void)strncpy(buf, input, sizeof(buf) - 1);
97buf[sizeof(buf) - 1] = '\e0';
98(void)strncat(buf, suffix, sizeof(buf) - 1 - strlen(buf));
99.Ed
100.Pp
101The above will copy as many characters from
102.Dq Li input
103to
104.Dq Li buf
105as will fit.
106It then appends as many characters from suffix as will fit (or none
107if there is no space).
108For operations like this, the
109.Xr strlcpy 3
110and
111.Xr strlcat 3
112functions are a better choice, as shown below.
113.Bd -literal -offset indent
114(void)strlcpy(buf, input, sizeof(buf));
115(void)strlcat(buf, suffix, sizeof(buf));
116.Ed
117.Sh SEE ALSO
118.Xr bcopy 3 ,
119.Xr memccpy 3 ,
120.Xr memcpy 3 ,
121.Xr memmove 3 ,
122.Xr strcpy 3 ,
123.Xr strlcat 3 ,
124.Xr strlcpy 3
125.Sh STANDARDS
126The
127.Fn strcat
128and
129.Fn strncat
130functions conform to
131.St -ansiC .
diff --git a/src/lib/libc/string/strcat.c b/src/lib/libc/string/strcat.c
new file mode 100644
index 0000000000..885822bae3
--- /dev/null
+++ b/src/lib/libc/string/strcat.c
@@ -0,0 +1,53 @@
1/*
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: strcat.c,v 1.6 2003/07/24 01:15:42 deraadt Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#ifndef _KERNEL
35#include <string.h>
36#else
37#include <lib/libkern/libkern.h>
38#endif
39
40#if defined(APIWARN)
41__warn_references(strcat,
42 "warning: strcat() is almost always misused, please use strlcat()");
43#endif
44
45char *
46strcat(char *s, const char *append)
47{
48 char *save = s;
49
50 for (; *s; ++s);
51 while ((*s++ = *append++) != '\0');
52 return(save);
53}
diff --git a/src/lib/libc/string/strchr.3 b/src/lib/libc/string/strchr.3
new file mode 100644
index 0000000000..86841bac25
--- /dev/null
+++ b/src/lib/libc/string/strchr.3
@@ -0,0 +1,102 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strchr.3,v 1.8 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt STRCHR 3
36.Os
37.Sh NAME
38.Nm strchr ,
39.Nm index
40.Nd locate first occurrence of a character in a string
41.Sh SYNOPSIS
42.Fd #include <string.h>
43.Ft char *
44.Fn strchr "const char *s" "int c"
45.Ft char *
46.Fn index "const char *s" "int c"
47.Sh DESCRIPTION
48The
49.Fn strchr
50function locates the first occurrence of the character
51.Fa c
52in the string
53.Fa s .
54The terminating NUL character is considered part of the string.
55If
56.Fa c
57is
58.Ql \e0 ,
59.Fn strchr
60locates the terminating
61.Ql \e0 .
62.Pp
63The
64.Fn index
65function is an old synonym for
66.Fn strchr .
67.Sh RETURN VALUES
68The
69.Fn strchr
70function returns a pointer to the located character or
71.Dv NULL
72if the character does not appear in the string.
73.Sh EXAMPLES
74After the following call to
75.Fn strchr ,
76.Va p
77will point to the string
78.Qq oobar :
79.Bd -literal -offset indent
80char *p;
81char *s = "foobar";
82
83p = strchr(s, 'o');
84.Ed
85.Sh SEE ALSO
86.Xr memchr 3 ,
87.Xr strcspn 3 ,
88.Xr strpbrk 3 ,
89.Xr strrchr 3 ,
90.Xr strsep 3 ,
91.Xr strspn 3 ,
92.Xr strstr 3 ,
93.Xr strtok 3
94.Sh STANDARDS
95The
96.Fn strchr
97function conforms to
98.St -ansiC .
99.Pp
100The
101.Fn index
102function is deprecated and shouldn't be used in new code.
diff --git a/src/lib/libc/string/strcmp.3 b/src/lib/libc/string/strcmp.3
new file mode 100644
index 0000000000..d025b9cf7c
--- /dev/null
+++ b/src/lib/libc/string/strcmp.3
@@ -0,0 +1,87 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strcmp.3,v 1.7 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt STRCMP 3
36.Os
37.Sh NAME
38.Nm strcmp ,
39.Nm strncmp
40.Nd compare strings
41.Sh SYNOPSIS
42.Fd #include <string.h>
43.Ft int
44.Fn strcmp "const char *s1" "const char *s2"
45.Ft int
46.Fn strncmp "const char *s1" "const char *s2" "size_t len"
47.Sh DESCRIPTION
48The
49.Fn strcmp
50and
51.Fn strncmp
52functions lexicographically compare the null-terminated strings
53.Fa s1
54and
55.Fa s2 .
56.Sh RETURN VALUES
57The
58.Fn strcmp
59and
60.Fn strncmp
61return an integer greater than, equal to, or less than 0, according
62to whether the string
63.Fa s1
64is greater than, equal to, or less than the string
65.Fa s2 .
66The comparison is done using unsigned characters, so that
67.Ql \e200
68is greater than
69.Ql \e0 .
70.Pp
71.Fn strncmp
72compares at most
73.Fa len
74characters.
75.Sh SEE ALSO
76.Xr bcmp 3 ,
77.Xr memcmp 3 ,
78.Xr strcasecmp 3 ,
79.Xr strcoll 3 ,
80.Xr strxfrm 3
81.Sh STANDARDS
82The
83.Fn strcmp
84and
85.Fn strncmp
86functions conform to
87.St -ansiC .
diff --git a/src/lib/libc/string/strcmp.c b/src/lib/libc/string/strcmp.c
new file mode 100644
index 0000000000..9bda0a8d75
--- /dev/null
+++ b/src/lib/libc/string/strcmp.c
@@ -0,0 +1,53 @@
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char *rcsid = "$OpenBSD: strcmp.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $";
35#endif /* LIBC_SCCS and not lint */
36
37#ifndef _KERNEL
38#include <string.h>
39#else
40#include <lib/libkern/libkern.h>
41#endif
42
43/*
44 * Compare strings.
45 */
46int
47strcmp(const char *s1, const char *s2)
48{
49 while (*s1 == *s2++)
50 if (*s1++ == 0)
51 return (0);
52 return (*(unsigned char *)s1 - *(unsigned char *)--s2);
53}
diff --git a/src/lib/libc/string/strcoll.3 b/src/lib/libc/string/strcoll.3
new file mode 100644
index 0000000000..fe0ff35510
--- /dev/null
+++ b/src/lib/libc/string/strcoll.3
@@ -0,0 +1,68 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strcoll.3,v 1.5 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt STRCOLL 3
36.Os
37.Sh NAME
38.Nm strcoll
39.Nd compare strings according to current collation
40.Sh SYNOPSIS
41.Fd #include <string.h>
42.Ft int
43.Fn strcoll "const char *s1" "const char *s2"
44.Sh DESCRIPTION
45The
46.Fn strcoll
47function lexicographically compares the null-terminated strings
48.Fa s1
49and
50.Fa s2
51according to the current locale collation
52and returns an integer greater than, equal to, or less than 0,
53according to whether
54.Fa s1
55is greater than, equal to, or less than
56.Fa s2 .
57.Sh SEE ALSO
58.Xr bcmp 3 ,
59.Xr memcmp 3 ,
60.Xr setlocale 3 ,
61.Xr strcasecmp 3 ,
62.Xr strcmp 3 ,
63.Xr strxfrm 3
64.Sh STANDARDS
65The
66.Fn strcoll
67function conforms to
68.St -ansiC .
diff --git a/src/lib/libc/string/strcoll.c b/src/lib/libc/string/strcoll.c
new file mode 100644
index 0000000000..9622b67281
--- /dev/null
+++ b/src/lib/libc/string/strcoll.c
@@ -0,0 +1,47 @@
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char *rcsid = "$OpenBSD: strcoll.c,v 1.4 2003/06/11 21:08:29 deraadt Exp $";
35#endif /* LIBC_SCCS and not lint */
36
37#include <string.h>
38
39/*
40 * Compare strings according to LC_COLLATE category of current locale.
41 */
42int
43strcoll(const char *s1, const char *s2)
44{
45 /* LC_COLLATE is unimplemented, hence always "C" */
46 return (strcmp(s1, s2));
47}
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3
new file mode 100644
index 0000000000..8304508df6
--- /dev/null
+++ b/src/lib/libc/string/strcpy.3
@@ -0,0 +1,150 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strcpy.3,v 1.12 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt STRCPY 3
36.Os
37.Sh NAME
38.Nm strcpy ,
39.Nm strncpy
40.Nd copy strings
41.Sh SYNOPSIS
42.Fd #include <string.h>
43.Ft char *
44.Fn strcpy "char *dst" "const char *src"
45.Ft char *
46.Fn strncpy "char *dst" "const char *src" "size_t len"
47.Sh DESCRIPTION
48The
49.Fn strcpy
50and
51.Fn strncpy
52functions copy the string
53.Fa src
54to
55.Fa dst
56(including the terminating
57.Ql \e0
58character).
59.Pp
60.Fn strncpy
61copies not more than
62.Fa len
63characters into
64.Fa dst ,
65appending
66.Ql \e0
67characters if
68.Fa src
69is less than
70.Fa len
71characters long, and
72.Em not
73terminating
74.Fa dst
75if the length of
76.Fa src
77is greater than or equal to
78.Fa len .
79.Sh RETURN VALUES
80The
81.Fn strcpy
82and
83.Fn strncpy
84functions return
85.Fa dst .
86.Sh EXAMPLES
87The following sets
88.Va chararray
89to
90.Dq abc\e0\e0\e0 :
91.Bd -literal -offset indent
92(void)strncpy(chararray, "abc", 6);
93.Ed
94.Pp
95The following sets
96.Va chararray
97to
98.Dq abcdef
99and does
100.Em not
101null terminate
102.Va chararray
103because the source string is >= the length parameter.
104.Fn strncpy
105.Em only
106null terminates the destination string when the length of the source
107string is less than the length parameter.
108.Bd -literal -offset indent
109(void)strncpy(chararray, "abcdefgh", 6);
110.Ed
111.Pp
112The following copies as many characters from
113.Va input
114to
115.Va buf
116as will fit and null terminates the result.
117Because
118.Fn strncpy
119does
120.Em not
121guarantee to null terminate the string itself, we must do this by hand.
122.Bd -literal -offset indent
123char buf[BUFSIZ];
124
125(void)strncpy(buf, input, sizeof(buf) - 1);
126buf[sizeof(buf) - 1] = '\e0';
127.Ed
128.Pp
129Note that
130.Xr strlcpy 3
131is a better choice for this kind of operation.
132The equivalent using
133.Xr strlcpy 3
134is simply:
135.Bd -literal -offset indent
136(void)strlcpy(buf, input, sizeof(buf));
137.Ed
138.Sh SEE ALSO
139.Xr bcopy 3 ,
140.Xr memccpy 3 ,
141.Xr memcpy 3 ,
142.Xr memmove 3 ,
143.Xr strlcpy 3
144.Sh STANDARDS
145The
146.Fn strcpy
147and
148.Fn strncpy
149functions conform to
150.St -ansiC .
diff --git a/src/lib/libc/string/strcpy.c b/src/lib/libc/string/strcpy.c
new file mode 100644
index 0000000000..e5c3d7dcbe
--- /dev/null
+++ b/src/lib/libc/string/strcpy.c
@@ -0,0 +1,52 @@
1/*
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: strcpy.c,v 1.6 2003/07/24 01:15:42 deraadt Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#ifndef _KERNEL
35#include <string.h>
36#else
37#include <lib/libkern/libkern.h>
38#endif
39
40#if defined(APIWARN)
41__warn_references(strcpy,
42 "warning: strcpy() is almost always misused, please use strlcpy()");
43#endif
44
45char *
46strcpy(char *to, const char *from)
47{
48 char *save = to;
49
50 for (; (*to = *from) != '\0'; ++from, ++to);
51 return(save);
52}
diff --git a/src/lib/libc/string/strcspn.3 b/src/lib/libc/string/strcspn.3
new file mode 100644
index 0000000000..6dfe1b276e
--- /dev/null
+++ b/src/lib/libc/string/strcspn.3
@@ -0,0 +1,88 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strcspn.3,v 1.6 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt STRCSPN 3
36.Os
37.Sh NAME
38.Nm strcspn
39.Nd span the complement of a string
40.Sh SYNOPSIS
41.Fd #include <string.h>
42.Ft size_t
43.Fn strcspn "const char *s" "const char *charset"
44.Sh DESCRIPTION
45The
46.Fn strcspn
47function spans the initial part of the null-terminated string
48.Fa s
49as long as the characters from
50.Fa s
51do not occur in string
52.Fa charset
53(it spans the
54.Em complement
55of
56.Fa charset ) .
57.Sh RETURN VALUES
58The
59.Fn strcspn
60function returns the number of characters spanned.
61.Sh EXAMPLES
62The following call to
63.Fn strcspn
64will return 3, since the first three characters of string
65.Fa s
66do not occur in string
67.Fa charset :
68.Bd -literal -offset indent
69char *s = "foobar";
70char *charset = "bar";
71size_t span;
72
73span = strcspn(s, charset);
74.Ed
75.Sh SEE ALSO
76.Xr memchr 3 ,
77.Xr strchr 3 ,
78.Xr strpbrk 3 ,
79.Xr strrchr 3 ,
80.Xr strsep 3 ,
81.Xr strspn 3 ,
82.Xr strstr 3 ,
83.Xr strtok 3
84.Sh STANDARDS
85The
86.Fn strcspn
87function conforms to
88.St -ansiC .
diff --git a/src/lib/libc/string/strcspn.c b/src/lib/libc/string/strcspn.c
new file mode 100644
index 0000000000..f96f903809
--- /dev/null
+++ b/src/lib/libc/string/strcspn.c
@@ -0,0 +1,61 @@
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char *rcsid = "$OpenBSD: strcspn.c,v 1.4 2003/06/11 21:08:16 deraadt Exp $";
35#endif /* LIBC_SCCS and not lint */
36
37#include <string.h>
38
39/*
40 * Span the complement of string s2.
41 */
42size_t
43strcspn(const char *s1, const char *s2)
44{
45 const char *p, *spanp;
46 char c, sc;
47
48 /*
49 * Stop as soon as we find any character from s2. Note that there
50 * must be a NUL in s2; it suffices to stop when we find that, too.
51 */
52 for (p = s1;;) {
53 c = *p++;
54 spanp = s2;
55 do {
56 if ((sc = *spanp++) == c)
57 return (p - 1 - s1);
58 } while (sc != 0);
59 }
60 /* NOTREACHED */
61}
diff --git a/src/lib/libc/string/strdup.3 b/src/lib/libc/string/strdup.3
new file mode 100644
index 0000000000..59b77f7a16
--- /dev/null
+++ b/src/lib/libc/string/strdup.3
@@ -0,0 +1,84 @@
1.\" $OpenBSD: strdup.3,v 1.12 2003/06/02 20:18:38 millert Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" @(#)strdup.3 8.1 (Berkeley) 6/9/93
31.\"
32.Dd June 9, 1993
33.Dt STRDUP 3
34.Os
35.Sh NAME
36.Nm strdup
37.Nd save a copy of a string
38.Sh SYNOPSIS
39.Fd #include <string.h>
40.Ft char *
41.Fn strdup "const char *s"
42.Sh DESCRIPTION
43The
44.Fn strdup
45function allocates sufficient memory for a copy of the string
46.Fa s ,
47does the copy, and returns a pointer to it.
48The pointer may subsequently be used as an argument to the function
49.Xr free 3 .
50.Pp
51If insufficient memory is available,
52.Dv NULL
53is returned.
54.Sh EXAMPLES
55The following will point
56.Va p
57to an allocated area of memory containing the null-terminated string
58.Qq foobar :
59.Bd -literal -offset indent
60char *p;
61
62if ((p = strdup("foobar")) == NULL) {
63 fprintf(stderr, "Out of memory.\en");
64 exit(1);
65}
66.Ed
67.Sh ERRORS
68The
69.Fn strdup
70function may fail and set the external variable
71.Va errno
72for any of the errors specified for the library function
73.Xr malloc 3 .
74.Sh SEE ALSO
75.Xr free 3 ,
76.Xr malloc 3 ,
77.Xr strcpy 3 ,
78.Xr strlcpy 3 ,
79.Xr strlen 3
80.Sh HISTORY
81The
82.Fn strdup
83function first appeared in
84.Bx 4.4 .
diff --git a/src/lib/libc/string/strdup.c b/src/lib/libc/string/strdup.c
new file mode 100644
index 0000000000..bbea59888c
--- /dev/null
+++ b/src/lib/libc/string/strdup.c
@@ -0,0 +1,57 @@
1/* $OpenBSD: strdup.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $ */
2
3/*
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
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
17 * without specific prior written permission.
18 *
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
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#if defined(LIBC_SCCS) && !defined(lint)
33#if 0
34static char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93";
35#else
36static char *rcsid = "$OpenBSD: strdup.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $";
37#endif
38#endif /* LIBC_SCCS and not lint */
39
40#include <sys/types.h>
41
42#include <stddef.h>
43#include <stdlib.h>
44#include <string.h>
45
46char *
47strdup(const char *str)
48{
49 size_t siz;
50 char *copy;
51
52 siz = strlen(str) + 1;
53 if ((copy = malloc(siz)) == NULL)
54 return(NULL);
55 (void)memcpy(copy, str, siz);
56 return(copy);
57}
diff --git a/src/lib/libc/string/strerror.3 b/src/lib/libc/string/strerror.3
new file mode 100644
index 0000000000..fa573bc72a
--- /dev/null
+++ b/src/lib/libc/string/strerror.3
@@ -0,0 +1,85 @@
1.\" Copyright (c) 1980, 1991 Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strerror.3,v 1.6 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt STRERROR 3
36.Os
37.Sh NAME
38.Nm strerror
39.Nd get error message string
40.Sh SYNOPSIS
41.Fd #include <string.h>
42.Ft char *
43.Fn strerror "int errnum"
44.Ft int
45.Fn strerror_r "int errnum" "char *strerrbuf" "size_t buflen"
46.Sh DESCRIPTION
47The
48.Fn strerror
49function returns a pointer to the language-dependent error message
50string affiliated with the specified error number
51.Fa errnum .
52The returned string contains a maximum of
53.Dv NL_TEXTMAX
54characters, including the trailing NUL.
55.Pp
56The array pointed to is not to be modified by the program, but may be
57overwritten by subsequent calls to
58.Fn strerror .
59.Pp
60.Fn strerror_r
61is a thread safe version of
62.Fn strerror
63that places the error message in the given buffer
64.Fa strerrbuf .
65If the error message is larger then
66.Fa buflen
67the message will be truncated to fit within buflen and
68.Er ERANGE
69is returned.
70.Fn strerror_r
71returns zero upon successful completion.
72An error number is returned, otherwise.
73.Sh SEE ALSO
74.Xr intro 2 ,
75.Xr perror 3 ,
76.Xr setlocale 3
77.Sh STANDARDS
78The
79.Fn strerror
80function conforms to
81.St -ansiC .
82The
83.Fn strerror_r
84function conforms to
85.St -p1003.1 .
diff --git a/src/lib/libc/string/strerror.c b/src/lib/libc/string/strerror.c
new file mode 100644
index 0000000000..6496f50afd
--- /dev/null
+++ b/src/lib/libc/string/strerror.c
@@ -0,0 +1,50 @@
1/*
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: strerror.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#include <string.h>
35#include <limits.h>
36
37/*
38 * Since perror() is not allowed to change the contents of strerror()'s
39 * static buffer, both functions supply their own buffers to the
40 * internal function __strerror().
41 */
42
43extern char *__strerror(int, char *);
44
45char *
46strerror(int num)
47{
48 static char buf[NL_TEXTMAX];
49 return __strerror(num, buf);
50}
diff --git a/src/lib/libc/string/strerror_r.c b/src/lib/libc/string/strerror_r.c
new file mode 100644
index 0000000000..aab6db5303
--- /dev/null
+++ b/src/lib/libc/string/strerror_r.c
@@ -0,0 +1,30 @@
1/* $OpenBSD: strerror_r.c,v 1.1 2002/11/21 20:45:05 marc Exp $ */
2/* Public Domain <marc@snafu.org> */
3
4#if defined(LIBC_SCCS) && !defined(lint)
5static char *rcsid = "$OpenBSD: strerror_r.c,v 1.1 2002/11/21 20:45:05 marc Exp $";
6#endif /* LIBC_SCCS and not lint */
7
8#include <errno.h>
9#include <limits.h>
10#include <string.h>
11
12extern char *__strerror(int, char *);
13
14int
15strerror_r(int errnum, char *strerrbuf, size_t buflen)
16{
17 int save_errno;
18 int ret_errno;
19 char buf[NL_TEXTMAX];
20
21 save_errno = errno;
22 errno = 0;
23 __strerror(errnum, buf);
24 if (strlcpy(strerrbuf, buf, buflen) >= buflen)
25 errno = ERANGE;
26 ret_errno = errno;
27 errno = save_errno;
28
29 return (ret_errno);
30}
diff --git a/src/lib/libc/string/string.3 b/src/lib/libc/string/string.3
new file mode 100644
index 0000000000..e29aa6c02c
--- /dev/null
+++ b/src/lib/libc/string/string.3
@@ -0,0 +1,157 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek.
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" $OpenBSD: string.3,v 1.9 2003/06/02 20:18:38 millert Exp $
31.\"
32.Dd April 19, 1991
33.Dt STRING 3
34.Os
35.Sh NAME
36.Nm strcat ,
37.Nm strlcat ,
38.Nm strncat ,
39.Nm strchr ,
40.Nm strrchr ,
41.Nm strcmp ,
42.Nm strncmp ,
43.Nm strcasecmp ,
44.Nm strncasecmp ,
45.Nm strcpy ,
46.Nm strlcpy ,
47.Nm strncpy ,
48.Nm strerror ,
49.Nm strlen ,
50.Nm strpbrk ,
51.Nm strsep ,
52.Nm strspn ,
53.Nm strcspn ,
54.Nm strstr ,
55.Nm strtok ,
56.Nm index ,
57.Nm rindex
58.Nd string specific functions
59.Sh SYNOPSIS
60.Fd #include <string.h>
61.Ft char *
62.Fn strcat "char *s" "const char *append"
63.Ft char *
64.Fn strlcat "char *s" "const char *append" "size_t size"
65.Ft char *
66.Fn strncat "char *s" "const char *append" "size_t count"
67.Ft char *
68.Fn strchr "const char *s" "int c"
69.Ft char *
70.Fn strrchr "const char *s" "int c"
71.Ft int
72.Fn strcmp "const char *s1" "const char *s2"
73.Ft int
74.Fn strncmp "const char *s1" "const char *s2" "size_t count"
75.Ft int
76.Fn strcasecmp "const char *s1" "const char *s2"
77.Ft int
78.Fn strncasecmp "const char *s1" "const char *s2" "size_t count"
79.Ft char *
80.Fn strcpy "char *dst" "const char *src"
81.Ft char *
82.Fn strlcpy "char *dst" "const char *src" "size_t size"
83.Ft char *
84.Fn strncpy "char *dst" "const char *src" "size_t count"
85.Ft char *
86.Fn strerror "int errno"
87.Ft size_t
88.Fn strlen "const char *s"
89.Ft char *
90.Fn strpbrk "const char *s" "const char *charset"
91.Ft char *
92.Fn strsep "char **stringp" "const char *delim"
93.Ft size_t
94.Fn strspn "const char *s" "const char *charset"
95.Ft size_t
96.Fn strcspn "const char *s" "const char *charset"
97.Ft char *
98.Fn strstr "const char *big" "const char *little"
99.Ft char *
100.Fn strtok "char *s" "const char *delim"
101.Ft char *
102.Fn index "const char *s" "int c"
103.Ft char *
104.Fn rindex "const char *s" "int c"
105.Sh DESCRIPTION
106The string functions
107manipulate strings terminated by a
108null byte.
109.Pp
110See the specific manual pages for more information.
111For manipulating variable length generic objects as byte
112strings (without the null byte check), see
113.Xr bstring 3 .
114.Pp
115Except as noted in their specific manual pages,
116the string functions do not test the destination
117for size limitations.
118.Sh SEE ALSO
119.Xr bstring 3 ,
120.Xr index 3 ,
121.Xr rindex 3 ,
122.Xr strcasecmp 3 ,
123.Xr strcat 3 ,
124.Xr strchr 3 ,
125.Xr strcmp 3 ,
126.Xr strcpy 3 ,
127.Xr strcspn 3 ,
128.Xr strerror 3 ,
129.Xr strlcat 3 ,
130.Xr strlcpy 3 ,
131.Xr strlen 3 ,
132.Xr strpbrk 3 ,
133.Xr strrchr 3 ,
134.Xr strsep 3 ,
135.Xr strspn 3 ,
136.Xr strstr 3 ,
137.Xr strtok 3
138.Sh STANDARDS
139The
140.Fn strcat ,
141.Fn strncat ,
142.Fn strchr ,
143.Fn strrchr ,
144.Fn strcmp ,
145.Fn strncmp ,
146.Fn strcpy ,
147.Fn strncpy ,
148.Fn strerror ,
149.Fn strlen ,
150.Fn strpbrk ,
151.Fn strspn ,
152.Fn strcspn ,
153.Fn strstr ,
154and
155.Fn strtok
156functions conform to
157.St -ansiC .
diff --git a/src/lib/libc/string/strlcat.c b/src/lib/libc/string/strlcat.c
new file mode 100644
index 0000000000..2b6f1adbb4
--- /dev/null
+++ b/src/lib/libc/string/strlcat.c
@@ -0,0 +1,59 @@
1/* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */
2
3/*
4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#if defined(LIBC_SCCS) && !defined(lint)
20static char *rcsid = "$OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $";
21#endif /* LIBC_SCCS and not lint */
22
23#include <sys/types.h>
24#include <string.h>
25
26/*
27 * Appends src to string dst of size siz (unlike strncat, siz is the
28 * full size of dst, not space left). At most siz-1 characters
29 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
30 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
31 * If retval >= siz, truncation occurred.
32 */
33size_t
34strlcat(char *dst, const char *src, size_t siz)
35{
36 register char *d = dst;
37 register const char *s = src;
38 register size_t n = siz;
39 size_t dlen;
40
41 /* Find the end of dst and adjust bytes left but don't go past end */
42 while (n-- != 0 && *d != '\0')
43 d++;
44 dlen = d - dst;
45 n = siz - dlen;
46
47 if (n == 0)
48 return(dlen + strlen(s));
49 while (*s != '\0') {
50 if (n != 1) {
51 *d++ = *s;
52 n--;
53 }
54 s++;
55 }
56 *d = '\0';
57
58 return(dlen + (s - src)); /* count does not include NUL */
59}
diff --git a/src/lib/libc/string/strlcpy.3 b/src/lib/libc/string/strlcpy.3
new file mode 100644
index 0000000000..b392588879
--- /dev/null
+++ b/src/lib/libc/string/strlcpy.3
@@ -0,0 +1,179 @@
1.\" $OpenBSD: strlcpy.3,v 1.16 2003/06/17 21:56:24 millert Exp $
2.\"
3.\" Copyright (c) 1998, 2000 Todd C. Miller <Todd.Miller@courtesan.com>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.Dd June 22, 1998
18.Dt STRLCPY 3
19.Os
20.Sh NAME
21.Nm strlcpy ,
22.Nm strlcat
23.Nd size-bounded string copying and concatenation
24.Sh SYNOPSIS
25.Fd #include <string.h>
26.Ft size_t
27.Fn strlcpy "char *dst" "const char *src" "size_t size"
28.Ft size_t
29.Fn strlcat "char *dst" "const char *src" "size_t size"
30.Sh DESCRIPTION
31The
32.Fn strlcpy
33and
34.Fn strlcat
35functions copy and concatenate strings respectively.
36They are designed
37to be safer, more consistent, and less error prone replacements for
38.Xr strncpy 3
39and
40.Xr strncat 3 .
41Unlike those functions,
42.Fn strlcpy
43and
44.Fn strlcat
45take the full size of the buffer (not just the length) and guarantee to
46NUL-terminate the result (as long as
47.Fa size
48is larger than 0 or, in the case of
49.Fn strlcat ,
50as long as there is at least one byte free in
51.Fa dst ) .
52Note that you should include a byte for the NUL in
53.Fa size .
54Also note that
55.Fn strlcpy
56and
57.Fn strlcat
58only operate on true
59.Dq C
60strings.
61This means that for
62.Fn strlcpy
63.Fa src
64must be NUL-terminated and for
65.Fn strlcat
66both
67.Fa src
68and
69.Fa dst
70must be NUL-terminated.
71.Pp
72The
73.Fn strlcpy
74function copies up to
75.Fa size
76- 1 characters from the NUL-terminated string
77.Fa src
78to
79.Fa dst ,
80NUL-terminating the result.
81.Pp
82The
83.Fn strlcat
84function appends the NUL-terminated string
85.Fa src
86to the end of
87.Fa dst .
88It will append at most
89.Fa size
90- strlen(dst) - 1 bytes, NUL-terminating the result.
91.Sh RETURN VALUES
92The
93.Fn strlcpy
94and
95.Fn strlcat
96functions return the total length of the string they tried to create.
97For
98.Fn strlcpy
99that means the length of
100.Fa src .
101For
102.Fn strlcat
103that means the initial length of
104.Fa dst
105plus
106the length of
107.Fa src .
108While this may seem somewhat confusing it was done to make
109truncation detection simple.
110.Pp
111Note however, that if
112.Fn strlcat
113traverses
114.Fa size
115characters without finding a NUL, the length of the string is considered
116to be
117.Fa size
118and the destination string will not be NUL-terminated (since there was
119no space for the NUL).
120This keeps
121.Fn strlcat
122from running off the end of a string.
123In practice this should not happen (as it means that either
124.Fa size
125is incorrect or that
126.Fa dst
127is not a proper
128.Dq C
129string).
130The check exists to prevent potential security problems in incorrect code.
131.Sh EXAMPLES
132The following code fragment illustrates the simple case:
133.Bd -literal -offset indent
134char *s, *p, buf[BUFSIZ];
135
136\&...
137
138(void)strlcpy(buf, s, sizeof(buf));
139(void)strlcat(buf, p, sizeof(buf));
140.Ed
141.Pp
142To detect truncation, perhaps while building a pathname, something
143like the following might be used:
144.Bd -literal -offset indent
145char *dir, *file, pname[MAXPATHLEN];
146
147\&...
148
149if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname))
150 goto toolong;
151if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname))
152 goto toolong;
153.Ed
154.Pp
155Since we know how many characters we copied the first time, we can
156speed things up a bit by using a copy instead of an append:
157.Bd -literal -offset indent
158char *dir, *file, pname[MAXPATHLEN];
159size_t n;
160
161\&...
162
163n = strlcpy(pname, dir, sizeof(pname));
164if (n >= sizeof(pname))
165 goto toolong;
166if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n)
167 goto toolong;
168.Ed
169.Pp
170However, one may question the validity of such optimizations, as they
171defeat the whole purpose of
172.Fn strlcpy
173and
174.Fn strlcat .
175As a matter of fact, the first version of this manual page got it wrong.
176.Sh SEE ALSO
177.Xr snprintf 3 ,
178.Xr strncat 3 ,
179.Xr strncpy 3
diff --git a/src/lib/libc/string/strlcpy.c b/src/lib/libc/string/strlcpy.c
new file mode 100644
index 0000000000..82cbfac016
--- /dev/null
+++ b/src/lib/libc/string/strlcpy.c
@@ -0,0 +1,55 @@
1/* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */
2
3/*
4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#if defined(LIBC_SCCS) && !defined(lint)
20static char *rcsid = "$OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $";
21#endif /* LIBC_SCCS and not lint */
22
23#include <sys/types.h>
24#include <string.h>
25
26/*
27 * Copy src to string dst of size siz. At most siz-1 characters
28 * will be copied. Always NUL terminates (unless siz == 0).
29 * Returns strlen(src); if retval >= siz, truncation occurred.
30 */
31size_t
32strlcpy(char *dst, const char *src, size_t siz)
33{
34 register char *d = dst;
35 register const char *s = src;
36 register size_t n = siz;
37
38 /* Copy as many bytes as will fit */
39 if (n != 0 && --n != 0) {
40 do {
41 if ((*d++ = *s++) == 0)
42 break;
43 } while (--n != 0);
44 }
45
46 /* Not enough room in dst, add NUL and traverse rest of src */
47 if (n == 0) {
48 if (siz != 0)
49 *d = '\0'; /* NUL-terminate dst */
50 while (*s++)
51 ;
52 }
53
54 return(s - src - 1); /* count does not include NUL */
55}
diff --git a/src/lib/libc/string/strlen.3 b/src/lib/libc/string/strlen.3
new file mode 100644
index 0000000000..32bbc88542
--- /dev/null
+++ b/src/lib/libc/string/strlen.3
@@ -0,0 +1,61 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strlen.3,v 1.5 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt STRLEN 3
36.Os
37.Sh NAME
38.Nm strlen
39.Nd find length of a string
40.Sh SYNOPSIS
41.Fd #include <string.h>
42.Ft size_t
43.Fn strlen "const char *s"
44.Sh DESCRIPTION
45The
46.Fn strlen
47function computes the length of the string
48.Fa s .
49.Sh RETURN VALUES
50The
51.Fn strlen
52function returns the number of characters that precede the terminating
53.Tn NUL
54character.
55.Sh SEE ALSO
56.Xr string 3
57.Sh STANDARDS
58The
59.Fn strlen
60function conforms to
61.St -ansiC .
diff --git a/src/lib/libc/string/strlen.c b/src/lib/libc/string/strlen.c
new file mode 100644
index 0000000000..298a9966a3
--- /dev/null
+++ b/src/lib/libc/string/strlen.c
@@ -0,0 +1,51 @@
1/* $OpenBSD: strlen.c,v 1.6 2003/06/11 21:08:16 deraadt Exp $ */
2
3/*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
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
17 * without specific prior written permission.
18 *
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
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#if defined(LIBC_SCCS) && !defined(lint)
33static char *rcsid = "$OpenBSD: strlen.c,v 1.6 2003/06/11 21:08:16 deraadt Exp $";
34#endif /* LIBC_SCCS and not lint */
35
36#if !defined(_KERNEL) && !defined(_STANDALONE)
37#include <string.h>
38#else
39#include <lib/libkern/libkern.h>
40#endif
41
42size_t
43strlen(const char *str)
44{
45 const char *s;
46
47 for (s = str; *s; ++s)
48 ;
49 return (s - str);
50}
51
diff --git a/src/lib/libc/string/strmode.3 b/src/lib/libc/string/strmode.3
new file mode 100644
index 0000000000..bc45c4ce1f
--- /dev/null
+++ b/src/lib/libc/string/strmode.3
@@ -0,0 +1,159 @@
1.\" $OpenBSD: strmode.3,v 1.11 2003/09/02 18:24:21 jmc Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" @(#)strmode.3 8.3 (Berkeley) 7/28/94
31.\"
32.Dd July 28, 1994
33.Dt STRMODE 3
34.Os
35.Sh NAME
36.Nm strmode
37.Nd convert inode status information into a symbolic string
38.Sh SYNOPSIS
39.Fd #include <string.h>
40.Ft void
41.Fn strmode "mode_t mode" "char *bp"
42.Sh DESCRIPTION
43The
44.Fn strmode
45function converts a file
46.Fa mode
47(the type and permission information associated with an inode, see
48.Xr stat 2 )
49into a symbolic string which is stored in the location referenced by
50.Fa bp .
51This stored string is eleven characters in length plus a trailing null byte.
52.Pp
53The first character is the inode type, and will be one of the following:
54.Pp
55.Bl -tag -width flag -offset indent -compact
56.It \-
57regular file
58.It b
59block special
60.It c
61character special
62.It d
63directory
64.It l
65symbolic link
66.It p
67FIFO
68.It s
69socket
70.It w
71whiteout
72.It \&?
73unknown inode type
74.El
75.Pp
76The next nine characters encode three sets of permissions, in three
77characters each.
78The first three characters are the permissions for the owner of the
79file, the second three for the group the file belongs to, and the
80third for the
81.Dq other ,
82or default, set of users.
83.Pp
84Permission checking is done as specifically as possible.
85If read permission is denied to the owner of a file in the first set
86of permissions, the owner of the file will not be able to read the file.
87This is true even if the owner is in the file's group and the group
88permissions allow reading or the
89.Dq other
90permissions allow reading.
91.Pp
92If the first character of the three character set is an
93.Sq r ,
94the file is readable for that set of users; if a dash
95.Pq Ql - ,
96it is not readable.
97.Pp
98If the second character of the three character set is a
99.Sq w ,
100the file is writable for that set of users; if a dash
101.Pq Ql - ,
102it is not writable.
103.Pp
104The third character is the first of the following characters that apply:
105.Bl -tag -width xxxx
106.It S
107If the character is part of the owner permissions and the file is not
108executable or the directory is not searchable by the owner, and the
109set-user-ID bit is set.
110.It S
111If the character is part of the group permissions and the file is not
112executable or the directory is not searchable by the group, and the
113set-group-ID bit is set.
114.It T
115If the character is part of the other permissions and the file is not
116executable or the directory is not searchable by others, and the
117.Dq sticky
118.Pq Dv S_ISVTX
119bit is set.
120.It s
121If the character is part of the owner permissions and the file is
122executable or the directory searchable by the owner, and the set-user-ID
123bit is set.
124.It s
125If the character is part of the group permissions and the file is
126executable or the directory searchable by the group, and the set-group-ID
127bit is set.
128.It t
129If the character is part of the other permissions and the file is
130executable or the directory searchable by others, and the
131.Dq sticky
132.Pq Dv S_ISVTX
133bit is set.
134.It x
135The file is executable or the directory is searchable.
136.It \-
137None of the above apply.
138.El
139.Pp
140The last character is a plus sign
141.Pq Ql +
142if there are any alternate
143or additional access control methods associated with the inode, otherwise
144it will be a space.
145.Sh RETURN VALUES
146The
147.Fn strmode
148function always returns 0.
149.Sh SEE ALSO
150.Xr chmod 1 ,
151.Xr find 1 ,
152.Xr stat 2 ,
153.Xr getmode 3 ,
154.Xr setmode 3
155.Sh HISTORY
156The
157.Fn strmode
158function first appeared in
159.Bx 4.4 .
diff --git a/src/lib/libc/string/strmode.c b/src/lib/libc/string/strmode.c
new file mode 100644
index 0000000000..bd78403cb2
--- /dev/null
+++ b/src/lib/libc/string/strmode.c
@@ -0,0 +1,148 @@
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: strmode.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#include <sys/types.h>
35#include <sys/stat.h>
36#include <string.h>
37
38/* XXX mode should be mode_t */
39
40void
41strmode(int mode, char *p)
42{
43 /* print type */
44 switch (mode & S_IFMT) {
45 case S_IFDIR: /* directory */
46 *p++ = 'd';
47 break;
48 case S_IFCHR: /* character special */
49 *p++ = 'c';
50 break;
51 case S_IFBLK: /* block special */
52 *p++ = 'b';
53 break;
54 case S_IFREG: /* regular */
55 *p++ = '-';
56 break;
57 case S_IFLNK: /* symbolic link */
58 *p++ = 'l';
59 break;
60 case S_IFSOCK: /* socket */
61 *p++ = 's';
62 break;
63#ifdef S_IFIFO
64 case S_IFIFO: /* fifo */
65 *p++ = 'p';
66 break;
67#endif
68#ifdef S_IFWHT
69 case S_IFWHT: /* whiteout */
70 *p++ = 'w';
71 break;
72#endif
73 default: /* unknown */
74 *p++ = '?';
75 break;
76 }
77 /* usr */
78 if (mode & S_IRUSR)
79 *p++ = 'r';
80 else
81 *p++ = '-';
82 if (mode & S_IWUSR)
83 *p++ = 'w';
84 else
85 *p++ = '-';
86 switch (mode & (S_IXUSR | S_ISUID)) {
87 case 0:
88 *p++ = '-';
89 break;
90 case S_IXUSR:
91 *p++ = 'x';
92 break;
93 case S_ISUID:
94 *p++ = 'S';
95 break;
96 case S_IXUSR | S_ISUID:
97 *p++ = 's';
98 break;
99 }
100 /* group */
101 if (mode & S_IRGRP)
102 *p++ = 'r';
103 else
104 *p++ = '-';
105 if (mode & S_IWGRP)
106 *p++ = 'w';
107 else
108 *p++ = '-';
109 switch (mode & (S_IXGRP | S_ISGID)) {
110 case 0:
111 *p++ = '-';
112 break;
113 case S_IXGRP:
114 *p++ = 'x';
115 break;
116 case S_ISGID:
117 *p++ = 'S';
118 break;
119 case S_IXGRP | S_ISGID:
120 *p++ = 's';
121 break;
122 }
123 /* other */
124 if (mode & S_IROTH)
125 *p++ = 'r';
126 else
127 *p++ = '-';
128 if (mode & S_IWOTH)
129 *p++ = 'w';
130 else
131 *p++ = '-';
132 switch (mode & (S_IXOTH | S_ISVTX)) {
133 case 0:
134 *p++ = '-';
135 break;
136 case S_IXOTH:
137 *p++ = 'x';
138 break;
139 case S_ISVTX:
140 *p++ = 'T';
141 break;
142 case S_IXOTH | S_ISVTX:
143 *p++ = 't';
144 break;
145 }
146 *p++ = ' '; /* will be a '+' if ACL's implemented */
147 *p = '\0';
148}
diff --git a/src/lib/libc/string/strncat.c b/src/lib/libc/string/strncat.c
new file mode 100644
index 0000000000..05d35e0900
--- /dev/null
+++ b/src/lib/libc/string/strncat.c
@@ -0,0 +1,60 @@
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char *rcsid = "$OpenBSD: strncat.c,v 1.4 2003/06/11 21:08:16 deraadt Exp $";
35#endif /* LIBC_SCCS and not lint */
36
37#include <string.h>
38
39/*
40 * Concatenate src on the end of dst. At most strlen(dst)+n+1 bytes
41 * are written at dst (at most n+1 bytes being appended). Return dst.
42 */
43char *
44strncat(char *dst, const char *src, size_t n)
45{
46 if (n != 0) {
47 char *d = dst;
48 const char *s = src;
49
50 while (*d != 0)
51 d++;
52 do {
53 if ((*d = *s++) == 0)
54 break;
55 d++;
56 } while (--n != 0);
57 *d = 0;
58 }
59 return (dst);
60}
diff --git a/src/lib/libc/string/strncmp.c b/src/lib/libc/string/strncmp.c
new file mode 100644
index 0000000000..82420e215e
--- /dev/null
+++ b/src/lib/libc/string/strncmp.c
@@ -0,0 +1,53 @@
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: strncmp.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#ifndef _KERNEL
35#include <string.h>
36#else
37#include <lib/libkern/libkern.h>
38#endif
39
40int
41strncmp(const char *s1, const char *s2, size_t n)
42{
43
44 if (n == 0)
45 return (0);
46 do {
47 if (*s1 != *s2++)
48 return (*(unsigned char *)s1 - *(unsigned char *)--s2);
49 if (*s1++ == 0)
50 break;
51 } while (--n != 0);
52 return (0);
53}
diff --git a/src/lib/libc/string/strncpy.c b/src/lib/libc/string/strncpy.c
new file mode 100644
index 0000000000..00493f4f7f
--- /dev/null
+++ b/src/lib/libc/string/strncpy.c
@@ -0,0 +1,60 @@
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char *rcsid = "$OpenBSD: strncpy.c,v 1.4 2003/06/11 21:08:16 deraadt Exp $";
35#endif /* LIBC_SCCS and not lint */
36
37#include <string.h>
38
39/*
40 * Copy src to dst, truncating or null-padding to always copy n bytes.
41 * Return dst.
42 */
43char *
44strncpy(char *dst, const char *src, size_t n)
45{
46 if (n != 0) {
47 char *d = dst;
48 const char *s = src;
49
50 do {
51 if ((*d++ = *s++) == 0) {
52 /* NUL pad the remaining n-1 bytes */
53 while (--n != 0)
54 *d++ = 0;
55 break;
56 }
57 } while (--n != 0);
58 }
59 return (dst);
60}
diff --git a/src/lib/libc/string/strpbrk.3 b/src/lib/libc/string/strpbrk.3
new file mode 100644
index 0000000000..7e54f9de54
--- /dev/null
+++ b/src/lib/libc/string/strpbrk.3
@@ -0,0 +1,72 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strpbrk.3,v 1.6 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt STRPBRK 3
36.Os
37.Sh NAME
38.Nm strpbrk
39.Nd locate multiple characters in string
40.Sh SYNOPSIS
41.Fd #include <string.h>
42.Ft char *
43.Fn strpbrk "const char *s" "const char *charset"
44.Sh DESCRIPTION
45The
46.Fn strpbrk
47function locates in the null-terminated string
48.Fa s
49the first occurrence of any character in the string
50.Fa charset
51and returns a pointer to this character.
52If no characters from
53.Fa charset
54occur anywhere in
55.Fa s ,
56.Fn strpbrk
57returns
58.Dv NULL .
59.Sh SEE ALSO
60.Xr memchr 3 ,
61.Xr strchr 3 ,
62.Xr strcspn 3 ,
63.Xr strrchr 3 ,
64.Xr strsep 3 ,
65.Xr strspn 3 ,
66.Xr strstr 3 ,
67.Xr strtok 3
68.Sh STANDARDS
69The
70.Fn strpbrk
71function conforms to
72.St -ansiC .
diff --git a/src/lib/libc/string/strpbrk.c b/src/lib/libc/string/strpbrk.c
new file mode 100644
index 0000000000..024c42bdc3
--- /dev/null
+++ b/src/lib/libc/string/strpbrk.c
@@ -0,0 +1,51 @@
1/*
2 * Copyright (c) 1985 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: strpbrk.c,v 1.4 2003/06/11 21:08:16 deraadt Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#include <string.h>
35
36/*
37 * Find the first occurrence in s1 of a character in s2 (excluding NUL).
38 */
39char *
40strpbrk(const char *s1, const char *s2)
41{
42 const char *scanp;
43 int c, sc;
44
45 while ((c = *s1++) != 0) {
46 for (scanp = s2; (sc = *scanp++) != 0;)
47 if (sc == c)
48 return ((char *)(s1 - 1));
49 }
50 return (NULL);
51}
diff --git a/src/lib/libc/string/strrchr.3 b/src/lib/libc/string/strrchr.3
new file mode 100644
index 0000000000..d7e0eeff85
--- /dev/null
+++ b/src/lib/libc/string/strrchr.3
@@ -0,0 +1,104 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strrchr.3,v 1.7 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt STRRCHR 3
36.Os
37.Sh NAME
38.Nm strrchr ,
39.Nm rindex
40.Nd locate last occurrence of a character in a string
41.Sh SYNOPSIS
42.Fd #include <string.h>
43.Ft char *
44.Fn strrchr "const char *s" "int c"
45.Ft char *
46.Fn rindex "const char *s" "int c"
47.Sh DESCRIPTION
48The
49.Fn strrchr
50function locates the last occurrence of the character
51.Fa c
52in the string
53.Fa s .
54The terminating
55.Tn NUL
56character is considered part of the string.
57If
58.Fa c
59is
60.Ql \e0 ,
61.Fn strrchr
62locates the terminating
63.Ql \e0 .
64.Pp
65The
66.Fn rindex
67function is an old synonym for
68.Fn strrchr .
69.Sh RETURN VALUES
70The
71.Fn strrchr
72function returns a pointer to the located character or
73.Dv NULL
74if the character does not appear in the string.
75.Sh EXAMPLES
76After the following call to
77.Fn strrchr ,
78.Va p
79will point to the string
80.Qq obar :
81.Bd -literal -offset indent
82char *p;
83char *s = "foobar";
84
85p = strrchr(s, 'o');
86.Ed
87.Sh SEE ALSO
88.Xr memchr 3 ,
89.Xr strchr 3 ,
90.Xr strcspn 3 ,
91.Xr strpbrk 3 ,
92.Xr strsep 3 ,
93.Xr strspn 3 ,
94.Xr strstr 3 ,
95.Xr strtok 3
96.Sh STANDARDS
97The
98.Fn strrchr
99function conforms to
100.St -ansiC .
101.Pp
102The
103.Fn rindex
104function is deprecated and shouldn't be used in new code.
diff --git a/src/lib/libc/string/strsep.3 b/src/lib/libc/string/strsep.3
new file mode 100644
index 0000000000..ddcc22916a
--- /dev/null
+++ b/src/lib/libc/string/strsep.3
@@ -0,0 +1,112 @@
1.\" $OpenBSD: strsep.3,v 1.11 2003/06/02 20:18:38 millert Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\" notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\" notice, this list of conditions and the following disclaimer in the
16.\" documentation and/or other materials provided with the distribution.
17.\" 3. Neither the name of the University nor the names of its contributors
18.\" may be used to endorse or promote products derived from this software
19.\" without specific prior written permission.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31.\" SUCH DAMAGE.
32.\"
33.\" @(#)strsep.3 8.1 (Berkeley) 6/9/93
34.\"
35.Dd June 9, 1993
36.Dt STRSEP 3
37.Os
38.Sh NAME
39.Nm strsep
40.Nd separate strings
41.Sh SYNOPSIS
42.Fd #include <string.h>
43.Ft char *
44.Fn strsep "char **stringp" "const char *delim"
45.Sh DESCRIPTION
46The
47.Fn strsep
48function locates, in the string referenced by
49.Fa *stringp ,
50the first occurrence of any character in the string
51.Fa delim
52(or the terminating
53.Ql \e0
54character) and replaces it with a
55.Ql \e0 .
56The location of the next character after the delimiter character
57(or
58.Dv NULL ,
59if the end of the string was reached) is stored in
60.Fa *stringp .
61The original value of
62.Fa *stringp
63is returned.
64.Pp
65An
66.Dq empty
67field, i.e., one caused by two adjacent delimiter characters,
68can be detected by comparing the location referenced by the pointer returned
69by
70.Fn strsep
71to
72.Ql \e0 .
73.Pp
74If
75.Fa *stringp
76is initially
77.Dv NULL ,
78.Fn strsep
79returns
80.Dv NULL .
81.Sh EXAMPLES
82The following uses
83.Fn strsep
84to parse a string, containing tokens delimited by whitespace, into an
85argument vector:
86.Bd -literal -offset indent
87char **ap, *argv[10], *inputstring;
88
89for (ap = argv; ap < &argv[9] &&
90 (*ap = strsep(&inputstring, " \et")) != NULL;) {
91 if (**ap != '\e0')
92 ap++;
93}
94*ap = NULL;
95.Ed
96.Sh HISTORY
97The
98.Fn strsep
99function is intended as a replacement for the
100.Fn strtok
101function.
102While the
103.Fn strtok
104function should be preferred for portability reasons (it conforms to
105.St -ansiC )
106it is unable to handle empty fields, i.e., detect fields delimited by
107two adjacent delimiter characters, or to be used for more than a single
108string at a time.
109The
110.Fn strsep
111function first appeared in
112.Bx 4.4 .
diff --git a/src/lib/libc/string/strsep.c b/src/lib/libc/string/strsep.c
new file mode 100644
index 0000000000..337d78e466
--- /dev/null
+++ b/src/lib/libc/string/strsep.c
@@ -0,0 +1,79 @@
1/* $OpenBSD: strsep.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $ */
2
3/*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
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
17 * without specific prior written permission.
18 *
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
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <string.h>
33#include <stdio.h>
34
35#if defined(LIBC_SCCS) && !defined(lint)
36#if 0
37static char sccsid[] = "@(#)strsep.c 8.1 (Berkeley) 6/4/93";
38#else
39static char *rcsid = "$OpenBSD: strsep.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $";
40#endif
41#endif /* LIBC_SCCS and not lint */
42
43/*
44 * Get next token from string *stringp, where tokens are possibly-empty
45 * strings separated by characters from delim.
46 *
47 * Writes NULs into the string at *stringp to end tokens.
48 * delim need not remain constant from call to call.
49 * On return, *stringp points past the last NUL written (if there might
50 * be further tokens), or is NULL (if there are definitely no more tokens).
51 *
52 * If *stringp is NULL, strsep returns NULL.
53 */
54char *
55strsep(char **stringp, const char *delim)
56{
57 char *s;
58 const char *spanp;
59 int c, sc;
60 char *tok;
61
62 if ((s = *stringp) == NULL)
63 return (NULL);
64 for (tok = s;;) {
65 c = *s++;
66 spanp = delim;
67 do {
68 if ((sc = *spanp++) == c) {
69 if (c == 0)
70 s = NULL;
71 else
72 s[-1] = 0;
73 *stringp = s;
74 return (tok);
75 }
76 } while (sc != 0);
77 }
78 /* NOTREACHED */
79}
diff --git a/src/lib/libc/string/strsignal.3 b/src/lib/libc/string/strsignal.3
new file mode 100644
index 0000000000..b1082c2dd0
--- /dev/null
+++ b/src/lib/libc/string/strsignal.3
@@ -0,0 +1,56 @@
1.\" Copyright (c) 1980, 1991 Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strsignal.3,v 1.4 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt STRSIGNAL 3
36.Os
37.Sh NAME
38.Nm strsignal
39.Nd get signal description string
40.Sh SYNOPSIS
41.Fd #include <string.h>
42.Ft char *
43.Fn strsignal "int sig"
44.Sh DESCRIPTION
45The
46.Fn strsignal
47function returns a pointer to the language-dependent string describing
48a signal.
49.Pp
50The array pointed to is not to be modified by the program, but may be
51overwritten by subsequent calls to
52.Fn strsignal .
53.Sh SEE ALSO
54.Xr intro 2 ,
55.Xr psignal 3 ,
56.Xr setlocale 3
diff --git a/src/lib/libc/string/strsignal.c b/src/lib/libc/string/strsignal.c
new file mode 100644
index 0000000000..afe0deb272
--- /dev/null
+++ b/src/lib/libc/string/strsignal.c
@@ -0,0 +1,45 @@
1/*
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: strsignal.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#include <string.h>
35#include <limits.h>
36
37extern char *__strsignal(int, char *);
38
39char *
40strsignal(int sig)
41{
42 static char buf[NL_TEXTMAX];
43
44 return __strsignal(sig, buf);
45}
diff --git a/src/lib/libc/string/strspn.3 b/src/lib/libc/string/strspn.3
new file mode 100644
index 0000000000..54077b810c
--- /dev/null
+++ b/src/lib/libc/string/strspn.3
@@ -0,0 +1,84 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strspn.3,v 1.7 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt STRSPN 3
36.Os
37.Sh NAME
38.Nm strspn
39.Nd span a string
40.Sh SYNOPSIS
41.Fd #include <string.h>
42.Ft size_t
43.Fn strspn "const char *s" "const char *charset"
44.Sh DESCRIPTION
45The
46.Fn strspn
47function spans the initial part of the null-terminated string
48.Fa s
49as long as the characters from
50.Fa s
51occur in string
52.Fa charset .
53.Sh RETURN VALUES
54The
55.Fn strspn
56function returns the number of characters spanned.
57.Sh EXAMPLES
58The following call to
59.Fn strspn
60will return 3, since the first three characters of string
61.Fa s
62are part of string
63.Fa charset :
64.Bd -literal -offset indent
65char *s = "foobar";
66char *charset = "of";
67size_t span;
68
69span = strspn(s, charset);
70.Ed
71.Sh SEE ALSO
72.Xr memchr 3 ,
73.Xr strchr 3 ,
74.Xr strcspn 3 ,
75.Xr strpbrk 3 ,
76.Xr strrchr 3 ,
77.Xr strsep 3 ,
78.Xr strstr 3 ,
79.Xr strtok 3
80.Sh STANDARDS
81The
82.Fn strspn
83function conforms to
84.St -ansiC .
diff --git a/src/lib/libc/string/strspn.c b/src/lib/libc/string/strspn.c
new file mode 100644
index 0000000000..d51a8cd3e6
--- /dev/null
+++ b/src/lib/libc/string/strspn.c
@@ -0,0 +1,54 @@
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: strspn.c,v 1.4 2003/06/11 21:08:29 deraadt Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#include <string.h>
35
36/*
37 * Span the string s2 (skip characters that are in s2).
38 */
39size_t
40strspn(const char *s1, const char *s2)
41{
42 const char *p = s1, *spanp;
43 char c, sc;
44
45 /*
46 * Skip any characters in s2, excluding the terminating \0.
47 */
48cont:
49 c = *p++;
50 for (spanp = s2; (sc = *spanp++) != 0;)
51 if (sc == c)
52 goto cont;
53 return (p - 1 - s1);
54}
diff --git a/src/lib/libc/string/strstr.3 b/src/lib/libc/string/strstr.3
new file mode 100644
index 0000000000..47777dd89f
--- /dev/null
+++ b/src/lib/libc/string/strstr.3
@@ -0,0 +1,81 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strstr.3,v 1.6 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt STRSTR 3
36.Os
37.Sh NAME
38.Nm strstr
39.Nd locate a substring in a string
40.Sh SYNOPSIS
41.Fd #include <string.h>
42.Ft char *
43.Fn strstr "const char *big" "const char *little"
44.Sh DESCRIPTION
45The
46.Fn strstr
47function locates the first occurrence of the null-terminated string
48.Fa little
49in the null-terminated string
50.Fa big .
51If
52.Fa little
53is the empty string,
54.Fn strstr
55returns
56.Fa big ;
57if
58.Fa little
59occurs nowhere in
60.Fa big ,
61.Fn strstr
62returns
63.Dv NULL ;
64otherwise
65.Fn strstr
66returns a pointer to the first character of the first occurrence of
67.Fa little .
68.Sh SEE ALSO
69.Xr memchr 3 ,
70.Xr strchr 3 ,
71.Xr strcspn 3 ,
72.Xr strpbrk 3 ,
73.Xr strrchr 3 ,
74.Xr strsep 3 ,
75.Xr strspn 3 ,
76.Xr strtok 3
77.Sh STANDARDS
78The
79.Fn strstr
80function conforms to
81.St -ansiC .
diff --git a/src/lib/libc/string/strstr.c b/src/lib/libc/string/strstr.c
new file mode 100644
index 0000000000..e6a94c5ce6
--- /dev/null
+++ b/src/lib/libc/string/strstr.c
@@ -0,0 +1,59 @@
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char *rcsid = "$OpenBSD: strstr.c,v 1.4 2003/06/11 21:08:16 deraadt Exp $";
35#endif /* LIBC_SCCS and not lint */
36
37#include <string.h>
38
39/*
40 * Find the first occurrence of find in s.
41 */
42char *
43strstr(const char *s, const char *find)
44{
45 char c, sc;
46 size_t len;
47
48 if ((c = *find++) != 0) {
49 len = strlen(find);
50 do {
51 do {
52 if ((sc = *s++) == 0)
53 return (NULL);
54 } while (sc != c);
55 } while (strncmp(s, find, len) != 0);
56 s--;
57 }
58 return ((char *)s);
59}
diff --git a/src/lib/libc/string/strtok.3 b/src/lib/libc/string/strtok.3
new file mode 100644
index 0000000000..bdf28f68f6
--- /dev/null
+++ b/src/lib/libc/string/strtok.3
@@ -0,0 +1,152 @@
1.\" Copyright (c) 1988, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strtok.3,v 1.16 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt STRTOK 3
36.Os
37.Sh NAME
38.Nm strtok ,
39.Nm strtok_r
40.Nd string token operations
41.Sh SYNOPSIS
42.Fd #include <string.h>
43.Ft char *
44.Fn strtok "char *str" "const char *sep"
45.Ft char *
46.Fn strtok_r "char *str" "const char *sep" "char **last"
47.Sh DESCRIPTION
48.Bf -symbolic
49This interface is obsoleted by
50.Xr strsep 3 .
51.Ef
52.Pp
53The
54.Fn strtok
55function is used to isolate sequential tokens in a null-terminated string,
56.Fa str .
57These tokens are separated in the string by at least one of the
58characters in
59.Fa sep .
60The first time that
61.Fn strtok
62is called,
63.Fa str
64should be specified; subsequent calls, wishing to obtain further tokens
65from the same string, should pass a null pointer instead.
66The separator string,
67.Fa sep ,
68must be supplied each time, and may change between calls.
69.Pp
70The
71.Fn strtok_r
72function is a version of
73.Fn strtok
74that takes an explicit context argument and is reentrant.
75.Pp
76The
77.Fn strtok
78and
79.Fn strtok_r
80functions return a pointer to the beginning of each subsequent token
81in the string, after replacing the separator character itself with an
82.Tn ASCII NUL
83character.
84When no more tokens remain, a null pointer is returned.
85.Pp
86Since
87.Fn strtok
88and
89.Fn strtok_r
90modify the string,
91.Fa str
92should not point to an area in the initialized data segment.
93.Sh EXAMPLES
94The following will construct an array of pointers to each individual word in
95the string
96.Va s :
97.Bd -literal -offset indent
98#define MAXTOKENS 128
99
100char s[512], *p, *tokens[MAXTOKENS];
101char *last;
102int i = 0;
103
104snprintf(s, sizeof(s), "cat dog horse cow");
105
106for ((p = strtok_r(s, " ", &last)); p;
107 (p = strtok_r(NULL, " ", &last)), i++) {
108 if (i < MAXTOKENS - 1)
109 tokens[i] = p;
110}
111tokens[i] = NULL;
112.Ed
113.Pp
114That is,
115.Li tokens[0]
116will point to
117.Qq cat ,
118.Li tokens[1]
119will point to
120.Qq dog ,
121.Li tokens[2]
122will point to
123.Qq horse ,
124and
125.Li tokens[3]
126will point to
127.Qq cow .
128.Sh SEE ALSO
129.Xr memchr 3 ,
130.Xr strchr 3 ,
131.Xr strcspn 3 ,
132.Xr strpbrk 3 ,
133.Xr strrchr 3 ,
134.Xr strsep 3 ,
135.Xr strspn 3 ,
136.Xr strstr 3
137.Sh STANDARDS
138The
139.Fn strtok
140function conforms to
141.St -ansiC .
142.Sh BUGS
143The System V
144.Fn strtok ,
145if handed a string containing only delimiter characters,
146will not alter the next starting point, so that a call to
147.Fn strtok
148with a different (or empty) delimiter string
149may return a non-null value.
150Since this implementation always alters the next starting point,
151such a sequence of calls would always return
152.Dv NULL .
diff --git a/src/lib/libc/string/strtok.c b/src/lib/libc/string/strtok.c
new file mode 100644
index 0000000000..6498eac8cc
--- /dev/null
+++ b/src/lib/libc/string/strtok.c
@@ -0,0 +1,90 @@
1/*
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: strtok.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#include <string.h>
35
36char *
37strtok(char *s, const char *delim)
38{
39 static char *last;
40
41 return strtok_r(s, delim, &last);
42}
43
44char *
45strtok_r(char *s, const char *delim, char **last)
46{
47 char *spanp;
48 int c, sc;
49 char *tok;
50
51
52 if (s == NULL && (s = *last) == NULL)
53 return (NULL);
54
55 /*
56 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
57 */
58cont:
59 c = *s++;
60 for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
61 if (c == sc)
62 goto cont;
63 }
64
65 if (c == 0) { /* no non-delimiter characters */
66 *last = NULL;
67 return (NULL);
68 }
69 tok = s - 1;
70
71 /*
72 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
73 * Note that delim must have one NUL; we stop if we see that, too.
74 */
75 for (;;) {
76 c = *s++;
77 spanp = (char *)delim;
78 do {
79 if ((sc = *spanp++) == c) {
80 if (c == 0)
81 s = NULL;
82 else
83 s[-1] = 0;
84 *last = s;
85 return (tok);
86 }
87 } while (sc != 0);
88 }
89 /* NOTREACHED */
90}
diff --git a/src/lib/libc/string/strxfrm.3 b/src/lib/libc/string/strxfrm.3
new file mode 100644
index 0000000000..edf2bfb8a0
--- /dev/null
+++ b/src/lib/libc/string/strxfrm.3
@@ -0,0 +1,62 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the
15.\" documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: strxfrm.3,v 1.4 2003/06/02 20:18:38 millert Exp $
33.\"
34.Dd June 29, 1991
35.Dt STRXFRM 3
36.Os
37.Sh NAME
38.Nm strxfrm
39.Nd transform a string under locale
40.Sh SYNOPSIS
41.Fd #include <string.h>
42.Ft size_t
43.Fn strxfrm "char *dst" "const char *src" "size_t n"
44.Sh DESCRIPTION
45The
46.Fn strxfrm
47function does something horrible (see
48.Tn ANSI
49standard).
50In this implementation it just copies.
51.Sh SEE ALSO
52.Xr bcmp 3 ,
53.Xr memcmp 3 ,
54.\" .Xr setlocale 3 ,
55.Xr strcasecmp 3 ,
56.Xr strcmp 3 ,
57.Xr strcoll 3
58.Sh STANDARDS
59The
60.Fn strxfrm
61function conforms to
62.St -ansiC .
diff --git a/src/lib/libc/string/strxfrm.c b/src/lib/libc/string/strxfrm.c
new file mode 100644
index 0000000000..a2e2dbc440
--- /dev/null
+++ b/src/lib/libc/string/strxfrm.c
@@ -0,0 +1,54 @@
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char *rcsid = "$OpenBSD: strxfrm.c,v 1.5 2003/09/06 22:43:12 tedu Exp $";
35#endif /* LIBC_SCCS and not lint */
36
37#include <string.h>
38
39/*
40 * Transform src, storing the result in dst, such that
41 * strcmp() on transformed strings returns what strcoll()
42 * on the original untransformed strings would return.
43 */
44size_t
45strxfrm(char *dst, const char *src, size_t n)
46{
47
48 /*
49 * Since locales are unimplemented, this is just a copy.
50 */
51 if (n == 0)
52 return (strlen(src));
53 return (strlcpy(dst, src, n));
54}
diff --git a/src/lib/libc/string/swab.3 b/src/lib/libc/string/swab.3
new file mode 100644
index 0000000000..abeff903de
--- /dev/null
+++ b/src/lib/libc/string/swab.3
@@ -0,0 +1,61 @@
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
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.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\" may be used to endorse or promote products derived from this software
14.\" without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\" $OpenBSD: swab.3,v 1.5 2003/06/02 20:18:38 millert Exp $
29.\"
30.Dd May 1, 1991
31.Dt SWAB 3
32.Os
33.Sh NAME
34.Nm swab
35.Nd swap adjacent bytes
36.Sh SYNOPSIS
37.Fd #include <unistd.h>
38.Ft void
39.Fn swab "const void *src" "void *dst" "size_t len"
40.Sh DESCRIPTION
41The function
42.Fn swab
43copies
44.Fa len
45bytes from the location referenced by
46.Fa src
47to the location referenced by
48.Fa dst ,
49swapping adjacent bytes.
50.Pp
51The argument
52.Fa len
53must be an even number.
54.Sh SEE ALSO
55.Xr bzero 3 ,
56.Xr memset 3
57.Sh HISTORY
58A
59.Fn swab
60function appeared in
61.At v7 .
diff --git a/src/lib/libc/string/swab.c b/src/lib/libc/string/swab.c
new file mode 100644
index 0000000000..b928f21aa7
--- /dev/null
+++ b/src/lib/libc/string/swab.c
@@ -0,0 +1,58 @@
1/*
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Jeffrey Mogul.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char *rcsid = "$OpenBSD: swab.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $";
35#endif /* LIBC_SCCS and not lint */
36
37#include <unistd.h>
38
39void
40swab(const void *from, void *to, size_t len)
41{
42 register unsigned long temp;
43 register int n;
44 register char *fp, *tp;
45
46 n = (len >> 1) + 1;
47 fp = (char *)from;
48 tp = (char *)to;
49#define STEP temp = *fp++,*tp++ = *fp++,*tp++ = temp
50 /* round to multiple of 8 */
51 while ((--n) & 07)
52 STEP;
53 n >>= 3;
54 while (--n >= 0) {
55 STEP; STEP; STEP; STEP;
56 STEP; STEP; STEP; STEP;
57 }
58}