HAMMER 60F/Many: Mirroring
[dragonfly.git] / lib / libc / string / strtok.c
blobded182e389ed01c320d9cfad0acd3ba27afb49ef
1 /*
2 * Copyright (c) 1998 Softweyr LLC. All rights reserved.
4 * strtok_r, from Berkeley strtok
5 * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
7 * Copyright (c) 1988, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notices, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notices, this list of conditions and the following disclaimer in the
19 * 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:
24 * This product includes software developed by Softweyr LLC, the
25 * University of California, Berkeley, and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
34 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
35 * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
37 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
38 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 * $FreeBSD: src/lib/libc/string/strtok.c,v 1.2.6.1 2001/07/09 23:30:07 obrien Exp $
44 * $DragonFly: src/lib/libc/string/strtok.c,v 1.3 2005/04/28 13:25:12 joerg Exp $
47 #include <stddef.h>
48 #include <string.h>
50 char *
51 strtok_r(char *s, const char *delim, char **last)
53 const char *spanp;
54 int c, sc;
55 char *tok;
57 if (s == NULL && (s = *last) == NULL)
59 return NULL;
63 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
65 cont:
66 c = *s++;
67 for (spanp = delim; (sc = *spanp++) != 0; )
69 if (c == sc)
71 goto cont;
75 if (c == 0) /* no non-delimiter characters */
77 *last = NULL;
78 return NULL;
80 tok = s - 1;
83 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
84 * Note that delim must have one NUL; we stop if we see that, too.
86 for (;;)
88 c = *s++;
89 spanp = delim;
92 if ((sc = *spanp++) == c)
94 if (c == 0)
96 s = NULL;
98 else
100 char *w = s - 1;
101 *w = '\0';
103 *last = s;
104 return tok;
107 while (sc != 0);
109 /* NOTREACHED */
113 char *
114 strtok(char *s, const char *delim)
116 static char *last;
118 return strtok_r(s, delim, &last);
122 #if defined(DEBUG_STRTOK)
125 * Test the tokenizer.
128 main()
130 char test[80], blah[80];
131 char *sep = "\\/:;=-";
132 char *word, *phrase, *brkt, *brkb;
134 printf("String tokenizer test:\n");
136 strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
138 for (word = strtok(test, sep);
139 word;
140 word = strtok(NULL, sep))
142 printf("Next word is \"%s\".\n", word);
145 phrase = "foo";
147 strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
149 for (word = strtok_r(test, sep, &brkt);
150 word;
151 word = strtok_r(NULL, sep, &brkt))
153 strcpy(blah, "blah:blat:blab:blag");
155 for (phrase = strtok_r(blah, sep, &brkb);
156 phrase;
157 phrase = strtok_r(NULL, sep, &brkb))
159 printf("So far we're at %s:%s\n", word, phrase);
163 return 0;
166 #endif /* DEBUG_STRTOK */