1157016Sdes/*	$OpenBSD: strsep.c,v 1.6 2005/08/08 08:05:37 espie Exp $	*/
2126274Sdes
398937Sdes/*-
498937Sdes * Copyright (c) 1990, 1993
598937Sdes *	The Regents of the University of California.  All rights reserved.
698937Sdes *
798937Sdes * Redistribution and use in source and binary forms, with or without
898937Sdes * modification, are permitted provided that the following conditions
998937Sdes * are met:
1098937Sdes * 1. Redistributions of source code must retain the above copyright
1198937Sdes *    notice, this list of conditions and the following disclaimer.
1298937Sdes * 2. Redistributions in binary form must reproduce the above copyright
1398937Sdes *    notice, this list of conditions and the following disclaimer in the
1498937Sdes *    documentation and/or other materials provided with the distribution.
15124208Sdes * 3. Neither the name of the University nor the names of its contributors
1698937Sdes *    may be used to endorse or promote products derived from this software
1798937Sdes *    without specific prior written permission.
1898937Sdes *
1998937Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2098937Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2198937Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2298937Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2398937Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2498937Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2598937Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2698937Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2798937Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2898937Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2998937Sdes * SUCH DAMAGE.
3098937Sdes */
3198937Sdes
32157016Sdes/* OPENBSD ORIGINAL: lib/libc/string/strsep.c */
33157016Sdes
34106121Sdes#include "includes.h"
3598937Sdes
3698937Sdes#if !defined(HAVE_STRSEP)
3798937Sdes
3898937Sdes#include <string.h>
3998937Sdes#include <stdio.h>
4098937Sdes
4198937Sdes/*
4298937Sdes * Get next token from string *stringp, where tokens are possibly-empty
4398937Sdes * strings separated by characters from delim.
4498937Sdes *
4598937Sdes * Writes NULs into the string at *stringp to end tokens.
4698937Sdes * delim need not remain constant from call to call.
4798937Sdes * On return, *stringp points past the last NUL written (if there might
4898937Sdes * be further tokens), or is NULL (if there are definitely no more tokens).
4998937Sdes *
5098937Sdes * If *stringp is NULL, strsep returns NULL.
5198937Sdes */
5298937Sdeschar *
5398937Sdesstrsep(char **stringp, const char *delim)
5498937Sdes{
55124208Sdes	char *s;
56124208Sdes	const char *spanp;
57124208Sdes	int c, sc;
5898937Sdes	char *tok;
5998937Sdes
6098937Sdes	if ((s = *stringp) == NULL)
6198937Sdes		return (NULL);
6298937Sdes	for (tok = s;;) {
6398937Sdes		c = *s++;
6498937Sdes		spanp = delim;
6598937Sdes		do {
6698937Sdes			if ((sc = *spanp++) == c) {
6798937Sdes				if (c == 0)
6898937Sdes					s = NULL;
6998937Sdes				else
7098937Sdes					s[-1] = 0;
7198937Sdes				*stringp = s;
7298937Sdes				return (tok);
7398937Sdes			}
7498937Sdes		} while (sc != 0);
7598937Sdes	}
7698937Sdes	/* NOTREACHED */
7798937Sdes}
7898937Sdes
7998937Sdes#endif /* !defined(HAVE_STRSEP) */
80