150476Speter/* $OpenBSD: strsep.c,v 1.6 2005/08/08 08:05:37 espie Exp $ */ 232236Shelbig 332236Shelbig/*- 432236Shelbig * Copyright (c) 1990, 1993 532236Shelbig * The Regents of the University of California. All rights reserved. 632236Shelbig * 732236Shelbig * Redistribution and use in source and binary forms, with or without 832236Shelbig * modification, are permitted provided that the following conditions 932236Shelbig * are met: 1032236Shelbig * 1. Redistributions of source code must retain the above copyright 1132236Shelbig * notice, this list of conditions and the following disclaimer. 1232236Shelbig * 2. Redistributions in binary form must reproduce the above copyright 1332236Shelbig * notice, this list of conditions and the following disclaimer in the 1432236Shelbig * documentation and/or other materials provided with the distribution. 1532236Shelbig * 3. Neither the name of the University nor the names of its contributors 1632236Shelbig * may be used to endorse or promote products derived from this software 1732236Shelbig * without specific prior written permission. 1832236Shelbig * 1932236Shelbig * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2032236Shelbig * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2132236Shelbig * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2232236Shelbig * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2332236Shelbig * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2432236Shelbig * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2532236Shelbig * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2632236Shelbig * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2732236Shelbig * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2832236Shelbig * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2932236Shelbig * SUCH DAMAGE. 3032236Shelbig */ 3132236Shelbig 3232236Shelbig/* OPENBSD ORIGINAL: lib/libc/string/strsep.c */ 3332236Shelbig 3432236Shelbig#include "includes.h" 3532236Shelbig 3632236Shelbig#if !defined(HAVE_STRSEP) 3732236Shelbig 3832236Shelbig#include <string.h> 3932236Shelbig#include <stdio.h> 4032236Shelbig 4132236Shelbig/* 4232236Shelbig * Get next token from string *stringp, where tokens are possibly-empty 4332236Shelbig * strings separated by characters from delim. 4432236Shelbig * 4532236Shelbig * Writes NULs into the string at *stringp to end tokens. 4632236Shelbig * delim need not remain constant from call to call. 4732236Shelbig * On return, *stringp points past the last NUL written (if there might 4832236Shelbig * be further tokens), or is NULL (if there are definitely no more tokens). 4932236Shelbig * 5032236Shelbig * If *stringp is NULL, strsep returns NULL. 5132236Shelbig */ 5232236Shelbigchar * 5332236Shelbigstrsep(char **stringp, const char *delim) 5432236Shelbig{ 5532236Shelbig char *s; 5632236Shelbig const char *spanp; 5732236Shelbig int c, sc; 5832236Shelbig char *tok; 5932236Shelbig 6032236Shelbig if ((s = *stringp) == NULL) 6132236Shelbig return (NULL); 6232236Shelbig for (tok = s;;) { 6332236Shelbig c = *s++; 6432236Shelbig spanp = delim; 6532236Shelbig do { 6654090Sache if ((sc = *spanp++) == c) { 6732236Shelbig if (c == 0) 6832236Shelbig s = NULL; 6932236Shelbig else 7032236Shelbig s[-1] = 0; 7132236Shelbig *stringp = s; 7232236Shelbig return (tok); 7332236Shelbig } 7432236Shelbig } while (sc != 0); 7532236Shelbig } 7632236Shelbig /* NOTREACHED */ 7732236Shelbig} 7854090Sache 7953943Sache#endif /* !defined(HAVE_STRSEP) */ 8053943Sache