11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1990, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes * 4. Neither the name of the University nor the names of its contributors
141573Srgrimes *    may be used to endorse or promote products derived from this software
151573Srgrimes *    without specific prior written permission.
161573Srgrimes *
171573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271573Srgrimes * SUCH DAMAGE.
281573Srgrimes */
291573Srgrimes
3086170Sobrien#if defined(LIBC_SCCS) && !defined(lint)
3186170Sobrienstatic char sccsid[] = "@(#)strsep.c	8.1 (Berkeley) 6/4/93";
3286170Sobrien#endif /* LIBC_SCCS and not lint */
331573Srgrimes#include <sys/cdefs.h>
3486170Sobrien__FBSDID("$FreeBSD$");
3586170Sobrien
36104799Srwatson#include <sys/param.h>
37104799Srwatson#include <sys/libkern.h>
381573Srgrimes
391573Srgrimes/*
401573Srgrimes * Get next token from string *stringp, where tokens are possibly-empty
418870Srgrimes * strings separated by characters from delim.
421573Srgrimes *
431573Srgrimes * Writes NULs into the string at *stringp to end tokens.
441573Srgrimes * delim need not remain constant from call to call.
451573Srgrimes * On return, *stringp points past the last NUL written (if there might
461573Srgrimes * be further tokens), or is NULL (if there are definitely no more tokens).
471573Srgrimes *
481573Srgrimes * If *stringp is NULL, strsep returns NULL.
491573Srgrimes */
501573Srgrimeschar *
511573Srgrimesstrsep(stringp, delim)
5292889Sobrien	char **stringp;
5392889Sobrien	const char *delim;
541573Srgrimes{
5592889Sobrien	char *s;
5692889Sobrien	const char *spanp;
5792889Sobrien	int c, sc;
581573Srgrimes	char *tok;
591573Srgrimes
601573Srgrimes	if ((s = *stringp) == NULL)
611573Srgrimes		return (NULL);
621573Srgrimes	for (tok = s;;) {
631573Srgrimes		c = *s++;
641573Srgrimes		spanp = delim;
651573Srgrimes		do {
661573Srgrimes			if ((sc = *spanp++) == c) {
671573Srgrimes				if (c == 0)
681573Srgrimes					s = NULL;
691573Srgrimes				else
701573Srgrimes					s[-1] = 0;
711573Srgrimes				*stringp = s;
721573Srgrimes				return (tok);
731573Srgrimes			}
741573Srgrimes		} while (sc != 0);
751573Srgrimes	}
761573Srgrimes	/* NOTREACHED */
771573Srgrimes}
78