Sync lib/libc/string with FreeBSD:
[dragonfly.git] / lib / libc / string / strtok.c
CommitLineData
716024cd 1/*-
984263bc
MD
2 * Copyright (c) 1998 Softweyr LLC. All rights reserved.
3 *
4 * strtok_r, from Berkeley strtok
5 * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
6 *
7 * Copyright (c) 1988, 1993
8 * The Regents of the University of California. All rights reserved.
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:
984263bc
MD
13 * 1. Redistributions of source code must retain the above copyright
14 * notices, this list of conditions and the following disclaimer.
984263bc
MD
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notices, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
984263bc
MD
18 * 4. 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 SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
26 * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1de703da 33 *
716024cd
PA
34 * @(#)strtok.c 8.1 (Berkeley) 6/4/93
35 * $FreeBSD: src/lib/libc/string/strtok.c,v 1.10 2007/12/12 18:33:06 wes Exp $
c01a9d5f 36 * $DragonFly: src/lib/libc/string/strtok.c,v 1.3 2005/04/28 13:25:12 joerg Exp $
984263bc
MD
37 */
38
984263bc 39#include <stddef.h>
716024cd
PA
40#ifdef DEBUG_STRTOK
41#include <stdio.h>
42#endif
984263bc
MD
43#include <string.h>
44
716024cd
PA
45char *__strtok_r(char *, const char *, char **);
46
47__weak_reference(__strtok_r, strtok_r);
48
984263bc 49char *
716024cd 50__strtok_r(char *s, const char *delim, char **last)
984263bc 51{
716024cd
PA
52 const char *spanp;
53 int c, sc;
54 char *tok;
55
56 if (s == NULL && (s = *last) == NULL)
57 return (NULL);
58
59 /*
60 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
61 */
984263bc 62cont:
984263bc 63 c = *s++;
716024cd
PA
64 for (spanp = delim; (sc = *spanp++) != 0;) {
65 if (c == sc)
66 goto cont;
984263bc 67 }
984263bc 68
716024cd
PA
69 if (c == 0) { /* no non-delimiter characters */
70 *last = NULL;
71 return (NULL);
72 }
73 tok = s - 1;
74
75 /*
76 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
77 * Note that delim must have one NUL; we stop if we see that, too.
78 */
79 for (;;) {
80 c = *s++;
81 spanp = delim;
82 do {
83 if ((sc = *spanp++) == c) {
84 if (c == 0)
85 s = NULL;
86 else
87 s[-1] = '\0';
88 *last = s;
89 return (tok);
90 }
91 } while (sc != 0);
92 }
93 /* NOTREACHED */
94}
984263bc
MD
95
96char *
97strtok(char *s, const char *delim)
98{
716024cd 99 static char *last;
984263bc 100
716024cd 101 return (__strtok_r(s, delim, &last));
984263bc
MD
102}
103
716024cd 104#ifdef DEBUG_STRTOK
984263bc
MD
105/*
106 * Test the tokenizer.
107 */
108int
716024cd 109main(void)
984263bc 110{
716024cd
PA
111 char blah[80], test[80];
112 char *brkb, *brkt, *phrase, *sep, *word;
984263bc 113
716024cd
PA
114 sep = "\\/:;=-";
115 phrase = "foo";
984263bc 116
716024cd
PA
117 printf("String tokenizer test:\n");
118 strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
119 for (word = strtok(test, sep); word; word = strtok(NULL, sep))
120 printf("Next word is \"%s\".\n", word);
121 strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
984263bc 122
716024cd
PA
123 for (word = strtok_r(test, sep, &brkt); word;
124 word = strtok_r(NULL, sep, &brkt)) {
125 strcpy(blah, "blah:blat:blab:blag");
984263bc 126
716024cd
PA
127 for (phrase = strtok_r(blah, sep, &brkb); phrase;
128 phrase = strtok_r(NULL, sep, &brkb))
129 printf("So far we're at %s:%s\n", word, phrase);
984263bc 130 }
984263bc 131
716024cd 132 return (0);
984263bc
MD
133}
134
135#endif /* DEBUG_STRTOK */