1236769Sobrien/* $NetBSD: stresep.c,v 1.2 2007/12/06 22:07:07 seb Exp $ */ 2236769Sobrien 3236769Sobrien/*- 4236769Sobrien * Copyright (c) 1990, 1993 5236769Sobrien * The Regents of the University of California. All rights reserved. 6236769Sobrien * 7236769Sobrien * Redistribution and use in source and binary forms, with or without 8236769Sobrien * modification, are permitted provided that the following conditions 9236769Sobrien * are met: 10236769Sobrien * 1. Redistributions of source code must retain the above copyright 11236769Sobrien * notice, this list of conditions and the following disclaimer. 12236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright 13236769Sobrien * notice, this list of conditions and the following disclaimer in the 14236769Sobrien * documentation and/or other materials provided with the distribution. 15236769Sobrien * 3. Neither the name of the University nor the names of its contributors 16236769Sobrien * may be used to endorse or promote products derived from this software 17236769Sobrien * without specific prior written permission. 18236769Sobrien * 19236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22236769Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29236769Sobrien * SUCH DAMAGE. 30236769Sobrien */ 31236769Sobrien 32236769Sobrien#ifdef HAVE_CONFIG_H 33236769Sobrien# include <config.h> 34236769Sobrien#endif 35236769Sobrien 36236769Sobrien#include <sys/cdefs.h> 37236769Sobrien 38236769Sobrien#include <assert.h> 39236769Sobrien#include <string.h> 40236769Sobrien 41236769Sobrien#if !defined(HAVE_STRESEP) 42236769Sobrienchar * stresep(char **stringp, const char *delim, int esc); 43236769Sobrien/* 44236769Sobrien * Get next token from string *stringp, where tokens are possibly-empty 45236769Sobrien * strings separated by characters from delim. If esc is not NUL, then 46236769Sobrien * the characters followed by esc are ignored and are not taken into account 47236769Sobrien * when splitting the string. 48236769Sobrien * 49236769Sobrien * Writes NULs into the string at *stringp to end tokens. 50236769Sobrien * delim need not remain constant from call to call. 51236769Sobrien * On return, *stringp points past the last NUL written (if there might 52236769Sobrien * be further tokens), or is NULL (if there are definitely no more tokens). 53236769Sobrien * 54236769Sobrien * If *stringp is NULL, stresep returns NULL. 55236769Sobrien */ 56236769Sobrienchar * 57236769Sobrienstresep(char **stringp, const char *delim, int esc) 58236769Sobrien{ 59236769Sobrien char *s; 60236769Sobrien const char *spanp; 61236769Sobrien int c, sc; 62236769Sobrien char *tok; 63236769Sobrien 64236769Sobrien if (stringp == NULL || delim == NULL) 65236769Sobrien return NULL; 66236769Sobrien 67236769Sobrien if ((s = *stringp) == NULL) 68236769Sobrien return NULL; 69236769Sobrien for (tok = s;;) { 70236769Sobrien c = *s++; 71236769Sobrien while (esc != '\0' && c == esc) { 72236769Sobrien (void)strcpy(s - 1, s); 73236769Sobrien c = *s++; 74236769Sobrien } 75236769Sobrien spanp = delim; 76236769Sobrien do { 77236769Sobrien if ((sc = *spanp++) == c) { 78236769Sobrien if (c == 0) 79236769Sobrien s = NULL; 80236769Sobrien else 81236769Sobrien s[-1] = 0; 82236769Sobrien *stringp = s; 83236769Sobrien return tok; 84236769Sobrien } 85236769Sobrien } while (sc != 0); 86236769Sobrien } 87236769Sobrien} 88236769Sobrien#endif 89236769Sobrien 90