strsep.c revision 251069
1251839Sbapt/*- 2255852Sdteske * Copyright (c) 1990, 1993 3251839Sbapt * The Regents of the University of California. All rights reserved. 4251839Sbapt * 5251839Sbapt * Redistribution and use in source and binary forms, with or without 6251839Sbapt * modification, are permitted provided that the following conditions 7251839Sbapt * are met: 8251839Sbapt * 1. Redistributions of source code must retain the above copyright 9251839Sbapt * notice, this list of conditions and the following disclaimer. 10251839Sbapt * 2. Redistributions in binary form must reproduce the above copyright 11251839Sbapt * notice, this list of conditions and the following disclaimer in the 12251839Sbapt * documentation and/or other materials provided with the distribution. 13251839Sbapt * 3. Neither the name of the University nor the names of its contributors 14251839Sbapt * may be used to endorse or promote products derived from this software 15251839Sbapt * without specific prior written permission. 16251839Sbapt * 17251839Sbapt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18251839Sbapt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19251839Sbapt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20251839Sbapt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21251839Sbapt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22251839Sbapt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23251839Sbapt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24251839Sbapt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25251839Sbapt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26251839Sbapt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27251839Sbapt * SUCH DAMAGE. 28251839Sbapt */ 29251839Sbapt 30251839Sbapt#if defined(LIBC_SCCS) && !defined(lint) 31251839Sbaptstatic char sccsid[] = "@(#)strsep.c 8.1 (Berkeley) 6/4/93"; 32251839Sbapt#endif /* LIBC_SCCS and not lint */ 33251839Sbapt#include <sys/cdefs.h> 34251839Sbapt__FBSDID("$FreeBSD: head/lib/libc/string/strsep.c 251069 2013-05-28 20:57:40Z emaste $"); 35251839Sbapt 36251839Sbapt#include <string.h> 37251839Sbapt#include <stdio.h> 38251839Sbapt 39251839Sbapt/* 40251839Sbapt * Get next token from string *stringp, where tokens are possibly-empty 41251839Sbapt * strings separated by characters from delim. 42251839Sbapt * 43251839Sbapt * Writes NULs into the string at *stringp to end tokens. 44251839Sbapt * delim need not remain constant from call to call. 45251839Sbapt * On return, *stringp points past the last NUL written (if there might 46251839Sbapt * be further tokens), or is NULL (if there are definitely no more tokens). 47251839Sbapt * 48251839Sbapt * If *stringp is NULL, strsep returns NULL. 49251839Sbapt */ 50251839Sbaptchar * 51251839Sbaptstrsep(char **stringp, const char *delim) 52251839Sbapt{ 53251839Sbapt char *s; 54251839Sbapt const char *spanp; 55251839Sbapt int c, sc; 56251839Sbapt char *tok; 57251839Sbapt 58251839Sbapt if ((s = *stringp) == NULL) 59251839Sbapt return (NULL); 60251839Sbapt for (tok = s;;) { 61251839Sbapt c = *s++; 62251839Sbapt spanp = delim; 63251839Sbapt do { 64251839Sbapt if ((sc = *spanp++) == c) { 65251839Sbapt if (c == 0) 66251839Sbapt s = NULL; 67251839Sbapt else 68251839Sbapt s[-1] = 0; 69251839Sbapt *stringp = s; 70251839Sbapt return (tok); 71251839Sbapt } 72251839Sbapt } while (sc != 0); 73251839Sbapt } 74251839Sbapt /* NOTREACHED */ 75251839Sbapt} 76251839Sbapt