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.inc151
-rw-r--r--src/lib/libc/string/__strerror.c114
-rw-r--r--src/lib/libc/string/__strsignal.c102
-rw-r--r--src/lib/libc/string/bcmp.370
-rw-r--r--src/lib/libc/string/bcmp.c63
-rw-r--r--src/lib/libc/string/bcopy.370
-rw-r--r--src/lib/libc/string/bcopy.c138
-rw-r--r--src/lib/libc/string/bm.3115
-rw-r--r--src/lib/libc/string/bm.c219
-rw-r--r--src/lib/libc/string/bstring.3110
-rw-r--r--src/lib/libc/string/bzero.365
-rw-r--r--src/lib/libc/string/bzero.c56
-rw-r--r--src/lib/libc/string/ffs.360
-rw-r--r--src/lib/libc/string/ffs.c49
-rw-r--r--src/lib/libc/string/index.c54
-rw-r--r--src/lib/libc/string/memccpy.376
-rw-r--r--src/lib/libc/string/memccpy.c64
-rw-r--r--src/lib/libc/string/memchr.378
-rw-r--r--src/lib/libc/string/memchr.c58
-rw-r--r--src/lib/libc/string/memcmp.381
-rw-r--r--src/lib/libc/string/memcmp.c60
-rw-r--r--src/lib/libc/string/memcpy.380
-rw-r--r--src/lib/libc/string/memmove.373
-rw-r--r--src/lib/libc/string/memset.371
-rw-r--r--src/lib/libc/string/memset.c58
-rw-r--r--src/lib/libc/string/rindex.c56
-rw-r--r--src/lib/libc/string/strcasecmp.388
-rw-r--r--src/lib/libc/string/strcasecmp.c120
-rw-r--r--src/lib/libc/string/strcat.3135
-rw-r--r--src/lib/libc/string/strcat.c54
-rw-r--r--src/lib/libc/string/strchr.3106
-rw-r--r--src/lib/libc/string/strcmp.391
-rw-r--r--src/lib/libc/string/strcmp.c58
-rw-r--r--src/lib/libc/string/strcoll.372
-rw-r--r--src/lib/libc/string/strcoll.c52
-rw-r--r--src/lib/libc/string/strcpy.3154
-rw-r--r--src/lib/libc/string/strcpy.c53
-rw-r--r--src/lib/libc/string/strcspn.392
-rw-r--r--src/lib/libc/string/strcspn.c67
-rw-r--r--src/lib/libc/string/strdup.387
-rw-r--r--src/lib/libc/string/strdup.c62
-rw-r--r--src/lib/libc/string/strerror.369
-rw-r--r--src/lib/libc/string/strerror.c55
-rw-r--r--src/lib/libc/string/string.3161
-rw-r--r--src/lib/libc/string/strlcat.c73
-rw-r--r--src/lib/libc/string/strlcpy.3190
-rw-r--r--src/lib/libc/string/strlcpy.c69
-rw-r--r--src/lib/libc/string/strlen.365
-rw-r--r--src/lib/libc/string/strlen.c56
-rw-r--r--src/lib/libc/string/strmode.3163
-rw-r--r--src/lib/libc/string/strmode.c152
-rw-r--r--src/lib/libc/string/strncat.c67
-rw-r--r--src/lib/libc/string/strncmp.c59
-rw-r--r--src/lib/libc/string/strncpy.c67
-rw-r--r--src/lib/libc/string/strpbrk.376
-rw-r--r--src/lib/libc/string/strpbrk.c56
-rw-r--r--src/lib/libc/string/strrchr.3108
-rw-r--r--src/lib/libc/string/strsep.3116
-rw-r--r--src/lib/libc/string/strsep.c85
-rw-r--r--src/lib/libc/string/strsignal.360
-rw-r--r--src/lib/libc/string/strsignal.c49
-rw-r--r--src/lib/libc/string/strspn.388
-rw-r--r--src/lib/libc/string/strspn.c60
-rw-r--r--src/lib/libc/string/strstr.385
-rw-r--r--src/lib/libc/string/strstr.c64
-rw-r--r--src/lib/libc/string/strtok.3156
-rw-r--r--src/lib/libc/string/strtok.c99
-rw-r--r--src/lib/libc/string/strxfrm.366
-rw-r--r--src/lib/libc/string/strxfrm.c73
-rw-r--r--src/lib/libc/string/swab.365
-rw-r--r--src/lib/libc/string/swab.c65
71 files changed, 6099 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..e7b81d0c43
--- /dev/null
+++ b/src/lib/libc/string/Makefile.inc
@@ -0,0 +1,151 @@
1# $OpenBSD: Makefile.inc,v 1.10 2001/09/05 16:27:01 mickey 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 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
diff --git a/src/lib/libc/string/__strerror.c b/src/lib/libc/string/__strerror.c
new file mode 100644
index 0000000000..ae19ab3365
--- /dev/null
+++ b/src/lib/libc/string/__strerror.c
@@ -0,0 +1,114 @@
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: __strerror.c,v 1.8 2001/12/08 20:37:32 deraadt Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#ifdef NLS
39#define catclose _catclose
40#define catgets _catgets
41#define catopen _catopen
42#include <nl_types.h>
43#endif
44
45#define sys_errlist _sys_errlist
46#define sys_nerr _sys_nerr
47
48#include <errno.h>
49#include <limits.h>
50#include <stdio.h>
51#include <string.h>
52
53static char *
54itoa(num)
55 int num;
56{
57 static char buffer[11];
58 char *p;
59
60 p = buffer + 4;
61 while (num >= 10) {
62 *--p = (num % 10) + '0';
63 num /= 10;
64 }
65 *p = (num % 10) + '0';
66 return p;
67}
68
69/*
70 * Since perror() is not allowed to change the contents of strerror()'s
71 * static buffer, both functions supply their own buffers to the
72 * internal function __strerror().
73 */
74
75char *
76__strerror(num, buf)
77 int num;
78 char *buf;
79{
80#define UPREFIX "Unknown error: "
81 register unsigned int errnum;
82#ifdef NLS
83 int save_errno;
84 nl_catd catd;
85
86 catd = catopen("libc", 0);
87#endif
88
89 errnum = num; /* convert to unsigned */
90 if (errnum < sys_nerr) {
91#ifdef NLS
92 strlcpy(buf, catgets(catd, 1, errnum,
93 (char *)sys_errlist[errnum]), NL_TEXTMAX);
94#else
95 return(sys_errlist[errnum]);
96#endif
97 } else {
98#ifdef NLS
99 strlcpy(buf, catgets(catd, 1, 0xffff, UPREFIX), NL_TEXTMAX);
100#else
101 strcpy(buf, UPREFIX);
102#endif
103 strncat(buf, itoa(errnum), NL_TEXTMAX-strlen(buf)-1);
104 errno = EINVAL;
105 }
106
107#ifdef NLS
108 save_errno = errno;
109 catclose(catd);
110 errno = save_errno;
111#endif
112
113 return buf;
114}
diff --git a/src/lib/libc/string/__strsignal.c b/src/lib/libc/string/__strsignal.c
new file mode 100644
index 0000000000..4ca5bad3c0
--- /dev/null
+++ b/src/lib/libc/string/__strsignal.c
@@ -0,0 +1,102 @@
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: __strsignal.c,v 1.6 2001/06/27 00:58:56 lebel Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#ifdef NLS
39#define catclose _catclose
40#define catgets _catgets
41#define catopen _catopen
42#include <nl_types.h>
43#endif
44
45#define sys_siglist _sys_siglist
46
47#include <stdio.h>
48#include <limits.h>
49#include <signal.h>
50#include <string.h>
51
52static char *itoa(num)
53 int num;
54{
55 static char buffer[11];
56 char *p;
57
58 p = buffer + 4;
59 while (num >= 10) {
60 *--p = (num % 10) + '0';
61 num /= 10;
62 }
63 *p = (num % 10) + '0';
64 return p;
65}
66
67char *
68__strsignal(num, buf)
69 int num;
70 char *buf;
71{
72#define UPREFIX "Unknown signal: "
73 register unsigned int signum;
74
75#ifdef NLS
76 nl_catd catd ;
77 catd = catopen("libc", 0);
78#endif
79
80 signum = num; /* convert to unsigned */
81 if (signum < NSIG) {
82#ifdef NLS
83 strlcpy(buf, catgets(catd, 2, signum,
84 (char *)sys_siglist[signum]), NL_TEXTMAX);
85#else
86 return((char *)sys_siglist[signum]);
87#endif
88 } else {
89#ifdef NLS
90 strlcpy(buf, catgets(catd, 1, 0xffff, UPREFIX), NL_TEXTMAX);
91#else
92 strcpy(buf, UPREFIX);
93#endif
94 strncat(buf, itoa(signum), NL_TEXTMAX-strlen(buf)-1);
95 }
96
97#ifdef NLS
98 catclose(catd);
99#endif
100
101 return buf;
102}
diff --git a/src/lib/libc/string/bcmp.3 b/src/lib/libc/string/bcmp.3
new file mode 100644
index 0000000000..d880c1e6f0
--- /dev/null
+++ b/src/lib/libc/string/bcmp.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.
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. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" $OpenBSD: bcmp.3,v 1.5 2000/04/21 15:24:18 aaron Exp $
35.\"
36.Dd April 19, 1991
37.Dt BCMP 3
38.Os
39.Sh NAME
40.Nm bcmp
41.Nd compare byte string
42.Sh SYNOPSIS
43.Fd #include <string.h>
44.Ft int
45.Fn bcmp "const void *b1" "const void *b2" "size_t len"
46.Sh DESCRIPTION
47The
48.Fn bcmp
49function compares byte string
50.Fa b1
51against byte string
52.Fa b2 ,
53returning zero if they are identical, non-zero otherwise.
54Both strings are assumed to be
55.Fa len
56bytes long.
57Zero-length strings are always identical.
58.Pp
59The strings may overlap.
60.Sh SEE ALSO
61.Xr memcmp 3 ,
62.Xr strcasecmp 3 ,
63.Xr strcmp 3 ,
64.Xr strcoll 3 ,
65.Xr strxfrm 3
66.Sh HISTORY
67A
68.Fn bcmp
69function first appeared in
70.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..4ed00975a4
--- /dev/null
+++ b/src/lib/libc/string/bcmp.c
@@ -0,0 +1,63 @@
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: bcmp.c,v 1.4 1996/08/19 08:33:57 tholo Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#ifndef _KERNEL
39#include <string.h>
40#else
41#include <lib/libkern/libkern.h>
42#endif
43
44/*
45 * bcmp -- vax cmpc3 instruction
46 */
47int
48bcmp(b1, b2, length)
49 const void *b1, *b2;
50 register size_t length;
51{
52 register char *p1, *p2;
53
54 if (length == 0)
55 return(0);
56 p1 = (char *)b1;
57 p2 = (char *)b2;
58 do
59 if (*p1++ != *p2++)
60 break;
61 while (--length);
62 return(length);
63}
diff --git a/src/lib/libc/string/bcopy.3 b/src/lib/libc/string/bcopy.3
new file mode 100644
index 0000000000..b290418f4a
--- /dev/null
+++ b/src/lib/libc/string/bcopy.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.
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. All advertising materials mentioning features or use of this software
16.\" must display the following acknowledgement:
17.\" This product includes software developed by the University of
18.\" California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\" may be used to endorse or promote products derived from this software
21.\" without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\" $OpenBSD: bcopy.3,v 1.5 2000/04/21 15:24:19 aaron Exp $
36.\"
37.Dd April 19, 1991
38.Dt BCOPY 3
39.Os
40.Sh NAME
41.Nm bcopy
42.Nd copy byte string
43.Sh SYNOPSIS
44.Fd #include <string.h>
45.Ft void
46.Fn bcopy "const void *src" "void *dst" "size_t len"
47.Sh DESCRIPTION
48The
49.Fn bcopy
50function copies
51.Fa len
52bytes from string
53.Fa src
54to string
55.Fa dst .
56The two strings may overlap.
57If
58.Fa len
59is zero, no bytes are copied.
60.Sh SEE ALSO
61.Xr memccpy 3 ,
62.Xr memcpy 3 ,
63.Xr memmove 3 ,
64.Xr strcpy 3 ,
65.Xr strncpy 3
66.Sh HISTORY
67A
68.Fn bcopy
69function appeared in
70.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..023a3b2db2
--- /dev/null
+++ b/src/lib/libc/string/bcopy.c
@@ -0,0 +1,138 @@
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char *rcsid = "$OpenBSD: bcopy.c,v 1.2 1996/08/19 08:33:58 tholo Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#include <string.h>
42
43/*
44 * sizeof(word) MUST BE A POWER OF TWO
45 * SO THAT wmask BELOW IS ALL ONES
46 */
47typedef long word; /* "word" used for optimal copy speed */
48
49#define wsize sizeof(word)
50#define wmask (wsize - 1)
51
52/*
53 * Copy a block of memory, handling overlap.
54 * This is the routine that actually implements
55 * (the portable versions of) bcopy, memcpy, and memmove.
56 */
57#ifdef MEMCOPY
58void *
59memcpy(dst0, src0, length)
60#else
61#ifdef MEMMOVE
62void *
63memmove(dst0, src0, length)
64#else
65void
66bcopy(src0, dst0, length)
67#endif
68#endif
69 void *dst0;
70 const void *src0;
71 register size_t length;
72{
73 register char *dst = dst0;
74 register const char *src = src0;
75 register size_t t;
76
77 if (length == 0 || dst == src) /* nothing to do */
78 goto done;
79
80 /*
81 * Macros: loop-t-times; and loop-t-times, t>0
82 */
83#define TLOOP(s) if (t) TLOOP1(s)
84#define TLOOP1(s) do { s; } while (--t)
85
86 if ((unsigned long)dst < (unsigned long)src) {
87 /*
88 * Copy forward.
89 */
90 t = (long)src; /* only need low bits */
91 if ((t | (long)dst) & wmask) {
92 /*
93 * Try to align operands. This cannot be done
94 * unless the low bits match.
95 */
96 if ((t ^ (long)dst) & wmask || length < wsize)
97 t = length;
98 else
99 t = wsize - (t & wmask);
100 length -= t;
101 TLOOP1(*dst++ = *src++);
102 }
103 /*
104 * Copy whole words, then mop up any trailing bytes.
105 */
106 t = length / wsize;
107 TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
108 t = length & wmask;
109 TLOOP(*dst++ = *src++);
110 } else {
111 /*
112 * Copy backwards. Otherwise essentially the same.
113 * Alignment works as before, except that it takes
114 * (t&wmask) bytes to align, not wsize-(t&wmask).
115 */
116 src += length;
117 dst += length;
118 t = (long)src;
119 if ((t | (long)dst) & wmask) {
120 if ((t ^ (long)dst) & wmask || length <= wsize)
121 t = length;
122 else
123 t &= wmask;
124 length -= t;
125 TLOOP1(*--dst = *--src);
126 }
127 t = length / wsize;
128 TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
129 t = length & wmask;
130 TLOOP(*--dst = *--src);
131 }
132done:
133#if defined(MEMCOPY) || defined(MEMMOVE)
134 return (dst0);
135#else
136 return;
137#endif
138}
diff --git a/src/lib/libc/string/bm.3 b/src/lib/libc/string/bm.3
new file mode 100644
index 0000000000..7138ee4271
--- /dev/null
+++ b/src/lib/libc/string/bm.3
@@ -0,0 +1,115 @@
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. All advertising materials mentioning features or use of this software
16.\" must display the following acknowledgement:
17.\" This product includes software developed by the University of
18.\" California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\" may be used to endorse or promote products derived from this software
21.\" without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\" $OpenBSD: bm.3,v 1.6 2001/06/23 14:42:12 deraadt Exp $
36.\"
37.Dd June 29, 1999
38.Dt BM 3
39.Os
40.Sh NAME
41.Nm bm_comp ,
42.Nm bm_exec ,
43.Nm bm_free
44.Nd Boyer-Moore string search
45.Sh SYNOPSIS
46.Fd #include <sys/types.h>
47.Fd #include <bm.h>
48.Ft bm_pat *
49.Fn bm_comp "u_char *pattern" "size_t patlen" "u_char freq[256]"
50.Ft u_char *
51.Fn bm_exec "bm_pat *pdesc" "u_char *text" "size_t len"
52.Ft void
53.Fn bm_free "bm_pat *pdesc"
54.Sh DESCRIPTION
55These routines implement an efficient mechanism to find an
56occurrence of a byte string within another byte string.
57.Pp
58.Fn bm_comp
59evaluates
60.Fa patlen
61bytes starting at
62.Fa pattern
63and returns a pointer to a structure describing them.
64The bytes referenced by
65.Fa pattern
66may be of any value.
67.Pp
68The search takes advantage of the frequency distribution of the
69bytes in the text to be searched.
70If specified,
71.Ar freq
72should be an array of 256 values,
73with higher values indicating that the corresponding character occurs
74more frequently.
75(A less than optimal frequency distribution can only result in less
76than optimal performance, not incorrect results.)
77If
78.Ar freq
79is
80.Dv NULL ,
81a system default table is used.
82.Pp
83.Fn bm_exec
84returns a pointer to the leftmost occurrence of the string given to
85.Fn bm_comp
86within
87.Ar text ,
88or
89.Dv NULL
90if none occurs.
91The number of bytes in
92.Ar text
93must be specified by
94.Ar len .
95.Pp
96Space allocated for the returned description is discarded
97by calling
98.Fn bm_free
99with the returned description as an argument.
100.Pp
101The asymptotic speed of
102.Fn bm_exec
103is
104.Pf O Ns Pq len / patlen .
105.Sh SEE ALSO
106.Xr regexp 3 ,
107.Xr strstr 3
108.Rs
109.%R "Fast String Searching"
110.%A Andrew Hume
111.%A Daniel Sunday
112.%J "Software Practice and Experience"
113.%V Volume 21, 11 (November 1991)
114.%P 1221-48
115.Re
diff --git a/src/lib/libc/string/bm.c b/src/lib/libc/string/bm.c
new file mode 100644
index 0000000000..b191d340f6
--- /dev/null
+++ b/src/lib/libc/string/bm.c
@@ -0,0 +1,219 @@
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char *rcsid = "$OpenBSD: bm.c,v 1.3 1996/08/19 08:33:59 tholo Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#include <sys/types.h>
42
43#include <bm.h>
44#include <errno.h>
45#include <stdlib.h>
46#include <string.h>
47
48/*
49 * XXX
50 * The default frequency table starts at 99 and counts down. The default
51 * table should probably be oriented toward text, and will necessarily be
52 * locale specific. This one is for English. It was derived from the
53 * OSF/1 and 4.4BSD formatted and unformatted manual pages, and about 100Mb
54 * of email and random text. Change it if you can find something better.
55 */
56static u_char const freq_def[256] = {
57 0, 0, 0, 0, 0, 0, 0, 0,
58 0, 77, 90, 0, 0, 0, 0, 0,
59 0, 0, 0, 0, 0, 0, 0, 0,
60 0, 0, 0, 0, 0, 0, 0, 0,
61 99, 28, 42, 27, 16, 14, 20, 51,
62 66, 65, 59, 24, 75, 76, 84, 56,
63 72, 74, 64, 55, 54, 47, 41, 37,
64 44, 61, 70, 43, 23, 53, 49, 22,
65 33, 58, 40, 46, 45, 57, 60, 26,
66 30, 63, 21, 12, 32, 50, 38, 39,
67 34, 11, 48, 67, 62, 35, 15, 29,
68 71, 18, 9, 17, 25, 13, 10, 52,
69 36, 95, 78, 86, 87, 98, 82, 80,
70 88, 94, 19, 68, 89, 83, 93, 96,
71 81, 7, 91, 92, 97, 85, 69, 73,
72 31, 79, 8, 5, 4, 6, 3, 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 0, 0, 0, 0, 0, 0, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0,
87 0, 0, 0, 0, 0, 0, 0, 0,
88 0, 0, 0, 0, 0, 0, 0, 0,
89};
90
91bm_pat *
92bm_comp(pb, len, freq)
93 u_char const *pb;
94 size_t len;
95 u_char const *freq;
96{
97 register u_char const *pe, *p;
98 register size_t *d, r;
99 register int j;
100 int sv_errno;
101 bm_pat *pat;
102
103 if (len == 0) {
104 errno = EINVAL;
105 return (NULL);
106 }
107 if ((pat = malloc(sizeof(*pat))) == NULL)
108 return (NULL);
109 pat->pat = NULL;
110 pat->delta = NULL;
111
112 pat->patlen = len; /* copy pattern */
113 if ((pat->pat = malloc(pat->patlen)) == NULL)
114 goto mem;
115 memcpy(pat->pat, pb, pat->patlen);
116 /* get skip delta */
117 if ((pat->delta = malloc(256 * sizeof(*d))) == NULL)
118 goto mem;
119 for (j = 0, d = pat->delta; j < 256; j++)
120 d[j] = pat->patlen;
121 for (pe = pb + pat->patlen - 1; pb <= pe; pb++)
122 d[*pb] = pe - pb;
123
124 if (freq == NULL) /* default freq table */
125 freq = freq_def;
126 r = 0; /* get guard */
127 for (pb = pat->pat, pe = pb + pat->patlen - 1; pb < pe; pb++)
128 if (freq[*pb] < freq[pat->pat[r]])
129 r = pb - pat->pat;
130 pat->rarec = pat->pat[r];
131 pat->rareoff = r - (pat->patlen - 1);
132
133 /* get md2 shift */
134 for (pe = pat->pat + pat->patlen - 1, p = pe - 1; p >= pat->pat; p--)
135 if (*p == *pe)
136 break;
137
138 /* *p is first leftward reoccurrence of *pe */
139 pat->md2 = pe - p;
140 return (pat);
141
142mem: sv_errno = errno;
143 bm_free(pat);
144 errno = sv_errno;
145 return (NULL);
146}
147
148void
149bm_free(pat)
150 bm_pat *pat;
151{
152 if (pat->pat != NULL)
153 free(pat->pat);
154 if (pat->delta != NULL)
155 free(pat->delta);
156 free(pat);
157}
158
159u_char *
160bm_exec(pat, base, n)
161 bm_pat *pat;
162 u_char *base;
163 size_t n;
164{
165 register u_char *e, *ep, *p, *q, *s;
166 register size_t *d0, k, md2, n1, ro;
167 register int rc;
168
169 if (n == 0)
170 return (NULL);
171
172 d0 = pat->delta;
173 n1 = pat->patlen - 1;
174 md2 = pat->md2;
175 ro = pat->rareoff;
176 rc = pat->rarec;
177 ep = pat->pat + pat->patlen - 1;
178 s = base + (pat->patlen - 1);
179
180 /* fast loop up to n - 3 * patlen */
181 e = base + n - 3 * pat->patlen;
182 while (s < e) {
183 k = d0[*s]; /* ufast skip loop */
184 while (k) {
185 k = d0[*(s += k)];
186 k = d0[*(s += k)];
187 }
188 if (s >= e)
189 break;
190 if (s[ro] != rc) /* guard test */
191 goto mismatch1;
192 /* fwd match */
193 for (p = pat->pat, q = s - n1; p < ep;)
194 if (*q++ != *p++)
195 goto mismatch1;
196 return (s - n1);
197
198mismatch1: s += md2; /* md2 shift */
199 }
200
201 /* slow loop up to end */
202 e = base + n;
203 while (s < e) {
204 s += d0[*s]; /* step */
205 if (s >= e)
206 break;
207 if (s[ro] != rc) /* guard test */
208 goto mismatch2;
209 /* fwd match */
210 for (p = pat->pat, q = s - n1; p <= ep;)
211 if (*q++ != *p++)
212 goto mismatch2;
213 return (s - n1);
214
215mismatch2: s += md2; /* md2 shift */
216 }
217
218 return (NULL);
219}
diff --git a/src/lib/libc/string/bstring.3 b/src/lib/libc/string/bstring.3
new file mode 100644
index 0000000000..85283321c8
--- /dev/null
+++ b/src/lib/libc/string/bstring.3
@@ -0,0 +1,110 @@
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. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" $OpenBSD: bstring.3,v 1.4 2000/04/21 15:24:19 aaron Exp $
35.\"
36.Dd April 19, 1991
37.Dt BSTRING 3
38.Os
39.Sh NAME
40.Nm bcmp ,
41.Nm bcopy ,
42.Nm bzero ,
43.Nm memccpy ,
44.Nm memchr ,
45.Nm memcmp ,
46.Nm memcpy ,
47.Nm memmove ,
48.Nm memset
49.Nd byte string operations
50.Sh SYNOPSIS
51.Fd #include <string.h>
52.Ft int
53.Fn bcmp "const void *b1" "const void *b2" "size_t len"
54.Ft void
55.Fn bcopy "const void *src" "void *dst" "size_t len"
56.Ft void
57.Fn bzero "void *b" "size_t len"
58.Ft void *
59.Fn memchr "const void *b" "int c" "size_t len"
60.Ft int
61.Fn memcmp "const void *b1" "const void *b2" "size_t len"
62.Ft void *
63.Fn memccpy "void *dst" "const void *src" "int c" "size_t len"
64.Ft void *
65.Fn memcpy "void *dst" "const void *src" "size_t len"
66.Ft void *
67.Fn memmove "void *dst" "const void *src" "size_t len"
68.Ft void *
69.Fn memset "void *b" "int c" "size_t len"
70.Sh DESCRIPTION
71These functions operate on variable length strings of bytes.
72They do not check for terminating null bytes as the routines
73listed in
74.Xr string 3
75do.
76.Pp
77See the specific manual pages for more information.
78.Sh SEE ALSO
79.Xr bcmp 3 ,
80.Xr bcopy 3 ,
81.Xr bzero 3 ,
82.Xr memccpy 3 ,
83.Xr memchr 3 ,
84.Xr memcmp 3 ,
85.Xr memcpy 3 ,
86.Xr memmove 3 ,
87.Xr memset 3
88.Sh STANDARDS
89The functions
90.Fn memchr ,
91.Fn memcmp ,
92.Fn memcpy ,
93.Fn memmove ,
94and
95.Fn memset
96conform to
97.St -ansiC .
98.Sh HISTORY
99The
100.Fn bzero
101and
102.Fn memccpy
103functions appeared in
104.Bx 4.3 .
105The
106.Fn bcmp
107and
108.Fn bcopy
109functions appeared in
110.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..a735d5ef3f
--- /dev/null
+++ b/src/lib/libc/string/bzero.3
@@ -0,0 +1,65 @@
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. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" $OpenBSD: bzero.3,v 1.5 2000/04/21 15:24:19 aaron Exp $
35.\"
36.Dd April 19, 1991
37.Dt BZERO 3
38.Os
39.Sh NAME
40.Nm bzero
41.Nd write zeroes to a byte string
42.Sh SYNOPSIS
43.Fd #include <string.h>
44.Ft void
45.Fn bzero "void *b" "size_t len"
46.Sh DESCRIPTION
47The
48.Fn bzero
49function writes
50.Fa len
51zero bytes to the string
52.Fa b .
53If
54.Fa len
55is zero,
56.Fn bzero
57does nothing.
58.Sh SEE ALSO
59.Xr memset 3 ,
60.Xr swab 3
61.Sh HISTORY
62A
63.Fn bzero
64function appeared in
65.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..3e660a307f
--- /dev/null
+++ b/src/lib/libc/string/bzero.c
@@ -0,0 +1,56 @@
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: bzero.c,v 1.3 1996/08/19 08:34:00 tholo Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#ifndef _KERNEL
39#include <string.h>
40#else
41#include <lib/libkern/libkern.h>
42#endif
43
44/*
45 * bzero -- vax movc5 instruction
46 */
47void
48bzero(b, length)
49 void *b;
50 register size_t length;
51{
52 register char *p;
53
54 for (p = b; length--;)
55 *p++ = '\0';
56}
diff --git a/src/lib/libc/string/ffs.3 b/src/lib/libc/string/ffs.3
new file mode 100644
index 0000000000..dc7a20741e
--- /dev/null
+++ b/src/lib/libc/string/ffs.3
@@ -0,0 +1,60 @@
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. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" $OpenBSD: ffs.3,v 1.3 2000/04/21 15:24:19 aaron Exp $
35.\"
36.Dd April 19, 1991
37.Dt FFS 3
38.Os
39.Sh NAME
40.Nm ffs
41.Nd find first bit set in a bit string
42.Sh SYNOPSIS
43.Fd #include <string.h>
44.Ft int
45.Fn ffs "int value"
46.Sh DESCRIPTION
47The
48.Fn ffs
49function finds the first bit set in
50.Fa value
51and returns the index of that bit.
52Bits are numbered starting from 1, starting at the rightmost bit.
53A return value of 0 means that the argument was zero.
54.Sh SEE ALSO
55.Xr bitstring 3
56.Sh HISTORY
57The
58.Fn ffs
59function appeared in
60.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..887ce437d4
--- /dev/null
+++ b/src/lib/libc/string/ffs.c
@@ -0,0 +1,49 @@
1/* $OpenBSD: ffs.c,v 1.5 2000/07/02 03:10:38 mickey 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.5 2000/07/02 03:10:38 mickey 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(mask)
23 register int mask;
24{
25 register int bit;
26 register unsigned int r = mask;
27 static const signed char t[16] = {
28 -28, 1, 2, 1,
29 3, 1, 2, 1,
30 4, 1, 2, 1,
31 3, 1, 2, 1
32 };
33
34 bit = 0;
35 if (!(r & 0xffff)) {
36 bit += 16;
37 r >>= 16;
38 }
39 if (!(r & 0xff)) {
40 bit += 8;
41 r >>= 8;
42 }
43 if (!(r & 0xf)) {
44 bit += 4;
45 r >>= 4;
46 }
47
48 return (bit + t[ r & 0xf ]);
49}
diff --git a/src/lib/libc/string/index.c b/src/lib/libc/string/index.c
new file mode 100644
index 0000000000..8e4b146493
--- /dev/null
+++ b/src/lib/libc/string/index.c
@@ -0,0 +1,54 @@
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: index.c,v 1.3 2002/07/24 04:16:01 millert Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#include <string.h>
39
40char *
41#ifdef STRCHR
42strchr(const char *p, int ch)
43#else
44index(const char *p, int ch)
45#endif
46{
47 for (;; ++p) {
48 if (*p == ch)
49 return((char *)p);
50 if (!*p)
51 return((char *)NULL);
52 }
53 /* NOTREACHED */
54}
diff --git a/src/lib/libc/string/memccpy.3 b/src/lib/libc/string/memccpy.3
new file mode 100644
index 0000000000..feedeff3a6
--- /dev/null
+++ b/src/lib/libc/string/memccpy.3
@@ -0,0 +1,76 @@
1.\" $OpenBSD: memccpy.3,v 1.6 2000/04/21 15:24:19 aaron 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. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" @(#)memccpy.3 8.1 (Berkeley) 6/9/93
35.\"
36.Dd June 9, 1993
37.Dt MEMCCPY 3
38.Os
39.Sh NAME
40.Nm memccpy
41.Nd copy string until character found
42.Sh SYNOPSIS
43.Fd #include <string.h>
44.Ft void *
45.Fn memccpy "void *dst" "const void *src" "int c" "size_t len"
46.Sh DESCRIPTION
47The
48.Fn memccpy
49function copies bytes from string
50.Fa src
51to string
52.Fa dst .
53If the character
54.Fa c
55(as converted to an
56.Li unsigned char )
57occurs in the string
58.Fa src ,
59the copy stops and a pointer to the byte after the copy of
60.Fa c
61in the string
62.Fa dst
63is returned.
64Otherwise,
65.Fa len
66bytes are copied, and a null pointer is returned.
67.Sh SEE ALSO
68.Xr bcopy 3 ,
69.Xr memcpy 3 ,
70.Xr memmove 3 ,
71.Xr strcpy 3
72.Sh HISTORY
73The
74.Fn memccpy
75function first appeared in
76.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..020e6e5679
--- /dev/null
+++ b/src/lib/libc/string/memccpy.c
@@ -0,0 +1,64 @@
1/* $OpenBSD: memccpy.c,v 1.3 1997/08/20 04:09:39 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#if defined(LIBC_SCCS) && !defined(lint)
37#if 0
38static char sccsid[] = "@(#)memccpy.c 8.1 (Berkeley) 6/4/93";
39#else
40static char *rcsid = "$OpenBSD: memccpy.c,v 1.3 1997/08/20 04:09:39 millert Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */
43
44#include <string.h>
45
46void *
47memccpy(t, f, c, n)
48 void *t;
49 const void *f;
50 int c;
51 register size_t n;
52{
53
54 if (n) {
55 register unsigned char *tp = t;
56 register const unsigned char *fp = f;
57 register unsigned char uc = c;
58 do {
59 if ((*tp++ = *fp++) == uc)
60 return (tp);
61 } while (--n != 0);
62 }
63 return (0);
64}
diff --git a/src/lib/libc/string/memchr.3 b/src/lib/libc/string/memchr.3
new file mode 100644
index 0000000000..632c440b5b
--- /dev/null
+++ b/src/lib/libc/string/memchr.3
@@ -0,0 +1,78 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: memchr.3,v 1.5 2000/04/21 15:24:19 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt MEMCHR 3
40.Os
41.Sh NAME
42.Nm memchr
43.Nd locate byte in byte string
44.Sh SYNOPSIS
45.Fd #include <string.h>
46.Ft void *
47.Fn memchr "const void *b" "int c" "size_t len"
48.Sh DESCRIPTION
49The
50.Fn memchr
51function locates the first occurrence of
52.Fa c
53(converted to an
54.Li unsigned char )
55in string
56.Fa b .
57.Sh RETURN VALUES
58The
59.Fn memchr
60function returns a pointer to the byte located, or
61.Dv NULL
62if no such byte exists within
63.Fa len
64bytes.
65.Sh SEE ALSO
66.Xr strchr 3 ,
67.Xr strcspn 3 ,
68.Xr strpbrk 3 ,
69.Xr strrchr 3 ,
70.Xr strsep 3 ,
71.Xr strspn 3 ,
72.Xr strstr 3 ,
73.Xr strtok 3
74.Sh STANDARDS
75The
76.Fn memchr
77function conforms to
78.St -ansiC .
diff --git a/src/lib/libc/string/memchr.c b/src/lib/libc/string/memchr.c
new file mode 100644
index 0000000000..d605d4734f
--- /dev/null
+++ b/src/lib/libc/string/memchr.c
@@ -0,0 +1,58 @@
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char *rcsid = "$OpenBSD: memchr.c,v 1.4 2001/09/07 08:09:31 espie Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#include <string.h>
42
43void *
44memchr(s, c, n)
45 const void *s;
46 int c;
47 size_t n;
48{
49 if (n != 0) {
50 const unsigned char *p = s;
51
52 do {
53 if (*p++ == (unsigned char)c)
54 return ((void *)(p - 1));
55 } while (--n != 0);
56 }
57 return (NULL);
58}
diff --git a/src/lib/libc/string/memcmp.3 b/src/lib/libc/string/memcmp.3
new file mode 100644
index 0000000000..db4fef364b
--- /dev/null
+++ b/src/lib/libc/string/memcmp.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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: memcmp.3,v 1.4 2000/04/21 15:24:19 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt MEMCMP 3
40.Os
41.Sh NAME
42.Nm memcmp
43.Nd compare byte string
44.Sh SYNOPSIS
45.Fd #include <string.h>
46.Ft int
47.Fn memcmp "const void *b1" "const void *b2" "size_t len"
48.Sh DESCRIPTION
49The
50.Fn memcmp
51function compares byte string
52.Fa b1
53against byte string
54.Fa b2 .
55Both strings are assumed to be
56.Fa len
57bytes long.
58.Sh RETURN VALUES
59The
60.Fn memcmp
61function returns zero if the two strings are identical,
62otherwise returns the difference between the first two differing bytes
63(treated as
64.Li unsigned char
65values, so that
66.Sq Li \e200
67is greater than
68.Sq Li \&\e0 ,
69for example).
70Zero-length strings are always identical.
71.Sh SEE ALSO
72.Xr bcmp 3 ,
73.Xr strcasecmp 3 ,
74.Xr strcmp 3 ,
75.Xr strcoll 3 ,
76.Xr strxfrm 3
77.Sh STANDARDS
78The
79.Fn memcmp
80function conforms to
81.St -ansiC .
diff --git a/src/lib/libc/string/memcmp.c b/src/lib/libc/string/memcmp.c
new file mode 100644
index 0000000000..5ce33e2998
--- /dev/null
+++ b/src/lib/libc/string/memcmp.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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char *rcsid = "$OpenBSD: memcmp.c,v 1.2 1996/08/19 08:34:05 tholo Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#include <string.h>
42
43/*
44 * Compare memory regions.
45 */
46int
47memcmp(s1, s2, n)
48 const void *s1, *s2;
49 size_t n;
50{
51 if (n != 0) {
52 register const unsigned char *p1 = s1, *p2 = s2;
53
54 do {
55 if (*p1++ != *p2++)
56 return (*--p1 - *--p2);
57 } while (--n != 0);
58 }
59 return (0);
60}
diff --git a/src/lib/libc/string/memcpy.3 b/src/lib/libc/string/memcpy.3
new file mode 100644
index 0000000000..030eadff20
--- /dev/null
+++ b/src/lib/libc/string/memcpy.3
@@ -0,0 +1,80 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: memcpy.3,v 1.3 2000/04/21 15:24:19 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt MEMCPY 3
40.Os
41.Sh NAME
42.Nm memcpy
43.Nd copy byte string
44.Sh SYNOPSIS
45.Fd #include <string.h>
46.Ft void *
47.Fn memcpy "void *dst" "const void *src" "size_t len"
48.Sh DESCRIPTION
49The
50.Fn memcpy
51function copies
52.Fa len
53bytes from string
54.Fa src
55to string
56.Fa dst .
57.Sh RETURN VALUES
58The
59.Fn memcpy
60function returns the original value of
61.Fa dst .
62.Sh SEE ALSO
63.Xr bcopy 3 ,
64.Xr memccpy 3 ,
65.Xr memmove 3 ,
66.Xr strcpy 3
67.Sh STANDARDS
68The
69.Fn memcpy
70function conforms to
71.St -ansiC .
72.Sh BUGS
73In this implementation
74.Fn memcpy
75is implemented using
76.Xr bcopy 3 ,
77and therefore the strings may overlap.
78On other systems, copying overlapping strings may produce surprises.
79A simpler solution is to not use
80.Fn memcpy .
diff --git a/src/lib/libc/string/memmove.3 b/src/lib/libc/string/memmove.3
new file mode 100644
index 0000000000..026dab0f9f
--- /dev/null
+++ b/src/lib/libc/string/memmove.3
@@ -0,0 +1,73 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: memmove.3,v 1.3 2000/04/21 15:24:19 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt MEMMOVE 3
40.Os
41.Sh NAME
42.Nm memmove
43.Nd copy byte string
44.Sh SYNOPSIS
45.Fd #include <string.h>
46.Ft void *
47.Fn memmove "void *dst" "const void *src" "size_t len"
48.Sh DESCRIPTION
49The
50.Fn memmove
51function copies
52.Fa len
53bytes from string
54.Fa src
55to string
56.Fa dst .
57The two strings may overlap;
58the copy is always done in a non-destructive manner.
59.Sh RETURN VALUES
60The
61.Fn memmove
62function returns the original value of
63.Fa dst .
64.Sh SEE ALSO
65.Xr bcopy 3 ,
66.Xr memccpy 3 ,
67.Xr memcpy 3 ,
68.Xr strcpy 3
69.Sh STANDARDS
70The
71.Fn memmove
72function conforms to
73.St -ansiC .
diff --git a/src/lib/libc/string/memset.3 b/src/lib/libc/string/memset.3
new file mode 100644
index 0000000000..9da14983c7
--- /dev/null
+++ b/src/lib/libc/string/memset.3
@@ -0,0 +1,71 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: memset.3,v 1.4 2000/04/21 15:24:19 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt MEMSET 3
40.Os
41.Sh NAME
42.Nm memset
43.Nd write a byte to byte string
44.Sh SYNOPSIS
45.Fd #include <string.h>
46.Ft void *
47.Fn memset "void *b" "int c" "size_t len"
48.Sh DESCRIPTION
49The
50.Fn memset
51function writes
52.Fa len
53bytes of value
54.Fa c
55(converted to an
56.Li unsigned char )
57to the string
58.Fa b .
59.Sh RETURN VALUES
60The
61.Fn memset
62function returns the original value of
63.Fa b .
64.Sh SEE ALSO
65.Xr bzero 3 ,
66.Xr swab 3
67.Sh STANDARDS
68The
69.Fn memset
70function conforms to
71.St -ansiC .
diff --git a/src/lib/libc/string/memset.c b/src/lib/libc/string/memset.c
new file mode 100644
index 0000000000..c3373a21a9
--- /dev/null
+++ b/src/lib/libc/string/memset.c
@@ -0,0 +1,58 @@
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char *rcsid = "$OpenBSD: memset.c,v 1.2 1996/08/19 08:34:07 tholo Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#include <string.h>
42
43void *
44memset(dst, c, n)
45 void *dst;
46 register int c;
47 register size_t n;
48{
49
50 if (n != 0) {
51 register char *d = dst;
52
53 do
54 *d++ = c;
55 while (--n != 0);
56 }
57 return (dst);
58}
diff --git a/src/lib/libc/string/rindex.c b/src/lib/libc/string/rindex.c
new file mode 100644
index 0000000000..7c7c94ae81
--- /dev/null
+++ b/src/lib/libc/string/rindex.c
@@ -0,0 +1,56 @@
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: rindex.c,v 1.3 2002/07/24 04:16:01 millert Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#include <string.h>
39
40char *
41#ifdef STRRCHR
42strrchr(const char *p, int ch)
43#else
44rindex(const char *p, int ch)
45#endif
46{
47 register char *save;
48
49 for (save = NULL;; ++p) {
50 if (*p == ch)
51 save = (char *)p;
52 if (!*p)
53 return(save);
54 }
55 /* NOTREACHED */
56}
diff --git a/src/lib/libc/string/strcasecmp.3 b/src/lib/libc/string/strcasecmp.3
new file mode 100644
index 0000000000..3cfc76e36f
--- /dev/null
+++ b/src/lib/libc/string/strcasecmp.3
@@ -0,0 +1,88 @@
1.\" $OpenBSD: strcasecmp.3,v 1.6 2000/04/21 15:24:19 aaron 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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" @(#)strcasecmp.3 8.1 (Berkeley) 6/9/93
37.\"
38.Dd June 9, 1993
39.Dt STRCASECMP 3
40.Os
41.Sh NAME
42.Nm strcasecmp ,
43.Nm strncasecmp
44.Nd compare strings, ignoring case
45.Sh SYNOPSIS
46.Fd #include <string.h>
47.Ft int
48.Fn strcasecmp "const char *s1" "const char *s2"
49.Ft int
50.Fn strncasecmp "const char *s1" "const char *s2" "size_t len"
51.Sh DESCRIPTION
52The
53.Fn strcasecmp
54and
55.Fn strncasecmp
56functions compare the null-terminated strings
57.Fa s1
58and
59.Fa s2
60and return an integer greater than, equal to, or less than 0,
61according as
62.Fa s1
63is lexicographically greater than, equal to, or less than
64.Fa s2
65after translation of each corresponding character to lower-case.
66The strings themselves are not modified.
67The comparison is done using unsigned characters, so that
68.Sq Li \e200
69is greater than
70.Ql \e0 .
71.Pp
72.Fn strncasecmp
73compares at most
74.Fa len
75characters.
76.Sh SEE ALSO
77.Xr bcmp 3 ,
78.Xr memcmp 3 ,
79.Xr strcmp 3 ,
80.Xr strcoll 3 ,
81.Xr strxfrm 3
82.Sh HISTORY
83The
84.Fn strcasecmp
85and
86.Fn strncasecmp
87functions first appeared in
88.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..1f487524aa
--- /dev/null
+++ b/src/lib/libc/string/strcasecmp.c
@@ -0,0 +1,120 @@
1/* $OpenBSD: strcasecmp.c,v 1.3 1997/08/20 04:13:57 millert 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <string.h>
37
38#if defined(LIBC_SCCS) && !defined(lint)
39#if 0
40static char sccsid[] = "@(#)strcasecmp.c 8.1 (Berkeley) 6/4/93";
41#else
42static char *rcsid = "$OpenBSD: strcasecmp.c,v 1.3 1997/08/20 04:13:57 millert Exp $";
43#endif
44#endif /* LIBC_SCCS and not lint */
45
46typedef unsigned char u_char;
47
48/*
49 * This array is designed for mapping upper and lower case letter
50 * together for a case independent comparison. The mappings are
51 * based upon ascii character sequences.
52 */
53static const u_char charmap[] = {
54 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
55 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
56 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
57 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
58 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
59 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
60 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
61 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
62 '\100', '\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', '\133', '\134', '\135', '\136', '\137',
66 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
67 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
68 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
69 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
70 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
71 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
72 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
73 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
74 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
75 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
76 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
77 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
78 '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
79 '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
80 '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
81 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
82 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
83 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
84 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
85 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
86};
87
88int
89strcasecmp(s1, s2)
90 const char *s1, *s2;
91{
92 register const u_char *cm = charmap,
93 *us1 = (const u_char *)s1,
94 *us2 = (const u_char *)s2;
95
96 while (cm[*us1] == cm[*us2++])
97 if (*us1++ == '\0')
98 return (0);
99 return (cm[*us1] - cm[*--us2]);
100}
101
102int
103strncasecmp(s1, s2, n)
104 const char *s1, *s2;
105 register size_t n;
106{
107 if (n != 0) {
108 register const u_char *cm = charmap,
109 *us1 = (const u_char *)s1,
110 *us2 = (const u_char *)s2;
111
112 do {
113 if (cm[*us1] != cm[*us2++])
114 return (cm[*us1] - cm[*--us2]);
115 if (*us1++ == '\0')
116 break;
117 } while (--n != 0);
118 }
119 return (0);
120}
diff --git a/src/lib/libc/string/strcat.3 b/src/lib/libc/string/strcat.3
new file mode 100644
index 0000000000..e9e5163bd3
--- /dev/null
+++ b/src/lib/libc/string/strcat.3
@@ -0,0 +1,135 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strcat.3,v 1.8 2000/04/21 15:24:19 aaron Exp $
37.\"
38.Dd July 8, 1997
39.Dt STRCAT 3
40.Os
41.Sh NAME
42.Nm strcat ,
43.Nm strncat
44.Nd concatenate strings
45.Sh SYNOPSIS
46.Fd #include <string.h>
47.Ft char *
48.Fn strcat "char *s" "const char *append"
49.Ft char *
50.Fn strncat "char *s" "const char *append" "size_t count"
51.Sh DESCRIPTION
52The
53.Fn strcat
54and
55.Fn strncat
56functions append a copy of the null-terminated string
57.Fa append
58to the end of the null-terminated string
59.Fa s ,
60then add a terminating
61.Ql \e0 .
62The string
63.Fa s
64must have sufficient space to hold the result.
65.Pp
66The
67.Fn strncat
68function appends not more than
69.Fa count
70characters where space for the terminating
71.Ql \e0
72should not be included in
73.Fa count .
74.Sh RETURN VALUES
75The
76.Fn strcat
77and
78.Fn strncat
79functions return the pointer
80.Fa s .
81.Sh EXAMPLES
82The following appends
83.Dq Li abc
84to
85.Dq Li chararray :
86.Bd -literal -offset indent
87char *letters = "abcdefghi";
88
89(void)strncat(chararray, letters, 3);
90.Ed
91.Pp
92The following example shows how to use
93.Fn strncat
94safely in conjunction with
95.Xr strncpy 3 .
96.Bd -literal -offset indent
97char buf[BUFSIZ];
98char *input, *suffix;
99
100(void)strncpy(buf, input, sizeof(buf) - 1);
101buf[sizeof(buf) - 1] = '\e0';
102(void)strncat(buf, suffix, sizeof(buf) - 1 - strlen(buf));
103.Ed
104.Pp
105The above will copy as many characters from
106.Dq Li input
107to
108.Dq Li buf
109as will fit.
110It then appends as many characters from suffix as will fit (or none
111if there is no space).
112For operations like this, the
113.Xr strlcpy 3
114and
115.Xr strlcat 3
116functions are a better choice, as shown below.
117.Bd -literal -offset indent
118(void)strlcpy(buf, input, sizeof(buf));
119(void)strlcat(buf, suffix, sizeof(buf));
120.Ed
121.Sh SEE ALSO
122.Xr bcopy 3 ,
123.Xr memccpy 3 ,
124.Xr memcpy 3 ,
125.Xr memmove 3 ,
126.Xr strcpy 3 ,
127.Xr strlcat 3 ,
128.Xr strlcpy 3
129.Sh STANDARDS
130The
131.Fn strcat
132and
133.Fn strncat
134functions conform to
135.St -ansiC .
diff --git a/src/lib/libc/string/strcat.c b/src/lib/libc/string/strcat.c
new file mode 100644
index 0000000000..374a2b7464
--- /dev/null
+++ b/src/lib/libc/string/strcat.c
@@ -0,0 +1,54 @@
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: strcat.c,v 1.4 1996/08/19 08:34:10 tholo Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#ifndef _KERNEL
39#include <string.h>
40#else
41#include <lib/libkern/libkern.h>
42#endif
43
44char *
45strcat(s, append)
46 register char *s;
47 register const char *append;
48{
49 char *save = s;
50
51 for (; *s; ++s);
52 while ((*s++ = *append++) != '\0');
53 return(save);
54}
diff --git a/src/lib/libc/string/strchr.3 b/src/lib/libc/string/strchr.3
new file mode 100644
index 0000000000..1333a305d6
--- /dev/null
+++ b/src/lib/libc/string/strchr.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 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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strchr.3,v 1.7 2000/04/21 15:24:19 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRCHR 3
40.Os
41.Sh NAME
42.Nm strchr ,
43.Nm index
44.Nd locate first occurrence of a character in a string
45.Sh SYNOPSIS
46.Fd #include <string.h>
47.Ft char *
48.Fn strchr "const char *s" "int c"
49.Ft char *
50.Fn index "const char *s" "int c"
51.Sh DESCRIPTION
52The
53.Fn strchr
54function locates the first occurrence of the character
55.Fa c
56in the string
57.Fa s .
58The terminating NUL character is considered part of the string.
59If
60.Fa c
61is
62.Ql \e0 ,
63.Fn strchr
64locates the terminating
65.Ql \e0 .
66.Pp
67The
68.Fn index
69function is an old synonym for
70.Fn strchr .
71.Sh RETURN VALUES
72The
73.Fn strchr
74function returns a pointer to the located character or
75.Dv NULL
76if the character does not appear in the string.
77.Sh EXAMPLES
78After the following call to
79.Fn strchr ,
80.Va p
81will point to the string
82.Qq oobar :
83.Bd -literal -offset indent
84char *p;
85char *s = "foobar";
86
87p = strchr(s, 'o');
88.Ed
89.Sh SEE ALSO
90.Xr memchr 3 ,
91.Xr strcspn 3 ,
92.Xr strpbrk 3 ,
93.Xr strrchr 3 ,
94.Xr strsep 3 ,
95.Xr strspn 3 ,
96.Xr strstr 3 ,
97.Xr strtok 3
98.Sh STANDARDS
99The
100.Fn strchr
101function conforms to
102.St -ansiC .
103.Pp
104The
105.Fn index
106function 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..c1173a423a
--- /dev/null
+++ b/src/lib/libc/string/strcmp.3
@@ -0,0 +1,91 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strcmp.3,v 1.5 2000/04/21 15:24:19 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRCMP 3
40.Os
41.Sh NAME
42.Nm strcmp ,
43.Nm strncmp
44.Nd compare strings
45.Sh SYNOPSIS
46.Fd #include <string.h>
47.Ft int
48.Fn strcmp "const char *s1" "const char *s2"
49.Ft int
50.Fn strncmp "const char *s1" "const char *s2" "size_t len"
51.Sh DESCRIPTION
52The
53.Fn strcmp
54and
55.Fn strncmp
56functions lexicographically compare the null-terminated strings
57.Fa s1
58and
59.Fa s2 .
60.Sh RETURN VALUES
61The
62.Fn strcmp
63and
64.Fn strncmp
65return an integer greater than, equal to, or less than 0, according
66as the string
67.Fa s1
68is greater than, equal to, or less than the string
69.Fa s2 .
70The comparison is done using unsigned characters, so that
71.Ql \e200
72is greater than
73.Ql \e0 .
74.Pp
75.Fn strncmp
76compares at most
77.Fa len
78characters.
79.Sh SEE ALSO
80.Xr bcmp 3 ,
81.Xr memcmp 3 ,
82.Xr strcasecmp 3 ,
83.Xr strcoll 3 ,
84.Xr strxfrm 3
85.Sh STANDARDS
86The
87.Fn strcmp
88and
89.Fn strncmp
90functions conform to
91.St -ansiC .
diff --git a/src/lib/libc/string/strcmp.c b/src/lib/libc/string/strcmp.c
new file mode 100644
index 0000000000..9a5b208323
--- /dev/null
+++ b/src/lib/libc/string/strcmp.c
@@ -0,0 +1,58 @@
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char *rcsid = "$OpenBSD: strcmp.c,v 1.3 1996/08/19 08:34:12 tholo Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#ifndef _KERNEL
42#include <string.h>
43#else
44#include <lib/libkern/libkern.h>
45#endif
46
47/*
48 * Compare strings.
49 */
50int
51strcmp(s1, s2)
52 register const char *s1, *s2;
53{
54 while (*s1 == *s2++)
55 if (*s1++ == 0)
56 return (0);
57 return (*(unsigned char *)s1 - *(unsigned char *)--s2);
58}
diff --git a/src/lib/libc/string/strcoll.3 b/src/lib/libc/string/strcoll.3
new file mode 100644
index 0000000000..5d3e44ee96
--- /dev/null
+++ b/src/lib/libc/string/strcoll.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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strcoll.3,v 1.3 2000/04/21 15:24:19 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRCOLL 3
40.Os
41.Sh NAME
42.Nm strcoll
43.Nd compare strings according to current collation
44.Sh SYNOPSIS
45.Fd #include <string.h>
46.Ft int
47.Fn strcoll "const char *s1" "const char *s2"
48.Sh DESCRIPTION
49The
50.Fn strcoll
51function lexicographically compares the null-terminated strings
52.Fa s1
53and
54.Fa s2
55according to the current locale collation
56and returns an integer greater than, equal to, or less than 0,
57according as
58.Fa s1
59is greater than, equal to, or less than
60.Fa s2 .
61.Sh SEE ALSO
62.Xr bcmp 3 ,
63.Xr memcmp 3 ,
64.Xr setlocale 3 ,
65.Xr strcasecmp 3 ,
66.Xr strcmp 3 ,
67.Xr strxfrm 3
68.Sh STANDARDS
69The
70.Fn strcoll
71function conforms to
72.St -ansiC .
diff --git a/src/lib/libc/string/strcoll.c b/src/lib/libc/string/strcoll.c
new file mode 100644
index 0000000000..dca0b10d25
--- /dev/null
+++ b/src/lib/libc/string/strcoll.c
@@ -0,0 +1,52 @@
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char *rcsid = "$OpenBSD: strcoll.c,v 1.2 1996/08/19 08:34:13 tholo Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#include <string.h>
42
43/*
44 * Compare strings according to LC_COLLATE category of current locale.
45 */
46int
47strcoll(s1, s2)
48 const char *s1, *s2;
49{
50 /* LC_COLLATE is unimplemented, hence always "C" */
51 return (strcmp(s1, s2));
52}
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3
new file mode 100644
index 0000000000..d3c095916b
--- /dev/null
+++ b/src/lib/libc/string/strcpy.3
@@ -0,0 +1,154 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strcpy.3,v 1.11 2002/05/13 17:04:43 millert Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRCPY 3
40.Os
41.Sh NAME
42.Nm strcpy ,
43.Nm strncpy
44.Nd copy strings
45.Sh SYNOPSIS
46.Fd #include <string.h>
47.Ft char *
48.Fn strcpy "char *dst" "const char *src"
49.Ft char *
50.Fn strncpy "char *dst" "const char *src" "size_t len"
51.Sh DESCRIPTION
52The
53.Fn strcpy
54and
55.Fn strncpy
56functions copy the string
57.Fa src
58to
59.Fa dst
60(including the terminating
61.Ql \e0
62character).
63.Pp
64.Fn strncpy
65copies not more than
66.Fa len
67characters into
68.Fa dst ,
69appending
70.Ql \e0
71characters if
72.Fa src
73is less than
74.Fa len
75characters long, and
76.Em not
77terminating
78.Fa dst
79if the length of
80.Fa src
81is greater than or equal to
82.Fa len .
83.Sh RETURN VALUES
84The
85.Fn strcpy
86and
87.Fn strncpy
88functions return
89.Fa dst .
90.Sh EXAMPLES
91The following sets
92.Va chararray
93to
94.Dq abc\e0\e0\e0 :
95.Bd -literal -offset indent
96(void)strncpy(chararray, "abc", 6);
97.Ed
98.Pp
99The following sets
100.Va chararray
101to
102.Dq abcdef
103and does
104.Em not
105null terminate
106.Va chararray
107because the source string is >= the length parameter.
108.Fn strncpy
109.Em only
110null terminates the destination string when the length of the source
111string is less than the length parameter.
112.Bd -literal -offset indent
113(void)strncpy(chararray, "abcdefgh", 6);
114.Ed
115.Pp
116The following copies as many characters from
117.Va input
118to
119.Va buf
120as will fit and null terminates the result.
121Because
122.Fn strncpy
123does
124.Em not
125guarantee to null terminate the string itself, we must do this by hand.
126.Bd -literal -offset indent
127char buf[BUFSIZ];
128
129(void)strncpy(buf, input, sizeof(buf) - 1);
130buf[sizeof(buf) - 1] = '\e0';
131.Ed
132.Pp
133Note that
134.Xr strlcpy 3
135is a better choice for this kind of operation.
136The equivalent using
137.Xr strlcpy 3
138is simply:
139.Bd -literal -offset indent
140(void)strlcpy(buf, input, sizeof(buf));
141.Ed
142.Sh SEE ALSO
143.Xr bcopy 3 ,
144.Xr memccpy 3 ,
145.Xr memcpy 3 ,
146.Xr memmove 3 ,
147.Xr strlcpy 3
148.Sh STANDARDS
149The
150.Fn strcpy
151and
152.Fn strncpy
153functions conform to
154.St -ansiC .
diff --git a/src/lib/libc/string/strcpy.c b/src/lib/libc/string/strcpy.c
new file mode 100644
index 0000000000..76b063fc10
--- /dev/null
+++ b/src/lib/libc/string/strcpy.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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: strcpy.c,v 1.4 1996/08/19 08:34:14 tholo Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#ifndef _KERNEL
39#include <string.h>
40#else
41#include <lib/libkern/libkern.h>
42#endif
43
44char *
45strcpy(to, from)
46 register char *to;
47 register const char *from;
48{
49 char *save = to;
50
51 for (; (*to = *from) != '\0'; ++from, ++to);
52 return(save);
53}
diff --git a/src/lib/libc/string/strcspn.3 b/src/lib/libc/string/strcspn.3
new file mode 100644
index 0000000000..37e8f163f1
--- /dev/null
+++ b/src/lib/libc/string/strcspn.3
@@ -0,0 +1,92 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strcspn.3,v 1.5 2000/04/21 15:24:20 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRCSPN 3
40.Os
41.Sh NAME
42.Nm strcspn
43.Nd span the complement of a string
44.Sh SYNOPSIS
45.Fd #include <string.h>
46.Ft size_t
47.Fn strcspn "const char *s" "const char *charset"
48.Sh DESCRIPTION
49The
50.Fn strcspn
51function spans the initial part of the null-terminated string
52.Fa s
53as long as the characters from
54.Fa s
55do not occur in string
56.Fa charset
57(it spans the
58.Em complement
59of
60.Fa charset ) .
61.Sh RETURN VALUES
62The
63.Fn strcspn
64function returns the number of characters spanned.
65.Sh EXAMPLES
66The following call to
67.Fn strcspn
68will return 3, since the first three characters of string
69.Fa s
70do not occur in string
71.Fa charset :
72.Bd -literal -offset indent
73char *s = "foobar";
74char *charset = "bar";
75size_t span;
76
77span = strcspn(s, charset);
78.Ed
79.Sh SEE ALSO
80.Xr memchr 3 ,
81.Xr strchr 3 ,
82.Xr strpbrk 3 ,
83.Xr strrchr 3 ,
84.Xr strsep 3 ,
85.Xr strspn 3 ,
86.Xr strstr 3 ,
87.Xr strtok 3
88.Sh STANDARDS
89The
90.Fn strcspn
91function conforms to
92.St -ansiC .
diff --git a/src/lib/libc/string/strcspn.c b/src/lib/libc/string/strcspn.c
new file mode 100644
index 0000000000..f7261564a7
--- /dev/null
+++ b/src/lib/libc/string/strcspn.c
@@ -0,0 +1,67 @@
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char *rcsid = "$OpenBSD: strcspn.c,v 1.2 1996/08/19 08:34:15 tholo Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#include <string.h>
42
43/*
44 * Span the complement of string s2.
45 */
46size_t
47strcspn(s1, s2)
48 const char *s1;
49 register const char *s2;
50{
51 register const char *p, *spanp;
52 register char c, sc;
53
54 /*
55 * Stop as soon as we find any character from s2. Note that there
56 * must be a NUL in s2; it suffices to stop when we find that, too.
57 */
58 for (p = s1;;) {
59 c = *p++;
60 spanp = s2;
61 do {
62 if ((sc = *spanp++) == c)
63 return (p - 1 - s1);
64 } while (sc != 0);
65 }
66 /* NOTREACHED */
67}
diff --git a/src/lib/libc/string/strdup.3 b/src/lib/libc/string/strdup.3
new file mode 100644
index 0000000000..9c154c7d70
--- /dev/null
+++ b/src/lib/libc/string/strdup.3
@@ -0,0 +1,87 @@
1.\" $OpenBSD: strdup.3,v 1.10 2000/04/21 15:32:15 aaron 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. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" @(#)strdup.3 8.1 (Berkeley) 6/9/93
35.\"
36.Dd June 9, 1993
37.Dt STRDUP 3
38.Os
39.Sh NAME
40.Nm strdup
41.Nd save a copy of a string
42.Sh SYNOPSIS
43.Fd #include <string.h>
44.Ft char *
45.Fn strdup "const char *s"
46.Sh DESCRIPTION
47The
48.Fn strdup
49function allocates sufficient memory for a copy of the string
50.Fa s ,
51does the copy, and returns a pointer to it.
52The pointer may subsequently be used as an argument to the function
53.Xr free 3 .
54.Pp
55If insufficient memory is available,
56.Dv NULL
57is returned.
58.Sh EXAMPLES
59The following will point
60.Va p
61to an allocated area of memory containing the null-terminated string
62.Qq foobar :
63.Bd -literal -offset indent
64char *p;
65
66if ((p = strdup("foobar")) == NULL) {
67 fprintf(stderr, "Out of memory.\en");
68 exit(1);
69}
70.Ed
71.Sh ERRORS
72The
73.Fn strdup
74function may fail and set the external variable
75.Va errno
76for any of the errors specified for the library function
77.Xr malloc 3 .
78.Sh SEE ALSO
79.Xr free 3 ,
80.Xr malloc 3 ,
81.Xr strcpy 3 ,
82.Xr strlen 3
83.Sh HISTORY
84The
85.Fn strdup
86function first appeared in
87.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..be7f7ad094
--- /dev/null
+++ b/src/lib/libc/string/strdup.c
@@ -0,0 +1,62 @@
1/* $OpenBSD: strdup.c,v 1.3 1997/08/20 04:18:52 millert 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#if defined(LIBC_SCCS) && !defined(lint)
37#if 0
38static char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93";
39#else
40static char *rcsid = "$OpenBSD: strdup.c,v 1.3 1997/08/20 04:18:52 millert Exp $";
41#endif
42#endif /* LIBC_SCCS and not lint */
43
44#include <sys/types.h>
45
46#include <stddef.h>
47#include <stdlib.h>
48#include <string.h>
49
50char *
51strdup(str)
52 const char *str;
53{
54 size_t siz;
55 char *copy;
56
57 siz = strlen(str) + 1;
58 if ((copy = malloc(siz)) == NULL)
59 return(NULL);
60 (void)memcpy(copy, str, siz);
61 return(copy);
62}
diff --git a/src/lib/libc/string/strerror.3 b/src/lib/libc/string/strerror.3
new file mode 100644
index 0000000000..11bacd313e
--- /dev/null
+++ b/src/lib/libc/string/strerror.3
@@ -0,0 +1,69 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strerror.3,v 1.4 2000/10/23 19:14:41 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRERROR 3
40.Os
41.Sh NAME
42.Nm strerror
43.Nd get error message string
44.Sh SYNOPSIS
45.Fd #include <string.h>
46.Ft char *
47.Fn strerror "int errnum"
48.Sh DESCRIPTION
49The
50.Fn strerror
51function returns a pointer to the language-dependent error message
52string affiliated with the specified error number
53.Fa errnum .
54The returned string contains a maximum of
55.Dv NL_TEXTMAX
56characters, including the trailing NUL.
57.Pp
58The array pointed to is not to be modified by the program, but may be
59overwritten by subsequent calls to
60.Fn strerror .
61.Sh SEE ALSO
62.Xr intro 2 ,
63.Xr perror 3 ,
64.Xr setlocale 3
65.Sh STANDARDS
66The
67.Fn strerror
68function conforms to
69.St -ansiC .
diff --git a/src/lib/libc/string/strerror.c b/src/lib/libc/string/strerror.c
new file mode 100644
index 0000000000..b884a67df3
--- /dev/null
+++ b/src/lib/libc/string/strerror.c
@@ -0,0 +1,55 @@
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: strerror.c,v 1.3 2002/02/16 21:27:24 millert Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#include <string.h>
39#include <limits.h>
40
41/*
42 * Since perror() is not allowed to change the contents of strerror()'s
43 * static buffer, both functions supply their own buffers to the
44 * internal function __strerror().
45 */
46
47extern char *__strerror(int, char *);
48
49char *
50strerror(num)
51 int num;
52{
53 static char buf[NL_TEXTMAX];
54 return __strerror(num, buf);
55}
diff --git a/src/lib/libc/string/string.3 b/src/lib/libc/string/string.3
new file mode 100644
index 0000000000..7a67b96b53
--- /dev/null
+++ b/src/lib/libc/string/string.3
@@ -0,0 +1,161 @@
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. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" $OpenBSD: string.3,v 1.8 2000/04/21 15:24:20 aaron Exp $
35.\"
36.Dd April 19, 1991
37.Dt STRING 3
38.Os
39.Sh NAME
40.Nm strcat ,
41.Nm strlcat ,
42.Nm strncat ,
43.Nm strchr ,
44.Nm strrchr ,
45.Nm strcmp ,
46.Nm strncmp ,
47.Nm strcasecmp ,
48.Nm strncasecmp ,
49.Nm strcpy ,
50.Nm strlcpy ,
51.Nm strncpy ,
52.Nm strerror ,
53.Nm strlen ,
54.Nm strpbrk ,
55.Nm strsep ,
56.Nm strspn ,
57.Nm strcspn ,
58.Nm strstr ,
59.Nm strtok ,
60.Nm index ,
61.Nm rindex
62.Nd string specific functions
63.Sh SYNOPSIS
64.Fd #include <string.h>
65.Ft char *
66.Fn strcat "char *s" "const char *append"
67.Ft char *
68.Fn strlcat "char *s" "const char *append" "size_t size"
69.Ft char *
70.Fn strncat "char *s" "const char *append" "size_t count"
71.Ft char *
72.Fn strchr "const char *s" "int c"
73.Ft char *
74.Fn strrchr "const char *s" "int c"
75.Ft int
76.Fn strcmp "const char *s1" "const char *s2"
77.Ft int
78.Fn strncmp "const char *s1" "const char *s2" "size_t count"
79.Ft int
80.Fn strcasecmp "const char *s1" "const char *s2"
81.Ft int
82.Fn strncasecmp "const char *s1" "const char *s2" "size_t count"
83.Ft char *
84.Fn strcpy "char *dst" "const char *src"
85.Ft char *
86.Fn strlcpy "char *dst" "const char *src" "size_t size"
87.Ft char *
88.Fn strncpy "char *dst" "const char *src" "size_t count"
89.Ft char *
90.Fn strerror "int errno"
91.Ft size_t
92.Fn strlen "const char *s"
93.Ft char *
94.Fn strpbrk "const char *s" "const char *charset"
95.Ft char *
96.Fn strsep "char **stringp" "const char *delim"
97.Ft size_t
98.Fn strspn "const char *s" "const char *charset"
99.Ft size_t
100.Fn strcspn "const char *s" "const char *charset"
101.Ft char *
102.Fn strstr "const char *big" "const char *little"
103.Ft char *
104.Fn strtok "char *s" "const char *delim"
105.Ft char *
106.Fn index "const char *s" "int c"
107.Ft char *
108.Fn rindex "const char *s" "int c"
109.Sh DESCRIPTION
110The string functions
111manipulate strings terminated by a
112null byte.
113.Pp
114See the specific manual pages for more information.
115For manipulating variable length generic objects as byte
116strings (without the null byte check), see
117.Xr bstring 3 .
118.Pp
119Except as noted in their specific manual pages,
120the string functions do not test the destination
121for size limitations.
122.Sh SEE ALSO
123.Xr bstring 3 ,
124.Xr index 3 ,
125.Xr rindex 3 ,
126.Xr strcasecmp 3 ,
127.Xr strcat 3 ,
128.Xr strchr 3 ,
129.Xr strcmp 3 ,
130.Xr strcpy 3 ,
131.Xr strcspn 3 ,
132.Xr strerror 3 ,
133.Xr strlcat 3 ,
134.Xr strlcpy 3 ,
135.Xr strlen 3 ,
136.Xr strpbrk 3 ,
137.Xr strrchr 3 ,
138.Xr strsep 3 ,
139.Xr strspn 3 ,
140.Xr strstr 3 ,
141.Xr strtok 3
142.Sh STANDARDS
143The
144.Fn strcat ,
145.Fn strncat ,
146.Fn strchr ,
147.Fn strrchr ,
148.Fn strcmp ,
149.Fn strncmp ,
150.Fn strcpy ,
151.Fn strncpy ,
152.Fn strerror ,
153.Fn strlen ,
154.Fn strpbrk ,
155.Fn strspn ,
156.Fn strcspn ,
157.Fn strstr ,
158and
159.Fn strtok
160functions conform to
161.St -ansiC .
diff --git a/src/lib/libc/string/strlcat.c b/src/lib/libc/string/strlcat.c
new file mode 100644
index 0000000000..b309648155
--- /dev/null
+++ b/src/lib/libc/string/strlcat.c
@@ -0,0 +1,73 @@
1/* $OpenBSD: strlcat.c,v 1.8 2001/05/13 15:40:15 deraadt Exp $ */
2
3/*
4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5 * 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. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: strlcat.c,v 1.8 2001/05/13 15:40:15 deraadt Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#include <sys/types.h>
35#include <string.h>
36
37/*
38 * Appends src to string dst of size siz (unlike strncat, siz is the
39 * full size of dst, not space left). At most siz-1 characters
40 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
41 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
42 * If retval >= siz, truncation occurred.
43 */
44size_t
45strlcat(dst, src, siz)
46 char *dst;
47 const char *src;
48 size_t siz;
49{
50 register char *d = dst;
51 register const char *s = src;
52 register size_t n = siz;
53 size_t dlen;
54
55 /* Find the end of dst and adjust bytes left but don't go past end */
56 while (n-- != 0 && *d != '\0')
57 d++;
58 dlen = d - dst;
59 n = siz - dlen;
60
61 if (n == 0)
62 return(dlen + strlen(s));
63 while (*s != '\0') {
64 if (n != 1) {
65 *d++ = *s;
66 n--;
67 }
68 s++;
69 }
70 *d = '\0';
71
72 return(dlen + (s - src)); /* count does not include NUL */
73}
diff --git a/src/lib/libc/string/strlcpy.3 b/src/lib/libc/string/strlcpy.3
new file mode 100644
index 0000000000..b103588b96
--- /dev/null
+++ b/src/lib/libc/string/strlcpy.3
@@ -0,0 +1,190 @@
1.\" $OpenBSD: strlcpy.3,v 1.14 2002/04/30 16:31:42 mpech Exp $
2.\"
3.\" Copyright (c) 1998, 2000 Todd C. Miller <Todd.Miller@courtesan.com>
4.\" 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. The name of the author may not be used to endorse or promote products
15.\" derived from this software without specific prior written permission.
16.\"
17.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27.\"
28.Dd June 22, 1998
29.Dt STRLCPY 3
30.Os
31.Sh NAME
32.Nm strlcpy ,
33.Nm strlcat
34.Nd size-bounded string copying and concatenation
35.Sh SYNOPSIS
36.Fd #include <string.h>
37.Ft size_t
38.Fn strlcpy "char *dst" "const char *src" "size_t size"
39.Ft size_t
40.Fn strlcat "char *dst" "const char *src" "size_t size"
41.Sh DESCRIPTION
42The
43.Fn strlcpy
44and
45.Fn strlcat
46functions copy and concatenate strings respectively.
47They are designed
48to be safer, more consistent, and less error prone replacements for
49.Xr strncpy 3
50and
51.Xr strncat 3 .
52Unlike those functions,
53.Fn strlcpy
54and
55.Fn strlcat
56take the full size of the buffer (not just the length) and guarantee to
57NUL-terminate the result (as long as
58.Fa size
59is larger than 0 or, in the case of
60.Fn strlcat ,
61as long as there is at least one byte free in
62.Fa dst ) .
63Note that you should include a byte for the NUL in
64.Fa size .
65Also note that
66.Fn strlcpy
67and
68.Fn strlcat
69only operate on true
70.Dq C
71strings.
72This means that for
73.Fn strlcpy
74.Fa src
75must be NUL-terminated and for
76.Fn strlcat
77both
78.Fa src
79and
80.Fa dst
81must be NUL-terminated.
82.Pp
83The
84.Fn strlcpy
85function copies up to
86.Fa size
87- 1 characters from the NUL-terminated string
88.Fa src
89to
90.Fa dst ,
91NUL-terminating the result.
92.Pp
93The
94.Fn strlcat
95function appends the NUL-terminated string
96.Fa src
97to the end of
98.Fa dst .
99It will append at most
100.Fa size
101- strlen(dst) - 1 bytes, NUL-terminating the result.
102.Sh RETURN VALUES
103The
104.Fn strlcpy
105and
106.Fn strlcat
107functions return the total length of the string they tried to create.
108For
109.Fn strlcpy
110that means the length of
111.Fa src .
112For
113.Fn strlcat
114that means the initial length of
115.Fa dst
116plus
117the length of
118.Fa src .
119While this may seem somewhat confusing it was done to make
120truncation detection simple.
121.Pp
122Note however, that if
123.Fn strlcat
124traverses
125.Fa size
126characters without finding a NUL, the length of the string is considered
127to be
128.Fa size
129and the destination string will not be NUL-terminated (since there was
130no space for the NUL).
131This keeps
132.Fn strlcat
133from running off the end of a string.
134In practice this should not happen (as it means that either
135.Fa size
136is incorrect or that
137.Fa dst
138is not a proper
139.Dq C
140string).
141The check exists to prevent potential security problems in incorrect code.
142.Sh EXAMPLES
143The following code fragment illustrates the simple case:
144.Bd -literal -offset indent
145char *s, *p, buf[BUFSIZ];
146
147\&...
148
149(void)strlcpy(buf, s, sizeof(buf));
150(void)strlcat(buf, p, sizeof(buf));
151.Ed
152.Pp
153To detect truncation, perhaps while building a pathname, something
154like the following might be used:
155.Bd -literal -offset indent
156char *dir, *file, pname[MAXPATHLEN];
157
158\&...
159
160if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname))
161 goto toolong;
162if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname))
163 goto toolong;
164.Ed
165.Pp
166Since we know how many characters we copied the first time, we can
167speed things up a bit by using a copy instead of an append:
168.Bd -literal -offset indent
169char *dir, *file, pname[MAXPATHLEN];
170size_t n;
171
172\&...
173
174n = strlcpy(pname, dir, sizeof(pname));
175if (n >= sizeof(pname))
176 goto toolong;
177if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n)
178 goto toolong;
179.Ed
180.Pp
181However, one may question the validity of such optimizations, as they
182defeat the whole purpose of
183.Fn strlcpy
184and
185.Fn strlcat .
186As a matter of fact, the first version of this manual page got it wrong.
187.Sh SEE ALSO
188.Xr snprintf 3 ,
189.Xr strncat 3 ,
190.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..5f586964b7
--- /dev/null
+++ b/src/lib/libc/string/strlcpy.c
@@ -0,0 +1,69 @@
1/* $OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $ */
2
3/*
4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
5 * 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. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
31static char *rcsid = "$OpenBSD: strlcpy.c,v 1.5 2001/05/13 15:40:16 deraadt Exp $";
32#endif /* LIBC_SCCS and not lint */
33
34#include <sys/types.h>
35#include <string.h>
36
37/*
38 * Copy src to string dst of size siz. At most siz-1 characters
39 * will be copied. Always NUL terminates (unless siz == 0).
40 * Returns strlen(src); if retval >= siz, truncation occurred.
41 */
42size_t
43strlcpy(dst, src, siz)
44 char *dst;
45 const char *src;
46 size_t siz;
47{
48 register char *d = dst;
49 register const char *s = src;
50 register size_t n = siz;
51
52 /* Copy as many bytes as will fit */
53 if (n != 0 && --n != 0) {
54 do {
55 if ((*d++ = *s++) == 0)
56 break;
57 } while (--n != 0);
58 }
59
60 /* Not enough room in dst, add NUL and traverse rest of src */
61 if (n == 0) {
62 if (siz != 0)
63 *d = '\0'; /* NUL-terminate dst */
64 while (*s++)
65 ;
66 }
67
68 return(s - src - 1); /* count does not include NUL */
69}
diff --git a/src/lib/libc/string/strlen.3 b/src/lib/libc/string/strlen.3
new file mode 100644
index 0000000000..759e724056
--- /dev/null
+++ b/src/lib/libc/string/strlen.3
@@ -0,0 +1,65 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strlen.3,v 1.4 2000/04/21 15:24:20 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRLEN 3
40.Os
41.Sh NAME
42.Nm strlen
43.Nd find length of a string
44.Sh SYNOPSIS
45.Fd #include <string.h>
46.Ft size_t
47.Fn strlen "const char *s"
48.Sh DESCRIPTION
49The
50.Fn strlen
51function computes the length of the string
52.Fa s .
53.Sh RETURN VALUES
54The
55.Fn strlen
56function returns the number of characters that precede the terminating
57.Tn NUL
58character.
59.Sh SEE ALSO
60.Xr string 3
61.Sh STANDARDS
62The
63.Fn strlen
64function conforms to
65.St -ansiC .
diff --git a/src/lib/libc/string/strlen.c b/src/lib/libc/string/strlen.c
new file mode 100644
index 0000000000..ab006e04c2
--- /dev/null
+++ b/src/lib/libc/string/strlen.c
@@ -0,0 +1,56 @@
1/* $OpenBSD: strlen.c,v 1.4 2001/07/29 21:15:23 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#if defined(LIBC_SCCS) && !defined(lint)
37static char *rcsid = "$OpenBSD: strlen.c,v 1.4 2001/07/29 21:15:23 millert Exp $";
38#endif /* LIBC_SCCS and not lint */
39
40#if !defined(_KERNEL) && !defined(_STANDALONE)
41#include <string.h>
42#else
43#include <lib/libkern/libkern.h>
44#endif
45
46size_t
47strlen(str)
48 const char *str;
49{
50 const char *s;
51
52 for (s = str; *s; ++s)
53 ;
54 return (s - str);
55}
56
diff --git a/src/lib/libc/string/strmode.3 b/src/lib/libc/string/strmode.3
new file mode 100644
index 0000000000..bf311f1f99
--- /dev/null
+++ b/src/lib/libc/string/strmode.3
@@ -0,0 +1,163 @@
1.\" $OpenBSD: strmode.3,v 1.8 2000/04/21 15:24:20 aaron 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. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" @(#)strmode.3 8.3 (Berkeley) 7/28/94
35.\"
36.Dd July 28, 1994
37.Dt STRMODE 3
38.Os
39.Sh NAME
40.Nm strmode
41.Nd convert inode status information into a symbolic string
42.Sh SYNOPSIS
43.Fd #include <string.h>
44.Ft void
45.Fn strmode "mode_t mode" "char *bp"
46.Sh DESCRIPTION
47The
48.Fn strmode
49function converts a file
50.Fa mode
51(the type and permission information associated with an inode, see
52.Xr stat 2 )
53into a symbolic string which is stored in the location referenced by
54.Fa bp .
55This stored string is eleven characters in length plus a trailing null byte.
56.Pp
57The first character is the inode type, and will be one of the following:
58.Pp
59.Bl -tag -width flag -offset indent -compact
60.It \-
61regular file
62.It b
63block special
64.It c
65character special
66.It d
67directory
68.It l
69symbolic link
70.It p
71fifo
72.It s
73socket
74.It w
75whiteout
76.It ?
77unknown inode type
78.El
79.Pp
80The next nine characters encode three sets of permissions, in three
81characters each.
82The first three characters are the permissions for the owner of the
83file, the second three for the group the file belongs to, and the
84third for the
85.Dq other ,
86or default, set of users.
87.Pp
88Permission checking is done as specifically as possible.
89If read permission is denied to the owner of a file in the first set
90of permissions, the owner of the file will not be able to read the file.
91This is true even if the owner is in the file's group and the group
92permissions allow reading or the
93.Dq other
94permissions allow reading.
95.Pp
96If the first character of the three character set is an
97.Sq r ,
98the file is readable for that set of users; if a dash
99.Pq Ql - ,
100it is not readable.
101.Pp
102If the second character of the three character set is a
103.Sq w ,
104the file is writable for that set of users; if a dash
105.Pq Ql - ,
106it is not writable.
107.Pp
108The third character is the first of the following characters that apply:
109.Bl -tag -width xxxx
110.It S
111If the character is part of the owner permissions and the file is not
112executable or the directory is not searchable by the owner, and the
113set-user-ID bit is set.
114.It S
115If the character is part of the group permissions and the file is not
116executable or the directory is not searchable by the group, and the
117set-group-ID bit is set.
118.It T
119If the character is part of the other permissions and the file is not
120executable or the directory is not searchable by others, and the
121.Dq sticky
122.Pq Dv S_ISVTX
123bit is set.
124.It s
125If the character is part of the owner permissions and the file is
126executable or the directory searchable by the owner, and the set-user-ID
127bit is set.
128.It s
129If the character is part of the group permissions and the file is
130executable or the directory searchable by the group, and the set-group-ID
131bit is set.
132.It t
133If the character is part of the other permissions and the file is
134executable or the directory searchable by others, and the
135.Dq sticky
136.Pq Dv S_ISVTX
137bit is set.
138.It x
139The file is executable or the directory is searchable.
140.It \-
141None of the above apply.
142.El
143.Pp
144The last character is a plus sign
145.Pq Ql +
146if there are any alternate
147or additional access control methods associated with the inode, otherwise
148it will be a space.
149.Sh RETURN VALUES
150The
151.Fn strmode
152function always returns 0.
153.Sh SEE ALSO
154.Xr chmod 1 ,
155.Xr find 1 ,
156.Xr stat 2 ,
157.Xr getmode 3 ,
158.Xr setmode 3
159.Sh HISTORY
160The
161.Fn strmode
162function first appeared in
163.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..5e7f15e857
--- /dev/null
+++ b/src/lib/libc/string/strmode.c
@@ -0,0 +1,152 @@
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: strmode.c,v 1.3 1997/06/13 13:57:20 deraadt Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <string.h>
41
42void
43strmode(mode, p)
44 register mode_t mode;
45 register char *p;
46{
47 /* print type */
48 switch (mode & S_IFMT) {
49 case S_IFDIR: /* directory */
50 *p++ = 'd';
51 break;
52 case S_IFCHR: /* character special */
53 *p++ = 'c';
54 break;
55 case S_IFBLK: /* block special */
56 *p++ = 'b';
57 break;
58 case S_IFREG: /* regular */
59 *p++ = '-';
60 break;
61 case S_IFLNK: /* symbolic link */
62 *p++ = 'l';
63 break;
64 case S_IFSOCK: /* socket */
65 *p++ = 's';
66 break;
67#ifdef S_IFIFO
68 case S_IFIFO: /* fifo */
69 *p++ = 'p';
70 break;
71#endif
72#ifdef S_IFWHT
73 case S_IFWHT: /* whiteout */
74 *p++ = 'w';
75 break;
76#endif
77 default: /* unknown */
78 *p++ = '?';
79 break;
80 }
81 /* usr */
82 if (mode & S_IRUSR)
83 *p++ = 'r';
84 else
85 *p++ = '-';
86 if (mode & S_IWUSR)
87 *p++ = 'w';
88 else
89 *p++ = '-';
90 switch (mode & (S_IXUSR | S_ISUID)) {
91 case 0:
92 *p++ = '-';
93 break;
94 case S_IXUSR:
95 *p++ = 'x';
96 break;
97 case S_ISUID:
98 *p++ = 'S';
99 break;
100 case S_IXUSR | S_ISUID:
101 *p++ = 's';
102 break;
103 }
104 /* group */
105 if (mode & S_IRGRP)
106 *p++ = 'r';
107 else
108 *p++ = '-';
109 if (mode & S_IWGRP)
110 *p++ = 'w';
111 else
112 *p++ = '-';
113 switch (mode & (S_IXGRP | S_ISGID)) {
114 case 0:
115 *p++ = '-';
116 break;
117 case S_IXGRP:
118 *p++ = 'x';
119 break;
120 case S_ISGID:
121 *p++ = 'S';
122 break;
123 case S_IXGRP | S_ISGID:
124 *p++ = 's';
125 break;
126 }
127 /* other */
128 if (mode & S_IROTH)
129 *p++ = 'r';
130 else
131 *p++ = '-';
132 if (mode & S_IWOTH)
133 *p++ = 'w';
134 else
135 *p++ = '-';
136 switch (mode & (S_IXOTH | S_ISVTX)) {
137 case 0:
138 *p++ = '-';
139 break;
140 case S_IXOTH:
141 *p++ = 'x';
142 break;
143 case S_ISVTX:
144 *p++ = 'T';
145 break;
146 case S_IXOTH | S_ISVTX:
147 *p++ = 't';
148 break;
149 }
150 *p++ = ' '; /* will be a '+' if ACL's implemented */
151 *p = '\0';
152}
diff --git a/src/lib/libc/string/strncat.c b/src/lib/libc/string/strncat.c
new file mode 100644
index 0000000000..27ae2ba324
--- /dev/null
+++ b/src/lib/libc/string/strncat.c
@@ -0,0 +1,67 @@
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char *rcsid = "$OpenBSD: strncat.c,v 1.2 1996/08/19 08:34:21 tholo Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#include <string.h>
42
43/*
44 * Concatenate src on the end of dst. At most strlen(dst)+n+1 bytes
45 * are written at dst (at most n+1 bytes being appended). Return dst.
46 */
47char *
48strncat(dst, src, n)
49 char *dst;
50 const char *src;
51 register size_t n;
52{
53 if (n != 0) {
54 register char *d = dst;
55 register const char *s = src;
56
57 while (*d != 0)
58 d++;
59 do {
60 if ((*d = *s++) == 0)
61 break;
62 d++;
63 } while (--n != 0);
64 *d = 0;
65 }
66 return (dst);
67}
diff --git a/src/lib/libc/string/strncmp.c b/src/lib/libc/string/strncmp.c
new file mode 100644
index 0000000000..0224957f8b
--- /dev/null
+++ b/src/lib/libc/string/strncmp.c
@@ -0,0 +1,59 @@
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: strncmp.c,v 1.3 1996/08/19 08:34:21 tholo Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#ifndef _KERNEL
39#include <string.h>
40#else
41#include <lib/libkern/libkern.h>
42#endif
43
44int
45strncmp(s1, s2, n)
46 register const char *s1, *s2;
47 register size_t n;
48{
49
50 if (n == 0)
51 return (0);
52 do {
53 if (*s1 != *s2++)
54 return (*(unsigned char *)s1 - *(unsigned char *)--s2);
55 if (*s1++ == 0)
56 break;
57 } while (--n != 0);
58 return (0);
59}
diff --git a/src/lib/libc/string/strncpy.c b/src/lib/libc/string/strncpy.c
new file mode 100644
index 0000000000..01bc8a872e
--- /dev/null
+++ b/src/lib/libc/string/strncpy.c
@@ -0,0 +1,67 @@
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char *rcsid = "$OpenBSD: strncpy.c,v 1.2 1996/08/19 08:34:22 tholo Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#include <string.h>
42
43/*
44 * Copy src to dst, truncating or null-padding to always copy n bytes.
45 * Return dst.
46 */
47char *
48strncpy(dst, src, n)
49 char *dst;
50 const char *src;
51 register size_t n;
52{
53 if (n != 0) {
54 register char *d = dst;
55 register const char *s = src;
56
57 do {
58 if ((*d++ = *s++) == 0) {
59 /* NUL pad the remaining n-1 bytes */
60 while (--n != 0)
61 *d++ = 0;
62 break;
63 }
64 } while (--n != 0);
65 }
66 return (dst);
67}
diff --git a/src/lib/libc/string/strpbrk.3 b/src/lib/libc/string/strpbrk.3
new file mode 100644
index 0000000000..52bf13b924
--- /dev/null
+++ b/src/lib/libc/string/strpbrk.3
@@ -0,0 +1,76 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strpbrk.3,v 1.5 2000/04/21 15:24:20 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRPBRK 3
40.Os
41.Sh NAME
42.Nm strpbrk
43.Nd locate multiple characters in string
44.Sh SYNOPSIS
45.Fd #include <string.h>
46.Ft char *
47.Fn strpbrk "const char *s" "const char *charset"
48.Sh DESCRIPTION
49The
50.Fn strpbrk
51function locates in the null-terminated string
52.Fa s
53the first occurrence of any character in the string
54.Fa charset
55and returns a pointer to this character.
56If no characters from
57.Fa charset
58occur anywhere in
59.Fa s ,
60.Fn strpbrk
61returns
62.Dv NULL .
63.Sh SEE ALSO
64.Xr memchr 3 ,
65.Xr strchr 3 ,
66.Xr strcspn 3 ,
67.Xr strrchr 3 ,
68.Xr strsep 3 ,
69.Xr strspn 3 ,
70.Xr strstr 3 ,
71.Xr strtok 3
72.Sh STANDARDS
73The
74.Fn strpbrk
75function conforms to
76.St -ansiC .
diff --git a/src/lib/libc/string/strpbrk.c b/src/lib/libc/string/strpbrk.c
new file mode 100644
index 0000000000..748a3a8c94
--- /dev/null
+++ b/src/lib/libc/string/strpbrk.c
@@ -0,0 +1,56 @@
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: strpbrk.c,v 1.2 1996/08/19 08:34:23 tholo Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#include <string.h>
39
40/*
41 * Find the first occurrence in s1 of a character in s2 (excluding NUL).
42 */
43char *
44strpbrk(s1, s2)
45 register const char *s1, *s2;
46{
47 register const char *scanp;
48 register int c, sc;
49
50 while ((c = *s1++) != 0) {
51 for (scanp = s2; (sc = *scanp++) != 0;)
52 if (sc == c)
53 return ((char *)(s1 - 1));
54 }
55 return (NULL);
56}
diff --git a/src/lib/libc/string/strrchr.3 b/src/lib/libc/string/strrchr.3
new file mode 100644
index 0000000000..394fc2aad3
--- /dev/null
+++ b/src/lib/libc/string/strrchr.3
@@ -0,0 +1,108 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strrchr.3,v 1.6 2000/04/21 15:24:20 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRRCHR 3
40.Os
41.Sh NAME
42.Nm strrchr ,
43.Nm rindex
44.Nd locate last occurrence of a character in a string
45.Sh SYNOPSIS
46.Fd #include <string.h>
47.Ft char *
48.Fn strrchr "const char *s" "int c"
49.Ft char *
50.Fn rindex "const char *s" "int c"
51.Sh DESCRIPTION
52The
53.Fn strrchr
54function locates the last occurrence of the character
55.Fa c
56in the string
57.Fa s .
58The terminating
59.Tn NUL
60character is considered part of the string.
61If
62.Fa c
63is
64.Ql \e0 ,
65.Fn strrchr
66locates the terminating
67.Ql \e0 .
68.Pp
69The
70.Fn rindex
71function is an old synonym for
72.Fn strrchr .
73.Sh RETURN VALUES
74The
75.Fn strrchr
76function returns a pointer to the located character or
77.Dv NULL
78if the character does not appear in the string.
79.Sh EXAMPLES
80After the following call to
81.Fn strrchr ,
82.Va p
83will point to the string
84.Qq obar :
85.Bd -literal -offset indent
86char *p;
87char *s = "foobar";
88
89p = strrchr(s, 'o');
90.Ed
91.Sh SEE ALSO
92.Xr memchr 3 ,
93.Xr strchr 3 ,
94.Xr strcspn 3 ,
95.Xr strpbrk 3 ,
96.Xr strsep 3 ,
97.Xr strspn 3 ,
98.Xr strstr 3 ,
99.Xr strtok 3
100.Sh STANDARDS
101The
102.Fn strrchr
103function conforms to
104.St -ansiC .
105.Pp
106The
107.Fn rindex
108function 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..848dab1eb3
--- /dev/null
+++ b/src/lib/libc/string/strsep.3
@@ -0,0 +1,116 @@
1.\" $OpenBSD: strsep.3,v 1.10 2001/11/21 14:32:45 deraadt 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. All advertising materials mentioning features or use of this software
18.\" must display the following acknowledgement:
19.\" This product includes software developed by the University of
20.\" California, Berkeley and its contributors.
21.\" 4. Neither the name of the University nor the names of its contributors
22.\" may be used to endorse or promote products derived from this software
23.\" without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35.\" SUCH DAMAGE.
36.\"
37.\" @(#)strsep.3 8.1 (Berkeley) 6/9/93
38.\"
39.Dd June 9, 1993
40.Dt STRSEP 3
41.Os
42.Sh NAME
43.Nm strsep
44.Nd separate strings
45.Sh SYNOPSIS
46.Fd #include <string.h>
47.Ft char *
48.Fn strsep "char **stringp" "const char *delim"
49.Sh DESCRIPTION
50The
51.Fn strsep
52function locates, in the string referenced by
53.Fa *stringp ,
54the first occurrence of any character in the string
55.Fa delim
56(or the terminating
57.Ql \e0
58character) and replaces it with a
59.Ql \e0 .
60The location of the next character after the delimiter character
61(or
62.Dv NULL ,
63if the end of the string was reached) is stored in
64.Fa *stringp .
65The original value of
66.Fa *stringp
67is returned.
68.Pp
69An
70.Dq empty
71field, i.e., one caused by two adjacent delimiter characters,
72can be detected by comparing the location referenced by the pointer returned
73by
74.Fn strsep
75to
76.Ql \e0 .
77.Pp
78If
79.Fa *stringp
80is initially
81.Dv NULL ,
82.Fn strsep
83returns
84.Dv NULL .
85.Sh EXAMPLES
86The following uses
87.Fn strsep
88to parse a string, containing tokens delimited by whitespace, into an
89argument vector:
90.Bd -literal -offset indent
91char **ap, *argv[10], *inputstring;
92
93for (ap = argv; ap < &argv[9] &&
94 (*ap = strsep(&inputstring, " \et")) != NULL;) {
95 if (**ap != '\e0')
96 ap++;
97}
98*ap = NULL;
99.Ed
100.Sh HISTORY
101The
102.Fn strsep
103function is intended as a replacement for the
104.Fn strtok
105function.
106While the
107.Fn strtok
108function should be preferred for portability reasons (it conforms to
109.St -ansiC )
110it is unable to handle empty fields, i.e., detect fields delimited by
111two adjacent delimiter characters, or to be used for more than a single
112string at a time.
113The
114.Fn strsep
115function first appeared in
116.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..b69b715fc5
--- /dev/null
+++ b/src/lib/libc/string/strsep.c
@@ -0,0 +1,85 @@
1/* $OpenBSD: strsep.c,v 1.3 1997/08/20 04:28:14 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <string.h>
37#include <stdio.h>
38
39#if defined(LIBC_SCCS) && !defined(lint)
40#if 0
41static char sccsid[] = "@(#)strsep.c 8.1 (Berkeley) 6/4/93";
42#else
43static char *rcsid = "$OpenBSD: strsep.c,v 1.3 1997/08/20 04:28:14 millert Exp $";
44#endif
45#endif /* LIBC_SCCS and not lint */
46
47/*
48 * Get next token from string *stringp, where tokens are possibly-empty
49 * strings separated by characters from delim.
50 *
51 * Writes NULs into the string at *stringp to end tokens.
52 * delim need not remain constant from call to call.
53 * On return, *stringp points past the last NUL written (if there might
54 * be further tokens), or is NULL (if there are definitely no more tokens).
55 *
56 * If *stringp is NULL, strsep returns NULL.
57 */
58char *
59strsep(stringp, delim)
60 register char **stringp;
61 register const char *delim;
62{
63 register char *s;
64 register const char *spanp;
65 register int c, sc;
66 char *tok;
67
68 if ((s = *stringp) == NULL)
69 return (NULL);
70 for (tok = s;;) {
71 c = *s++;
72 spanp = delim;
73 do {
74 if ((sc = *spanp++) == c) {
75 if (c == 0)
76 s = NULL;
77 else
78 s[-1] = 0;
79 *stringp = s;
80 return (tok);
81 }
82 } while (sc != 0);
83 }
84 /* NOTREACHED */
85}
diff --git a/src/lib/libc/string/strsignal.3 b/src/lib/libc/string/strsignal.3
new file mode 100644
index 0000000000..83b3775f4e
--- /dev/null
+++ b/src/lib/libc/string/strsignal.3
@@ -0,0 +1,60 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strsignal.3,v 1.3 1999/05/23 14:11:03 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRSIGNAL 3
40.Os
41.Sh NAME
42.Nm strsignal
43.Nd get signal description string
44.Sh SYNOPSIS
45.Fd #include <string.h>
46.Ft char *
47.Fn strsignal "int sig"
48.Sh DESCRIPTION
49The
50.Fn strsignal
51function returns a pointer to the language-dependent string describing
52a signal.
53.Pp
54The array pointed to is not to be modified by the program, but may be
55overwritten by subsequent calls to
56.Fn strsignal .
57.Sh SEE ALSO
58.Xr intro 2 ,
59.Xr psignal 3 ,
60.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..90118f70f9
--- /dev/null
+++ b/src/lib/libc/string/strsignal.c
@@ -0,0 +1,49 @@
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: strsignal.c,v 1.3 2002/02/16 21:27:24 millert Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#include <string.h>
39#include <limits.h>
40
41extern char *__strsignal(int, char *);
42
43char *
44strsignal(sig)
45 int sig;
46{
47 static char buf[NL_TEXTMAX];
48 return __strsignal(sig, buf);
49}
diff --git a/src/lib/libc/string/strspn.3 b/src/lib/libc/string/strspn.3
new file mode 100644
index 0000000000..ca965aeb18
--- /dev/null
+++ b/src/lib/libc/string/strspn.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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strspn.3,v 1.6 2000/04/21 15:24:20 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRSPN 3
40.Os
41.Sh NAME
42.Nm strspn
43.Nd span a string
44.Sh SYNOPSIS
45.Fd #include <string.h>
46.Ft size_t
47.Fn strspn "const char *s" "const char *charset"
48.Sh DESCRIPTION
49The
50.Fn strspn
51function spans the initial part of the null-terminated string
52.Fa s
53as long as the characters from
54.Fa s
55occur in string
56.Fa charset .
57.Sh RETURN VALUES
58The
59.Fn strspn
60function returns the number of characters spanned.
61.Sh EXAMPLES
62The following call to
63.Fn strspn
64will return 3, since the first three characters of string
65.Fa s
66are part of string
67.Fa charset :
68.Bd -literal -offset indent
69char *s = "foobar";
70char *charset = "of";
71size_t span;
72
73span = strspn(s, charset);
74.Ed
75.Sh SEE ALSO
76.Xr memchr 3 ,
77.Xr strchr 3 ,
78.Xr strcspn 3 ,
79.Xr strpbrk 3 ,
80.Xr strrchr 3 ,
81.Xr strsep 3 ,
82.Xr strstr 3 ,
83.Xr strtok 3
84.Sh STANDARDS
85The
86.Fn strspn
87function conforms to
88.St -ansiC .
diff --git a/src/lib/libc/string/strspn.c b/src/lib/libc/string/strspn.c
new file mode 100644
index 0000000000..41940f9190
--- /dev/null
+++ b/src/lib/libc/string/strspn.c
@@ -0,0 +1,60 @@
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: strspn.c,v 1.2 1996/08/19 08:34:26 tholo Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#include <string.h>
39
40/*
41 * Span the string s2 (skip characters that are in s2).
42 */
43size_t
44strspn(s1, s2)
45 const char *s1;
46 register const char *s2;
47{
48 register const char *p = s1, *spanp;
49 register char c, sc;
50
51 /*
52 * Skip any characters in s2, excluding the terminating \0.
53 */
54cont:
55 c = *p++;
56 for (spanp = s2; (sc = *spanp++) != 0;)
57 if (sc == c)
58 goto cont;
59 return (p - 1 - s1);
60}
diff --git a/src/lib/libc/string/strstr.3 b/src/lib/libc/string/strstr.3
new file mode 100644
index 0000000000..10beea42ca
--- /dev/null
+++ b/src/lib/libc/string/strstr.3
@@ -0,0 +1,85 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strstr.3,v 1.5 2000/04/21 15:24:20 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRSTR 3
40.Os
41.Sh NAME
42.Nm strstr
43.Nd locate a substring in a string
44.Sh SYNOPSIS
45.Fd #include <string.h>
46.Ft char *
47.Fn strstr "const char *big" "const char *little"
48.Sh DESCRIPTION
49The
50.Fn strstr
51function locates the first occurrence of the null-terminated string
52.Fa little
53in the null-terminated string
54.Fa big .
55If
56.Fa little
57is the empty string,
58.Fn strstr
59returns
60.Fa big ;
61if
62.Fa little
63occurs nowhere in
64.Fa big ,
65.Fn strstr
66returns
67.Dv NULL ;
68otherwise
69.Fn strstr
70returns a pointer to the first character of the first occurrence of
71.Fa little .
72.Sh SEE ALSO
73.Xr memchr 3 ,
74.Xr strchr 3 ,
75.Xr strcspn 3 ,
76.Xr strpbrk 3 ,
77.Xr strrchr 3 ,
78.Xr strsep 3 ,
79.Xr strspn 3 ,
80.Xr strtok 3
81.Sh STANDARDS
82The
83.Fn strstr
84function conforms to
85.St -ansiC .
diff --git a/src/lib/libc/string/strstr.c b/src/lib/libc/string/strstr.c
new file mode 100644
index 0000000000..763c7e29d7
--- /dev/null
+++ b/src/lib/libc/string/strstr.c
@@ -0,0 +1,64 @@
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char *rcsid = "$OpenBSD: strstr.c,v 1.2 1996/08/19 08:34:27 tholo Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#include <string.h>
42
43/*
44 * Find the first occurrence of find in s.
45 */
46char *
47strstr(s, find)
48 register const char *s, *find;
49{
50 register char c, sc;
51 register size_t len;
52
53 if ((c = *find++) != 0) {
54 len = strlen(find);
55 do {
56 do {
57 if ((sc = *s++) == 0)
58 return (NULL);
59 } while (sc != c);
60 } while (strncmp(s, find, len) != 0);
61 s--;
62 }
63 return ((char *)s);
64}
diff --git a/src/lib/libc/string/strtok.3 b/src/lib/libc/string/strtok.3
new file mode 100644
index 0000000000..b8dad8eff0
--- /dev/null
+++ b/src/lib/libc/string/strtok.3
@@ -0,0 +1,156 @@
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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strtok.3,v 1.15 2001/08/06 10:42:26 mpech Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRTOK 3
40.Os
41.Sh NAME
42.Nm strtok ,
43.Nm strtok_r
44.Nd string token operations
45.Sh SYNOPSIS
46.Fd #include <string.h>
47.Ft char *
48.Fn strtok "char *str" "const char *sep"
49.Ft char *
50.Fn strtok_r "char *str" "const char *sep" "char **last"
51.Sh DESCRIPTION
52.Bf -symbolic
53This interface is obsoleted by
54.Xr strsep 3 .
55.Ef
56.Pp
57The
58.Fn strtok
59function is used to isolate sequential tokens in a null-terminated string,
60.Fa str .
61These tokens are separated in the string by at least one of the
62characters in
63.Fa sep .
64The first time that
65.Fn strtok
66is called,
67.Fa str
68should be specified; subsequent calls, wishing to obtain further tokens
69from the same string, should pass a null pointer instead.
70The separator string,
71.Fa sep ,
72must be supplied each time, and may change between calls.
73.Pp
74The
75.Fn strtok_r
76function is a version of
77.Fn strtok
78that takes an explicit context argument and is reentrant.
79.Pp
80The
81.Fn strtok
82and
83.Fn strtok_r
84functions return a pointer to the beginning of each subsequent token
85in the string, after replacing the separator character itself with an
86.Tn ASCII NUL
87character.
88When no more tokens remain, a null pointer is returned.
89.Pp
90Since
91.Fn strtok
92and
93.Fn strtok_r
94modify the string,
95.Fa str
96should not point to an area in the initialized data segment.
97.Sh EXAMPLES
98The following will construct an array of pointers to each individual word in
99the string
100.Va s :
101.Bd -literal -offset indent
102#define MAXTOKENS 128
103
104char s[512], *p, *tokens[MAXTOKENS];
105char *last;
106int i = 0;
107
108snprintf(s, sizeof(s), "cat dog horse cow");
109
110for ((p = strtok_r(s, " ", &last)); p;
111 (p = strtok_r(NULL, " ", &last)), i++) {
112 if (i < MAXTOKENS - 1)
113 tokens[i] = p;
114}
115tokens[i] = NULL;
116.Ed
117.Pp
118That is,
119.Li tokens[0]
120will point to
121.Qq cat ,
122.Li tokens[1]
123will point to
124.Qq dog ,
125.Li tokens[2]
126will point to
127.Qq horse ,
128and
129.Li tokens[3]
130will point to
131.Qq cow .
132.Sh SEE ALSO
133.Xr memchr 3 ,
134.Xr strchr 3 ,
135.Xr strcspn 3 ,
136.Xr strpbrk 3 ,
137.Xr strrchr 3 ,
138.Xr strsep 3 ,
139.Xr strspn 3 ,
140.Xr strstr 3
141.Sh STANDARDS
142The
143.Fn strtok
144function conforms to
145.St -ansiC .
146.Sh BUGS
147The System V
148.Fn strtok ,
149if handed a string containing only delimiter characters,
150will not alter the next starting point, so that a call to
151.Fn strtok
152with a different (or empty) delimiter string
153may return a non-null value.
154Since this implementation always alters the next starting point,
155such a sequence of calls would always return
156.Dv NULL .
diff --git a/src/lib/libc/string/strtok.c b/src/lib/libc/string/strtok.c
new file mode 100644
index 0000000000..d925dc75d0
--- /dev/null
+++ b/src/lib/libc/string/strtok.c
@@ -0,0 +1,99 @@
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: strtok.c,v 1.3 1999/11/09 11:19:46 art Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#include <string.h>
39
40char *
41strtok(s, delim)
42 register char *s;
43 register const char *delim;
44{
45 static char *last;
46
47 return strtok_r(s, delim, &last);
48}
49
50char *
51strtok_r(s, delim, last)
52 register char *s;
53 register const char *delim;
54 char **last;
55{
56 register char *spanp;
57 register int c, sc;
58 char *tok;
59
60
61 if (s == NULL && (s = *last) == NULL)
62 return (NULL);
63
64 /*
65 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
66 */
67cont:
68 c = *s++;
69 for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
70 if (c == sc)
71 goto cont;
72 }
73
74 if (c == 0) { /* no non-delimiter characters */
75 *last = NULL;
76 return (NULL);
77 }
78 tok = s - 1;
79
80 /*
81 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
82 * Note that delim must have one NUL; we stop if we see that, too.
83 */
84 for (;;) {
85 c = *s++;
86 spanp = (char *)delim;
87 do {
88 if ((sc = *spanp++) == c) {
89 if (c == 0)
90 s = NULL;
91 else
92 s[-1] = 0;
93 *last = s;
94 return (tok);
95 }
96 } while (sc != 0);
97 }
98 /* NOTREACHED */
99}
diff --git a/src/lib/libc/string/strxfrm.3 b/src/lib/libc/string/strxfrm.3
new file mode 100644
index 0000000000..1e5338993a
--- /dev/null
+++ b/src/lib/libc/string/strxfrm.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 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. All advertising materials mentioning features or use of this software
17.\" must display the following acknowledgement:
18.\" This product includes software developed by the University of
19.\" California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\" may be used to endorse or promote products derived from this software
22.\" without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\" $OpenBSD: strxfrm.3,v 1.3 2000/04/21 15:24:20 aaron Exp $
37.\"
38.Dd June 29, 1991
39.Dt STRXFRM 3
40.Os
41.Sh NAME
42.Nm strxfrm
43.Nd transform a string under locale
44.Sh SYNOPSIS
45.Fd #include <string.h>
46.Ft size_t
47.Fn strxfrm "char *dst" "const char *src" "size_t n"
48.Sh DESCRIPTION
49The
50.Fn strxfrm
51function does something horrible (see
52.Tn ANSI
53standard).
54In this implementation it just copies.
55.Sh SEE ALSO
56.Xr bcmp 3 ,
57.Xr memcmp 3 ,
58.\" .Xr setlocale 3 ,
59.Xr strcasecmp 3 ,
60.Xr strcmp 3 ,
61.Xr strcoll 3
62.Sh STANDARDS
63The
64.Fn strxfrm
65function conforms to
66.St -ansiC .
diff --git a/src/lib/libc/string/strxfrm.c b/src/lib/libc/string/strxfrm.c
new file mode 100644
index 0000000000..6b258edecc
--- /dev/null
+++ b/src/lib/libc/string/strxfrm.c
@@ -0,0 +1,73 @@
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char *rcsid = "$OpenBSD: strxfrm.c,v 1.2 1996/08/19 08:34:29 tholo Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#include <string.h>
42
43/*
44 * Transform src, storing the result in dst, such that
45 * strcmp() on transformed strings returns what strcoll()
46 * on the original untransformed strings would return.
47 */
48size_t
49strxfrm(dst, src, n)
50 register char *dst;
51 register const char *src;
52 register size_t n;
53{
54 register size_t r = 0;
55 register int c;
56
57 /*
58 * Since locales are unimplemented, this is just a copy.
59 */
60 if (n != 0) {
61 while ((c = *src++) != 0) {
62 r++;
63 if (--n == 0) {
64 while (*src++ != 0)
65 r++;
66 break;
67 }
68 *dst++ = c;
69 }
70 *dst = 0;
71 }
72 return (r);
73}
diff --git a/src/lib/libc/string/swab.3 b/src/lib/libc/string/swab.3
new file mode 100644
index 0000000000..628eb6b15f
--- /dev/null
+++ b/src/lib/libc/string/swab.3
@@ -0,0 +1,65 @@
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. All advertising materials mentioning features or use of this software
13.\" must display the following acknowledgement:
14.\" This product includes software developed by the University of
15.\" California, Berkeley and its contributors.
16.\" 4. 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: swab.3,v 1.4 2000/01/03 22:52:59 aaron Exp $
33.\"
34.Dd May 1, 1991
35.Dt SWAB 3
36.Os
37.Sh NAME
38.Nm swab
39.Nd swap adjacent bytes
40.Sh SYNOPSIS
41.Fd #include <unistd.h>
42.Ft void
43.Fn swab "const void *src" "void *dst" "size_t len"
44.Sh DESCRIPTION
45The function
46.Fn swab
47copies
48.Fa len
49bytes from the location referenced by
50.Fa src
51to the location referenced by
52.Fa dst ,
53swapping adjacent bytes.
54.Pp
55The argument
56.Fa len
57must be an even number.
58.Sh SEE ALSO
59.Xr bzero 3 ,
60.Xr memset 3
61.Sh HISTORY
62A
63.Fn swab
64function appeared in
65.At v7 .
diff --git a/src/lib/libc/string/swab.c b/src/lib/libc/string/swab.c
new file mode 100644
index 0000000000..311cf13a53
--- /dev/null
+++ b/src/lib/libc/string/swab.c
@@ -0,0 +1,65 @@
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char *rcsid = "$OpenBSD: swab.c,v 1.3 1998/02/10 02:19:48 deraadt Exp $";
39#endif /* LIBC_SCCS and not lint */
40
41#include <unistd.h>
42
43void
44swab(from, to, len)
45 const void *from;
46 void *to;
47 size_t len;
48{
49 register unsigned long temp;
50 register int n;
51 register char *fp, *tp;
52
53 n = (len >> 1) + 1;
54 fp = (char *)from;
55 tp = (char *)to;
56#define STEP temp = *fp++,*tp++ = *fp++,*tp++ = temp
57 /* round to multiple of 8 */
58 while ((--n) & 07)
59 STEP;
60 n >>= 3;
61 while (--n >= 0) {
62 STEP; STEP; STEP; STEP;
63 STEP; STEP; STEP; STEP;
64 }
65}