Blame snmplib/strtok_r.c

Packit fcad23
Packit fcad23
/*	$NetBSD: strtok_r.c,v 1.9 2003/08/07 16:43:53 agc Exp $	*/
Packit fcad23
Packit fcad23
/*
Packit fcad23
 * Copyright (c) 1988 Regents of the University of California.
Packit fcad23
 * All rights reserved.
Packit fcad23
 *
Packit fcad23
 * Redistribution and use in source and binary forms, with or without
Packit fcad23
 * modification, are permitted provided that the following conditions
Packit fcad23
 * are met:
Packit fcad23
 * 1. Redistributions of source code must retain the above copyright
Packit fcad23
 *    notice, this list of conditions and the following disclaimer.
Packit fcad23
 * 2. Redistributions in binary form must reproduce the above copyright
Packit fcad23
 *    notice, this list of conditions and the following disclaimer in the
Packit fcad23
 *    documentation and/or other materials provided with the distribution.
Packit fcad23
 * 3. Neither the name of the University nor the names of its contributors
Packit fcad23
 *    may be used to endorse or promote products derived from this software
Packit fcad23
 *    without specific prior written permission.
Packit fcad23
 *
Packit fcad23
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit fcad23
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit fcad23
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit fcad23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit fcad23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit fcad23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit fcad23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit fcad23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit fcad23
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit fcad23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit fcad23
 * SUCH DAMAGE.
Packit fcad23
 */
Packit fcad23
#include <net-snmp/net-snmp-config.h>
Packit fcad23
Packit fcad23
#ifndef WIN32
Packit fcad23
#include <sys/cdefs.h>
Packit fcad23
#endif
Packit fcad23
Packit fcad23
#include <string.h>
Packit fcad23
Packit fcad23
#include <net-snmp/library/system.h>
Packit fcad23
Packit fcad23
/*
Packit fcad23
 * thread-safe version of strtok
Packit fcad23
 */
Packit fcad23
char *
Packit fcad23
strtok_r(char *s, const char *delim, char **lasts)
Packit fcad23
{
Packit fcad23
	const char *spanp;
Packit fcad23
	int c, sc;
Packit fcad23
	char *tok;
Packit fcad23
Packit fcad23
	/* s may be NULL */
Packit fcad23
	/*netsnmp_assert(delim != NULL);*/
Packit fcad23
	/*netsnmp_assert(lasts != NULL);*/
Packit fcad23
Packit fcad23
	if (s == NULL && (s = *lasts) == NULL)
Packit fcad23
		return (NULL);
Packit fcad23
Packit fcad23
	/*
Packit fcad23
	 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
Packit fcad23
	 */
Packit fcad23
cont:
Packit fcad23
	c = *s++;
Packit fcad23
	for (spanp = delim; (sc = *spanp++) != 0;) {
Packit fcad23
		if (c == sc)
Packit fcad23
			goto cont;
Packit fcad23
	}
Packit fcad23
Packit fcad23
	if (c == 0) {		/* no non-delimiter characters */
Packit fcad23
		*lasts = NULL;
Packit fcad23
		return (NULL);
Packit fcad23
	}
Packit fcad23
	tok = s - 1;
Packit fcad23
Packit fcad23
	/*
Packit fcad23
	 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
Packit fcad23
	 * Note that delim must have one NUL; we stop if we see that, too.
Packit fcad23
	 */
Packit fcad23
	for (;;) {
Packit fcad23
		c = *s++;
Packit fcad23
		spanp = delim;
Packit fcad23
		do {
Packit fcad23
			if ((sc = *spanp++) == c) {
Packit fcad23
				if (c == 0)
Packit fcad23
					s = NULL;
Packit fcad23
				else
Packit fcad23
					s[-1] = 0;
Packit fcad23
				*lasts = s;
Packit fcad23
				return (tok);
Packit fcad23
			}
Packit fcad23
		} while (sc != 0);
Packit fcad23
	}
Packit fcad23
	/* NOTREACHED */
Packit fcad23
}