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) |
46b5f26a | 9 | static char sccsid[] = "@(#)strtok.c 5.8 (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 | 16 | strtok(s, delim) |
46b5f26a KB |
17 | register char *s; |
18 | register const char *delim; | |
5afa231a | 19 | { |
e6b5cdc0 KB |
20 | register char *spanp; |
21 | register int c, sc; | |
22 | char *tok; | |
23 | static char *last; | |
24 | ||
5afa231a | 25 | |
e6b5cdc0 KB |
26 | if (s == NULL && (s = last) == NULL) |
27 | return (NULL); | |
28 | ||
29 | /* | |
30 | * Skip (span) leading delimiters (s += strspn(s, delim), sort of). | |
31 | */ | |
32 | cont: | |
33 | c = *s++; | |
46b5f26a | 34 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
e6b5cdc0 KB |
35 | if (c == sc) |
36 | goto cont; | |
5afa231a RE |
37 | } |
38 | ||
e6b5cdc0 KB |
39 | if (c == 0) { /* no non-delimiter characters */ |
40 | last = NULL; | |
41 | return (NULL); | |
42 | } | |
43 | tok = s - 1; | |
5afa231a | 44 | |
e6b5cdc0 KB |
45 | /* |
46 | * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). | |
47 | * Note that delim must have one NUL; we stop if we see that, too. | |
48 | */ | |
49 | for (;;) { | |
50 | c = *s++; | |
46b5f26a | 51 | spanp = (char *)delim; |
e6b5cdc0 KB |
52 | do { |
53 | if ((sc = *spanp++) == c) { | |
54 | if (c == 0) | |
55 | s = NULL; | |
56 | else | |
57 | s[-1] = 0; | |
58 | last = s; | |
59 | return (tok); | |
60 | } | |
61 | } while (sc != 0); | |
5afa231a | 62 | } |
e6b5cdc0 | 63 | /* NOTREACHED */ |
5afa231a | 64 | } |