Commit | Line | Data |
---|---|---|
97f8808c MT |
1 | /*- |
2 | * Copyright (c) 1990 The Regents of the University of California. | |
c6aafc39 KB |
3 | * All rights reserved. |
4 | * | |
97f8808c | 5 | * %sccs.include.redist.c% |
c6aafc39 KB |
6 | */ |
7 | ||
a64329b4 | 8 | #include <sys/cdefs.h> |
97f8808c MT |
9 | #include <string.h> |
10 | #include <stdio.h> | |
11 | ||
c6aafc39 | 12 | #if defined(LIBC_SCCS) && !defined(lint) |
a64329b4 | 13 | static const char sccsid[] = "@(#)strsep.c 5.4 (Berkeley) %G%"; |
c6aafc39 KB |
14 | #endif /* LIBC_SCCS and not lint */ |
15 | ||
97f8808c MT |
16 | /* |
17 | * Get next token from string *stringp, where tokens are nonempty | |
18 | * strings separated by characters from delim. | |
19 | * | |
20 | * Writes NULs into the string at *stringp to end tokens. | |
21 | * delim need not remain constant from call to call. | |
22 | * On return, *stringp points past the last NUL written (if there might | |
23 | * be further tokens), or is NULL (if there are definitely no more tokens). | |
24 | * | |
25 | * If *stringp is NULL, strtoken returns NULL. | |
26 | */ | |
c6aafc39 | 27 | char * |
97f8808c MT |
28 | strsep(stringp, delim) |
29 | register char **stringp; | |
30 | register const char *delim; | |
c6aafc39 | 31 | { |
97f8808c MT |
32 | register char *s; |
33 | register const char *spanp; | |
c6aafc39 | 34 | register int c, sc; |
c6aafc39 KB |
35 | char *tok; |
36 | ||
97f8808c MT |
37 | if ((s = *stringp) == NULL) |
38 | return (NULL); | |
39 | for (tok = s;;) { | |
40 | c = *s++; | |
c6aafc39 KB |
41 | spanp = delim; |
42 | do { | |
43 | if ((sc = *spanp++) == c) { | |
97f8808c MT |
44 | if (c == 0) |
45 | s = NULL; | |
46 | else | |
47 | s[-1] = 0; | |
48 | *stringp = s; | |
49 | return (tok); | |
c6aafc39 | 50 | } |
97f8808c | 51 | } while (sc != 0); |
c6aafc39 KB |
52 | } |
53 | /* NOTREACHED */ | |
54 | } |