stdc.library: Add missing strtok_r.c
[AROS.git] / compiler / stdc / strtok_r.c
blob7fb185f416c965a95e222c15b610ddbe00577bfb
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX.1-2001 function strtok_r().
6 */
8 /*****************************************************************************
10 NAME */
11 #include <string.h>
13 char * strtok_r (
15 /* SYNOPSIS */
16 char * str,
17 const char * sep,
18 char **saveptr)
20 /* FUNCTION
21 Separates a string by the characters in sep.
23 INPUTS
24 str - The string to check or NULL if the next word in
25 the last string is to be searched.
26 sep - Characters which separate "words" in str.
27 saveptr - internal context for next scan
29 RESULT
30 The first word in str or the next one if str is NULL.
32 NOTES
33 The function changes str !
35 EXAMPLE
36 char buffer[64];
37 char *ptr;
39 strcpy (buffer, "Hello, this is a test.");
41 // Init. Returns "Hello"
42 strtok_r (str, " \t,.", &ptr);
44 // Next word. Returns "this"
45 strtok_r (NULL, " \t,.", &ptr);
47 // Next word. Returns "is"
48 strtok_r (NULL, " \t", &ptr);
50 // Next word. Returns "a"
51 strtok_r (NULL, " \t", &ptr);
53 // Next word. Returns "test."
54 strtok_r (NULL, " \t", &ptr);
56 // Next word. Returns NULL.
57 strtok_r (NULL, " \t", &ptr);
59 BUGS
61 SEE ALSO
63 INTERNALS
65 ******************************************************************************/
67 char * t = *saveptr;
69 if (str != NULL)
70 t = str;
71 else
72 str = t;
74 str += strspn (str, sep);
76 if (*str == '\0')
77 return NULL;
79 t = str;
81 t += strcspn (str, sep);
83 if (*t != '\0')
84 *t ++ = '\0';
86 *saveptr = t;
87 return str;
88 } /* strtok_r */