diff options
author | guenther <> | 2012-01-17 02:48:01 +0000 |
---|---|---|
committer | guenther <> | 2012-01-17 02:48:01 +0000 |
commit | 4eca3ab70369d6a5f04d2cab60742d0d7f171399 (patch) | |
tree | 10185abaa3846292f512d026acaf2a4992992b28 | |
parent | efaeb5589cc66ce5e81f0b91547c67428ad04ed5 (diff) | |
download | openbsd-4eca3ab70369d6a5f04d2cab60742d0d7f171399.tar.gz openbsd-4eca3ab70369d6a5f04d2cab60742d0d7f171399.tar.bz2 openbsd-4eca3ab70369d6a5f04d2cab60742d0d7f171399.zip |
Add stpcpy() and stpncpy(), pointless crap that made it into POSIX.
Includes friendly linker warning to wake up those dumb enough to use it.
Committing for kettenis@, who ran away to the hills after writing it
-rw-r--r-- | src/lib/libc/string/Makefile.inc | 8 | ||||
-rw-r--r-- | src/lib/libc/string/stpcpy.3 | 117 | ||||
-rw-r--r-- | src/lib/libc/string/stpcpy.c | 44 | ||||
-rw-r--r-- | src/lib/libc/string/stpncpy.c | 56 |
4 files changed, 222 insertions, 3 deletions
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc index 40713956e5..25e67e4f87 100644 --- a/src/lib/libc/string/Makefile.inc +++ b/src/lib/libc/string/Makefile.inc | |||
@@ -1,9 +1,10 @@ | |||
1 | # $OpenBSD: Makefile.inc,v 1.27 2011/07/09 16:32:11 nicm Exp $ | 1 | # $OpenBSD: Makefile.inc,v 1.28 2012/01/17 02:48:01 guenther Exp $ |
2 | 2 | ||
3 | # string sources | 3 | # string sources |
4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string | 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string |
5 | 5 | ||
6 | SRCS+= bm.c memccpy.c memrchr.c strcasecmp.c strcasestr.c strcoll.c strdup.c \ | 6 | SRCS+= bm.c memccpy.c memrchr.c stpcpy.c stpncpy.c \ |
7 | strcasecmp.c strcasestr.c strcoll.c strdup.c \ | ||
7 | strerror.c strerror_r.c strlcat.c strmode.c strndup.c strnlen.c \ | 8 | strerror.c strerror_r.c strlcat.c strmode.c strndup.c strnlen.c \ |
8 | strsignal.c strtok.c strxfrm.c \ | 9 | strsignal.c strtok.c strxfrm.c \ |
9 | wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ | 10 | wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ |
@@ -140,7 +141,7 @@ strrchr.ln: rindex.c | |||
140 | ${LIBCSRCDIR}/string/rindex.c | 141 | ${LIBCSRCDIR}/string/rindex.c |
141 | 142 | ||
142 | MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \ | 143 | MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \ |
143 | memcmp.3 memcpy.3 memmove.3 memset.3 strcasecmp.3 strcat.3 \ | 144 | memcmp.3 memcpy.3 memmove.3 memset.3 stpcpy.3 strcasecmp.3 strcat.3 \ |
144 | strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strerror.3 \ | 145 | strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strerror.3 \ |
145 | string.3 strlen.3 strmode.3 strdup.3 strpbrk.3 strrchr.3 strsep.3 \ | 146 | string.3 strlen.3 strmode.3 strdup.3 strpbrk.3 strrchr.3 strsep.3 \ |
146 | strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 strlcpy.3 \ | 147 | strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 strlcpy.3 \ |
@@ -150,6 +151,7 @@ MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \ | |||
150 | 151 | ||
151 | MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3 | 152 | MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3 |
152 | MLINKS+=memchr.3 memrchr.3 | 153 | MLINKS+=memchr.3 memrchr.3 |
154 | MLINKS+=stpcpy.3 stpncpy.3 | ||
153 | MLINKS+=strchr.3 index.3 | 155 | MLINKS+=strchr.3 index.3 |
154 | MLINKS+=strrchr.3 rindex.3 | 156 | MLINKS+=strrchr.3 rindex.3 |
155 | MLINKS+=strcasecmp.3 strncasecmp.3 | 157 | MLINKS+=strcasecmp.3 strncasecmp.3 |
diff --git a/src/lib/libc/string/stpcpy.3 b/src/lib/libc/string/stpcpy.3 new file mode 100644 index 0000000000..7067186e2b --- /dev/null +++ b/src/lib/libc/string/stpcpy.3 | |||
@@ -0,0 +1,117 @@ | |||
1 | .\" $OpenBSD: stpcpy.3,v 1.1 2012/01/17 02:48:01 guenther Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1990, 1991 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 and the American National Standards Committee X3, | ||
8 | .\" on Information Processing Systems. | ||
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 | .Dd $Mdocdate: January 17 2012 $ | ||
35 | .Dt STPCPY 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm stpcpy , | ||
39 | .Nm stpncpy | ||
40 | .Nd copy strings | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <string.h> | ||
43 | .Ft char * | ||
44 | .Fn stpcpy "char *dst" "const char *src" | ||
45 | .Ft char * | ||
46 | .Fn stpncpy "char *dst" "const char *src" "size_t len" | ||
47 | .Sh DESCRIPTION | ||
48 | The | ||
49 | .Fn stpcpy | ||
50 | and | ||
51 | .Fn stpncpy | ||
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 | The | ||
61 | .Fn stpncpy | ||
62 | function copies not more than | ||
63 | .Fa len | ||
64 | characters into | ||
65 | .Fa dst , | ||
66 | appending | ||
67 | .Ql \e0 | ||
68 | characters if | ||
69 | .Fa src | ||
70 | is less than | ||
71 | .Fa len | ||
72 | characters long, and | ||
73 | .Em not | ||
74 | terminating | ||
75 | .Fa dst | ||
76 | if the length of | ||
77 | .Fa src | ||
78 | is greater than or equal to | ||
79 | .Fa len . | ||
80 | .Sh RETURN VALUES | ||
81 | The | ||
82 | .Fn stpcpy | ||
83 | function returns a pointer to the terminating | ||
84 | .Ql \e0 | ||
85 | character written into | ||
86 | .Fa dst . | ||
87 | .Pp | ||
88 | The | ||
89 | .Fn stpncpy | ||
90 | function returns a pointer to the first | ||
91 | .Ql \e0 | ||
92 | character written into | ||
93 | .Fa dst , | ||
94 | or to | ||
95 | .Fa &dst[len] | ||
96 | if the length of | ||
97 | .Fa src | ||
98 | is greater than or equal to | ||
99 | .Fa len . | ||
100 | .Sh SEE ALSO | ||
101 | .Xr strcpy 3 , | ||
102 | .Xr strncpy 3 , | ||
103 | .Xr strlcpy 3 , | ||
104 | .Sh STANDARDS | ||
105 | The | ||
106 | .Fn stpcpy | ||
107 | and | ||
108 | .Fn stpncpy | ||
109 | functions conform to | ||
110 | .St -p1003.1-2008 . | ||
111 | .Sh HISTORY | ||
112 | The | ||
113 | .Fn stpcpy | ||
114 | and | ||
115 | .Fn stpncpy | ||
116 | functions first appeared in | ||
117 | .Ox 5.1 . | ||
diff --git a/src/lib/libc/string/stpcpy.c b/src/lib/libc/string/stpcpy.c new file mode 100644 index 0000000000..d3d61e0f14 --- /dev/null +++ b/src/lib/libc/string/stpcpy.c | |||
@@ -0,0 +1,44 @@ | |||
1 | /* $OpenBSD: stpcpy.c,v 1.1 2012/01/17 02:48:01 guenther 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 | #include <string.h> | ||
33 | |||
34 | #if defined(APIWARN) | ||
35 | __warn_references(stpcpy, | ||
36 | "warning: stpcpy() is dangerous GNU crap; don't use it"); | ||
37 | #endif | ||
38 | |||
39 | char * | ||
40 | stpcpy(char *to, const char *from) | ||
41 | { | ||
42 | for (; (*to = *from) != '\0'; ++from, ++to); | ||
43 | return(to); | ||
44 | } | ||
diff --git a/src/lib/libc/string/stpncpy.c b/src/lib/libc/string/stpncpy.c new file mode 100644 index 0000000000..b772486d59 --- /dev/null +++ b/src/lib/libc/string/stpncpy.c | |||
@@ -0,0 +1,56 @@ | |||
1 | /* $OpenBSD: stpncpy.c,v 1.1 2012/01/17 02:48:01 guenther 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 | #include <string.h> | ||
36 | |||
37 | char * | ||
38 | stpncpy(char *dst, const char *src, size_t n) | ||
39 | { | ||
40 | if (n != 0) { | ||
41 | char *d = dst; | ||
42 | const char *s = src; | ||
43 | |||
44 | dst = &dst[n]; | ||
45 | do { | ||
46 | if ((*d++ = *s++) == 0) { | ||
47 | dst = d; | ||
48 | /* NUL pad the remaining n-1 bytes */ | ||
49 | while (--n != 0) | ||
50 | *d++ = 0; | ||
51 | break; | ||
52 | } | ||
53 | } while (--n != 0); | ||
54 | } | ||
55 | return (dst); | ||
56 | } | ||