strtok.cpp revision 1.1 1 1.1 uch /* $NetBSD: strtok.cpp,v 1.1 2004/08/06 17:23:14 uch Exp $ */
2 1.1 uch
3 1.1 uch /*
4 1.1 uch * Copyright (c) 1988, 1993
5 1.1 uch * The Regents of the University of California. All rights reserved.
6 1.1 uch *
7 1.1 uch * Redistribution and use in source and binary forms, with or without
8 1.1 uch * modification, are permitted provided that the following conditions
9 1.1 uch * are met:
10 1.1 uch * 1. Redistributions of source code must retain the above copyright
11 1.1 uch * notice, this list of conditions and the following disclaimer.
12 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 uch * notice, this list of conditions and the following disclaimer in the
14 1.1 uch * documentation and/or other materials provided with the distribution.
15 1.1 uch * 3. Neither the name of the University nor the names of its contributors
16 1.1 uch * may be used to endorse or promote products derived from this software
17 1.1 uch * without specific prior written permission.
18 1.1 uch *
19 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 uch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 uch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 uch * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 uch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 uch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 uch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 uch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 uch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 uch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 uch * SUCH DAMAGE.
30 1.1 uch */
31 1.1 uch
32 1.1 uch #include <sys/cdefs.h>
33 1.1 uch #include <sys/types.h>
34 1.1 uch #include <stdlib.h>
35 1.1 uch #include <libsa_string.h>
36 1.1 uch
37 1.1 uch LIBSA_NAMESPACE_BEGIN
38 1.1 uch __BEGIN_DECLS
39 1.1 uch char *
40 1.1 uch strtok(char *s, const char *delim)
41 1.1 uch {
42 1.1 uch const char *spanp;
43 1.1 uch int c, sc;
44 1.1 uch char *tok;
45 1.1 uch static char *last;
46 1.1 uch
47 1.1 uch if (s == NULL && (s = last) == NULL)
48 1.1 uch return (NULL);
49 1.1 uch
50 1.1 uch /*
51 1.1 uch * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
52 1.1 uch */
53 1.1 uch cont:
54 1.1 uch c = *s++;
55 1.1 uch for (spanp = delim; (sc = *spanp++) != 0;) {
56 1.1 uch if (c == sc)
57 1.1 uch goto cont;
58 1.1 uch }
59 1.1 uch
60 1.1 uch if (c == 0) { /* no non-delimiter characters */
61 1.1 uch last = NULL;
62 1.1 uch return (NULL);
63 1.1 uch }
64 1.1 uch tok = s - 1;
65 1.1 uch
66 1.1 uch /*
67 1.1 uch * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
68 1.1 uch * Note that delim must have one NUL; we stop if we see that, too.
69 1.1 uch */
70 1.1 uch for (;;) {
71 1.1 uch c = *s++;
72 1.1 uch spanp = delim;
73 1.1 uch do {
74 1.1 uch if ((sc = *spanp++) == c) {
75 1.1 uch if (c == 0)
76 1.1 uch s = NULL;
77 1.1 uch else
78 1.1 uch s[-1] = 0;
79 1.1 uch last = s;
80 1.1 uch return (tok);
81 1.1 uch }
82 1.1 uch } while (sc != 0);
83 1.1 uch }
84 1.1 uch /* NOTREACHED */
85 1.1 uch }
86 1.1 uch __END_DECLS
87 1.1 uch LIBSA_NAMESPACE_END
88