199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Softweyr LLC. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * strtok_r, from Berkeley strtok
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1988, 1993
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The Regents of the University of California. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notices, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notices, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 3. Neither the name of the University nor the names of its contributors
199767f8919635c4928607450d9e0abb932109ceToomas Soome * may be used to endorse or promote products derived from this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
199767f8919635c4928607450d9e0abb932109ceToomas Soome * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
199767f8919635c4928607450d9e0abb932109ceToomas Soome * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(LIBC_SCCS) && !defined(lint)
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char sccsid[] = "@(#)strtok.c 8.1 (Berkeley) 6/4/93";
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* LIBC_SCCS and not lint */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stddef.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG_STRTOK
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stdio.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomechar *__strtok_r(char *, const char *, char **);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome__weak_reference(__strtok_r, strtok_r);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomechar *
199767f8919635c4928607450d9e0abb932109ceToomas Soome__strtok_r(char *s, const char *delim, char **last)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *spanp, *tok;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int c, sc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s == NULL && (s = *last) == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomecont:
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = *s++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c == sc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto cont;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c == 0) { /* no non-delimiter characters */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *last = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome tok = s - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note that delim must have one NUL; we stop if we see that, too.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (;;) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = *s++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome spanp = (char *)delim;
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((sc = *spanp++) == c) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome s = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome s[-1] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome *last = s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (tok);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (sc != 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* NOTREACHED */
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomechar *
199767f8919635c4928607450d9e0abb932109ceToomas Soomestrtok(char *s, const char *delim)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome static char *last;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (__strtok_r(s, delim, &last));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef DEBUG_STRTOK
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Test the tokenizer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomemain(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char blah[80], test[80];
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *brkb, *brkt, *phrase, *sep, *word;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome sep = "\\/:;=-";
199767f8919635c4928607450d9e0abb932109ceToomas Soome phrase = "foo";
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("String tokenizer test:\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (word = strtok(test, sep); word; word = strtok(NULL, sep))
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Next word is \"%s\".\n", word);
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (word = strtok_r(test, sep, &brkt); word;
199767f8919635c4928607450d9e0abb932109ceToomas Soome word = strtok_r(NULL, sep, &brkt)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(blah, "blah:blat:blab:blag");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (phrase = strtok_r(blah, sep, &brkb); phrase;
199767f8919635c4928607450d9e0abb932109ceToomas Soome phrase = strtok_r(NULL, sep, &brkb))
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("So far we're at %s:%s\n", word, phrase);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* DEBUG_STRTOK */