11.4Schristos/*	$NetBSD: stresep.c,v 1.4 2017/08/23 10:27:41 christos Exp $	*/
21.1Schristos
31.1Schristos/*-
41.1Schristos * Copyright (c) 1990, 1993
51.1Schristos *	The Regents of the University of California.  All rights reserved.
61.1Schristos *
71.1Schristos * Redistribution and use in source and binary forms, with or without
81.1Schristos * modification, are permitted provided that the following conditions
91.1Schristos * are met:
101.1Schristos * 1. Redistributions of source code must retain the above copyright
111.1Schristos *    notice, this list of conditions and the following disclaimer.
121.1Schristos * 2. Redistributions in binary form must reproduce the above copyright
131.1Schristos *    notice, this list of conditions and the following disclaimer in the
141.1Schristos *    documentation and/or other materials provided with the distribution.
151.1Schristos * 3. Neither the name of the University nor the names of its contributors
161.1Schristos *    may be used to endorse or promote products derived from this software
171.1Schristos *    without specific prior written permission.
181.1Schristos *
191.1Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
201.1Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211.1Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221.1Schristos * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
231.1Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241.1Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251.1Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261.1Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271.1Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281.1Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291.1Schristos * SUCH DAMAGE.
301.1Schristos */
311.1Schristos
321.1Schristos#include <sys/cdefs.h>
331.1Schristos#if defined(LIBC_SCCS) && !defined(lint)
341.1Schristos#if 0
351.1Schristosstatic char sccsid[] = "@(#)strsep.c	8.1 (Berkeley) 6/4/93";
361.1Schristos#else
371.4Schristos__RCSID("$NetBSD: stresep.c,v 1.4 2017/08/23 10:27:41 christos Exp $");
381.1Schristos#endif
391.1Schristos#endif /* LIBC_SCCS and not lint */
401.1Schristos
411.1Schristos#include "namespace.h"
421.1Schristos
431.1Schristos#include <assert.h>
441.1Schristos#include <string.h>
451.1Schristos
461.1Schristos#ifdef __weak_alias
471.1Schristos__weak_alias(stresep,_stresep)
481.1Schristos#endif
491.1Schristos
501.1Schristos/*
511.1Schristos * Get next token from string *stringp, where tokens are possibly-empty
521.1Schristos * strings separated by characters from delim. If esc is not NUL, then
531.1Schristos * the characters followed by esc are ignored and are not taken into account
541.1Schristos * when splitting the string.
551.1Schristos *
561.1Schristos * Writes NULs into the string at *stringp to end tokens.
571.1Schristos * delim need not remain constant from call to call.
581.1Schristos * On return, *stringp points past the last NUL written (if there might
591.1Schristos * be further tokens), or is NULL (if there are definitely no more tokens).
601.1Schristos *
611.1Schristos * If *stringp is NULL, stresep returns NULL.
621.1Schristos */
631.1Schristoschar *
641.1Schristosstresep(char **stringp, const char *delim, int esc)
651.1Schristos{
661.1Schristos	char *s;
671.1Schristos	const char *spanp;
681.1Schristos	int c, sc;
691.4Schristos	size_t l;
701.1Schristos	char *tok;
711.1Schristos
721.1Schristos	_DIAGASSERT(stringp != NULL);
731.1Schristos	_DIAGASSERT(delim != NULL);
741.1Schristos
751.1Schristos	if ((s = *stringp) == NULL)
761.1Schristos		return NULL;
771.4Schristos	l = strlen(s) + 1;
781.1Schristos	for (tok = s;;) {
791.1Schristos		c = *s++;
801.4Schristos		l--;
811.2Sseb		while (esc != '\0' && c == esc) {
821.4Schristos			memmove(s - 1, s, l);
831.1Schristos			c = *s++;
841.4Schristos			l--;
851.1Schristos		}
861.1Schristos		spanp = delim;
871.1Schristos		do {
881.1Schristos			if ((sc = *spanp++) == c) {
891.4Schristos				if (c == '\0')
901.1Schristos					s = NULL;
911.1Schristos				else
921.4Schristos					s[-1] = '\0';
931.1Schristos				*stringp = s;
941.1Schristos				return tok;
951.1Schristos			}
961.4Schristos		} while (sc != '\0');
971.1Schristos	}
981.1Schristos}
99