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 * 14 * 1. Redistributions of source code must retain the above copyright 15 * notices, this list of conditions and the following disclaimer. 16 * 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notices, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * 24 * This product includes software developed by Softweyr LLC, the 25 * University of California, Berkeley, and its contributors. 26 * 27 * 4. Neither the name of the University nor the names of its contributors 28 * may be used to endorse or promote products derived from this software 29 * without specific prior written permission. 30 * 31 * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS 32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 34 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE 35 * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 37 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 38 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 */ 43 44 #include <stddef.h> 45 #include <string.h> 46 47 char * 48 strtok_r(char *s, const char *delim, char **last) 49 { 50 char *spanp; 51 int c, sc; 52 char *tok; 53 54 if (s == NULL && (s = *last) == NULL) 55 { 56 return NULL; 57 } 58 59 /* 60 * Skip (span) leading delimiters (s += strspn(s, delim), sort of). 61 */ 62 cont: 63 c = *s++; 64 for (spanp = (char *)delim; (sc = *spanp++) != 0; ) 65 { 66 if (c == sc) 67 { 68 goto cont; 69 } 70 } 71 72 if (c == 0) /* no non-delimiter characters */ 73 { 74 *last = NULL; 75 return NULL; 76 } 77 tok = s - 1; 78 79 /* 80 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). 81 * Note that delim must have one NUL; we stop if we see that, too. 82 */ 83 for (;;) 84 { 85 c = *s++; 86 spanp = (char *)delim; 87 do 88 { 89 if ((sc = *spanp++) == c) 90 { 91 if (c == 0) 92 { 93 s = NULL; 94 } 95 else 96 { 97 char *w = s - 1; 98 *w = '\0'; 99 } 100 *last = s; 101 return tok; 102 } 103 } 104 while (sc != 0); 105 } 106 /* NOTREACHED */ 107 } 108 109 110 char * 111 strtok(char *s, const char *delim) 112 { 113 static char *last; 114 115 return strtok_r(s, delim, &last); 116 } 117 118 119 #if defined(DEBUG_STRTOK) 120 121 /* 122 * Test the tokenizer. 123 */ 124 int 125 main() 126 { 127 char test[80], blah[80]; 128 char *sep = "\\/:;=-"; 129 char *word, *phrase, *brkt, *brkb; 130 131 printf("String tokenizer test:\n"); 132 133 strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); 134 135 for (word = strtok(test, sep); 136 word; 137 word = strtok(NULL, sep)) 138 { 139 printf("Next word is \"%s\".\n", word); 140 } 141 142 phrase = "foo"; 143 144 strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function."); 145 146 for (word = strtok_r(test, sep, &brkt); 147 word; 148 word = strtok_r(NULL, sep, &brkt)) 149 { 150 strcpy(blah, "blah:blat:blab:blag"); 151 152 for (phrase = strtok_r(blah, sep, &brkb); 153 phrase; 154 phrase = strtok_r(NULL, sep, &brkb)) 155 { 156 printf("So far we're at %s:%s\n", word, phrase); 157 } 158 } 159 160 return 0; 161 } 162 163 #endif /* DEBUG_STRTOK */ 164