Home | History | Annotate | Line # | Download | only in string
strsep.c revision 1.2
      1  1.2     chs /*	$NetBSD: strsep.c,v 1.2 2007/02/19 18:33:09 chs Exp $	*/
      2  1.1  dyoung 
      3  1.1  dyoung /*-
      4  1.1  dyoung  * Copyright (c) 1990, 1993
      5  1.1  dyoung  *	The Regents of the University of California.  All rights reserved.
      6  1.1  dyoung  *
      7  1.1  dyoung  * Redistribution and use in source and binary forms, with or without
      8  1.1  dyoung  * modification, are permitted provided that the following conditions
      9  1.1  dyoung  * are met:
     10  1.1  dyoung  * 1. Redistributions of source code must retain the above copyright
     11  1.1  dyoung  *    notice, this list of conditions and the following disclaimer.
     12  1.1  dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  dyoung  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  dyoung  *    documentation and/or other materials provided with the distribution.
     15  1.1  dyoung  * 3. Neither the name of the University nor the names of its contributors
     16  1.1  dyoung  *    may be used to endorse or promote products derived from this software
     17  1.1  dyoung  *    without specific prior written permission.
     18  1.1  dyoung  *
     19  1.1  dyoung  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1  dyoung  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1  dyoung  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1  dyoung  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1  dyoung  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  dyoung  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1  dyoung  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1  dyoung  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  dyoung  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  dyoung  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  dyoung  * SUCH DAMAGE.
     30  1.1  dyoung  */
     31  1.1  dyoung 
     32  1.1  dyoung #include <sys/cdefs.h>
     33  1.1  dyoung #if defined(LIBC_SCCS) && !defined(lint)
     34  1.1  dyoung #if 0
     35  1.1  dyoung static char sccsid[] = "@(#)strsep.c	8.1 (Berkeley) 6/4/93";
     36  1.1  dyoung #else
     37  1.2     chs __RCSID("$NetBSD: strsep.c,v 1.2 2007/02/19 18:33:09 chs Exp $");
     38  1.1  dyoung #endif
     39  1.1  dyoung #endif /* LIBC_SCCS and not lint */
     40  1.1  dyoung 
     41  1.1  dyoung #if !defined(_KERNEL) && !defined(_STANDALONE)
     42  1.1  dyoung #include "namespace.h"
     43  1.1  dyoung 
     44  1.1  dyoung #include <assert.h>
     45  1.1  dyoung #include <string.h>
     46  1.1  dyoung 
     47  1.1  dyoung #ifdef __weak_alias
     48  1.1  dyoung __weak_alias(strsep,_strsep)
     49  1.1  dyoung #endif
     50  1.1  dyoung 
     51  1.2     chs #else
     52  1.2     chs #include <sys/param.h>
     53  1.2     chs #include <lib/libkern/libkern.h>
     54  1.2     chs #endif /* !_KERNEL && !_STANDALONE */
     55  1.2     chs 
     56  1.1  dyoung #if !HAVE_STRSEP
     57  1.1  dyoung /*
     58  1.1  dyoung  * Get next token from string *stringp, where tokens are possibly-empty
     59  1.1  dyoung  * strings separated by characters from delim.
     60  1.1  dyoung  *
     61  1.1  dyoung  * Writes NULs into the string at *stringp to end tokens.
     62  1.1  dyoung  * delim need not remain constant from call to call.
     63  1.1  dyoung  * On return, *stringp points past the last NUL written (if there might
     64  1.1  dyoung  * be further tokens), or is NULL (if there are definitely no more tokens).
     65  1.1  dyoung  *
     66  1.1  dyoung  * If *stringp is NULL, strsep returns NULL.
     67  1.1  dyoung  */
     68  1.1  dyoung char *
     69  1.1  dyoung strsep(stringp, delim)
     70  1.1  dyoung 	char **stringp;
     71  1.1  dyoung 	const char *delim;
     72  1.1  dyoung {
     73  1.1  dyoung 	char *s;
     74  1.1  dyoung 	const char *spanp;
     75  1.1  dyoung 	int c, sc;
     76  1.1  dyoung 	char *tok;
     77  1.1  dyoung 
     78  1.1  dyoung 	_DIAGASSERT(stringp != NULL);
     79  1.1  dyoung 	_DIAGASSERT(delim != NULL);
     80  1.1  dyoung 
     81  1.1  dyoung 	if ((s = *stringp) == NULL)
     82  1.1  dyoung 		return (NULL);
     83  1.1  dyoung 	for (tok = s;;) {
     84  1.1  dyoung 		c = *s++;
     85  1.1  dyoung 		spanp = delim;
     86  1.1  dyoung 		do {
     87  1.1  dyoung 			if ((sc = *spanp++) == c) {
     88  1.1  dyoung 				if (c == 0)
     89  1.1  dyoung 					s = NULL;
     90  1.1  dyoung 				else
     91  1.1  dyoung 					s[-1] = 0;
     92  1.1  dyoung 				*stringp = s;
     93  1.1  dyoung 				return (tok);
     94  1.1  dyoung 			}
     95  1.1  dyoung 		} while (sc != 0);
     96  1.1  dyoung 	}
     97  1.1  dyoung 	/* NOTREACHED */
     98  1.1  dyoung }
     99  1.1  dyoung #endif
    100