genericopenlibs/openenvcore/libc/src/string/strtok.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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  * © Portions copyright (c) 2005-2006  Nokia Corporation.  All rights reserved.
       
    34  */
       
    35 
       
    36 #if defined(LIBC_SCCS) && !defined(lint)
       
    37 static char sccsid[] = "@(#)strtok.c	8.1 (Berkeley) 6/4/93";
       
    38 #endif /* LIBC_SCCS and not lint */
       
    39 #include <sys/cdefs.h>
       
    40 #ifndef __SYMBIAN32__
       
    41 __FBSDID("$FreeBSD: src/lib/libc/string/strtok.c,v 1.9 2002/09/07 02:53:19 tjr Exp $");
       
    42 #endif
       
    43 
       
    44 #include <stddef.h>
       
    45 #ifdef DEBUG_STRTOK
       
    46 #include <stdio.h>
       
    47 #endif
       
    48 #include <string.h>
       
    49 
       
    50 #if (defined(__SYMBIAN32__) && (defined(__WINSCW__) || defined(__WINS__)))
       
    51 #include "libc_wsd_defs.h"
       
    52 #endif
       
    53 
       
    54 
       
    55 char	*__strtok_r(char *, const char *, char **);
       
    56 
       
    57 //__weak_reference(__strtok_r, strtok_r);
       
    58 
       
    59 char *
       
    60 __strtok_r(char *s, const char *delim, char **last)
       
    61 {
       
    62 	char *spanp, *tok;
       
    63 	int c, sc;
       
    64 
       
    65 	if (s == NULL && (s = *last) == NULL)
       
    66 		return (NULL);
       
    67 
       
    68 	/*
       
    69 	 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
       
    70 	 */
       
    71 cont:
       
    72 	c = *s++;
       
    73 	for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
       
    74 		if (c == sc)
       
    75 			goto cont;
       
    76 	}
       
    77 
       
    78 	if (c == 0) {		/* no non-delimiter characters */
       
    79 		*last = NULL;
       
    80 		return (NULL);
       
    81 	}
       
    82 	tok = s - 1;
       
    83 
       
    84 	/*
       
    85 	 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
       
    86 	 * Note that delim must have one NUL; we stop if we see that, too.
       
    87 	 */
       
    88 	for (;;) {
       
    89 		c = *s++;
       
    90 		spanp = (char *)delim;
       
    91 		do {
       
    92 			if ((sc = *spanp++) == c) {
       
    93 				if (c == 0)
       
    94 					s = NULL;
       
    95 				else
       
    96 					s[-1] = '\0';
       
    97 				*last = s;
       
    98 				return (tok);
       
    99 			}
       
   100 		} while (sc != 0);
       
   101 	}
       
   102 	/* NOTREACHED */
       
   103 }
       
   104 
       
   105 #ifdef EMULATOR
       
   106 
       
   107 GET_STATIC_VAR_FROM_TLS(last, char *)
       
   108 #define last (*GET_WSD_VAR_NAME(last, s)())
       
   109 #endif //EMULATOR
       
   110 
       
   111 EXPORT_C 
       
   112 char *
       
   113 strtok(char *s, const char *delim)
       
   114 {
       
   115 #ifndef EMULATOR
       
   116 	static char *last;
       
   117 #endif //EMULATOR
       
   118 
       
   119 	return (__strtok_r(s, delim, &last));
       
   120 }
       
   121 
       
   122 #ifdef DEBUG_STRTOK
       
   123 /*
       
   124  * Test the tokenizer.
       
   125  */
       
   126 int
       
   127 main(void)
       
   128 {
       
   129 	char blah[80], test[80];
       
   130 	char *brkb, *brkt, *phrase, *sep, *word;
       
   131 
       
   132 	sep = "\\/:;=-";
       
   133 	phrase = "foo";
       
   134 
       
   135 	printf("String tokenizer test:\n");
       
   136 	strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
       
   137 	for (word = strtok(test, sep); word; word = strtok(NULL, sep))
       
   138 		printf("Next word is \"%s\".\n", word);
       
   139 	strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
       
   140 
       
   141 	for (word = strtok_r(test, sep, &brkt); word;
       
   142 	    word = strtok_r(NULL, sep, &brkt)) {
       
   143 		strcpy(blah, "blah:blat:blab:blag");
       
   144 
       
   145 		for (phrase = strtok_r(blah, sep, &brkb); phrase;
       
   146 		    phrase = strtok_r(NULL, sep, &brkb))
       
   147 			printf("So far we're at %s:%s\n", word, phrase);
       
   148 	}
       
   149 
       
   150 	return (0);
       
   151 }
       
   152 
       
   153 #endif /* DEBUG_STRTOK */