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