diff options
Diffstat (limited to 'src/lib/libc/string')
97 files changed, 7210 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..124469cd3a --- /dev/null +++ b/src/lib/libc/string/Makefile.inc | |||
@@ -0,0 +1,178 @@ | |||
1 | # $OpenBSD: Makefile.inc,v 1.20 2007/10/25 22:41:02 miod Exp $ | ||
2 | |||
3 | # string sources | ||
4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string ${LIBCSRCDIR}/string | ||
5 | |||
6 | SRCS+= bm.c memccpy.c memrchr.c strcasecmp.c strcasestr.c strcoll.c strdup.c \ | ||
7 | strerror.c strerror_r.c strlcat.c strmode.c strsignal.c strtok.c \ | ||
8 | strxfrm.c \ | ||
9 | wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ | ||
10 | wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c wcsspn.c \ | ||
11 | wcsstr.c wcstok.c wcswcs.c wcswidth.c wmemchr.c wmemcmp.c wmemcpy.c \ | ||
12 | wmemmove.c wmemset.c | ||
13 | |||
14 | # machine-dependent net sources | ||
15 | # m-d Makefile.inc must include sources for: | ||
16 | # bcmp() bcopy() bzero() ffs() index() memchr() memcmp() memset() | ||
17 | # rindex() strcat() strcmp() strcpy() strcspn() strlen() strlcpy() | ||
18 | # strncat() strncmp() strncpy() strpbrk() strsep() | ||
19 | # strspn() strstr() swav() | ||
20 | # m-d Makefile.inc may include sources for: | ||
21 | # memcpy() memmove() strchr() strrchr() | ||
22 | |||
23 | .include "${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string/Makefile.inc" | ||
24 | |||
25 | # if no machine specific memmove(3), build one out of bcopy(3). | ||
26 | .if empty(SRCS:Mmemmove.S) | ||
27 | OBJS+= memmove.o | ||
28 | memmove.o: bcopy.c | ||
29 | ${CC} -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
30 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
31 | @mv ${.TARGET}.tmp ${.TARGET} | ||
32 | |||
33 | memmove.go: bcopy.c | ||
34 | ${CC} -g -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
35 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
36 | @mv ${.TARGET}.tmp ${.TARGET} | ||
37 | |||
38 | memmove.po: bcopy.c | ||
39 | ${CC} -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
40 | @${LD} -o ${.TARGET}.tmp -X -r ${.TARGET} | ||
41 | @mv ${.TARGET}.tmp ${.TARGET} | ||
42 | |||
43 | memmove.so: bcopy.c | ||
44 | ${CC} ${PICFLAG} -DPIC -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
45 | -o ${.TARGET} | ||
46 | .endif | ||
47 | |||
48 | # if no machine specific memcpy(3), build one out of bcopy(3). | ||
49 | # if there is a machine specific memmove(3), we'll assume it aliases | ||
50 | # memcpy(3). | ||
51 | .if empty(SRCS:Mmemcpy.S) | ||
52 | .if empty(SRCS:Mmemmove.S) | ||
53 | OBJS+= memcpy.o | ||
54 | memcpy.o: bcopy.c | ||
55 | ${CC} -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
56 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
57 | @mv ${.TARGET}.tmp ${.TARGET} | ||
58 | |||
59 | memcpy.go: bcopy.c | ||
60 | ${CC} -g -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
61 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
62 | @mv ${.TARGET}.tmp ${.TARGET} | ||
63 | |||
64 | memcpy.po: bcopy.c | ||
65 | ${CC} -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
66 | @${LD} -o ${.TARGET}.tmp -X -r ${.TARGET} | ||
67 | @mv ${.TARGET}.tmp ${.TARGET} | ||
68 | |||
69 | memcpy.so: bcopy.c | ||
70 | ${CC} ${PICFLAG} -DPIC -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
71 | -o ${.TARGET} | ||
72 | .endif | ||
73 | .endif | ||
74 | |||
75 | # if no machine specific strchr(3), build one out of index(3). | ||
76 | .if empty(SRCS:Mstrchr.S) | ||
77 | OBJS+= strchr.o | ||
78 | strchr.o: index.c | ||
79 | ${CC} -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
80 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
81 | @mv ${.TARGET}.tmp ${.TARGET} | ||
82 | |||
83 | strchr.go: index.c | ||
84 | ${CC} -g -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
85 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
86 | @mv ${.TARGET}.tmp ${.TARGET} | ||
87 | |||
88 | strchr.po: index.c | ||
89 | ${CC} -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
90 | @${LD} -o ${.TARGET}.tmp -X -r ${.TARGET} | ||
91 | @mv ${.TARGET}.tmp ${.TARGET} | ||
92 | |||
93 | strchr.so: index.c | ||
94 | ${CC} ${PICFLAG} -DPIC -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
95 | -o ${.TARGET} | ||
96 | .endif | ||
97 | |||
98 | # if no machine specific strrchr(3), build one out of rindex(3). | ||
99 | .if empty(SRCS:Mstrrchr.S) | ||
100 | OBJS+= strrchr.o | ||
101 | strrchr.o: rindex.c | ||
102 | ${CC} -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
103 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
104 | @mv ${.TARGET}.tmp ${.TARGET} | ||
105 | |||
106 | strrchr.go: rindex.c | ||
107 | ${CC} -g -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
108 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
109 | @mv ${.TARGET}.tmp ${.TARGET} | ||
110 | |||
111 | strrchr.po: rindex.c | ||
112 | ${CC} -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
113 | @${LD} -o ${.TARGET}.tmp -X -r ${.TARGET} | ||
114 | @mv ${.TARGET}.tmp ${.TARGET} | ||
115 | |||
116 | strrchr.so: rindex.c | ||
117 | ${CC} ${PICFLAG} -DPIC -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
118 | -o ${.TARGET} | ||
119 | .endif | ||
120 | |||
121 | # build .ln files for memmove, memcpy, strchr and strrchr always from | ||
122 | # bcopy, index, and rindex | ||
123 | LOBJS+= memmove.ln memcpy.ln strchr.ln strrchr.ln | ||
124 | |||
125 | memmove.ln: bcopy.c | ||
126 | lint ${LINTFLAGS} -DMEMMOVE ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \ | ||
127 | ${LIBCSRCDIR}/string/bcopy.c | ||
128 | |||
129 | memcpy.ln: bcopy.c | ||
130 | lint ${LINTFLAGS} -DMEMCOPY ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \ | ||
131 | ${LIBCSRCDIR}/string/bcopy.c | ||
132 | |||
133 | strchr.ln: index.c | ||
134 | lint ${LINTFLAGS} -DSTRCHR ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \ | ||
135 | ${LIBCSRCDIR}/string/index.c | ||
136 | |||
137 | strrchr.ln: rindex.c | ||
138 | lint ${LINTFLAGS} -DSTRRCHR ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \ | ||
139 | ${LIBCSRCDIR}/string/rindex.c | ||
140 | |||
141 | MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \ | ||
142 | memcmp.3 memcpy.3 memmove.3 memset.3 strcasecmp.3 strcat.3 \ | ||
143 | strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strerror.3 \ | ||
144 | string.3 strlen.3 strmode.3 strdup.3 strpbrk.3 strrchr.3 strsep.3 \ | ||
145 | strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 strlcpy.3 \ | ||
146 | wcstok.3 wmemchr.3 | ||
147 | |||
148 | MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3 | ||
149 | MLINKS+=memchr.3 memrchr.3 | ||
150 | MLINKS+=strchr.3 index.3 | ||
151 | MLINKS+=strrchr.3 rindex.3 | ||
152 | MLINKS+=strcasecmp.3 strncasecmp.3 | ||
153 | MLINKS+=strcat.3 strncat.3 | ||
154 | MLINKS+=strcmp.3 strncmp.3 | ||
155 | MLINKS+=strcpy.3 strncpy.3 | ||
156 | MLINKS+=strlcpy.3 strlcat.3 | ||
157 | MLINKS+=strstr.3 strcasestr.3 | ||
158 | MLINKS+=strtok.3 strtok_r.3 | ||
159 | MLINKS+=strerror.3 strerror_r.3 | ||
160 | MLINKS+=wmemchr.3 wmemcmp.3 | ||
161 | MLINKS+=wmemchr.3 wmemcpy.3 | ||
162 | MLINKS+=wmemchr.3 wmemmove.3 | ||
163 | MLINKS+=wmemchr.3 wmemset.3 | ||
164 | MLINKS+=wmemchr.3 wcscat.3 | ||
165 | MLINKS+=wmemchr.3 wcschr.3 | ||
166 | MLINKS+=wmemchr.3 wcscmp.3 | ||
167 | MLINKS+=wmemchr.3 wcscpy.3 | ||
168 | MLINKS+=wmemchr.3 wcscspn.3 | ||
169 | MLINKS+=wmemchr.3 wcslcat.3 | ||
170 | MLINKS+=wmemchr.3 wcslcpy.3 | ||
171 | MLINKS+=wmemchr.3 wcslen.3 | ||
172 | MLINKS+=wmemchr.3 wcsncat.3 | ||
173 | MLINKS+=wmemchr.3 wcsncmp.3 | ||
174 | MLINKS+=wmemchr.3 wcsncpy.3 | ||
175 | MLINKS+=wmemchr.3 wcspbrk.3 | ||
176 | MLINKS+=wmemchr.3 wcsrchr.3 | ||
177 | MLINKS+=wmemchr.3 wcsspn.3 | ||
178 | MLINKS+=wmemchr.3 wcsstr.3 | ||
diff --git a/src/lib/libc/string/bcmp.3 b/src/lib/libc/string/bcmp.3 new file mode 100644 index 0000000000..57e1a0fade --- /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.7 2007/05/31 19:19:32 jmc Exp $ | ||
31 | .\" | ||
32 | .Dd $Mdocdate: May 31 2007 $ | ||
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..fd5c93121e --- /dev/null +++ b/src/lib/libc/string/bcmp.c | |||
@@ -0,0 +1,55 @@ | |||
1 | /* $OpenBSD: bcmp.c,v 1.9 2008/03/19 03:00:23 ray Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1987 Regents of the University of California. | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. 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(_KERNEL) && !defined(_STANDALONE) | ||
33 | #include <string.h> | ||
34 | #else | ||
35 | #include <lib/libkern/libkern.h> | ||
36 | #endif | ||
37 | |||
38 | /* | ||
39 | * bcmp -- vax cmpc3 instruction | ||
40 | */ | ||
41 | int | ||
42 | bcmp(const void *b1, const void *b2, size_t length) | ||
43 | { | ||
44 | char *p1, *p2; | ||
45 | |||
46 | if (length == 0) | ||
47 | return (0); | ||
48 | p1 = (char *)b1; | ||
49 | p2 = (char *)b2; | ||
50 | do | ||
51 | if (*p1++ != *p2++) | ||
52 | return (1); | ||
53 | while (--length); | ||
54 | return (0); | ||
55 | } | ||
diff --git a/src/lib/libc/string/bcopy.3 b/src/lib/libc/string/bcopy.3 new file mode 100644 index 0000000000..3d3c8ee958 --- /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.9 2007/05/31 19:19:32 jmc Exp $ | ||
32 | .\" | ||
33 | .Dd $Mdocdate: May 31 2007 $ | ||
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..4308c6484a --- /dev/null +++ b/src/lib/libc/string/bcopy.c | |||
@@ -0,0 +1,128 @@ | |||
1 | /* $OpenBSD: bcopy.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
4 | * 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 | |||
34 | #include <string.h> | ||
35 | |||
36 | /* | ||
37 | * sizeof(word) MUST BE A POWER OF TWO | ||
38 | * SO THAT wmask BELOW IS ALL ONES | ||
39 | */ | ||
40 | typedef long word; /* "word" used for optimal copy speed */ | ||
41 | |||
42 | #define wsize sizeof(word) | ||
43 | #define wmask (wsize - 1) | ||
44 | |||
45 | /* | ||
46 | * Copy a block of memory, handling overlap. | ||
47 | * This is the routine that actually implements | ||
48 | * (the portable versions of) bcopy, memcpy, and memmove. | ||
49 | */ | ||
50 | #ifdef MEMCOPY | ||
51 | void * | ||
52 | memcpy(void *dst0, const void *src0, size_t length) | ||
53 | #else | ||
54 | #ifdef MEMMOVE | ||
55 | void * | ||
56 | memmove(void *dst0, const void *src0, size_t length) | ||
57 | #else | ||
58 | void | ||
59 | bcopy(const void *src0, void *dst0, size_t length) | ||
60 | #endif | ||
61 | #endif | ||
62 | { | ||
63 | char *dst = dst0; | ||
64 | const char *src = src0; | ||
65 | size_t t; | ||
66 | |||
67 | if (length == 0 || dst == src) /* nothing to do */ | ||
68 | goto done; | ||
69 | |||
70 | /* | ||
71 | * Macros: loop-t-times; and loop-t-times, t>0 | ||
72 | */ | ||
73 | #define TLOOP(s) if (t) TLOOP1(s) | ||
74 | #define TLOOP1(s) do { s; } while (--t) | ||
75 | |||
76 | if ((unsigned long)dst < (unsigned long)src) { | ||
77 | /* | ||
78 | * Copy forward. | ||
79 | */ | ||
80 | t = (long)src; /* only need low bits */ | ||
81 | if ((t | (long)dst) & wmask) { | ||
82 | /* | ||
83 | * Try to align operands. This cannot be done | ||
84 | * unless the low bits match. | ||
85 | */ | ||
86 | if ((t ^ (long)dst) & wmask || length < wsize) | ||
87 | t = length; | ||
88 | else | ||
89 | t = wsize - (t & wmask); | ||
90 | length -= t; | ||
91 | TLOOP1(*dst++ = *src++); | ||
92 | } | ||
93 | /* | ||
94 | * Copy whole words, then mop up any trailing bytes. | ||
95 | */ | ||
96 | t = length / wsize; | ||
97 | TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize); | ||
98 | t = length & wmask; | ||
99 | TLOOP(*dst++ = *src++); | ||
100 | } else { | ||
101 | /* | ||
102 | * Copy backwards. Otherwise essentially the same. | ||
103 | * Alignment works as before, except that it takes | ||
104 | * (t&wmask) bytes to align, not wsize-(t&wmask). | ||
105 | */ | ||
106 | src += length; | ||
107 | dst += length; | ||
108 | t = (long)src; | ||
109 | if ((t | (long)dst) & wmask) { | ||
110 | if ((t ^ (long)dst) & wmask || length <= wsize) | ||
111 | t = length; | ||
112 | else | ||
113 | t &= wmask; | ||
114 | length -= t; | ||
115 | TLOOP1(*--dst = *--src); | ||
116 | } | ||
117 | t = length / wsize; | ||
118 | TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src); | ||
119 | t = length & wmask; | ||
120 | TLOOP(*--dst = *--src); | ||
121 | } | ||
122 | done: | ||
123 | #if defined(MEMCOPY) || defined(MEMMOVE) | ||
124 | return (dst0); | ||
125 | #else | ||
126 | return; | ||
127 | #endif | ||
128 | } | ||
diff --git a/src/lib/libc/string/bm.3 b/src/lib/libc/string/bm.3 new file mode 100644 index 0000000000..749ffc9b74 --- /dev/null +++ b/src/lib/libc/string/bm.3 | |||
@@ -0,0 +1,112 @@ | |||
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.9 2007/05/31 19:19:32 jmc Exp $ | ||
32 | .\" | ||
33 | .Dd $Mdocdate: May 31 2007 $ | ||
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 "const unsigned char *pattern" "size_t patlen" \ | ||
46 | "const unsigned char freq[256]" | ||
47 | .Ft unsigned char * | ||
48 | .Fn bm_exec "bm_pat *pdesc" "unsigned char *text" "size_t len" | ||
49 | .Ft void | ||
50 | .Fn bm_free "bm_pat *pdesc" | ||
51 | .Sh DESCRIPTION | ||
52 | These routines implement an efficient mechanism to find an | ||
53 | occurrence of a byte string within another byte string. | ||
54 | .Pp | ||
55 | .Fn bm_comp | ||
56 | evaluates | ||
57 | .Fa patlen | ||
58 | bytes starting at | ||
59 | .Fa pattern | ||
60 | and returns a pointer to a structure describing them. | ||
61 | The bytes referenced by | ||
62 | .Fa pattern | ||
63 | may be of any value. | ||
64 | .Pp | ||
65 | The search takes advantage of the frequency distribution of the | ||
66 | bytes in the text to be searched. | ||
67 | If specified, | ||
68 | .Ar freq | ||
69 | should be an array of 256 values, | ||
70 | with higher values indicating that the corresponding character occurs | ||
71 | more frequently. | ||
72 | (A less than optimal frequency distribution can only result in less | ||
73 | than optimal performance, not incorrect results.) | ||
74 | If | ||
75 | .Ar freq | ||
76 | is | ||
77 | .Dv NULL , | ||
78 | a system default table is used. | ||
79 | .Pp | ||
80 | .Fn bm_exec | ||
81 | returns a pointer to the leftmost occurrence of the string given to | ||
82 | .Fn bm_comp | ||
83 | within | ||
84 | .Ar text , | ||
85 | or | ||
86 | .Dv NULL | ||
87 | if none occurs. | ||
88 | The number of bytes in | ||
89 | .Ar text | ||
90 | must be specified by | ||
91 | .Ar len . | ||
92 | .Pp | ||
93 | Space allocated for the returned description is discarded | ||
94 | by calling | ||
95 | .Fn bm_free | ||
96 | with the returned description as an argument. | ||
97 | .Pp | ||
98 | The asymptotic speed of | ||
99 | .Fn bm_exec | ||
100 | is | ||
101 | .Pf O Ns Pq len / patlen . | ||
102 | .Sh SEE ALSO | ||
103 | .Xr regexp 3 , | ||
104 | .Xr strstr 3 | ||
105 | .Rs | ||
106 | .%R "Fast String Searching" | ||
107 | .%A Andrew Hume | ||
108 | .%A Daniel Sunday | ||
109 | .%J "Software Practice and Experience" | ||
110 | .%V Volume 21, 11 (November 1991) | ||
111 | .%P 1221-48 | ||
112 | .Re | ||
diff --git a/src/lib/libc/string/bm.c b/src/lib/libc/string/bm.c new file mode 100644 index 0000000000..2c4c6ca720 --- /dev/null +++ b/src/lib/libc/string/bm.c | |||
@@ -0,0 +1,205 @@ | |||
1 | /* $OpenBSD: bm.c,v 1.7 2007/09/02 15:19:18 deraadt Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1994 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * This code is derived from software contributed to Berkeley by | ||
7 | * Andrew Hume of AT&T Bell Laboratories. | ||
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 | |||
34 | #include <sys/types.h> | ||
35 | |||
36 | #include <bm.h> | ||
37 | #include <errno.h> | ||
38 | #include <stdlib.h> | ||
39 | #include <string.h> | ||
40 | |||
41 | /* | ||
42 | * XXX | ||
43 | * The default frequency table starts at 99 and counts down. The default | ||
44 | * table should probably be oriented toward text, and will necessarily be | ||
45 | * locale specific. This one is for English. It was derived from the | ||
46 | * OSF/1 and 4.4BSD formatted and unformatted manual pages, and about 100Mb | ||
47 | * of email and random text. Change it if you can find something better. | ||
48 | */ | ||
49 | static u_char const freq_def[256] = { | ||
50 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
51 | 0, 77, 90, 0, 0, 0, 0, 0, | ||
52 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
53 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
54 | 99, 28, 42, 27, 16, 14, 20, 51, | ||
55 | 66, 65, 59, 24, 75, 76, 84, 56, | ||
56 | 72, 74, 64, 55, 54, 47, 41, 37, | ||
57 | 44, 61, 70, 43, 23, 53, 49, 22, | ||
58 | 33, 58, 40, 46, 45, 57, 60, 26, | ||
59 | 30, 63, 21, 12, 32, 50, 38, 39, | ||
60 | 34, 11, 48, 67, 62, 35, 15, 29, | ||
61 | 71, 18, 9, 17, 25, 13, 10, 52, | ||
62 | 36, 95, 78, 86, 87, 98, 82, 80, | ||
63 | 88, 94, 19, 68, 89, 83, 93, 96, | ||
64 | 81, 7, 91, 92, 97, 85, 69, 73, | ||
65 | 31, 79, 8, 5, 4, 6, 3, 0, | ||
66 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
67 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
68 | 0, 0, 0, 0, 0, 0, 0, 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 | }; | ||
83 | |||
84 | bm_pat * | ||
85 | bm_comp(u_char const *pb, size_t len, u_char const *freq) | ||
86 | { | ||
87 | u_char const *pe, *p; | ||
88 | size_t *d, r; | ||
89 | int j; | ||
90 | int sv_errno; | ||
91 | bm_pat *pat; | ||
92 | |||
93 | if (len == 0) { | ||
94 | errno = EINVAL; | ||
95 | return (NULL); | ||
96 | } | ||
97 | if ((pat = malloc(sizeof(*pat))) == NULL) | ||
98 | return (NULL); | ||
99 | pat->pat = NULL; | ||
100 | pat->delta = NULL; | ||
101 | |||
102 | pat->patlen = len; /* copy pattern */ | ||
103 | if ((pat->pat = malloc(pat->patlen)) == NULL) | ||
104 | goto mem; | ||
105 | memcpy(pat->pat, pb, pat->patlen); | ||
106 | /* get skip delta */ | ||
107 | if ((pat->delta = calloc(256, sizeof(*d))) == NULL) | ||
108 | goto mem; | ||
109 | for (j = 0, d = pat->delta; j < 256; j++) | ||
110 | d[j] = pat->patlen; | ||
111 | for (pe = pb + pat->patlen - 1; pb <= pe; pb++) | ||
112 | d[*pb] = pe - pb; | ||
113 | |||
114 | if (freq == NULL) /* default freq table */ | ||
115 | freq = freq_def; | ||
116 | r = 0; /* get guard */ | ||
117 | for (pb = pat->pat, pe = pb + pat->patlen - 1; pb < pe; pb++) | ||
118 | if (freq[*pb] < freq[pat->pat[r]]) | ||
119 | r = pb - pat->pat; | ||
120 | pat->rarec = pat->pat[r]; | ||
121 | pat->rareoff = r - (pat->patlen - 1); | ||
122 | |||
123 | /* get md2 shift */ | ||
124 | for (pe = pat->pat + pat->patlen - 1, p = pe - 1; p >= pat->pat; p--) | ||
125 | if (*p == *pe) | ||
126 | break; | ||
127 | |||
128 | /* *p is first leftward reoccurrence of *pe */ | ||
129 | pat->md2 = pe - p; | ||
130 | return (pat); | ||
131 | |||
132 | mem: sv_errno = errno; | ||
133 | bm_free(pat); | ||
134 | errno = sv_errno; | ||
135 | return (NULL); | ||
136 | } | ||
137 | |||
138 | void | ||
139 | bm_free(bm_pat *pat) | ||
140 | { | ||
141 | if (pat->pat != NULL) | ||
142 | free(pat->pat); | ||
143 | if (pat->delta != NULL) | ||
144 | free(pat->delta); | ||
145 | free(pat); | ||
146 | } | ||
147 | |||
148 | u_char * | ||
149 | bm_exec(bm_pat *pat, u_char *base, size_t n) | ||
150 | { | ||
151 | u_char *e, *ep, *p, *q, *s; | ||
152 | size_t *d0, k, md2, n1, ro; | ||
153 | int rc; | ||
154 | |||
155 | if (n == 0) | ||
156 | return (NULL); | ||
157 | |||
158 | d0 = pat->delta; | ||
159 | n1 = pat->patlen - 1; | ||
160 | md2 = pat->md2; | ||
161 | ro = pat->rareoff; | ||
162 | rc = pat->rarec; | ||
163 | ep = pat->pat + pat->patlen - 1; | ||
164 | s = base + (pat->patlen - 1); | ||
165 | |||
166 | /* fast loop up to n - 3 * patlen */ | ||
167 | e = base + n - 3 * pat->patlen; | ||
168 | while (s < e) { | ||
169 | k = d0[*s]; /* ufast skip loop */ | ||
170 | while (k) { | ||
171 | k = d0[*(s += k)]; | ||
172 | k = d0[*(s += k)]; | ||
173 | } | ||
174 | if (s >= e) | ||
175 | break; | ||
176 | if (s[ro] != rc) /* guard test */ | ||
177 | goto mismatch1; | ||
178 | /* fwd match */ | ||
179 | for (p = pat->pat, q = s - n1; p < ep;) | ||
180 | if (*q++ != *p++) | ||
181 | goto mismatch1; | ||
182 | return (s - n1); | ||
183 | |||
184 | mismatch1: s += md2; /* md2 shift */ | ||
185 | } | ||
186 | |||
187 | /* slow loop up to end */ | ||
188 | e = base + n; | ||
189 | while (s < e) { | ||
190 | s += d0[*s]; /* step */ | ||
191 | if (s >= e) | ||
192 | break; | ||
193 | if (s[ro] != rc) /* guard test */ | ||
194 | goto mismatch2; | ||
195 | /* fwd match */ | ||
196 | for (p = pat->pat, q = s - n1; p <= ep;) | ||
197 | if (*q++ != *p++) | ||
198 | goto mismatch2; | ||
199 | return (s - n1); | ||
200 | |||
201 | mismatch2: s += md2; /* md2 shift */ | ||
202 | } | ||
203 | |||
204 | return (NULL); | ||
205 | } | ||
diff --git a/src/lib/libc/string/bstring.3 b/src/lib/libc/string/bstring.3 new file mode 100644 index 0000000000..cd16ab022d --- /dev/null +++ b/src/lib/libc/string/bstring.3 | |||
@@ -0,0 +1,98 @@ | |||
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.8 2007/05/31 19:19:32 jmc Exp $ | ||
31 | .\" | ||
32 | .Dd $Mdocdate: May 31 2007 $ | ||
33 | .Dt BSTRING 3 | ||
34 | .Os | ||
35 | .Sh NAME | ||
36 | .Nm bstring | ||
37 | .Nd byte string operations | ||
38 | .Sh SYNOPSIS | ||
39 | .Fd #include <string.h> | ||
40 | .Ft int | ||
41 | .Fn bcmp "const void *b1" "const void *b2" "size_t len" | ||
42 | .Ft void | ||
43 | .Fn bcopy "const void *src" "void *dst" "size_t len" | ||
44 | .Ft void | ||
45 | .Fn bzero "void *b" "size_t len" | ||
46 | .Ft void * | ||
47 | .Fn memchr "const void *b" "int c" "size_t len" | ||
48 | .Ft int | ||
49 | .Fn memcmp "const void *b1" "const void *b2" "size_t len" | ||
50 | .Ft void * | ||
51 | .Fn memccpy "void *dst" "const void *src" "int c" "size_t len" | ||
52 | .Ft void * | ||
53 | .Fn memcpy "void *dst" "const void *src" "size_t len" | ||
54 | .Ft void * | ||
55 | .Fn memmove "void *dst" "const void *src" "size_t len" | ||
56 | .Ft void * | ||
57 | .Fn memset "void *b" "int c" "size_t len" | ||
58 | .Sh DESCRIPTION | ||
59 | These functions operate on variable length strings of bytes. | ||
60 | They do not check for terminating NUL bytes as the routines | ||
61 | listed in | ||
62 | .Xr string 3 | ||
63 | do. | ||
64 | .Pp | ||
65 | See the specific manual pages for more information. | ||
66 | .Sh SEE ALSO | ||
67 | .Xr bcmp 3 , | ||
68 | .Xr bcopy 3 , | ||
69 | .Xr bzero 3 , | ||
70 | .Xr memccpy 3 , | ||
71 | .Xr memchr 3 , | ||
72 | .Xr memcmp 3 , | ||
73 | .Xr memcpy 3 , | ||
74 | .Xr memmove 3 , | ||
75 | .Xr memset 3 | ||
76 | .Sh STANDARDS | ||
77 | The functions | ||
78 | .Fn memchr , | ||
79 | .Fn memcmp , | ||
80 | .Fn memcpy , | ||
81 | .Fn memmove , | ||
82 | and | ||
83 | .Fn memset | ||
84 | conform to | ||
85 | .St -ansiC . | ||
86 | .Sh HISTORY | ||
87 | The | ||
88 | .Fn bzero | ||
89 | and | ||
90 | .Fn memccpy | ||
91 | functions appeared in | ||
92 | .Bx 4.3 . | ||
93 | The | ||
94 | .Fn bcmp | ||
95 | and | ||
96 | .Fn bcopy | ||
97 | functions appeared in | ||
98 | .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..c5ca32f4d2 --- /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.7 2007/05/31 19:19:32 jmc Exp $ | ||
31 | .\" | ||
32 | .Dd $Mdocdate: May 31 2007 $ | ||
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..4d267d4f40 --- /dev/null +++ b/src/lib/libc/string/bzero.c | |||
@@ -0,0 +1,48 @@ | |||
1 | /* $OpenBSD: bzero.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1987 Regents of the University of California. | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. 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(_KERNEL) && !defined(_STANDALONE) | ||
33 | #include <string.h> | ||
34 | #else | ||
35 | #include <lib/libkern/libkern.h> | ||
36 | #endif | ||
37 | |||
38 | /* | ||
39 | * bzero -- vax movc5 instruction | ||
40 | */ | ||
41 | void | ||
42 | bzero(void *b, size_t length) | ||
43 | { | ||
44 | char *p; | ||
45 | |||
46 | for (p = b; length--;) | ||
47 | *p++ = '\0'; | ||
48 | } | ||
diff --git a/src/lib/libc/string/ffs.3 b/src/lib/libc/string/ffs.3 new file mode 100644 index 0000000000..27b26969e5 --- /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.5 2007/05/31 19:19:32 jmc Exp $ | ||
31 | .\" | ||
32 | .Dd $Mdocdate: May 31 2007 $ | ||
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..7dec1613a8 --- /dev/null +++ b/src/lib/libc/string/ffs.c | |||
@@ -0,0 +1,44 @@ | |||
1 | /* $OpenBSD: ffs.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Public domain. | ||
5 | * Written by Dale Rahn. | ||
6 | */ | ||
7 | |||
8 | #if !defined(_KERNEL) && !defined(_STANDALONE) | ||
9 | #include <string.h> | ||
10 | #else | ||
11 | #include <lib/libkern/libkern.h> | ||
12 | #endif | ||
13 | |||
14 | /* | ||
15 | * ffs -- vax ffs instruction | ||
16 | */ | ||
17 | int | ||
18 | ffs(int mask) | ||
19 | { | ||
20 | int bit; | ||
21 | unsigned int r = mask; | ||
22 | static const signed char t[16] = { | ||
23 | -28, 1, 2, 1, | ||
24 | 3, 1, 2, 1, | ||
25 | 4, 1, 2, 1, | ||
26 | 3, 1, 2, 1 | ||
27 | }; | ||
28 | |||
29 | bit = 0; | ||
30 | if (!(r & 0xffff)) { | ||
31 | bit += 16; | ||
32 | r >>= 16; | ||
33 | } | ||
34 | if (!(r & 0xff)) { | ||
35 | bit += 8; | ||
36 | r >>= 8; | ||
37 | } | ||
38 | if (!(r & 0xf)) { | ||
39 | bit += 4; | ||
40 | r >>= 4; | ||
41 | } | ||
42 | |||
43 | return (bit + t[ r & 0xf ]); | ||
44 | } | ||
diff --git a/src/lib/libc/string/index.c b/src/lib/libc/string/index.c new file mode 100644 index 0000000000..50e9ca35ab --- /dev/null +++ b/src/lib/libc/string/index.c | |||
@@ -0,0 +1,47 @@ | |||
1 | /* $OpenBSD: index.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. 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 | |||
31 | #include <string.h> | ||
32 | |||
33 | char * | ||
34 | #ifdef STRCHR | ||
35 | strchr(const char *p, int ch) | ||
36 | #else | ||
37 | index(const char *p, int ch) | ||
38 | #endif | ||
39 | { | ||
40 | for (;; ++p) { | ||
41 | if (*p == ch) | ||
42 | return((char *)p); | ||
43 | if (!*p) | ||
44 | return((char *)NULL); | ||
45 | } | ||
46 | /* NOTREACHED */ | ||
47 | } | ||
diff --git a/src/lib/libc/string/memccpy.3 b/src/lib/libc/string/memccpy.3 new file mode 100644 index 0000000000..ef53d95645 --- /dev/null +++ b/src/lib/libc/string/memccpy.3 | |||
@@ -0,0 +1,73 @@ | |||
1 | .\" $OpenBSD: memccpy.3,v 1.9 2007/05/31 19:19:32 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 | .\" @(#)memccpy.3 8.1 (Berkeley) 6/9/93 | ||
31 | .\" | ||
32 | .Dd $Mdocdate: May 31 2007 $ | ||
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..485c55fcab --- /dev/null +++ b/src/lib/libc/string/memccpy.c | |||
@@ -0,0 +1,48 @@ | |||
1 | /* $OpenBSD: memccpy.c,v 1.6 2005/08/08 08:05:37 espie 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 | |||
34 | void * | ||
35 | memccpy(void *t, const void *f, int c, size_t n) | ||
36 | { | ||
37 | |||
38 | if (n) { | ||
39 | unsigned char *tp = t; | ||
40 | const unsigned char *fp = f; | ||
41 | unsigned char uc = c; | ||
42 | do { | ||
43 | if ((*tp++ = *fp++) == uc) | ||
44 | return (tp); | ||
45 | } while (--n != 0); | ||
46 | } | ||
47 | return (0); | ||
48 | } | ||
diff --git a/src/lib/libc/string/memchr.3 b/src/lib/libc/string/memchr.3 new file mode 100644 index 0000000000..558848ac23 --- /dev/null +++ b/src/lib/libc/string/memchr.3 | |||
@@ -0,0 +1,93 @@ | |||
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.9 2007/09/03 16:50:14 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: September 3 2007 $ | ||
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 | .Ft void * | ||
45 | .Fn memrchr "const void *b" "int c" "size_t len" | ||
46 | .Sh DESCRIPTION | ||
47 | The | ||
48 | .Fn memchr | ||
49 | function locates the first occurrence of | ||
50 | .Fa c | ||
51 | (converted to an | ||
52 | .Li unsigned char ) | ||
53 | in string | ||
54 | .Fa b . | ||
55 | .Pp | ||
56 | The | ||
57 | .Fn memrchr | ||
58 | function behaves like | ||
59 | .Fn memchr , | ||
60 | except that it locates the last occurrence of | ||
61 | .Fa c | ||
62 | in string | ||
63 | .Fa b . | ||
64 | .Sh RETURN VALUES | ||
65 | The | ||
66 | .Fn memchr | ||
67 | and | ||
68 | .Fn memrchr | ||
69 | functions return a pointer to the byte located, or | ||
70 | .Dv NULL | ||
71 | if no such byte exists within | ||
72 | .Fa len | ||
73 | bytes. | ||
74 | .Sh SEE ALSO | ||
75 | .Xr strchr 3 , | ||
76 | .Xr strcspn 3 , | ||
77 | .Xr strpbrk 3 , | ||
78 | .Xr strrchr 3 , | ||
79 | .Xr strsep 3 , | ||
80 | .Xr strspn 3 , | ||
81 | .Xr strstr 3 , | ||
82 | .Xr strtok 3 | ||
83 | .Sh STANDARDS | ||
84 | The | ||
85 | .Fn memchr | ||
86 | function conforms to | ||
87 | .St -ansiC . | ||
88 | .Pp | ||
89 | The | ||
90 | .Fn memrchr | ||
91 | function is an | ||
92 | .Ox | ||
93 | extension. | ||
diff --git a/src/lib/libc/string/memchr.c b/src/lib/libc/string/memchr.c new file mode 100644 index 0000000000..4573e3ceb1 --- /dev/null +++ b/src/lib/libc/string/memchr.c | |||
@@ -0,0 +1,48 @@ | |||
1 | /* $OpenBSD: memchr.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
4 | * 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 | |||
34 | #include <string.h> | ||
35 | |||
36 | void * | ||
37 | memchr(const void *s, int c, size_t n) | ||
38 | { | ||
39 | if (n != 0) { | ||
40 | const unsigned char *p = s; | ||
41 | |||
42 | do { | ||
43 | if (*p++ == (unsigned char)c) | ||
44 | return ((void *)(p - 1)); | ||
45 | } while (--n != 0); | ||
46 | } | ||
47 | return (NULL); | ||
48 | } | ||
diff --git a/src/lib/libc/string/memcmp.3 b/src/lib/libc/string/memcmp.3 new file mode 100644 index 0000000000..8c35f388f2 --- /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.6 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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..49384a6fb9 --- /dev/null +++ b/src/lib/libc/string/memcmp.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* $OpenBSD: memcmp.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
4 | * 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 | |||
34 | #include <string.h> | ||
35 | |||
36 | /* | ||
37 | * Compare memory regions. | ||
38 | */ | ||
39 | int | ||
40 | memcmp(const void *s1, const void *s2, size_t n) | ||
41 | { | ||
42 | if (n != 0) { | ||
43 | const unsigned char *p1 = s1, *p2 = s2; | ||
44 | |||
45 | do { | ||
46 | if (*p1++ != *p2++) | ||
47 | return (*--p1 - *--p2); | ||
48 | } while (--n != 0); | ||
49 | } | ||
50 | return (0); | ||
51 | } | ||
diff --git a/src/lib/libc/string/memcpy.3 b/src/lib/libc/string/memcpy.3 new file mode 100644 index 0000000000..1deb03631b --- /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.7 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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..c6e3912669 --- /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.7 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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/memrchr.c b/src/lib/libc/string/memrchr.c new file mode 100644 index 0000000000..bd27ebc620 --- /dev/null +++ b/src/lib/libc/string/memrchr.c | |||
@@ -0,0 +1,38 @@ | |||
1 | /* $OpenBSD: memrchr.c,v 1.2 2007/11/27 16:22:12 martynas Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2007 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 | #include <string.h> | ||
20 | |||
21 | /* | ||
22 | * Reverse memchr() | ||
23 | * Find the last occurrence of 'c' in the buffer 's' of size 'n'. | ||
24 | */ | ||
25 | void * | ||
26 | memrchr(const void *s, int c, size_t n) | ||
27 | { | ||
28 | const unsigned char *cp; | ||
29 | |||
30 | if (n != 0) { | ||
31 | cp = (unsigned char *)s + n; | ||
32 | do { | ||
33 | if (*(--cp) == (unsigned char)c) | ||
34 | return((void *)cp); | ||
35 | } while (--n != 0); | ||
36 | } | ||
37 | return(NULL); | ||
38 | } | ||
diff --git a/src/lib/libc/string/memset.3 b/src/lib/libc/string/memset.3 new file mode 100644 index 0000000000..7799b5e2d2 --- /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.6 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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..61709c139d --- /dev/null +++ b/src/lib/libc/string/memset.c | |||
@@ -0,0 +1,47 @@ | |||
1 | /* $OpenBSD: memset.c,v 1.6 2008/03/15 21:40:39 ray Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
4 | * 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 | |||
34 | #include <string.h> | ||
35 | |||
36 | void * | ||
37 | memset(void *dst, int c, size_t n) | ||
38 | { | ||
39 | if (n != 0) { | ||
40 | unsigned char *d = dst; | ||
41 | |||
42 | do | ||
43 | *d++ = (unsigned char)c; | ||
44 | while (--n != 0); | ||
45 | } | ||
46 | return (dst); | ||
47 | } | ||
diff --git a/src/lib/libc/string/rindex.c b/src/lib/libc/string/rindex.c new file mode 100644 index 0000000000..bf9d6f7cf1 --- /dev/null +++ b/src/lib/libc/string/rindex.c | |||
@@ -0,0 +1,49 @@ | |||
1 | /* $OpenBSD: rindex.c,v 1.6 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1988 Regents of the University of California. | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. 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 | |||
31 | #include <string.h> | ||
32 | |||
33 | char * | ||
34 | #ifdef STRRCHR | ||
35 | strrchr(const char *p, int ch) | ||
36 | #else | ||
37 | rindex(const char *p, int ch) | ||
38 | #endif | ||
39 | { | ||
40 | char *save; | ||
41 | |||
42 | for (save = NULL;; ++p) { | ||
43 | if (*p == ch) | ||
44 | save = (char *)p; | ||
45 | if (!*p) | ||
46 | return(save); | ||
47 | } | ||
48 | /* NOTREACHED */ | ||
49 | } | ||
diff --git a/src/lib/libc/string/strcasecmp.3 b/src/lib/libc/string/strcasecmp.3 new file mode 100644 index 0000000000..c17aa694d1 --- /dev/null +++ b/src/lib/libc/string/strcasecmp.3 | |||
@@ -0,0 +1,84 @@ | |||
1 | .\" $OpenBSD: strcasecmp.3,v 1.10 2007/05/31 19:19:32 jmc 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 $Mdocdate: May 31 2007 $ | ||
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 NUL-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..2be09136c0 --- /dev/null +++ b/src/lib/libc/string/strcasecmp.c | |||
@@ -0,0 +1,105 @@ | |||
1 | /* $OpenBSD: strcasecmp.c,v 1.6 2005/08/08 08:05:37 espie 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 | typedef unsigned char u_char; | ||
35 | |||
36 | /* | ||
37 | * This array is designed for mapping upper and lower case letter | ||
38 | * together for a case independent comparison. The mappings are | ||
39 | * based upon ascii character sequences. | ||
40 | */ | ||
41 | static const u_char charmap[] = { | ||
42 | '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007', | ||
43 | '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017', | ||
44 | '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027', | ||
45 | '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037', | ||
46 | '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047', | ||
47 | '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057', | ||
48 | '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067', | ||
49 | '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077', | ||
50 | '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147', | ||
51 | '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157', | ||
52 | '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167', | ||
53 | '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137', | ||
54 | '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147', | ||
55 | '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157', | ||
56 | '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167', | ||
57 | '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177', | ||
58 | '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207', | ||
59 | '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217', | ||
60 | '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227', | ||
61 | '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237', | ||
62 | '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247', | ||
63 | '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257', | ||
64 | '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267', | ||
65 | '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277', | ||
66 | '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307', | ||
67 | '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317', | ||
68 | '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327', | ||
69 | '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337', | ||
70 | '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347', | ||
71 | '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357', | ||
72 | '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367', | ||
73 | '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377', | ||
74 | }; | ||
75 | |||
76 | int | ||
77 | strcasecmp(const char *s1, const char *s2) | ||
78 | { | ||
79 | const u_char *cm = charmap; | ||
80 | const u_char *us1 = (const u_char *)s1; | ||
81 | const u_char *us2 = (const u_char *)s2; | ||
82 | |||
83 | while (cm[*us1] == cm[*us2++]) | ||
84 | if (*us1++ == '\0') | ||
85 | return (0); | ||
86 | return (cm[*us1] - cm[*--us2]); | ||
87 | } | ||
88 | |||
89 | int | ||
90 | strncasecmp(const char *s1, const char *s2, size_t n) | ||
91 | { | ||
92 | if (n != 0) { | ||
93 | const u_char *cm = charmap; | ||
94 | const u_char *us1 = (const u_char *)s1; | ||
95 | const u_char *us2 = (const u_char *)s2; | ||
96 | |||
97 | do { | ||
98 | if (cm[*us1] != cm[*us2++]) | ||
99 | return (cm[*us1] - cm[*--us2]); | ||
100 | if (*us1++ == '\0') | ||
101 | break; | ||
102 | } while (--n != 0); | ||
103 | } | ||
104 | return (0); | ||
105 | } | ||
diff --git a/src/lib/libc/string/strcasestr.c b/src/lib/libc/string/strcasestr.c new file mode 100644 index 0000000000..aa74c0176d --- /dev/null +++ b/src/lib/libc/string/strcasestr.c | |||
@@ -0,0 +1,60 @@ | |||
1 | /* $OpenBSD: strcasestr.c,v 1.3 2006/03/31 05:34:55 deraadt Exp $ */ | ||
2 | /* $NetBSD: strcasestr.c,v 1.2 2005/02/09 21:35:47 kleink Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c) 1990, 1993 | ||
6 | * The Regents of the University of California. All rights reserved. | ||
7 | * | ||
8 | * This code is derived from software contributed to Berkeley by | ||
9 | * Chris Torek. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * 3. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #include <ctype.h> | ||
37 | #include <string.h> | ||
38 | |||
39 | /* | ||
40 | * Find the first occurrence of find in s, ignore case. | ||
41 | */ | ||
42 | char * | ||
43 | strcasestr(const char *s, const char *find) | ||
44 | { | ||
45 | char c, sc; | ||
46 | size_t len; | ||
47 | |||
48 | if ((c = *find++) != 0) { | ||
49 | c = (char)tolower((unsigned char)c); | ||
50 | len = strlen(find); | ||
51 | do { | ||
52 | do { | ||
53 | if ((sc = *s++) == 0) | ||
54 | return (NULL); | ||
55 | } while ((char)tolower((unsigned char)sc) != c); | ||
56 | } while (strncasecmp(s, find, len) != 0); | ||
57 | s--; | ||
58 | } | ||
59 | return ((char *)s); | ||
60 | } | ||
diff --git a/src/lib/libc/string/strcat.3 b/src/lib/libc/string/strcat.3 new file mode 100644 index 0000000000..4068db752c --- /dev/null +++ b/src/lib/libc/string/strcat.3 | |||
@@ -0,0 +1,133 @@ | |||
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.12 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 NUL-terminated string | ||
53 | .Fa append | ||
54 | to the end of the NUL-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 | .Va 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 | .Va input | ||
103 | to | ||
104 | .Va buf | ||
105 | as will fit. | ||
106 | It then appends as many characters from | ||
107 | .Va suffix | ||
108 | as will fit (or none | ||
109 | if there is no space). | ||
110 | For operations like this, the | ||
111 | .Xr strlcpy 3 | ||
112 | and | ||
113 | .Xr strlcat 3 | ||
114 | functions are a better choice, as shown below. | ||
115 | .Bd -literal -offset indent | ||
116 | (void)strlcpy(buf, input, sizeof(buf)); | ||
117 | (void)strlcat(buf, suffix, sizeof(buf)); | ||
118 | .Ed | ||
119 | .Sh SEE ALSO | ||
120 | .Xr bcopy 3 , | ||
121 | .Xr memccpy 3 , | ||
122 | .Xr memcpy 3 , | ||
123 | .Xr memmove 3 , | ||
124 | .Xr strcpy 3 , | ||
125 | .Xr strlcat 3 , | ||
126 | .Xr strlcpy 3 | ||
127 | .Sh STANDARDS | ||
128 | The | ||
129 | .Fn strcat | ||
130 | and | ||
131 | .Fn strncat | ||
132 | functions conform to | ||
133 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strcat.c b/src/lib/libc/string/strcat.c new file mode 100644 index 0000000000..7cea5229fb --- /dev/null +++ b/src/lib/libc/string/strcat.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* $OpenBSD: strcat.c,v 1.8 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1988 Regents of the University of California. | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. 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(_KERNEL) && !defined(_STANDALONE) | ||
33 | #include <string.h> | ||
34 | #else | ||
35 | #include <lib/libkern/libkern.h> | ||
36 | #endif | ||
37 | |||
38 | #if defined(APIWARN) | ||
39 | __warn_references(strcat, | ||
40 | "warning: strcat() is almost always misused, please use strlcat()"); | ||
41 | #endif | ||
42 | |||
43 | char * | ||
44 | strcat(char *s, const char *append) | ||
45 | { | ||
46 | char *save = s; | ||
47 | |||
48 | for (; *s; ++s); | ||
49 | while ((*s++ = *append++) != '\0'); | ||
50 | return(save); | ||
51 | } | ||
diff --git a/src/lib/libc/string/strchr.3 b/src/lib/libc/string/strchr.3 new file mode 100644 index 0000000000..6a9de9604e --- /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.9 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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..73cd5b40d8 --- /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.9 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 NUL-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..816fd111ac --- /dev/null +++ b/src/lib/libc/string/strcmp.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* $OpenBSD: strcmp.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1990 The Regents of the University of California. | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * This code is derived from software contributed to Berkeley by | ||
8 | * Chris Torek. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * 3. Neither the name of the University nor the names of its contributors | ||
19 | * may be used to endorse or promote products derived from this software | ||
20 | * without specific prior written permission. | ||
21 | * | ||
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | * SUCH DAMAGE. | ||
33 | */ | ||
34 | |||
35 | #if !defined(_KERNEL) && !defined(_STANDALONE) | ||
36 | #include <string.h> | ||
37 | #else | ||
38 | #include <lib/libkern/libkern.h> | ||
39 | #endif | ||
40 | |||
41 | /* | ||
42 | * Compare strings. | ||
43 | */ | ||
44 | int | ||
45 | strcmp(const char *s1, const char *s2) | ||
46 | { | ||
47 | while (*s1 == *s2++) | ||
48 | if (*s1++ == 0) | ||
49 | return (0); | ||
50 | return (*(unsigned char *)s1 - *(unsigned char *)--s2); | ||
51 | } | ||
diff --git a/src/lib/libc/string/strcoll.3 b/src/lib/libc/string/strcoll.3 new file mode 100644 index 0000000000..ea7452a749 --- /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.7 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 NUL-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..2df983bd65 --- /dev/null +++ b/src/lib/libc/string/strcoll.c | |||
@@ -0,0 +1,44 @@ | |||
1 | /* $OpenBSD: strcoll.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
4 | * 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 | |||
34 | #include <string.h> | ||
35 | |||
36 | /* | ||
37 | * Compare strings according to LC_COLLATE category of current locale. | ||
38 | */ | ||
39 | int | ||
40 | strcoll(const char *s1, const char *s2) | ||
41 | { | ||
42 | /* LC_COLLATE is unimplemented, hence always "C" */ | ||
43 | return (strcmp(s1, s2)); | ||
44 | } | ||
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3 new file mode 100644 index 0000000000..428e474d43 --- /dev/null +++ b/src/lib/libc/string/strcpy.3 | |||
@@ -0,0 +1,151 @@ | |||
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.15 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 | NUL terminate | ||
102 | .Va chararray | ||
103 | because the length of the source string is greater than or equal to the | ||
104 | length parameter. | ||
105 | .Fn strncpy | ||
106 | .Em only | ||
107 | NUL terminates the destination string when the length of the source | ||
108 | string is less than the length parameter. | ||
109 | .Bd -literal -offset indent | ||
110 | (void)strncpy(chararray, "abcdefgh", 6); | ||
111 | .Ed | ||
112 | .Pp | ||
113 | The following copies as many characters from | ||
114 | .Va input | ||
115 | to | ||
116 | .Va buf | ||
117 | as will fit and NUL terminates the result. | ||
118 | Because | ||
119 | .Fn strncpy | ||
120 | does | ||
121 | .Em not | ||
122 | guarantee to NUL terminate the string itself, it must be done by hand. | ||
123 | .Bd -literal -offset indent | ||
124 | char buf[BUFSIZ]; | ||
125 | |||
126 | (void)strncpy(buf, input, sizeof(buf) - 1); | ||
127 | buf[sizeof(buf) - 1] = '\e0'; | ||
128 | .Ed | ||
129 | .Pp | ||
130 | Note that | ||
131 | .Xr strlcpy 3 | ||
132 | is a better choice for this kind of operation. | ||
133 | The equivalent using | ||
134 | .Xr strlcpy 3 | ||
135 | is simply: | ||
136 | .Bd -literal -offset indent | ||
137 | (void)strlcpy(buf, input, sizeof(buf)); | ||
138 | .Ed | ||
139 | .Sh SEE ALSO | ||
140 | .Xr bcopy 3 , | ||
141 | .Xr memccpy 3 , | ||
142 | .Xr memcpy 3 , | ||
143 | .Xr memmove 3 , | ||
144 | .Xr strlcpy 3 | ||
145 | .Sh STANDARDS | ||
146 | The | ||
147 | .Fn strcpy | ||
148 | and | ||
149 | .Fn strncpy | ||
150 | functions conform to | ||
151 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strcpy.c b/src/lib/libc/string/strcpy.c new file mode 100644 index 0000000000..71d90d4100 --- /dev/null +++ b/src/lib/libc/string/strcpy.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* $OpenBSD: strcpy.c,v 1.8 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1988 Regents of the University of California. | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. 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(_KERNEL) && !defined(_STANDALONE) | ||
33 | #include <string.h> | ||
34 | #else | ||
35 | #include <lib/libkern/libkern.h> | ||
36 | #endif | ||
37 | |||
38 | #if defined(APIWARN) | ||
39 | __warn_references(strcpy, | ||
40 | "warning: strcpy() is almost always misused, please use strlcpy()"); | ||
41 | #endif | ||
42 | |||
43 | char * | ||
44 | strcpy(char *to, const char *from) | ||
45 | { | ||
46 | char *save = to; | ||
47 | |||
48 | for (; (*to = *from) != '\0'; ++from, ++to); | ||
49 | return(save); | ||
50 | } | ||
diff --git a/src/lib/libc/string/strcspn.3 b/src/lib/libc/string/strcspn.3 new file mode 100644 index 0000000000..21c727344d --- /dev/null +++ b/src/lib/libc/string/strcspn.3 | |||
@@ -0,0 +1,100 @@ | |||
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.9 2007/08/08 07:20:45 ray Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: August 8 2007 $ | ||
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 NUL-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 | .Pp | ||
76 | The following removes the first (if any) newline character from string | ||
77 | .Fa line . | ||
78 | This is useful for trimming the newline after a | ||
79 | .Xr fgets 3 | ||
80 | call. | ||
81 | .Bd -literal -offset indent | ||
82 | char line[BUFSIZ]; | ||
83 | |||
84 | if (fgets(line, sizeof(line), fp) != NULL) | ||
85 | line[strcspn(line, "\en")] = '\e0'; | ||
86 | .Ed | ||
87 | .Sh SEE ALSO | ||
88 | .Xr memchr 3 , | ||
89 | .Xr strchr 3 , | ||
90 | .Xr strpbrk 3 , | ||
91 | .Xr strrchr 3 , | ||
92 | .Xr strsep 3 , | ||
93 | .Xr strspn 3 , | ||
94 | .Xr strstr 3 , | ||
95 | .Xr strtok 3 | ||
96 | .Sh STANDARDS | ||
97 | The | ||
98 | .Fn strcspn | ||
99 | function conforms to | ||
100 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strcspn.c b/src/lib/libc/string/strcspn.c new file mode 100644 index 0000000000..1eb233614d --- /dev/null +++ b/src/lib/libc/string/strcspn.c | |||
@@ -0,0 +1,58 @@ | |||
1 | /* $OpenBSD: strcspn.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
4 | * 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 | |||
34 | #include <string.h> | ||
35 | |||
36 | /* | ||
37 | * Span the complement of string s2. | ||
38 | */ | ||
39 | size_t | ||
40 | strcspn(const char *s1, const char *s2) | ||
41 | { | ||
42 | const char *p, *spanp; | ||
43 | char c, sc; | ||
44 | |||
45 | /* | ||
46 | * Stop as soon as we find any character from s2. Note that there | ||
47 | * must be a NUL in s2; it suffices to stop when we find that, too. | ||
48 | */ | ||
49 | for (p = s1;;) { | ||
50 | c = *p++; | ||
51 | spanp = s2; | ||
52 | do { | ||
53 | if ((sc = *spanp++) == c) | ||
54 | return (p - 1 - s1); | ||
55 | } while (sc != 0); | ||
56 | } | ||
57 | /* NOTREACHED */ | ||
58 | } | ||
diff --git a/src/lib/libc/string/strdup.3 b/src/lib/libc/string/strdup.3 new file mode 100644 index 0000000000..5ef38e38f3 --- /dev/null +++ b/src/lib/libc/string/strdup.3 | |||
@@ -0,0 +1,84 @@ | |||
1 | .\" $OpenBSD: strdup.3,v 1.14 2007/05/31 19:19:32 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 | .\" @(#)strdup.3 8.1 (Berkeley) 6/9/93 | ||
31 | .\" | ||
32 | .Dd $Mdocdate: May 31 2007 $ | ||
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 NUL-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..a6aa1e03b0 --- /dev/null +++ b/src/lib/libc/string/strdup.c | |||
@@ -0,0 +1,49 @@ | |||
1 | /* $OpenBSD: strdup.c,v 1.6 2005/08/08 08:05:37 espie 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 | #include <sys/types.h> | ||
33 | |||
34 | #include <stddef.h> | ||
35 | #include <stdlib.h> | ||
36 | #include <string.h> | ||
37 | |||
38 | char * | ||
39 | strdup(const char *str) | ||
40 | { | ||
41 | size_t siz; | ||
42 | char *copy; | ||
43 | |||
44 | siz = strlen(str) + 1; | ||
45 | if ((copy = malloc(siz)) == NULL) | ||
46 | return(NULL); | ||
47 | (void)memcpy(copy, str, siz); | ||
48 | return(copy); | ||
49 | } | ||
diff --git a/src/lib/libc/string/strerror.3 b/src/lib/libc/string/strerror.3 new file mode 100644 index 0000000000..84c64c10bb --- /dev/null +++ b/src/lib/libc/string/strerror.3 | |||
@@ -0,0 +1,113 @@ | |||
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.9 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 | and | ||
50 | .Fn strerror_r | ||
51 | functions map the error number | ||
52 | .Fa errnum | ||
53 | to a language-dependent error message string. | ||
54 | .Pp | ||
55 | .Fn strerror | ||
56 | returns a string containing a maximum of | ||
57 | .Dv NL_TEXTMAX | ||
58 | characters, including the trailing NUL. | ||
59 | This string is not to be modified by the calling program, | ||
60 | but may be overwritten by subsequent calls to | ||
61 | .Fn strerror . | ||
62 | .Pp | ||
63 | .Fn strerror_r | ||
64 | is a thread safe version of | ||
65 | .Fn strerror | ||
66 | that places the error message in the specified buffer | ||
67 | .Fa strerrbuf . | ||
68 | .Sh RETURN VALUES | ||
69 | .Fn strerror | ||
70 | returns a pointer to the error message string. | ||
71 | If an error occurs, the error code is stored in | ||
72 | .Va errno . | ||
73 | .Pp | ||
74 | .Fn strerror_r | ||
75 | returns zero upon successful completion. | ||
76 | If an error occurs, the error code is stored in | ||
77 | .Va errno | ||
78 | and the error code is returned. | ||
79 | .Sh ERRORS | ||
80 | .Fn strerror | ||
81 | and | ||
82 | .Fn strerror_r | ||
83 | may fail if: | ||
84 | .Bl -tag -width Er | ||
85 | .It Bq Er EINVAL | ||
86 | .Fa errnum | ||
87 | is not a valid error number. | ||
88 | The returned error string will consist of an error message that includes | ||
89 | .Fa errnum . | ||
90 | .El | ||
91 | .Pp | ||
92 | .Fn strerror_r | ||
93 | may fail if: | ||
94 | .Bl -tag -width Er | ||
95 | .It Bq Er ERANGE | ||
96 | The error message is larger than | ||
97 | .Fa buflen | ||
98 | characters. | ||
99 | The message will be truncated to fit. | ||
100 | .El | ||
101 | .Sh SEE ALSO | ||
102 | .Xr intro 2 , | ||
103 | .Xr perror 3 , | ||
104 | .Xr setlocale 3 | ||
105 | .Sh STANDARDS | ||
106 | The | ||
107 | .Fn strerror | ||
108 | function conforms to | ||
109 | .St -ansiC . | ||
110 | The | ||
111 | .Fn strerror_r | ||
112 | function conforms to | ||
113 | .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..13996f08e9 --- /dev/null +++ b/src/lib/libc/string/strerror.c | |||
@@ -0,0 +1,41 @@ | |||
1 | /* $OpenBSD: strerror.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1988 Regents of the University of California. | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. 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 | |||
31 | #include <string.h> | ||
32 | #include <limits.h> | ||
33 | |||
34 | char * | ||
35 | strerror(int num) | ||
36 | { | ||
37 | static char buf[NL_TEXTMAX]; | ||
38 | |||
39 | (void)strerror_r(num, buf, sizeof(buf)); | ||
40 | return (buf); | ||
41 | } | ||
diff --git a/src/lib/libc/string/strerror_r.c b/src/lib/libc/string/strerror_r.c new file mode 100644 index 0000000000..aa63408ab8 --- /dev/null +++ b/src/lib/libc/string/strerror_r.c | |||
@@ -0,0 +1,137 @@ | |||
1 | /* $OpenBSD: strerror_r.c,v 1.7 2007/03/01 16:29:09 bluhm Exp $ */ | ||
2 | /* Public Domain <marc@snafu.org> */ | ||
3 | |||
4 | #ifdef NLS | ||
5 | #define catclose _catclose | ||
6 | #define catgets _catgets | ||
7 | #define catopen _catopen | ||
8 | #include <nl_types.h> | ||
9 | #endif | ||
10 | |||
11 | #define sys_errlist _sys_errlist | ||
12 | #define sys_nerr _sys_nerr | ||
13 | #define sys_siglist _sys_siglist | ||
14 | |||
15 | #include <errno.h> | ||
16 | #include <limits.h> | ||
17 | #include <signal.h> | ||
18 | #include <string.h> | ||
19 | |||
20 | static size_t | ||
21 | __digits10(unsigned int num) | ||
22 | { | ||
23 | size_t i = 0; | ||
24 | |||
25 | do { | ||
26 | num /= 10; | ||
27 | i++; | ||
28 | } while (num != 0); | ||
29 | |||
30 | return i; | ||
31 | } | ||
32 | |||
33 | static int | ||
34 | __itoa(int num, int sign, char *buffer, size_t start, size_t end) | ||
35 | { | ||
36 | size_t pos; | ||
37 | unsigned int a; | ||
38 | int neg; | ||
39 | |||
40 | if (sign && num < 0) { | ||
41 | a = -num; | ||
42 | neg = 1; | ||
43 | } | ||
44 | else { | ||
45 | a = num; | ||
46 | neg = 0; | ||
47 | } | ||
48 | |||
49 | pos = start + __digits10(a); | ||
50 | if (neg) | ||
51 | pos++; | ||
52 | |||
53 | if (pos < end) | ||
54 | buffer[pos] = '\0'; | ||
55 | else | ||
56 | return ERANGE; | ||
57 | pos--; | ||
58 | do { | ||
59 | buffer[pos] = (a % 10) + '0'; | ||
60 | pos--; | ||
61 | a /= 10; | ||
62 | } while (a != 0); | ||
63 | if (neg) | ||
64 | buffer[pos] = '-'; | ||
65 | return 0; | ||
66 | } | ||
67 | |||
68 | |||
69 | static int | ||
70 | __num2string(int num, int sign, int setid, char *buf, size_t buflen, | ||
71 | char * list[], size_t max, const char *def) | ||
72 | { | ||
73 | int ret = 0; | ||
74 | size_t len; | ||
75 | |||
76 | #ifdef NLS | ||
77 | nl_catd catd; | ||
78 | catd = catopen("libc", 0); | ||
79 | #endif | ||
80 | |||
81 | if (0 <= num && num < max) { | ||
82 | #ifdef NLS | ||
83 | len = strlcpy(buf, catgets(catd, setid, num, list[num]), | ||
84 | buflen); | ||
85 | #else | ||
86 | len = strlcpy(buf, list[num], buflen); | ||
87 | #endif | ||
88 | if (len >= buflen) | ||
89 | ret = ERANGE; | ||
90 | } else { | ||
91 | #ifdef NLS | ||
92 | len = strlcpy(buf, catgets(catd, setid, 0xffff, def), buflen); | ||
93 | #else | ||
94 | len = strlcpy(buf, def, buflen); | ||
95 | #endif | ||
96 | if (len >= buflen) | ||
97 | ret = ERANGE; | ||
98 | else { | ||
99 | ret = __itoa(num, sign, buf, len, buflen); | ||
100 | if (ret == 0) | ||
101 | ret = EINVAL; | ||
102 | } | ||
103 | } | ||
104 | |||
105 | #ifdef NLS | ||
106 | catclose(catd); | ||
107 | #endif | ||
108 | |||
109 | return ret; | ||
110 | } | ||
111 | |||
112 | #define UPREFIX "Unknown error: " | ||
113 | |||
114 | int | ||
115 | strerror_r(int errnum, char *strerrbuf, size_t buflen) | ||
116 | { | ||
117 | int save_errno; | ||
118 | int ret_errno; | ||
119 | |||
120 | save_errno = errno; | ||
121 | |||
122 | ret_errno = __num2string(errnum, 1, 1, strerrbuf, buflen, | ||
123 | sys_errlist, sys_nerr, UPREFIX); | ||
124 | |||
125 | errno = ret_errno ? ret_errno : save_errno; | ||
126 | return (ret_errno); | ||
127 | } | ||
128 | |||
129 | #define USIGPREFIX "Unknown signal: " | ||
130 | |||
131 | char * | ||
132 | __strsignal(int num, char *buf) | ||
133 | { | ||
134 | __num2string(num, 0, 2, buf, NL_TEXTMAX, (char **)sys_siglist, NSIG, | ||
135 | USIGPREFIX); | ||
136 | return buf; | ||
137 | } | ||
diff --git a/src/lib/libc/string/string.3 b/src/lib/libc/string/string.3 new file mode 100644 index 0000000000..4d47ad444d --- /dev/null +++ b/src/lib/libc/string/string.3 | |||
@@ -0,0 +1,136 @@ | |||
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.12 2007/05/31 19:19:32 jmc Exp $ | ||
31 | .\" | ||
32 | .Dd $Mdocdate: May 31 2007 $ | ||
33 | .Dt STRING 3 | ||
34 | .Os | ||
35 | .Sh NAME | ||
36 | .Nm string | ||
37 | .Nd string specific functions | ||
38 | .Sh SYNOPSIS | ||
39 | .Fd #include <string.h> | ||
40 | .Ft char * | ||
41 | .Fn strcat "char *s" "const char *append" | ||
42 | .Ft char * | ||
43 | .Fn strlcat "char *s" "const char *append" "size_t size" | ||
44 | .Ft char * | ||
45 | .Fn strncat "char *s" "const char *append" "size_t count" | ||
46 | .Ft char * | ||
47 | .Fn strchr "const char *s" "int c" | ||
48 | .Ft char * | ||
49 | .Fn strrchr "const char *s" "int c" | ||
50 | .Ft int | ||
51 | .Fn strcmp "const char *s1" "const char *s2" | ||
52 | .Ft int | ||
53 | .Fn strncmp "const char *s1" "const char *s2" "size_t count" | ||
54 | .Ft int | ||
55 | .Fn strcasecmp "const char *s1" "const char *s2" | ||
56 | .Ft int | ||
57 | .Fn strncasecmp "const char *s1" "const char *s2" "size_t count" | ||
58 | .Ft char * | ||
59 | .Fn strcpy "char *dst" "const char *src" | ||
60 | .Ft char * | ||
61 | .Fn strlcpy "char *dst" "const char *src" "size_t size" | ||
62 | .Ft char * | ||
63 | .Fn strncpy "char *dst" "const char *src" "size_t count" | ||
64 | .Ft char * | ||
65 | .Fn strerror "int errno" | ||
66 | .Ft size_t | ||
67 | .Fn strlen "const char *s" | ||
68 | .Ft char * | ||
69 | .Fn strpbrk "const char *s" "const char *charset" | ||
70 | .Ft char * | ||
71 | .Fn strsep "char **stringp" "const char *delim" | ||
72 | .Ft size_t | ||
73 | .Fn strspn "const char *s" "const char *charset" | ||
74 | .Ft size_t | ||
75 | .Fn strcspn "const char *s" "const char *charset" | ||
76 | .Ft char * | ||
77 | .Fn strstr "const char *big" "const char *little" | ||
78 | .Ft char * | ||
79 | .Fn strtok "char *s" "const char *delim" | ||
80 | .Ft char * | ||
81 | .Fn index "const char *s" "int c" | ||
82 | .Ft char * | ||
83 | .Fn rindex "const char *s" "int c" | ||
84 | .Sh DESCRIPTION | ||
85 | The string functions | ||
86 | manipulate strings terminated by a | ||
87 | NUL byte. | ||
88 | .Pp | ||
89 | See the specific manual pages for more information. | ||
90 | For manipulating variable length generic objects as byte | ||
91 | strings (without the NUL-byte check), see | ||
92 | .Xr bstring 3 . | ||
93 | .Pp | ||
94 | Except as noted in their specific manual pages, | ||
95 | the string functions do not test the destination | ||
96 | for size limitations. | ||
97 | .Sh SEE ALSO | ||
98 | .Xr bstring 3 , | ||
99 | .Xr index 3 , | ||
100 | .Xr rindex 3 , | ||
101 | .Xr strcasecmp 3 , | ||
102 | .Xr strcat 3 , | ||
103 | .Xr strchr 3 , | ||
104 | .Xr strcmp 3 , | ||
105 | .Xr strcpy 3 , | ||
106 | .Xr strcspn 3 , | ||
107 | .Xr strerror 3 , | ||
108 | .Xr strlcat 3 , | ||
109 | .Xr strlcpy 3 , | ||
110 | .Xr strlen 3 , | ||
111 | .Xr strpbrk 3 , | ||
112 | .Xr strrchr 3 , | ||
113 | .Xr strsep 3 , | ||
114 | .Xr strspn 3 , | ||
115 | .Xr strstr 3 , | ||
116 | .Xr strtok 3 | ||
117 | .Sh STANDARDS | ||
118 | The | ||
119 | .Fn strcat , | ||
120 | .Fn strncat , | ||
121 | .Fn strchr , | ||
122 | .Fn strrchr , | ||
123 | .Fn strcmp , | ||
124 | .Fn strncmp , | ||
125 | .Fn strcpy , | ||
126 | .Fn strncpy , | ||
127 | .Fn strerror , | ||
128 | .Fn strlen , | ||
129 | .Fn strpbrk , | ||
130 | .Fn strspn , | ||
131 | .Fn strcspn , | ||
132 | .Fn strstr , | ||
133 | and | ||
134 | .Fn strtok | ||
135 | functions conform to | ||
136 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strlcat.c b/src/lib/libc/string/strlcat.c new file mode 100644 index 0000000000..ceab094411 --- /dev/null +++ b/src/lib/libc/string/strlcat.c | |||
@@ -0,0 +1,55 @@ | |||
1 | /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie 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 | #include <sys/types.h> | ||
20 | #include <string.h> | ||
21 | |||
22 | /* | ||
23 | * Appends src to string dst of size siz (unlike strncat, siz is the | ||
24 | * full size of dst, not space left). At most siz-1 characters | ||
25 | * will be copied. Always NUL terminates (unless siz <= strlen(dst)). | ||
26 | * Returns strlen(src) + MIN(siz, strlen(initial dst)). | ||
27 | * If retval >= siz, truncation occurred. | ||
28 | */ | ||
29 | size_t | ||
30 | strlcat(char *dst, const char *src, size_t siz) | ||
31 | { | ||
32 | char *d = dst; | ||
33 | const char *s = src; | ||
34 | size_t n = siz; | ||
35 | size_t dlen; | ||
36 | |||
37 | /* Find the end of dst and adjust bytes left but don't go past end */ | ||
38 | while (n-- != 0 && *d != '\0') | ||
39 | d++; | ||
40 | dlen = d - dst; | ||
41 | n = siz - dlen; | ||
42 | |||
43 | if (n == 0) | ||
44 | return(dlen + strlen(s)); | ||
45 | while (*s != '\0') { | ||
46 | if (n != 1) { | ||
47 | *d++ = *s; | ||
48 | n--; | ||
49 | } | ||
50 | s++; | ||
51 | } | ||
52 | *d = '\0'; | ||
53 | |||
54 | return(dlen + (s - src)); /* count does not include NUL */ | ||
55 | } | ||
diff --git a/src/lib/libc/string/strlcpy.3 b/src/lib/libc/string/strlcpy.3 new file mode 100644 index 0000000000..feda63fae7 --- /dev/null +++ b/src/lib/libc/string/strlcpy.3 | |||
@@ -0,0 +1,186 @@ | |||
1 | .\" $OpenBSD: strlcpy.3,v 1.19 2007/05/31 19:19:32 jmc 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 $Mdocdate: May 31 2007 $ | ||
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 a byte for the NUL should be included 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 it is known how many characters were copied the first time, things | ||
156 | can be sped 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 | ||
180 | .Sh HISTORY | ||
181 | The | ||
182 | .Fn strlcpy | ||
183 | and | ||
184 | .Fn strlcat | ||
185 | functions first appeared in | ||
186 | .Ox 2.4 . | ||
diff --git a/src/lib/libc/string/strlcpy.c b/src/lib/libc/string/strlcpy.c new file mode 100644 index 0000000000..d32b6590f1 --- /dev/null +++ b/src/lib/libc/string/strlcpy.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 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 | #include <sys/types.h> | ||
20 | #include <string.h> | ||
21 | |||
22 | /* | ||
23 | * Copy src to string dst of size siz. At most siz-1 characters | ||
24 | * will be copied. Always NUL terminates (unless siz == 0). | ||
25 | * Returns strlen(src); if retval >= siz, truncation occurred. | ||
26 | */ | ||
27 | size_t | ||
28 | strlcpy(char *dst, const char *src, size_t siz) | ||
29 | { | ||
30 | char *d = dst; | ||
31 | const char *s = src; | ||
32 | size_t n = siz; | ||
33 | |||
34 | /* Copy as many bytes as will fit */ | ||
35 | if (n != 0) { | ||
36 | while (--n != 0) { | ||
37 | if ((*d++ = *s++) == '\0') | ||
38 | break; | ||
39 | } | ||
40 | } | ||
41 | |||
42 | /* Not enough room in dst, add NUL and traverse rest of src */ | ||
43 | if (n == 0) { | ||
44 | if (siz != 0) | ||
45 | *d = '\0'; /* NUL-terminate dst */ | ||
46 | while (*s++) | ||
47 | ; | ||
48 | } | ||
49 | |||
50 | return(s - src - 1); /* count does not include NUL */ | ||
51 | } | ||
diff --git a/src/lib/libc/string/strlen.3 b/src/lib/libc/string/strlen.3 new file mode 100644 index 0000000000..f8a4efe9d2 --- /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.6 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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..12d9ec4dad --- /dev/null +++ b/src/lib/libc/string/strlen.c | |||
@@ -0,0 +1,47 @@ | |||
1 | /* $OpenBSD: strlen.c,v 1.7 2005/08/08 08:05:37 espie 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(_KERNEL) && !defined(_STANDALONE) | ||
33 | #include <string.h> | ||
34 | #else | ||
35 | #include <lib/libkern/libkern.h> | ||
36 | #endif | ||
37 | |||
38 | size_t | ||
39 | strlen(const char *str) | ||
40 | { | ||
41 | const char *s; | ||
42 | |||
43 | for (s = str; *s; ++s) | ||
44 | ; | ||
45 | return (s - str); | ||
46 | } | ||
47 | |||
diff --git a/src/lib/libc/string/strmode.3 b/src/lib/libc/string/strmode.3 new file mode 100644 index 0000000000..ebeafcaf6e --- /dev/null +++ b/src/lib/libc/string/strmode.3 | |||
@@ -0,0 +1,157 @@ | |||
1 | .\" $OpenBSD: strmode.3,v 1.14 2007/05/31 19:19:32 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 $Mdocdate: May 31 2007 $ | ||
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 NUL 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 \&? | ||
71 | unknown inode type | ||
72 | .El | ||
73 | .Pp | ||
74 | The next nine characters encode three sets of permissions, in three | ||
75 | characters each. | ||
76 | The first three characters are the permissions for the owner of the | ||
77 | file, the second three for the group the file belongs to, and the | ||
78 | third for the | ||
79 | .Dq other , | ||
80 | or default, set of users. | ||
81 | .Pp | ||
82 | Permission checking is done as specifically as possible. | ||
83 | If read permission is denied to the owner of a file in the first set | ||
84 | of permissions, the owner of the file will not be able to read the file. | ||
85 | This is true even if the owner is in the file's group and the group | ||
86 | permissions allow reading or the | ||
87 | .Dq other | ||
88 | permissions allow reading. | ||
89 | .Pp | ||
90 | If the first character of the three character set is an | ||
91 | .Sq r , | ||
92 | the file is readable for that set of users; if a dash | ||
93 | .Pq Ql - , | ||
94 | it is not readable. | ||
95 | .Pp | ||
96 | If the second character of the three character set is a | ||
97 | .Sq w , | ||
98 | the file is writable for that set of users; if a dash | ||
99 | .Pq Ql - , | ||
100 | it is not writable. | ||
101 | .Pp | ||
102 | The third character is the first of the following characters that apply: | ||
103 | .Bl -tag -width xxxx | ||
104 | .It S | ||
105 | If the character is part of the owner permissions and the file is not | ||
106 | executable or the directory is not searchable by the owner, and the | ||
107 | set-user-ID bit is set. | ||
108 | .It S | ||
109 | If the character is part of the group permissions and the file is not | ||
110 | executable or the directory is not searchable by the group, and the | ||
111 | set-group-ID bit is set. | ||
112 | .It T | ||
113 | If the character is part of the other permissions and the file is not | ||
114 | executable or the directory is not searchable by others, and the | ||
115 | .Dq sticky | ||
116 | .Pq Dv S_ISVTX | ||
117 | bit is set. | ||
118 | .It s | ||
119 | If the character is part of the owner permissions and the file is | ||
120 | executable or the directory searchable by the owner, and the set-user-ID | ||
121 | bit is set. | ||
122 | .It s | ||
123 | If the character is part of the group permissions and the file is | ||
124 | executable or the directory searchable by the group, and the set-group-ID | ||
125 | bit is set. | ||
126 | .It t | ||
127 | If the character is part of the other permissions and the file is | ||
128 | executable or the directory searchable by others, and the | ||
129 | .Dq sticky | ||
130 | .Pq Dv S_ISVTX | ||
131 | bit is set. | ||
132 | .It x | ||
133 | The file is executable or the directory is searchable. | ||
134 | .It \- | ||
135 | None of the above apply. | ||
136 | .El | ||
137 | .Pp | ||
138 | The last character is a plus sign | ||
139 | .Pq Ql + | ||
140 | if there are any alternate | ||
141 | or additional access control methods associated with the inode, otherwise | ||
142 | it will be a space. | ||
143 | .Sh RETURN VALUES | ||
144 | The | ||
145 | .Fn strmode | ||
146 | function always returns 0. | ||
147 | .Sh SEE ALSO | ||
148 | .Xr chmod 1 , | ||
149 | .Xr find 1 , | ||
150 | .Xr stat 2 , | ||
151 | .Xr getmode 3 , | ||
152 | .Xr setmode 3 | ||
153 | .Sh HISTORY | ||
154 | The | ||
155 | .Fn strmode | ||
156 | function first appeared in | ||
157 | .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..6f0fa34ed8 --- /dev/null +++ b/src/lib/libc/string/strmode.c | |||
@@ -0,0 +1,140 @@ | |||
1 | /* $OpenBSD: strmode.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. 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 | |||
31 | #include <sys/types.h> | ||
32 | #include <sys/stat.h> | ||
33 | #include <string.h> | ||
34 | |||
35 | /* XXX mode should be mode_t */ | ||
36 | |||
37 | void | ||
38 | strmode(int mode, char *p) | ||
39 | { | ||
40 | /* print type */ | ||
41 | switch (mode & S_IFMT) { | ||
42 | case S_IFDIR: /* directory */ | ||
43 | *p++ = 'd'; | ||
44 | break; | ||
45 | case S_IFCHR: /* character special */ | ||
46 | *p++ = 'c'; | ||
47 | break; | ||
48 | case S_IFBLK: /* block special */ | ||
49 | *p++ = 'b'; | ||
50 | break; | ||
51 | case S_IFREG: /* regular */ | ||
52 | *p++ = '-'; | ||
53 | break; | ||
54 | case S_IFLNK: /* symbolic link */ | ||
55 | *p++ = 'l'; | ||
56 | break; | ||
57 | case S_IFSOCK: /* socket */ | ||
58 | *p++ = 's'; | ||
59 | break; | ||
60 | #ifdef S_IFIFO | ||
61 | case S_IFIFO: /* fifo */ | ||
62 | *p++ = 'p'; | ||
63 | break; | ||
64 | #endif | ||
65 | default: /* unknown */ | ||
66 | *p++ = '?'; | ||
67 | break; | ||
68 | } | ||
69 | /* usr */ | ||
70 | if (mode & S_IRUSR) | ||
71 | *p++ = 'r'; | ||
72 | else | ||
73 | *p++ = '-'; | ||
74 | if (mode & S_IWUSR) | ||
75 | *p++ = 'w'; | ||
76 | else | ||
77 | *p++ = '-'; | ||
78 | switch (mode & (S_IXUSR | S_ISUID)) { | ||
79 | case 0: | ||
80 | *p++ = '-'; | ||
81 | break; | ||
82 | case S_IXUSR: | ||
83 | *p++ = 'x'; | ||
84 | break; | ||
85 | case S_ISUID: | ||
86 | *p++ = 'S'; | ||
87 | break; | ||
88 | case S_IXUSR | S_ISUID: | ||
89 | *p++ = 's'; | ||
90 | break; | ||
91 | } | ||
92 | /* group */ | ||
93 | if (mode & S_IRGRP) | ||
94 | *p++ = 'r'; | ||
95 | else | ||
96 | *p++ = '-'; | ||
97 | if (mode & S_IWGRP) | ||
98 | *p++ = 'w'; | ||
99 | else | ||
100 | *p++ = '-'; | ||
101 | switch (mode & (S_IXGRP | S_ISGID)) { | ||
102 | case 0: | ||
103 | *p++ = '-'; | ||
104 | break; | ||
105 | case S_IXGRP: | ||
106 | *p++ = 'x'; | ||
107 | break; | ||
108 | case S_ISGID: | ||
109 | *p++ = 'S'; | ||
110 | break; | ||
111 | case S_IXGRP | S_ISGID: | ||
112 | *p++ = 's'; | ||
113 | break; | ||
114 | } | ||
115 | /* other */ | ||
116 | if (mode & S_IROTH) | ||
117 | *p++ = 'r'; | ||
118 | else | ||
119 | *p++ = '-'; | ||
120 | if (mode & S_IWOTH) | ||
121 | *p++ = 'w'; | ||
122 | else | ||
123 | *p++ = '-'; | ||
124 | switch (mode & (S_IXOTH | S_ISVTX)) { | ||
125 | case 0: | ||
126 | *p++ = '-'; | ||
127 | break; | ||
128 | case S_IXOTH: | ||
129 | *p++ = 'x'; | ||
130 | break; | ||
131 | case S_ISVTX: | ||
132 | *p++ = 'T'; | ||
133 | break; | ||
134 | case S_IXOTH | S_ISVTX: | ||
135 | *p++ = 't'; | ||
136 | break; | ||
137 | } | ||
138 | *p++ = ' '; /* will be a '+' if ACL's implemented */ | ||
139 | *p = '\0'; | ||
140 | } | ||
diff --git a/src/lib/libc/string/strncat.c b/src/lib/libc/string/strncat.c new file mode 100644 index 0000000000..c4df4f2fad --- /dev/null +++ b/src/lib/libc/string/strncat.c | |||
@@ -0,0 +1,57 @@ | |||
1 | /* $OpenBSD: strncat.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
4 | * 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 | |||
34 | #include <string.h> | ||
35 | |||
36 | /* | ||
37 | * Concatenate src on the end of dst. At most strlen(dst)+n+1 bytes | ||
38 | * are written at dst (at most n+1 bytes being appended). Return dst. | ||
39 | */ | ||
40 | char * | ||
41 | strncat(char *dst, const char *src, size_t n) | ||
42 | { | ||
43 | if (n != 0) { | ||
44 | char *d = dst; | ||
45 | const char *s = src; | ||
46 | |||
47 | while (*d != 0) | ||
48 | d++; | ||
49 | do { | ||
50 | if ((*d = *s++) == 0) | ||
51 | break; | ||
52 | d++; | ||
53 | } while (--n != 0); | ||
54 | *d = 0; | ||
55 | } | ||
56 | return (dst); | ||
57 | } | ||
diff --git a/src/lib/libc/string/strncmp.c b/src/lib/libc/string/strncmp.c new file mode 100644 index 0000000000..0aea80d7d9 --- /dev/null +++ b/src/lib/libc/string/strncmp.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* $OpenBSD: strncmp.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1989 The Regents of the University of California. | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. 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(_KERNEL) && !defined(_STANDALONE) | ||
33 | #include <string.h> | ||
34 | #else | ||
35 | #include <lib/libkern/libkern.h> | ||
36 | #endif | ||
37 | |||
38 | int | ||
39 | strncmp(const char *s1, const char *s2, size_t n) | ||
40 | { | ||
41 | |||
42 | if (n == 0) | ||
43 | return (0); | ||
44 | do { | ||
45 | if (*s1 != *s2++) | ||
46 | return (*(unsigned char *)s1 - *(unsigned char *)--s2); | ||
47 | if (*s1++ == 0) | ||
48 | break; | ||
49 | } while (--n != 0); | ||
50 | return (0); | ||
51 | } | ||
diff --git a/src/lib/libc/string/strncpy.c b/src/lib/libc/string/strncpy.c new file mode 100644 index 0000000000..4426cbe2e3 --- /dev/null +++ b/src/lib/libc/string/strncpy.c | |||
@@ -0,0 +1,62 @@ | |||
1 | /* $OpenBSD: strncpy.c,v 1.6 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1990 The Regents of the University of California. | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * This code is derived from software contributed to Berkeley by | ||
8 | * Chris Torek. | ||
9 | * | ||
10 | * Redistribution and use in source and binary forms, with or without | ||
11 | * modification, are permitted provided that the following conditions | ||
12 | * are met: | ||
13 | * 1. Redistributions of source code must retain the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer. | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in the | ||
17 | * documentation and/or other materials provided with the distribution. | ||
18 | * 3. Neither the name of the University nor the names of its contributors | ||
19 | * may be used to endorse or promote products derived from this software | ||
20 | * without specific prior written permission. | ||
21 | * | ||
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | * SUCH DAMAGE. | ||
33 | */ | ||
34 | |||
35 | #if !defined(_KERNEL) && !defined(_STANDALONE) | ||
36 | #include <string.h> | ||
37 | #else | ||
38 | #include <lib/libkern/libkern.h> | ||
39 | #endif | ||
40 | |||
41 | /* | ||
42 | * Copy src to dst, truncating or null-padding to always copy n bytes. | ||
43 | * Return dst. | ||
44 | */ | ||
45 | char * | ||
46 | strncpy(char *dst, const char *src, size_t n) | ||
47 | { | ||
48 | if (n != 0) { | ||
49 | char *d = dst; | ||
50 | const char *s = src; | ||
51 | |||
52 | do { | ||
53 | if ((*d++ = *s++) == 0) { | ||
54 | /* NUL pad the remaining n-1 bytes */ | ||
55 | while (--n != 0) | ||
56 | *d++ = 0; | ||
57 | break; | ||
58 | } | ||
59 | } while (--n != 0); | ||
60 | } | ||
61 | return (dst); | ||
62 | } | ||
diff --git a/src/lib/libc/string/strpbrk.3 b/src/lib/libc/string/strpbrk.3 new file mode 100644 index 0000000000..9d6c676c8d --- /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.8 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 NUL-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..cd3b71c0d3 --- /dev/null +++ b/src/lib/libc/string/strpbrk.c | |||
@@ -0,0 +1,48 @@ | |||
1 | /* $OpenBSD: strpbrk.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1985 Regents of the University of California. | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. 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 | |||
31 | #include <string.h> | ||
32 | |||
33 | /* | ||
34 | * Find the first occurrence in s1 of a character in s2 (excluding NUL). | ||
35 | */ | ||
36 | char * | ||
37 | strpbrk(const char *s1, const char *s2) | ||
38 | { | ||
39 | const char *scanp; | ||
40 | int c, sc; | ||
41 | |||
42 | while ((c = *s1++) != 0) { | ||
43 | for (scanp = s2; (sc = *scanp++) != 0;) | ||
44 | if (sc == c) | ||
45 | return ((char *)(s1 - 1)); | ||
46 | } | ||
47 | return (NULL); | ||
48 | } | ||
diff --git a/src/lib/libc/string/strrchr.3 b/src/lib/libc/string/strrchr.3 new file mode 100644 index 0000000000..0d1f4be971 --- /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.8 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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..cceeda7577 --- /dev/null +++ b/src/lib/libc/string/strsep.3 | |||
@@ -0,0 +1,112 @@ | |||
1 | .\" $OpenBSD: strsep.3,v 1.12 2007/05/31 19:19:32 jmc 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 $Mdocdate: May 31 2007 $ | ||
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..bcca681e03 --- /dev/null +++ b/src/lib/libc/string/strsep.c | |||
@@ -0,0 +1,71 @@ | |||
1 | /* $OpenBSD: strsep.c,v 1.6 2005/08/08 08:05:37 espie 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 | /* | ||
36 | * Get next token from string *stringp, where tokens are possibly-empty | ||
37 | * strings separated by characters from delim. | ||
38 | * | ||
39 | * Writes NULs into the string at *stringp to end tokens. | ||
40 | * delim need not remain constant from call to call. | ||
41 | * On return, *stringp points past the last NUL written (if there might | ||
42 | * be further tokens), or is NULL (if there are definitely no more tokens). | ||
43 | * | ||
44 | * If *stringp is NULL, strsep returns NULL. | ||
45 | */ | ||
46 | char * | ||
47 | strsep(char **stringp, const char *delim) | ||
48 | { | ||
49 | char *s; | ||
50 | const char *spanp; | ||
51 | int c, sc; | ||
52 | char *tok; | ||
53 | |||
54 | if ((s = *stringp) == NULL) | ||
55 | return (NULL); | ||
56 | for (tok = s;;) { | ||
57 | c = *s++; | ||
58 | spanp = delim; | ||
59 | do { | ||
60 | if ((sc = *spanp++) == c) { | ||
61 | if (c == 0) | ||
62 | s = NULL; | ||
63 | else | ||
64 | s[-1] = 0; | ||
65 | *stringp = s; | ||
66 | return (tok); | ||
67 | } | ||
68 | } while (sc != 0); | ||
69 | } | ||
70 | /* NOTREACHED */ | ||
71 | } | ||
diff --git a/src/lib/libc/string/strsignal.3 b/src/lib/libc/string/strsignal.3 new file mode 100644 index 0000000000..839402dcb1 --- /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.5 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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..aa541cefed --- /dev/null +++ b/src/lib/libc/string/strsignal.c | |||
@@ -0,0 +1,41 @@ | |||
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 | #include <string.h> | ||
31 | #include <limits.h> | ||
32 | |||
33 | extern char *__strsignal(int, char *); | ||
34 | |||
35 | char * | ||
36 | strsignal(int sig) | ||
37 | { | ||
38 | static char buf[NL_TEXTMAX]; | ||
39 | |||
40 | return __strsignal(sig, buf); | ||
41 | } | ||
diff --git a/src/lib/libc/string/strspn.3 b/src/lib/libc/string/strspn.3 new file mode 100644 index 0000000000..f902ca35d2 --- /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.9 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 NUL-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..385649c041 --- /dev/null +++ b/src/lib/libc/string/strspn.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* $OpenBSD: strspn.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1989 The Regents of the University of California. | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. 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 | |||
31 | #include <string.h> | ||
32 | |||
33 | /* | ||
34 | * Span the string s2 (skip characters that are in s2). | ||
35 | */ | ||
36 | size_t | ||
37 | strspn(const char *s1, const char *s2) | ||
38 | { | ||
39 | const char *p = s1, *spanp; | ||
40 | char c, sc; | ||
41 | |||
42 | /* | ||
43 | * Skip any characters in s2, excluding the terminating \0. | ||
44 | */ | ||
45 | cont: | ||
46 | c = *p++; | ||
47 | for (spanp = s2; (sc = *spanp++) != 0;) | ||
48 | if (sc == c) | ||
49 | goto cont; | ||
50 | return (p - 1 - s1); | ||
51 | } | ||
diff --git a/src/lib/libc/string/strstr.3 b/src/lib/libc/string/strstr.3 new file mode 100644 index 0000000000..38bd207727 --- /dev/null +++ b/src/lib/libc/string/strstr.3 | |||
@@ -0,0 +1,92 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
7 | .\" | ||
8 | .\" Redistribution and use in source and binary forms, with or without | ||
9 | .\" modification, are permitted provided that the following conditions | ||
10 | .\" are met: | ||
11 | .\" 1. Redistributions of source code must retain the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer. | ||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
14 | .\" notice, this list of conditions and the following disclaimer in the | ||
15 | .\" documentation and/or other materials provided with the distribution. | ||
16 | .\" 3. 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.10 2007/09/19 16:06:45 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: September 19 2007 $ | ||
35 | .Dt STRSTR 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strstr , strcasestr | ||
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 | .Ft char * | ||
45 | .Fn strcasestr "const char *big" "const char *little" | ||
46 | .Sh DESCRIPTION | ||
47 | The | ||
48 | .Fn strstr | ||
49 | function locates the first occurrence of the NUL-terminated string | ||
50 | .Fa little | ||
51 | in the NUL-terminated string | ||
52 | .Fa big . | ||
53 | .Pp | ||
54 | The | ||
55 | .Fn strcasestr | ||
56 | function is similar to | ||
57 | .Fn strstr | ||
58 | but ignores the case of both strings. | ||
59 | .Pp | ||
60 | If | ||
61 | .Fa little | ||
62 | is an empty string, | ||
63 | .Fa big | ||
64 | is returned; | ||
65 | if | ||
66 | .Fa little | ||
67 | occurs nowhere in | ||
68 | .Fa big , | ||
69 | .Dv NULL | ||
70 | is returned; | ||
71 | otherwise a pointer to the first character of the first occurrence of | ||
72 | .Fa little | ||
73 | is returned. | ||
74 | .Sh SEE ALSO | ||
75 | .Xr memchr 3 , | ||
76 | .Xr strchr 3 , | ||
77 | .Xr strcspn 3 , | ||
78 | .Xr strpbrk 3 , | ||
79 | .Xr strrchr 3 , | ||
80 | .Xr strsep 3 , | ||
81 | .Xr strspn 3 , | ||
82 | .Xr strtok 3 | ||
83 | .Sh STANDARDS | ||
84 | The | ||
85 | .Fn strstr | ||
86 | function conforms to | ||
87 | .St -ansiC . | ||
88 | .Sh HISTORY | ||
89 | The | ||
90 | .Fn strcasestr | ||
91 | function first appeared in | ||
92 | .Ox 3.8 . | ||
diff --git a/src/lib/libc/string/strstr.c b/src/lib/libc/string/strstr.c new file mode 100644 index 0000000000..95a865bf79 --- /dev/null +++ b/src/lib/libc/string/strstr.c | |||
@@ -0,0 +1,56 @@ | |||
1 | /* $OpenBSD: strstr.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
4 | * 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 | |||
34 | #include <string.h> | ||
35 | |||
36 | /* | ||
37 | * Find the first occurrence of find in s. | ||
38 | */ | ||
39 | char * | ||
40 | strstr(const char *s, const char *find) | ||
41 | { | ||
42 | char c, sc; | ||
43 | size_t len; | ||
44 | |||
45 | if ((c = *find++) != 0) { | ||
46 | len = strlen(find); | ||
47 | do { | ||
48 | do { | ||
49 | if ((sc = *s++) == 0) | ||
50 | return (NULL); | ||
51 | } while (sc != c); | ||
52 | } while (strncmp(s, find, len) != 0); | ||
53 | s--; | ||
54 | } | ||
55 | return ((char *)s); | ||
56 | } | ||
diff --git a/src/lib/libc/string/strtok.3 b/src/lib/libc/string/strtok.3 new file mode 100644 index 0000000000..ed7d5158ed --- /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.19 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 NUL-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))) { | ||
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..4e963a019e --- /dev/null +++ b/src/lib/libc/string/strtok.c | |||
@@ -0,0 +1,86 @@ | |||
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 | #include <string.h> | ||
31 | |||
32 | char * | ||
33 | strtok(char *s, const char *delim) | ||
34 | { | ||
35 | static char *last; | ||
36 | |||
37 | return strtok_r(s, delim, &last); | ||
38 | } | ||
39 | |||
40 | char * | ||
41 | strtok_r(char *s, const char *delim, char **last) | ||
42 | { | ||
43 | char *spanp; | ||
44 | int c, sc; | ||
45 | char *tok; | ||
46 | |||
47 | |||
48 | if (s == NULL && (s = *last) == NULL) | ||
49 | return (NULL); | ||
50 | |||
51 | /* | ||
52 | * Skip (span) leading delimiters (s += strspn(s, delim), sort of). | ||
53 | */ | ||
54 | cont: | ||
55 | c = *s++; | ||
56 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { | ||
57 | if (c == sc) | ||
58 | goto cont; | ||
59 | } | ||
60 | |||
61 | if (c == 0) { /* no non-delimiter characters */ | ||
62 | *last = NULL; | ||
63 | return (NULL); | ||
64 | } | ||
65 | tok = s - 1; | ||
66 | |||
67 | /* | ||
68 | * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). | ||
69 | * Note that delim must have one NUL; we stop if we see that, too. | ||
70 | */ | ||
71 | for (;;) { | ||
72 | c = *s++; | ||
73 | spanp = (char *)delim; | ||
74 | do { | ||
75 | if ((sc = *spanp++) == c) { | ||
76 | if (c == 0) | ||
77 | s = NULL; | ||
78 | else | ||
79 | s[-1] = 0; | ||
80 | *last = s; | ||
81 | return (tok); | ||
82 | } | ||
83 | } while (sc != 0); | ||
84 | } | ||
85 | /* NOTREACHED */ | ||
86 | } | ||
diff --git a/src/lib/libc/string/strxfrm.3 b/src/lib/libc/string/strxfrm.3 new file mode 100644 index 0000000000..73ce135191 --- /dev/null +++ b/src/lib/libc/string/strxfrm.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: strxfrm.3,v 1.8 2007/05/31 19:19:32 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 idea of | ||
46 | .Fn strxfrm | ||
47 | is to | ||
48 | .Dq un-localize | ||
49 | a string: the function transforms | ||
50 | .Ar src , | ||
51 | storing the result in | ||
52 | .Ar dst , | ||
53 | such that | ||
54 | .Xr strcmp 3 | ||
55 | on transformed strings returns what | ||
56 | .Xr strcoll 3 | ||
57 | on the original untransformed strings would return. | ||
58 | .Sh SEE ALSO | ||
59 | .Xr bcmp 3 , | ||
60 | .Xr memcmp 3 , | ||
61 | .Xr setlocale 3 , | ||
62 | .Xr strcasecmp 3 , | ||
63 | .Xr strcmp 3 , | ||
64 | .Xr strcoll 3 | ||
65 | .Sh STANDARDS | ||
66 | The | ||
67 | .Fn strxfrm | ||
68 | function conforms to | ||
69 | .St -ansiC . | ||
70 | .Sh BUGS | ||
71 | Since locales are not fully implemented on | ||
72 | .Ox , | ||
73 | .Fn strxfrm | ||
74 | just returns a copy of the original string. | ||
diff --git a/src/lib/libc/string/strxfrm.c b/src/lib/libc/string/strxfrm.c new file mode 100644 index 0000000000..6f289c901e --- /dev/null +++ b/src/lib/libc/string/strxfrm.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* $OpenBSD: strxfrm.c,v 1.6 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
4 | * 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 | |||
34 | #include <string.h> | ||
35 | |||
36 | /* | ||
37 | * Transform src, storing the result in dst, such that | ||
38 | * strcmp() on transformed strings returns what strcoll() | ||
39 | * on the original untransformed strings would return. | ||
40 | */ | ||
41 | size_t | ||
42 | strxfrm(char *dst, const char *src, size_t n) | ||
43 | { | ||
44 | |||
45 | /* | ||
46 | * Since locales are unimplemented, this is just a copy. | ||
47 | */ | ||
48 | if (n == 0) | ||
49 | return (strlen(src)); | ||
50 | return (strlcpy(dst, src, n)); | ||
51 | } | ||
diff --git a/src/lib/libc/string/swab.3 b/src/lib/libc/string/swab.3 new file mode 100644 index 0000000000..56def1ec88 --- /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.6 2007/05/31 19:19:32 jmc Exp $ | ||
29 | .\" | ||
30 | .Dd $Mdocdate: May 31 2007 $ | ||
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..b74db7e62a --- /dev/null +++ b/src/lib/libc/string/swab.c | |||
@@ -0,0 +1,61 @@ | |||
1 | /* $OpenBSD: swab.c,v 1.8 2008/03/15 21:54:09 ray Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1988 Regents of the University of California. | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * This code is derived from software contributed to Berkeley by | ||
7 | * Jeffrey Mogul. | ||
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 | |||
34 | #include <unistd.h> | ||
35 | |||
36 | void | ||
37 | swab(const void *from, void *to, size_t len) | ||
38 | { | ||
39 | size_t n; | ||
40 | char *fp, *tp; | ||
41 | char temp; | ||
42 | |||
43 | n = (len / 2) + 1; | ||
44 | fp = (char *)from; | ||
45 | tp = (char *)to; | ||
46 | #define STEP do { \ | ||
47 | temp = *fp++; \ | ||
48 | *tp++ = *fp++; \ | ||
49 | *tp++ = temp; \ | ||
50 | } while (0) | ||
51 | /* round to multiple of 8 */ | ||
52 | while ((--n) & 07) | ||
53 | STEP; | ||
54 | n >>= 3; | ||
55 | if (n == 0) | ||
56 | return; | ||
57 | while (n-- != 0) { | ||
58 | STEP; STEP; STEP; STEP; | ||
59 | STEP; STEP; STEP; STEP; | ||
60 | } | ||
61 | } | ||
diff --git a/src/lib/libc/string/wcscat.c b/src/lib/libc/string/wcscat.c new file mode 100644 index 0000000000..a4841a975a --- /dev/null +++ b/src/lib/libc/string/wcscat.c | |||
@@ -0,0 +1,55 @@ | |||
1 | /* $OpenBSD: wcscat.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wcscat.c,v 1.2 2001/01/03 14:29:36 lukem Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wcscat.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | |||
34 | #if defined(APIWARN) | ||
35 | __warn_references(wcscat, | ||
36 | "warning: wcscat() is almost always misused, please use wcslcat()"); | ||
37 | #endif | ||
38 | |||
39 | wchar_t * | ||
40 | wcscat(wchar_t *s1, const wchar_t *s2) | ||
41 | { | ||
42 | wchar_t *p; | ||
43 | wchar_t *q; | ||
44 | const wchar_t *r; | ||
45 | |||
46 | p = s1; | ||
47 | while (*p) | ||
48 | p++; | ||
49 | q = p; | ||
50 | r = s2; | ||
51 | while (*r) | ||
52 | *q++ = *r++; | ||
53 | *q = '\0'; | ||
54 | return s1; | ||
55 | } | ||
diff --git a/src/lib/libc/string/wcschr.c b/src/lib/libc/string/wcschr.c new file mode 100644 index 0000000000..b84a2d32e3 --- /dev/null +++ b/src/lib/libc/string/wcschr.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* $OpenBSD: wcschr.c,v 1.4 2008/08/23 05:34:36 djm Exp $ */ | ||
2 | /* $NetBSD: wcschr.c,v 1.2 2001/01/03 14:29:36 lukem Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wcschr.c,v 1.2 2000/12/21 05:07:25 itojun Exp | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | |||
34 | wchar_t * | ||
35 | wcschr(const wchar_t *s, wchar_t c) | ||
36 | { | ||
37 | const wchar_t *p; | ||
38 | |||
39 | p = s; | ||
40 | for (;;) { | ||
41 | if (*p == c) { | ||
42 | /* LINTED interface specification */ | ||
43 | return (wchar_t *)p; | ||
44 | } | ||
45 | if (!*p) | ||
46 | return NULL; | ||
47 | p++; | ||
48 | } | ||
49 | /* NOTREACHED */ | ||
50 | } | ||
diff --git a/src/lib/libc/string/wcscmp.c b/src/lib/libc/string/wcscmp.c new file mode 100644 index 0000000000..4a8f6ba3fb --- /dev/null +++ b/src/lib/libc/string/wcscmp.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* $OpenBSD: wcscmp.c,v 1.4 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wcscmp.c,v 1.5 2003/08/07 16:43:54 agc Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c) 1990, 1993 | ||
6 | * The Regents of the University of California. All rights reserved. | ||
7 | * | ||
8 | * This code is derived from software contributed to Berkeley by | ||
9 | * Chris Torek. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * 3. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #include <wchar.h> | ||
37 | #include "locale/runetype.h" | ||
38 | |||
39 | /* | ||
40 | * Compare strings. | ||
41 | */ | ||
42 | int | ||
43 | wcscmp(const wchar_t *s1, const wchar_t *s2) | ||
44 | { | ||
45 | |||
46 | while (*s1 == *s2++) | ||
47 | if (*s1++ == 0) | ||
48 | return (0); | ||
49 | /* XXX assumes wchar_t = int */ | ||
50 | return (*(const rune_t *)s1 - *(const rune_t *)--s2); | ||
51 | } | ||
diff --git a/src/lib/libc/string/wcscpy.c b/src/lib/libc/string/wcscpy.c new file mode 100644 index 0000000000..75fdb75fe4 --- /dev/null +++ b/src/lib/libc/string/wcscpy.c | |||
@@ -0,0 +1,52 @@ | |||
1 | /* $OpenBSD: wcscpy.c,v 1.4 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wcscpy.c,v 1.2 2001/01/03 14:29:36 lukem Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wcscpy.c,v 1.2 2000/12/21 04:51:09 itojun Exp | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | |||
34 | #if defined(APIWARN) | ||
35 | __warn_references(wcscpy, | ||
36 | "warning: wcscpy() is almost always misused, please use wcslcpy()"); | ||
37 | #endif | ||
38 | |||
39 | wchar_t * | ||
40 | wcscpy(wchar_t *s1, const wchar_t *s2) | ||
41 | { | ||
42 | wchar_t *p; | ||
43 | const wchar_t *q; | ||
44 | |||
45 | p = s1; | ||
46 | q = s2; | ||
47 | while (*q) | ||
48 | *p++ = *q++; | ||
49 | *p = '\0'; | ||
50 | |||
51 | return s1; | ||
52 | } | ||
diff --git a/src/lib/libc/string/wcscspn.c b/src/lib/libc/string/wcscspn.c new file mode 100644 index 0000000000..cf40092465 --- /dev/null +++ b/src/lib/libc/string/wcscspn.c | |||
@@ -0,0 +1,53 @@ | |||
1 | /* $OpenBSD: wcscspn.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wcscspn.c,v 1.2 2001/01/03 14:29:36 lukem Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wcscspn.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | |||
34 | size_t | ||
35 | wcscspn(const wchar_t *s, const wchar_t *set) | ||
36 | { | ||
37 | const wchar_t *p; | ||
38 | const wchar_t *q; | ||
39 | |||
40 | p = s; | ||
41 | while (*p) { | ||
42 | q = set; | ||
43 | while (*q) { | ||
44 | if (*p == *q) | ||
45 | goto done; | ||
46 | q++; | ||
47 | } | ||
48 | p++; | ||
49 | } | ||
50 | |||
51 | done: | ||
52 | return (p - s); | ||
53 | } | ||
diff --git a/src/lib/libc/string/wcslcat.c b/src/lib/libc/string/wcslcat.c new file mode 100644 index 0000000000..bcd52d1f3c --- /dev/null +++ b/src/lib/libc/string/wcslcat.c | |||
@@ -0,0 +1,68 @@ | |||
1 | /* $OpenBSD: wcslcat.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wcslcat.c,v 1.2 2001/01/03 14:33:02 lukem Exp $ */ | ||
3 | /* from OpenBSD: strlcat.c,v 1.3 2000/11/24 11:10:02 itojun Exp */ | ||
4 | |||
5 | /* | ||
6 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | ||
7 | * All rights reserved. | ||
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. The name of the author may not be used to endorse or promote products | ||
18 | * derived from this software without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
21 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | ||
22 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
23 | * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
26 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
27 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
28 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
29 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include <sys/types.h> | ||
33 | #include <wchar.h> | ||
34 | |||
35 | /* | ||
36 | * Appends src to string dst of size siz (unlike wcsncat, siz is the | ||
37 | * full size of dst, not space left). At most siz-1 characters | ||
38 | * will be copied. Always NUL terminates (unless siz == 0). | ||
39 | * Returns wcslen(initial dst) + wcslen(src); if retval >= siz, | ||
40 | * truncation occurred. | ||
41 | */ | ||
42 | size_t | ||
43 | wcslcat(wchar_t *dst, const wchar_t *src, size_t siz) | ||
44 | { | ||
45 | wchar_t *d = dst; | ||
46 | const wchar_t *s = src; | ||
47 | size_t n = siz; | ||
48 | size_t dlen; | ||
49 | |||
50 | /* Find the end of dst and adjust bytes left but don't go past end */ | ||
51 | while (*d != '\0' && n-- != 0) | ||
52 | d++; | ||
53 | dlen = d - dst; | ||
54 | n = siz - dlen; | ||
55 | |||
56 | if (n == 0) | ||
57 | return(dlen + wcslen(s)); | ||
58 | while (*s != '\0') { | ||
59 | if (n != 1) { | ||
60 | *d++ = *s; | ||
61 | n--; | ||
62 | } | ||
63 | s++; | ||
64 | } | ||
65 | *d = '\0'; | ||
66 | |||
67 | return(dlen + (s - src)); /* count does not include NUL */ | ||
68 | } | ||
diff --git a/src/lib/libc/string/wcslcpy.c b/src/lib/libc/string/wcslcpy.c new file mode 100644 index 0000000000..85f51df35f --- /dev/null +++ b/src/lib/libc/string/wcslcpy.c | |||
@@ -0,0 +1,64 @@ | |||
1 | /* $OpenBSD: wcslcpy.c,v 1.4 2006/05/05 15:27:38 millert Exp $ */ | ||
2 | /* $NetBSD: wcslcpy.c,v 1.2 2001/01/03 14:33:02 lukem Exp $ */ | ||
3 | /* from OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp */ | ||
4 | |||
5 | /* | ||
6 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | ||
7 | * All rights reserved. | ||
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. The name of the author may not be used to endorse or promote products | ||
18 | * derived from this software without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
21 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | ||
22 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
23 | * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
26 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
27 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
28 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
29 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include <sys/types.h> | ||
33 | #include <wchar.h> | ||
34 | |||
35 | /* | ||
36 | * Copy src to string dst of size siz. At most siz-1 characters | ||
37 | * will be copied. Always NUL terminates (unless siz == 0). | ||
38 | * Returns wcslen(src); if retval >= siz, truncation occurred. | ||
39 | */ | ||
40 | size_t | ||
41 | wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz) | ||
42 | { | ||
43 | wchar_t *d = dst; | ||
44 | const wchar_t *s = src; | ||
45 | size_t n = siz; | ||
46 | |||
47 | /* Copy as many bytes as will fit */ | ||
48 | if (n != 0) { | ||
49 | while (--n != 0) { | ||
50 | if ((*d++ = *s++) == '\0') | ||
51 | break; | ||
52 | } | ||
53 | } | ||
54 | |||
55 | /* Not enough room in dst, add NUL and traverse rest of src */ | ||
56 | if (n == 0) { | ||
57 | if (siz != 0) | ||
58 | *d = '\0'; /* NUL-terminate dst */ | ||
59 | while (*s++) | ||
60 | ; | ||
61 | } | ||
62 | |||
63 | return(s - src - 1); /* count does not include NUL */ | ||
64 | } | ||
diff --git a/src/lib/libc/string/wcslen.c b/src/lib/libc/string/wcslen.c new file mode 100644 index 0000000000..c1de4a271e --- /dev/null +++ b/src/lib/libc/string/wcslen.c | |||
@@ -0,0 +1,44 @@ | |||
1 | /* $OpenBSD: wcslen.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wcslen.c,v 1.2 2001/01/03 14:29:36 lukem Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wcslen.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | |||
34 | size_t | ||
35 | wcslen(const wchar_t *s) | ||
36 | { | ||
37 | const wchar_t *p; | ||
38 | |||
39 | p = s; | ||
40 | while (*p) | ||
41 | p++; | ||
42 | |||
43 | return p - s; | ||
44 | } | ||
diff --git a/src/lib/libc/string/wcsncat.c b/src/lib/libc/string/wcsncat.c new file mode 100644 index 0000000000..eb9a701514 --- /dev/null +++ b/src/lib/libc/string/wcsncat.c | |||
@@ -0,0 +1,52 @@ | |||
1 | /* $OpenBSD: wcsncat.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wcsncat.c,v 1.2 2001/01/03 14:29:36 lukem Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wcsncat.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | |||
34 | wchar_t * | ||
35 | wcsncat(wchar_t *s1, const wchar_t *s2, size_t n) | ||
36 | { | ||
37 | wchar_t *p; | ||
38 | wchar_t *q; | ||
39 | const wchar_t *r; | ||
40 | |||
41 | p = s1; | ||
42 | while (*p) | ||
43 | p++; | ||
44 | q = p; | ||
45 | r = s2; | ||
46 | while (*r && n) { | ||
47 | *q++ = *r++; | ||
48 | n--; | ||
49 | } | ||
50 | *q = '\0'; | ||
51 | return s1; | ||
52 | } | ||
diff --git a/src/lib/libc/string/wcsncmp.c b/src/lib/libc/string/wcsncmp.c new file mode 100644 index 0000000000..fb3c7057cf --- /dev/null +++ b/src/lib/libc/string/wcsncmp.c | |||
@@ -0,0 +1,52 @@ | |||
1 | /* $OpenBSD: wcsncmp.c,v 1.4 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wcsncmp.c,v 1.5 2003/08/07 16:43:54 agc Exp $ */ | ||
3 | |||
4 | /* | ||
5 | * Copyright (c) 1989, 1993 | ||
6 | * The Regents of the University of California. All rights reserved. | ||
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 | #include <wchar.h> | ||
34 | #include "locale/runetype.h" | ||
35 | |||
36 | int | ||
37 | wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n) | ||
38 | { | ||
39 | |||
40 | if (n == 0) | ||
41 | return (0); | ||
42 | do { | ||
43 | if (*s1 != *s2++) { | ||
44 | /* XXX assumes wchar_t = int */ | ||
45 | return (*(const rune_t *)s1 - | ||
46 | *(const rune_t *)--s2); | ||
47 | } | ||
48 | if (*s1++ == 0) | ||
49 | break; | ||
50 | } while (--n != 0); | ||
51 | return (0); | ||
52 | } | ||
diff --git a/src/lib/libc/string/wcsncpy.c b/src/lib/libc/string/wcsncpy.c new file mode 100644 index 0000000000..107696f1de --- /dev/null +++ b/src/lib/libc/string/wcsncpy.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* $OpenBSD: wcsncpy.c,v 1.4 2006/04/17 18:05:35 espie Exp $ */ | ||
2 | /* $NetBSD: wcsncpy.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wcsncpy.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | |||
34 | wchar_t * | ||
35 | wcsncpy(wchar_t *s1, const wchar_t *s2, size_t n) | ||
36 | { | ||
37 | wchar_t *p; | ||
38 | |||
39 | p = s1; | ||
40 | while (n && *s2) { | ||
41 | *p++ = *s2++; | ||
42 | n--; | ||
43 | } | ||
44 | while (n) { | ||
45 | *p++ = L'\0'; | ||
46 | n--; | ||
47 | } | ||
48 | |||
49 | return s1; | ||
50 | } | ||
diff --git a/src/lib/libc/string/wcspbrk.c b/src/lib/libc/string/wcspbrk.c new file mode 100644 index 0000000000..1923abfc89 --- /dev/null +++ b/src/lib/libc/string/wcspbrk.c | |||
@@ -0,0 +1,53 @@ | |||
1 | /* $OpenBSD: wcspbrk.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wcspbrk.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wcspbrk.c,v 1.2 2000/12/21 05:07:25 itojun Exp | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | |||
34 | wchar_t * | ||
35 | wcspbrk(const wchar_t *s, const wchar_t *set) | ||
36 | { | ||
37 | const wchar_t *p; | ||
38 | const wchar_t *q; | ||
39 | |||
40 | p = s; | ||
41 | while (*p) { | ||
42 | q = set; | ||
43 | while (*q) { | ||
44 | if (*p == *q) { | ||
45 | /* LINTED interface specification */ | ||
46 | return (wchar_t *)p; | ||
47 | } | ||
48 | q++; | ||
49 | } | ||
50 | p++; | ||
51 | } | ||
52 | return NULL; | ||
53 | } | ||
diff --git a/src/lib/libc/string/wcsrchr.c b/src/lib/libc/string/wcsrchr.c new file mode 100644 index 0000000000..3433310c56 --- /dev/null +++ b/src/lib/libc/string/wcsrchr.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* $OpenBSD: wcsrchr.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wcsrchr.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wcsrchr.c,v 1.2 2000/12/21 05:07:25 itojun Exp | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | |||
34 | wchar_t * | ||
35 | wcsrchr(const wchar_t *s, wchar_t c) | ||
36 | { | ||
37 | const wchar_t *p; | ||
38 | |||
39 | p = s; | ||
40 | while (*p) | ||
41 | p++; | ||
42 | while (s <= p) { | ||
43 | if (*p == c) { | ||
44 | /* LINTED interface specification */ | ||
45 | return (wchar_t *)p; | ||
46 | } | ||
47 | p--; | ||
48 | } | ||
49 | return NULL; | ||
50 | } | ||
diff --git a/src/lib/libc/string/wcsspn.c b/src/lib/libc/string/wcsspn.c new file mode 100644 index 0000000000..5162fa0763 --- /dev/null +++ b/src/lib/libc/string/wcsspn.c | |||
@@ -0,0 +1,55 @@ | |||
1 | /* $OpenBSD: wcsspn.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wcsspn.c,v 1.3 2001/09/21 16:09:15 yamt Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999,2001 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * $Citrus: xpg4dl/FreeBSD/lib/libc/string/wcsspn.c,v 1.3 2001/09/21 16:06:43 yamt Exp $ | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | |||
34 | size_t | ||
35 | wcsspn(const wchar_t *s, const wchar_t *set) | ||
36 | { | ||
37 | const wchar_t *p; | ||
38 | const wchar_t *q; | ||
39 | |||
40 | p = s; | ||
41 | while (*p) { | ||
42 | q = set; | ||
43 | while (*q) { | ||
44 | if (*p == *q) | ||
45 | break; | ||
46 | q++; | ||
47 | } | ||
48 | if (!*q) | ||
49 | goto done; | ||
50 | p++; | ||
51 | } | ||
52 | |||
53 | done: | ||
54 | return (p - s); | ||
55 | } | ||
diff --git a/src/lib/libc/string/wcsstr.c b/src/lib/libc/string/wcsstr.c new file mode 100644 index 0000000000..669e340280 --- /dev/null +++ b/src/lib/libc/string/wcsstr.c | |||
@@ -0,0 +1,70 @@ | |||
1 | /* $OpenBSD: wcsstr.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wcsstr.c,v 1.3 2003/03/05 20:18:17 tshiozak Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wcsstr.c,v 1.2 2000/12/21 05:07:25 itojun Exp | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | |||
34 | wchar_t * | ||
35 | #ifdef WCSWCS | ||
36 | wcswcs(const wchar_t *big, const wchar_t *little) | ||
37 | #else | ||
38 | wcsstr(const wchar_t *big, const wchar_t *little) | ||
39 | #endif | ||
40 | { | ||
41 | const wchar_t *p; | ||
42 | const wchar_t *q; | ||
43 | const wchar_t *r; | ||
44 | |||
45 | if (!*little) { | ||
46 | /* LINTED interface specification */ | ||
47 | return (wchar_t *)big; | ||
48 | } | ||
49 | if (wcslen(big) < wcslen(little)) | ||
50 | return NULL; | ||
51 | |||
52 | p = big; | ||
53 | q = little; | ||
54 | while (*p) { | ||
55 | q = little; | ||
56 | r = p; | ||
57 | while (*q) { | ||
58 | if (*r != *q) | ||
59 | break; | ||
60 | q++; | ||
61 | r++; | ||
62 | } | ||
63 | if (!*q) { | ||
64 | /* LINTED interface specification */ | ||
65 | return (wchar_t *)p; | ||
66 | } | ||
67 | p++; | ||
68 | } | ||
69 | return NULL; | ||
70 | } | ||
diff --git a/src/lib/libc/string/wcstok.3 b/src/lib/libc/string/wcstok.3 new file mode 100644 index 0000000000..fe65f120e3 --- /dev/null +++ b/src/lib/libc/string/wcstok.3 | |||
@@ -0,0 +1,136 @@ | |||
1 | .\" $OpenBSD: wcstok.3,v 1.5 2007/05/31 19:19:32 jmc Exp $ | ||
2 | .\" | ||
3 | .\" $NetBSD: wcstok.3,v 1.3 2003/09/08 17:54:33 wiz Exp $ | ||
4 | .\" | ||
5 | .\" Copyright (c) 1998 Softweyr LLC. All rights reserved. | ||
6 | .\" | ||
7 | .\" strtok_r, from Berkeley strtok | ||
8 | .\" Oct 13, 1998 by Wes Peters <wes@softweyr.com> | ||
9 | .\" | ||
10 | .\" Copyright (c) 1988, 1991, 1993 | ||
11 | .\" The Regents of the University of California. All rights reserved. | ||
12 | .\" | ||
13 | .\" This code is derived from software contributed to Berkeley by | ||
14 | .\" the American National Standards Committee X3, on Information | ||
15 | .\" Processing Systems. | ||
16 | .\" | ||
17 | .\" Redistribution and use in source and binary forms, with or without | ||
18 | .\" modification, are permitted provided that the following conditions | ||
19 | .\" are met: | ||
20 | .\" | ||
21 | .\" 1. Redistributions of source code must retain the above copyright | ||
22 | .\" notices, this list of conditions and the following disclaimer. | ||
23 | .\" | ||
24 | .\" 2. Redistributions in binary form must reproduce the above | ||
25 | .\" copyright notices, this list of conditions and the following | ||
26 | .\" disclaimer in the documentation and/or other materials provided | ||
27 | .\" with the distribution. | ||
28 | .\" | ||
29 | .\" 3. All advertising materials mentioning features or use of this | ||
30 | .\" software must display the following acknowledgement: | ||
31 | .\" | ||
32 | .\" This product includes software developed by Softweyr LLC, the | ||
33 | .\" University of California, Berkeley, and its contributors. | ||
34 | .\" | ||
35 | .\" 4. Neither the name of Softweyr LLC, the University nor the names | ||
36 | .\" of its contributors may be used to endorse or promote products | ||
37 | .\" derived from this software without specific prior written | ||
38 | .\" permission. | ||
39 | .\" | ||
40 | .\" THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND | ||
41 | .\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
42 | .\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
43 | .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
44 | .\" DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE REGENTS, OR | ||
45 | .\" CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
46 | .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
47 | .\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
48 | .\" USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
49 | .\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
50 | .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | ||
51 | .\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
52 | .\" SUCH DAMAGE. | ||
53 | .\" | ||
54 | .\" Original version ID: | ||
55 | .\" FreeBSD: src/lib/libc/string/wcstok.3,v 1.4 2002/10/15 09:49:54 tjr Exp | ||
56 | .\" | ||
57 | .Dd $Mdocdate: May 31 2007 $ | ||
58 | .Dt WCSTOK 3 | ||
59 | .Os | ||
60 | .Sh NAME | ||
61 | .Nm wcstok | ||
62 | .Nd split wide-character string into tokens | ||
63 | .Sh SYNOPSIS | ||
64 | .In wchar.h | ||
65 | .Ft wchar_t * | ||
66 | .Fn wcstok "wchar_t * restrict str" "const wchar_t * restrict sep" "wchar_t ** restrict last" | ||
67 | .Sh DESCRIPTION | ||
68 | The | ||
69 | .Fn wcstok | ||
70 | function | ||
71 | is used to isolate sequential tokens in a NUL-terminated wide character | ||
72 | string, | ||
73 | .Fa str . | ||
74 | These tokens are separated in the string by at least one of the | ||
75 | characters in | ||
76 | .Fa sep . | ||
77 | The first time that | ||
78 | .Fn wcstok | ||
79 | is called, | ||
80 | .Fa str | ||
81 | should be specified; subsequent calls, wishing to obtain further tokens | ||
82 | from the same string, should pass a null pointer instead. | ||
83 | The separator string, | ||
84 | .Fa sep , | ||
85 | must be supplied each time, and may change between calls. | ||
86 | The context pointer | ||
87 | .Fa last | ||
88 | must be provided on each call. | ||
89 | .Pp | ||
90 | The | ||
91 | .Fn wcstok | ||
92 | function is the wide character counterpart of the | ||
93 | .Fn strtok_r | ||
94 | function. | ||
95 | .Sh RETURN VALUES | ||
96 | The | ||
97 | .Fn wcstok | ||
98 | function | ||
99 | returns a pointer to the beginning of each subsequent token in the string, | ||
100 | after replacing the token itself with a NUL wide character (L'\e0'). | ||
101 | When no more tokens remain, a null pointer is returned. | ||
102 | .Sh EXAMPLES | ||
103 | The following code fragment splits a wide character string on | ||
104 | .Tn ASCII | ||
105 | space, tab, and newline characters and writes the tokens to | ||
106 | standard output: | ||
107 | .Bd -literal -offset indent | ||
108 | const wchar_t *seps = L" \et\en"; | ||
109 | wchar_t *last, *tok, text[] = L" \enone\ettwo\et\etthree \en"; | ||
110 | |||
111 | for (tok = wcstok(text, seps, &last); tok != NULL; | ||
112 | tok = wcstok(NULL, seps, &last)) | ||
113 | wprintf(L"%ls\en", tok); | ||
114 | .Ed | ||
115 | .Sh SEE ALSO | ||
116 | .Xr strtok 3 , | ||
117 | .Xr wcschr 3 , | ||
118 | .Xr wcscspn 3 , | ||
119 | .Xr wcspbrk 3 , | ||
120 | .Xr wcsrchr 3 , | ||
121 | .Xr wcsspn 3 | ||
122 | .Sh STANDARDS | ||
123 | The | ||
124 | .Fn wcstok | ||
125 | function | ||
126 | conforms to | ||
127 | .St -isoC-99 . | ||
128 | .Sh HISTORY | ||
129 | Some early implementations of | ||
130 | .Fn wcstok | ||
131 | omit the | ||
132 | context pointer argument, | ||
133 | .Fa last , | ||
134 | and maintain state across calls in a static variable like | ||
135 | .Fn strtok | ||
136 | does. | ||
diff --git a/src/lib/libc/string/wcstok.c b/src/lib/libc/string/wcstok.c new file mode 100644 index 0000000000..bc1ac3e8cb --- /dev/null +++ b/src/lib/libc/string/wcstok.c | |||
@@ -0,0 +1,93 @@ | |||
1 | /* $OpenBSD: wcstok.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wcstok.c,v 1.3 2003/07/10 08:50:48 tshiozak Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c) 1998 Softweyr LLC. All rights reserved. | ||
6 | * | ||
7 | * strtok_r, from Berkeley strtok | ||
8 | * Oct 13, 1998 by Wes Peters <wes@softweyr.com> | ||
9 | * | ||
10 | * Copyright (c) 1988, 1993 | ||
11 | * The Regents of the University of California. All rights reserved. | ||
12 | * | ||
13 | * Redistribution and use in source and binary forms, with or without | ||
14 | * modification, are permitted provided that the following conditions | ||
15 | * are met: | ||
16 | * 1. Redistributions of source code must retain the above copyright | ||
17 | * notices, this list of conditions and the following disclaimer. | ||
18 | * 2. Redistributions in binary form must reproduce the above copyright | ||
19 | * notices, this list of conditions and the following disclaimer in the | ||
20 | * documentation and/or other materials provided with the distribution. | ||
21 | * 3. All advertising materials mentioning features or use of this software | ||
22 | * must display the following acknowledgement: | ||
23 | * This product includes software developed by Softweyr LLC, the | ||
24 | * University of California, Berkeley, and its contributors. | ||
25 | * 4. Neither the name of the University nor the names of its contributors | ||
26 | * may be used to endorse or promote products derived from this software | ||
27 | * without specific prior written permission. | ||
28 | * | ||
29 | * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS | ||
30 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
31 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
32 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE | ||
33 | * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
34 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | ||
35 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
36 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
37 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
38 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
39 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
40 | * | ||
41 | * Original version ID: | ||
42 | * FreeBSD: src/lib/libc/string/wcstok.c,v 1.1 2002/09/07 08:16:57 tjr Exp | ||
43 | */ | ||
44 | |||
45 | #include <wchar.h> | ||
46 | |||
47 | wchar_t * | ||
48 | wcstok(wchar_t * __restrict s, const wchar_t * __restrict delim, | ||
49 | wchar_t ** __restrict last) | ||
50 | { | ||
51 | const wchar_t *spanp; | ||
52 | wchar_t c, sc; | ||
53 | wchar_t *tok; | ||
54 | |||
55 | if (s == NULL && (s = *last) == NULL) | ||
56 | return (NULL); | ||
57 | |||
58 | /* | ||
59 | * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of). | ||
60 | */ | ||
61 | cont: | ||
62 | c = *s++; | ||
63 | for (spanp = delim; (sc = *spanp++) != L'\0';) { | ||
64 | if (c == sc) | ||
65 | goto cont; | ||
66 | } | ||
67 | |||
68 | if (c == L'\0') { /* no non-delimiter characters */ | ||
69 | *last = NULL; | ||
70 | return (NULL); | ||
71 | } | ||
72 | tok = s - 1; | ||
73 | |||
74 | /* | ||
75 | * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of). | ||
76 | * Note that delim must have one NUL; we stop if we see that, too. | ||
77 | */ | ||
78 | for (;;) { | ||
79 | c = *s++; | ||
80 | spanp = delim; | ||
81 | do { | ||
82 | if ((sc = *spanp++) == c) { | ||
83 | if (c == L'\0') | ||
84 | s = NULL; | ||
85 | else | ||
86 | s[-1] = L'\0'; | ||
87 | *last = s; | ||
88 | return (tok); | ||
89 | } | ||
90 | } while (sc != L'\0'); | ||
91 | } | ||
92 | /* NOTREACHED */ | ||
93 | } | ||
diff --git a/src/lib/libc/string/wcswcs.c b/src/lib/libc/string/wcswcs.c new file mode 100644 index 0000000000..bd35605547 --- /dev/null +++ b/src/lib/libc/string/wcswcs.c | |||
@@ -0,0 +1,5 @@ | |||
1 | /* $OpenBSD: wcswcs.c,v 1.1 2005/04/13 16:35:58 espie Exp $ */ | ||
2 | /* $NetBSD: wcswcs.c,v 1.1 2003/03/05 20:18:17 tshiozak Exp $ */ | ||
3 | |||
4 | #define WCSWCS | ||
5 | #include "wcsstr.c" | ||
diff --git a/src/lib/libc/string/wcswidth.c b/src/lib/libc/string/wcswidth.c new file mode 100644 index 0000000000..cd240897b5 --- /dev/null +++ b/src/lib/libc/string/wcswidth.c | |||
@@ -0,0 +1,47 @@ | |||
1 | /* $OpenBSD: wcswidth.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wcswidth.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wcswidth.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | |||
34 | int | ||
35 | wcswidth(const wchar_t *s, size_t n) | ||
36 | { | ||
37 | int w; | ||
38 | |||
39 | w = 0; | ||
40 | while (n && *s) { | ||
41 | w += wcwidth(*s); | ||
42 | s++; | ||
43 | n--; | ||
44 | } | ||
45 | |||
46 | return w; | ||
47 | } | ||
diff --git a/src/lib/libc/string/wmemchr.3 b/src/lib/libc/string/wmemchr.3 new file mode 100644 index 0000000000..395488829d --- /dev/null +++ b/src/lib/libc/string/wmemchr.3 | |||
@@ -0,0 +1,141 @@ | |||
1 | .\" $OpenBSD: wmemchr.3,v 1.3 2007/05/31 19:19:32 jmc Exp $ | ||
2 | .\" | ||
3 | .\" $NetBSD: wmemchr.3,v 1.9 2003/09/08 17:54:33 wiz Exp $ | ||
4 | .\" | ||
5 | .\" Copyright (c) 1990, 1991, 1993 | ||
6 | .\" The Regents of the University of California. All rights reserved. | ||
7 | .\" | ||
8 | .\" This code is derived from software contributed to Berkeley by | ||
9 | .\" Chris Torek and the American National Standards Committee X3, | ||
10 | .\" on Information Processing Systems. | ||
11 | .\" | ||
12 | .\" Redistribution and use in source and binary forms, with or without | ||
13 | .\" modification, are permitted provided that the following conditions | ||
14 | .\" are met: | ||
15 | .\" 1. Redistributions of source code must retain the above copyright | ||
16 | .\" notice, this list of conditions and the following disclaimer. | ||
17 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
18 | .\" notice, this list of conditions and the following disclaimer in the | ||
19 | .\" documentation and/or other materials provided with the distribution. | ||
20 | .\" 3. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | ||
22 | .\" without specific prior written permission. | ||
23 | .\" | ||
24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
34 | .\" SUCH DAMAGE. | ||
35 | .\" | ||
36 | .\" from: @(#)strcpy.3 8.1 (Berkeley) 6/4/93 | ||
37 | .\" | ||
38 | .Dd $Mdocdate: May 31 2007 $ | ||
39 | .Dt WMEMCHR 3 | ||
40 | .Os | ||
41 | .Sh NAME | ||
42 | .Nm wmemchr , | ||
43 | .Nm wmemcmp , | ||
44 | .Nm wmemcpy , | ||
45 | .Nm wmemmove , | ||
46 | .Nm wmemset , | ||
47 | .Nm wcscat , | ||
48 | .Nm wcschr , | ||
49 | .Nm wcscmp , | ||
50 | .Nm wcscpy , | ||
51 | .Nm wcscspn , | ||
52 | .Nm wcslcat , | ||
53 | .Nm wcslcpy , | ||
54 | .Nm wcslen , | ||
55 | .Nm wcsncat , | ||
56 | .Nm wcsncmp , | ||
57 | .Nm wcsncpy , | ||
58 | .Nm wcspbrk , | ||
59 | .Nm wcsrchr , | ||
60 | .Nm wcsspn , | ||
61 | .Nm wcsstr | ||
62 | .Nd wide character string manipulation operations | ||
63 | .Sh SYNOPSIS | ||
64 | .In wchar.h | ||
65 | .Ft wchar_t * | ||
66 | .Fn wmemchr "const wchar_t *s" "wchar_t c" "size_t n" | ||
67 | .Ft int | ||
68 | .Fn wmemcmp "const wchar_t *s1" "const wchar_t *s2" "size_t n" | ||
69 | .Ft wchar_t * | ||
70 | .Fn wmemcpy "wchar_t * restrict s1" "const wchar_t * restrict s2" "size_t n" | ||
71 | .Ft wchar_t * | ||
72 | .Fn wmemmove "wchar_t *s1" "const wchar_t *s2" "size_t n" | ||
73 | .Ft wchar_t * | ||
74 | .Fn wmemset "wchar_t *s" "wchar_t c" "size_t n" | ||
75 | .Ft wchar_t * | ||
76 | .Fn wcscat "wchar_t * restrict s1" "const wchar_t * restrict s2" | ||
77 | .Ft wchar_t * | ||
78 | .Fn wcschr "const wchar_t *s" "wchar_t c" | ||
79 | .Ft int | ||
80 | .Fn wcscmp "const wchar_t *s1" "const wchar_t *s2" | ||
81 | .Ft wchar_t * | ||
82 | .Fn wcscpy "wchar_t * restrict s1" "const wchar_t * restrict s2" | ||
83 | .Ft size_t | ||
84 | .Fn wcscspn "const wchar_t *s1" "const wchar_t *s2" | ||
85 | .Ft size_t | ||
86 | .Fn wcslcat "wchar_t *s1" "const wchar_t *s2" "size_t n" | ||
87 | .Ft size_t | ||
88 | .Fn wcslcpy "wchar_t *s1" "const wchar_t *s2" "size_t n" | ||
89 | .Ft size_t | ||
90 | .Fn wcslen "const wchar_t *s" | ||
91 | .Ft wchar_t * | ||
92 | .Fn wcsncat "wchar_t * restrict s1" "const wchar_t * restrict s2" "size_t n" | ||
93 | .Ft int | ||
94 | .Fn wcsncmp "const wchar_t *s1" "const wchar_t * s2" "size_t n" | ||
95 | .Ft wchar_t * | ||
96 | .Fn wcsncpy "wchar_t * restrict s1" "const wchar_t * restrict s2" "size_t n" | ||
97 | .Ft wchar_t * | ||
98 | .Fn wcspbrk "const wchar_t *s1" "const wchar_t *s2" | ||
99 | .Ft wchar_t * | ||
100 | .Fn wcsrchr "const wchar_t *s" "wchar_t c" | ||
101 | .Ft size_t | ||
102 | .Fn wcsspn "const wchar_t *s1" "const wchar_t *s2" | ||
103 | .Ft wchar_t * | ||
104 | .Fn wcsstr "const wchar_t *s1" "const wchar_t *s2" | ||
105 | .Sh DESCRIPTION | ||
106 | These functions implement string manipulation operations over wide character | ||
107 | strings. | ||
108 | For a detailed description, refer to documents for the respective single-byte | ||
109 | counterpart, such as | ||
110 | .Xr memchr 3 . | ||
111 | .Sh SEE ALSO | ||
112 | .Xr memchr 3 , | ||
113 | .Xr memcmp 3 , | ||
114 | .Xr memcpy 3 , | ||
115 | .Xr memmove 3 , | ||
116 | .Xr memset 3 , | ||
117 | .Xr strcat 3 , | ||
118 | .Xr strchr 3 , | ||
119 | .Xr strcmp 3 , | ||
120 | .Xr strcpy 3 , | ||
121 | .Xr strcspn 3 , | ||
122 | .Xr strlcat 3 , | ||
123 | .Xr strlcpy 3 , | ||
124 | .Xr strlen 3 , | ||
125 | .Xr strncat 3 , | ||
126 | .Xr strncmp 3 , | ||
127 | .Xr strncpy 3 , | ||
128 | .Xr strpbrk 3 , | ||
129 | .Xr strrchr 3 , | ||
130 | .Xr strspn 3 , | ||
131 | .Xr strstr 3 | ||
132 | .Sh STANDARDS | ||
133 | These functions conform to | ||
134 | .St -isoC-99 | ||
135 | and were first introduced in | ||
136 | .St -isoC-amd1 , | ||
137 | with the exception of | ||
138 | .Fn wcslcat | ||
139 | and | ||
140 | .Fn wcslcpy , | ||
141 | which are extensions. | ||
diff --git a/src/lib/libc/string/wmemchr.c b/src/lib/libc/string/wmemchr.c new file mode 100644 index 0000000000..8c9517861b --- /dev/null +++ b/src/lib/libc/string/wmemchr.c | |||
@@ -0,0 +1,47 @@ | |||
1 | /* $OpenBSD: wmemchr.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wmemchr.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wmemchr.c,v 1.2 2000/12/20 14:08:31 itojun Exp | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | |||
34 | wchar_t * | ||
35 | wmemchr(const wchar_t *s, wchar_t c, size_t n) | ||
36 | { | ||
37 | size_t i; | ||
38 | |||
39 | for (i = 0; i < n; i++) { | ||
40 | if (*s == c) { | ||
41 | /* LINTED const castaway */ | ||
42 | return (wchar_t *)s; | ||
43 | } | ||
44 | s++; | ||
45 | } | ||
46 | return NULL; | ||
47 | } | ||
diff --git a/src/lib/libc/string/wmemcmp.c b/src/lib/libc/string/wmemcmp.c new file mode 100644 index 0000000000..39419aeddc --- /dev/null +++ b/src/lib/libc/string/wmemcmp.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* $OpenBSD: wmemcmp.c,v 1.4 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wmemcmp.c,v 1.3 2003/04/06 18:33:23 tshiozak Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wmemcmp.c,v 1.2 2000/12/20 14:08:31 itojun Exp | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | #include "locale/runetype.h" | ||
34 | |||
35 | int | ||
36 | wmemcmp(const wchar_t *s1, const wchar_t *s2, size_t n) | ||
37 | { | ||
38 | size_t i; | ||
39 | |||
40 | for (i = 0; i < n; i++) { | ||
41 | if (*s1 != *s2) { | ||
42 | /* wchar might be unsigned */ | ||
43 | return *(const rune_t *)s1 > | ||
44 | *(const rune_t *)s2 ? 1 : -1; | ||
45 | } | ||
46 | s1++; | ||
47 | s2++; | ||
48 | } | ||
49 | return 0; | ||
50 | } | ||
diff --git a/src/lib/libc/string/wmemcpy.c b/src/lib/libc/string/wmemcpy.c new file mode 100644 index 0000000000..9bbd83648e --- /dev/null +++ b/src/lib/libc/string/wmemcpy.c | |||
@@ -0,0 +1,40 @@ | |||
1 | /* $OpenBSD: wmemcpy.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wmemcpy.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wmemcpy.c,v 1.2 2000/12/20 14:08:31 itojun Exp | ||
30 | */ | ||
31 | |||
32 | #include <string.h> | ||
33 | #include <wchar.h> | ||
34 | |||
35 | wchar_t * | ||
36 | wmemcpy(wchar_t *d, const wchar_t *s, size_t n) | ||
37 | { | ||
38 | |||
39 | return (wchar_t *)memcpy(d, s, n * sizeof(wchar_t)); | ||
40 | } | ||
diff --git a/src/lib/libc/string/wmemmove.c b/src/lib/libc/string/wmemmove.c new file mode 100644 index 0000000000..21bbabcd0c --- /dev/null +++ b/src/lib/libc/string/wmemmove.c | |||
@@ -0,0 +1,40 @@ | |||
1 | /* $OpenBSD: wmemmove.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wmemmove.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wmemmove.c,v 1.2 2000/12/20 14:08:31 itojun Exp | ||
30 | */ | ||
31 | |||
32 | #include <string.h> | ||
33 | #include <wchar.h> | ||
34 | |||
35 | wchar_t * | ||
36 | wmemmove(wchar_t *d, const wchar_t *s, size_t n) | ||
37 | { | ||
38 | |||
39 | return (wchar_t *)memmove(d, s, n * sizeof(wchar_t)); | ||
40 | } | ||
diff --git a/src/lib/libc/string/wmemset.c b/src/lib/libc/string/wmemset.c new file mode 100644 index 0000000000..ac476dba6a --- /dev/null +++ b/src/lib/libc/string/wmemset.c | |||
@@ -0,0 +1,46 @@ | |||
1 | /* $OpenBSD: wmemset.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* $NetBSD: wmemset.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c)1999 Citrus Project, | ||
6 | * All rights reserved. | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | * citrus Id: wmemset.c,v 1.2 2000/12/20 14:08:31 itojun Exp | ||
30 | */ | ||
31 | |||
32 | #include <wchar.h> | ||
33 | |||
34 | wchar_t * | ||
35 | wmemset(wchar_t *s, wchar_t c, size_t n) | ||
36 | { | ||
37 | size_t i; | ||
38 | wchar_t *p; | ||
39 | |||
40 | p = s; | ||
41 | for (i = 0; i < n; i++) { | ||
42 | *p = c; | ||
43 | p++; | ||
44 | } | ||
45 | return s; | ||
46 | } | ||