1 /* 2 * Copyright (c) 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)strtok.c 8.1 (Berkeley) 06/04/93"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <stddef.h> 13 #include <string.h> 14 15 char * 16 strtok(s, delim) 17 register char *s; 18 register const char *delim; 19 { 20 register char *spanp; 21 register int c, sc; 22 char *tok; 23 static char *last; 24 25 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++; 34 for (spanp = (char *)delim; (sc = *spanp++) != 0;) { 35 if (c == sc) 36 goto cont; 37 } 38 39 if (c == 0) { /* no non-delimiter characters */ 40 last = NULL; 41 return (NULL); 42 } 43 tok = s - 1; 44 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++; 51 spanp = (char *)delim; 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); 62 } 63 /* NOTREACHED */ 64 } 65