Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
strtok.c
Go to the documentation of this file.
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  * 4. Neither the name of the University nor the names of its contributors
19  * may be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
26  * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <stddef.h>
36 #ifdef DEBUG_STRTOK
37 #include <stdio.h>
38 #endif
39 #include <string.h>
40 
41 char* __strtok_r(char* /*s*/ /*s*/, const char* /*delim*/ /*delim*/, char** /*last*/ /*last*/);
42 
43 char* __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  {
50  {
51  return (NULL);
52  }
53  }
54 
55 /*
56  * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
57  */
58 cont:
59  c = *s++;
60  for(spanp = (char*)(uintptr_t)delim; (sc = *spanp++) != 0;)
61  {
62  if(c == sc)
63  {
64  {
65  goto cont;
66  }
67  }
68  }
69 
70  if(c == 0)
71  { /* no non-delimiter characters */
72  *last = NULL;
73  return (NULL);
74  }
75  tok = s - 1;
76 
77  /*
78  * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
79  * Note that delim must have one NUL; we stop if we see that, too.
80  */
81  for(;;)
82  {
83  c = *s++;
84  spanp = (char*)(uintptr_t)delim;
85  do
86  {
87  if((sc = *spanp++) == c)
88  {
89  if(c == 0)
90  {
91  {
92  s = NULL;
93  }
94  }
95  else
96  {
97  {
98  s[-1] = '\0';
99  }
100  }
101  *last = s;
102  return (tok);
103  }
104  } while(sc != 0);
105  }
106  /* NOTREACHED */
107 }
108 
109 char* strtok(char* s, const char* delim)
110 {
111  static char* last;
112 
113  return (__strtok_r(s, delim, &last));
114 }
115 
116 #ifdef DEBUG_STRTOK
117 /*
118  * Test the tokenizer.
119  */
120 int main(void)
121 {
122  char blah[80], test[80];
123  char *brkb, *brkt, *phrase, *sep, *word;
124 
125  sep = "\\/:;=-";
126  phrase = "foo";
127 
128  printf("String tokenizer test:\n");
129  strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
130  for(word = strtok(test, sep); word; word = strtok(NULL, sep))
131  printf("Next word is \"%s\".\n", word);
132  strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
133 
134  for(word = strtok_r(test, sep, &brkt); word; word = strtok_r(NULL, sep, &brkt))
135  {
136  strcpy(blah, "blah:blat:blab:blag");
137 
138  for(phrase = strtok_r(blah, sep, &brkb); phrase; phrase = strtok_r(NULL, sep, &brkb))
139  printf("So far we're at %s:%s\n", word, phrase);
140  }
141 
142  return (0);
143 }
144 
145 #endif /* DEBUG_STRTOK */
char * strtok(char *s, const char *delim)
Finds the next token in a null-terminated byte string pointed to by s.
Definition: strtok.c:109
int main()
Definition: arithchk.c:136
#define NULL
Definition: stddef.h:15
char * __strtok_r(char *, const char *, char **)
Definition: strtok.c:43
int word
Definition: memcpy.c:39
char * strcpy(char *__restrict dst, const char *__restrict src)
Copies the null-terminated byte string pointed to by src to the character array whose first element i...
Definition: strcpy.c:26