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