os/ossrv/genericopenlibs/cstdlib/LCHAR/STRTOK.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* FUNCTION
sl@0
     3
* <<strtok>>---get next token from a string
sl@0
     4
* INDEX
sl@0
     5
* strtok
sl@0
     6
* INDEX
sl@0
     7
* strtok_r
sl@0
     8
* ANSI_SYNOPSIS
sl@0
     9
* #include <string.h>
sl@0
    10
* char *strtok(char *<[source]>, const char *<[delimiters]>)
sl@0
    11
* char *strtok_r(char *<[source]>, const char *<[delimiters]>,
sl@0
    12
* char **<[lasts]>)
sl@0
    13
* TRAD_SYNOPSIS
sl@0
    14
* #include <string.h>
sl@0
    15
* char *strtok(<[source]>, <[delimiters]>)
sl@0
    16
* char *<[source]>;
sl@0
    17
* char *<[delimiters]>;
sl@0
    18
* char *strtok_r(<[source]>, <[delimiters]>, <[lasts]>)
sl@0
    19
* char *<[source]>;
sl@0
    20
* char *<[delimiters]>;
sl@0
    21
* char **<[lasts]>;
sl@0
    22
* A series of calls to <<strtok>> break the string starting at
sl@0
    23
* <<*<[source]>>> into a sequence of tokens.  The tokens are
sl@0
    24
* delimited from one another by characters from the string at
sl@0
    25
* <<*<[delimiters]>>>, at the outset.
sl@0
    26
* The first call to <<strtok>> normally has a string address as
sl@0
    27
* the first argument; subsequent calls can use <<NULL>> as the
sl@0
    28
* first argument, to continue searching the same string.  You
sl@0
    29
* can continue searching a single string with different
sl@0
    30
* delimiters by using a different delimiter string on each call.
sl@0
    31
* <<strtok>> begins by searching for any character not in the
sl@0
    32
* <[delimiters]> string: the first such character is the
sl@0
    33
* beginning of a token (and its address will be the result of
sl@0
    34
* the <<strtok>> call). <<strtok>> then continues searching
sl@0
    35
* until it finds another delimiter character; it replaces that
sl@0
    36
* character by <<NULL>> and returns.  (If <<strtok>> comes to
sl@0
    37
* the end of the <<*<[source]>>> string without finding any more
sl@0
    38
* delimiters, the entire remainder of the string is treated as
sl@0
    39
* the next token).
sl@0
    40
* <<strtok>> starts its search at <<*<[source]>>>, unless you
sl@0
    41
* pass <<NULL>> as the first argument;  if <[source]> is
sl@0
    42
* <<NULL>>, <<strtok>> continues searching from the end of the
sl@0
    43
* last search. Exploiting the <<NULL>> first argument leads to
sl@0
    44
* non-reentrant code. You can easily circumvent this problem by
sl@0
    45
* saving the last delimiter address in your application, and
sl@0
    46
* always using it to pass a non-null <[source]> argument.
sl@0
    47
* RETURNS
sl@0
    48
* <<strtok>> returns a pointer to the next token, or <<NULL>> if
sl@0
    49
* no more tokens can be found.
sl@0
    50
* PORTABILITY
sl@0
    51
* <<strtok>> is ANSI C.
sl@0
    52
* <<strtok>> requires no supporting OS subroutines.
sl@0
    53
* QUICKREF
sl@0
    54
* strtok ansi impure
sl@0
    55
* 
sl@0
    56
*
sl@0
    57
*/
sl@0
    58
sl@0
    59
sl@0
    60
sl@0
    61
#include <sys/reent.h>
sl@0
    62
#include <string.h>
sl@0
    63
sl@0
    64
/**
sl@0
    65
Sequentially truncate string if delimiter is found.
sl@0
    66
If string is not NULL, the function scans string for the first occurrence of 
sl@0
    67
any character included in delimiters. If it is found, the function overwrites 
sl@0
    68
the delimiter in string by a null-character and returns a pointer to the token,
sl@0
    69
i.e. the part of the scanned string previous to the delimiter.
sl@0
    70
After a first call to strtok, the function may be called with NULL as string parameter,
sl@0
    71
and it will follow by where the last call to strtok found a delimiter.
sl@0
    72
@return A pointer to the last token found in string.
sl@0
    73
NULL is returned when there are no more tokens to be found.
sl@0
    74
@param s Null-terminated string to scan. 
sl@0
    75
@param delim Null-terminated string containing the separators.
sl@0
    76
*/
sl@0
    77
EXPORT_C char *
sl@0
    78
strtok (register char *s, register const char *delim)
sl@0
    79
{
sl@0
    80
    return strtok_r (s, delim, &(_REENT->_scanpoint));
sl@0
    81
}
sl@0
    82
sl@0
    83
/*
sl@0
    84
 * Copyright (c) 1988 Regents of the University of California.
sl@0
    85
 * All rights reserved.
sl@0
    86
 *
sl@0
    87
 * Redistribution and use in source and binary forms, with or without
sl@0
    88
 * modification, are permitted provided that the following conditions
sl@0
    89
 * are met:
sl@0
    90
 * 1. Redistributions of source code must retain the above copyright
sl@0
    91
 *    notice, this list of conditions and the following disclaimer.
sl@0
    92
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    93
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    94
 *    documentation and/or other materials provided with the distribution.
sl@0
    95
 * 3. All advertising materials mentioning features or use of this software
sl@0
    96
 *    must display the following acknowledgement:
sl@0
    97
 *	This product includes software developed by the University of
sl@0
    98
 *	California, Berkeley and its contributors.
sl@0
    99
 * 4. Neither the name of the University nor the names of its contributors
sl@0
   100
 *    may be used to endorse or promote products derived from this software
sl@0
   101
 *    without specific prior written permission.
sl@0
   102
 *
sl@0
   103
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
sl@0
   104
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
   105
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
   106
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
sl@0
   107
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
   108
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
   109
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
   110
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
   111
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
   112
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
   113
 * SUCH DAMAGE.
sl@0
   114
 */
sl@0
   115
sl@0
   116
EXPORT_C char *
sl@0
   117
strtok_r (register char *s, register const char *delim, char **lasts)
sl@0
   118
{
sl@0
   119
	register char *spanp;
sl@0
   120
	register int c, sc;
sl@0
   121
	char *tok;
sl@0
   122
sl@0
   123
sl@0
   124
	if (s == NULL && (s = *lasts) == NULL)
sl@0
   125
		return (NULL);
sl@0
   126
sl@0
   127
	/*
sl@0
   128
	 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
sl@0
   129
	 */
sl@0
   130
cont:
sl@0
   131
	c = *s++;
sl@0
   132
	for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
sl@0
   133
		if (c == sc)
sl@0
   134
			goto cont;
sl@0
   135
	}
sl@0
   136
sl@0
   137
	if (c == 0) {		/* no non-delimiter characters */
sl@0
   138
		*lasts = NULL;
sl@0
   139
		return (NULL);
sl@0
   140
	}
sl@0
   141
	tok = s - 1;
sl@0
   142
sl@0
   143
	/*
sl@0
   144
	 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
sl@0
   145
	 * Note that delim must have one NUL; we stop if we see that, too.
sl@0
   146
	 */
sl@0
   147
	for (;;) {
sl@0
   148
		c = *s++;
sl@0
   149
		spanp = (char *)delim;
sl@0
   150
		do {
sl@0
   151
			if ((sc = *spanp++) == c) {
sl@0
   152
				if (c == 0)
sl@0
   153
					s = NULL;
sl@0
   154
				else
sl@0
   155
					s[-1] = 0;
sl@0
   156
				*lasts = s;
sl@0
   157
				return (tok);
sl@0
   158
			}
sl@0
   159
		} while (sc != 0);
sl@0
   160
	}
sl@0
   161
	/* NOTREACHED */
sl@0
   162
}