1170530Ssam/*	$Id: compat_strsep.c,v 1.5 2020/06/15 01:37:15 schwarze Exp $	*/
2178354Ssam/*	$OpenBSD: strsep.c,v 1.8 2015/08/31 02:53:57 guenther Exp $	*/
3170530Ssam
4170530Ssam/*-
5170530Ssam * Copyright (c) 1990, 1993
6170530Ssam *	The Regents of the University of California.  All rights reserved.
7170530Ssam *
8170530Ssam * Redistribution and use in source and binary forms, with or without
9170530Ssam * modification, are permitted provided that the following conditions
10170530Ssam * are met:
11170530Ssam * 1. Redistributions of source code must retain the above copyright
12170530Ssam *    notice, this list of conditions and the following disclaimer.
13170530Ssam * 2. Redistributions in binary form must reproduce the above copyright
14170530Ssam *    notice, this list of conditions and the following disclaimer in the
15170530Ssam *    documentation and/or other materials provided with the distribution.
16170530Ssam * 3. Neither the name of the University nor the names of its contributors
17170530Ssam *    may be used to endorse or promote products derived from this software
18170530Ssam *    without specific prior written permission.
19170530Ssam *
20170530Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21170530Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22170530Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23170530Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24170530Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25170530Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26170530Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27170530Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28170530Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29170530Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30170530Ssam * SUCH DAMAGE.
31170530Ssam */
32170530Ssam#include "config.h"
33170530Ssam
34170530Ssam/*
35170530Ssam * Get next token from string *stringp, where tokens are possibly-empty
36178354Ssam * strings separated by characters from delim.
37170530Ssam *
38170530Ssam * Writes NULs into the string at *stringp to end tokens.
39170530Ssam * delim need not remain constant from call to call.
40170530Ssam * On return, *stringp points past the last NUL written (if there might
41170530Ssam * be further tokens), or is NULL (if there are definitely no more tokens).
42170530Ssam *
43170530Ssam * If *stringp is NULL, strsep returns NULL.
44170530Ssam */
45170530Ssamchar *
46170530Ssamstrsep(char **stringp, const char *delim)
47170530Ssam{
48170530Ssam	char *s;
49170530Ssam	const char *spanp;
50195377Ssam	int c, sc;
51178354Ssam	char *tok;
52170530Ssam
53170530Ssam	if ((s = *stringp) == NULL)
54170530Ssam		return (NULL);
55170530Ssam	for (tok = s;;) {
56170530Ssam		c = *s++;
57219456Sbschmidt		spanp = delim;
58219456Sbschmidt		do {
59219456Sbschmidt			if ((sc = *spanp++) == c) {
60219456Sbschmidt				if (c == 0)
61219456Sbschmidt					s = NULL;
62219456Sbschmidt				else
63219456Sbschmidt					s[-1] = 0;
64219456Sbschmidt				*stringp = s;
65219456Sbschmidt				return (tok);
66219456Sbschmidt			}
67219456Sbschmidt		} while (sc != 0);
68219456Sbschmidt	}
69219456Sbschmidt	/* NOTREACHED */
70219456Sbschmidt}
71219456Sbschmidt