Commit | Line | Data |
---|---|---|
5afa231a | 1 | /* |
e6b5cdc0 | 2 | * Copyright (c) 1988 Regents of the University of California. |
317e5946 KB |
3 | * All rights reserved. |
4 | * | |
019bea33 | 5 | * %sccs.include.redist.c% |
5afa231a RE |
6 | */ |
7 | ||
2ce81398 | 8 | #if defined(LIBC_SCCS) && !defined(lint) |
019bea33 | 9 | static char sccsid[] = "@(#)strtok.c 5.7 (Berkeley) %G%"; |
317e5946 | 10 | #endif /* LIBC_SCCS and not lint */ |
5afa231a | 11 | |
336401ac KB |
12 | #include <stddef.h> |
13 | #include <string.h> | |
e6b5cdc0 | 14 | |
5afa231a | 15 | char * |
e6b5cdc0 KB |
16 | strtok(s, delim) |
17 | register char *s, *delim; | |
5afa231a | 18 | { |
e6b5cdc0 KB |
19 | register char *spanp; |
20 | register int c, sc; | |
21 | char *tok; | |
22 | static char *last; | |
23 | ||
5afa231a | 24 | |
e6b5cdc0 KB |
25 | if (s == NULL && (s = last) == NULL) |
26 | return (NULL); | |
27 | ||
28 | /* | |
29 | * Skip (span) leading delimiters (s += strspn(s, delim), sort of). | |
30 | */ | |
31 | cont: | |
32 | c = *s++; | |
33 | for (spanp = delim; (sc = *spanp++) != 0;) { | |
34 | if (c == sc) | |
35 | goto cont; | |
5afa231a RE |
36 | } |
37 | ||
e6b5cdc0 KB |
38 | if (c == 0) { /* no non-delimiter characters */ |
39 | last = NULL; | |
40 | return (NULL); | |
41 | } | |
42 | tok = s - 1; | |
5afa231a | 43 | |
e6b5cdc0 KB |
44 | /* |
45 | * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). | |
46 | * Note that delim must have one NUL; we stop if we see that, too. | |
47 | */ | |
48 | for (;;) { | |
49 | c = *s++; | |
50 | spanp = delim; | |
51 | do { | |
52 | if ((sc = *spanp++) == c) { | |
53 | if (c == 0) | |
54 | s = NULL; | |
55 | else | |
56 | s[-1] = 0; | |
57 | last = s; | |
58 | return (tok); | |
59 | } | |
60 | } while (sc != 0); | |
5afa231a | 61 | } |
e6b5cdc0 | 62 | /* NOTREACHED */ |
5afa231a | 63 | } |