summaryrefslogtreecommitdiff
path: root/src/lib/libc/string/wcstok.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/string/wcstok.c')
-rw-r--r--src/lib/libc/string/wcstok.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/lib/libc/string/wcstok.c b/src/lib/libc/string/wcstok.c
new file mode 100644
index 0000000000..1b0790fdc6
--- /dev/null
+++ b/src/lib/libc/string/wcstok.c
@@ -0,0 +1,99 @@
1/* $OpenBSD: wcstok.c,v 1.1 2005/04/13 16:35:58 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#if defined(LIBC_SCCS) && !defined(lint)
46static char *rcsid = "$OpenBSD: wcstok.c,v 1.1 2005/04/13 16:35:58 espie Exp $";
47#endif /* LIBC_SCCS and not lint */
48
49#include <wchar.h>
50
51wchar_t *
52wcstok(s, delim, last)
53 wchar_t * __restrict s;
54 const wchar_t * __restrict delim;
55 wchar_t ** __restrict last;
56{
57 const wchar_t *spanp;
58 wchar_t c, sc;
59 wchar_t *tok;
60
61 if (s == NULL && (s = *last) == NULL)
62 return (NULL);
63
64 /*
65 * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
66 */
67cont:
68 c = *s++;
69 for (spanp = delim; (sc = *spanp++) != L'\0';) {
70 if (c == sc)
71 goto cont;
72 }
73
74 if (c == L'\0') { /* no non-delimiter characters */
75 *last = NULL;
76 return (NULL);
77 }
78 tok = s - 1;
79
80 /*
81 * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
82 * Note that delim must have one NUL; we stop if we see that, too.
83 */
84 for (;;) {
85 c = *s++;
86 spanp = delim;
87 do {
88 if ((sc = *spanp++) == c) {
89 if (c == L'\0')
90 s = NULL;
91 else
92 s[-1] = L'\0';
93 *last = s;
94 return (tok);
95 }
96 } while (sc != L'\0');
97 }
98 /* NOTREACHED */
99}