1 1.3 nonaka /* $NetBSD: strtok.cpp,v 1.3 2011/09/23 16:00:15 nonaka Exp $ */ 2 1.1 uch 3 1.1 uch /* 4 1.1 uch * Copyright (c) 1988, 1993 5 1.1 uch * The Regents of the University of California. All rights reserved. 6 1.1 uch * 7 1.1 uch * Redistribution and use in source and binary forms, with or without 8 1.1 uch * modification, are permitted provided that the following conditions 9 1.1 uch * are met: 10 1.1 uch * 1. Redistributions of source code must retain the above copyright 11 1.1 uch * notice, this list of conditions and the following disclaimer. 12 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 uch * notice, this list of conditions and the following disclaimer in the 14 1.1 uch * documentation and/or other materials provided with the distribution. 15 1.1 uch * 3. Neither the name of the University nor the names of its contributors 16 1.1 uch * may be used to endorse or promote products derived from this software 17 1.1 uch * without specific prior written permission. 18 1.1 uch * 19 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 1.1 uch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 uch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 uch * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 1.1 uch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 uch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 uch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 uch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 uch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 uch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 uch * SUCH DAMAGE. 30 1.1 uch */ 31 1.1 uch 32 1.3 nonaka #include "stand.h" 33 1.1 uch #include <sys/cdefs.h> 34 1.1 uch #include <sys/types.h> 35 1.1 uch #include <stdlib.h> 36 1.1 uch #include <libsa_string.h> 37 1.1 uch 38 1.1 uch LIBSA_NAMESPACE_BEGIN 39 1.1 uch __BEGIN_DECLS 40 1.1 uch char * 41 1.1 uch strtok(char *s, const char *delim) 42 1.1 uch { 43 1.1 uch const char *spanp; 44 1.1 uch int c, sc; 45 1.1 uch char *tok; 46 1.1 uch static char *last; 47 1.1 uch 48 1.1 uch if (s == NULL && (s = last) == NULL) 49 1.1 uch return (NULL); 50 1.1 uch 51 1.1 uch /* 52 1.1 uch * Skip (span) leading delimiters (s += strspn(s, delim), sort of). 53 1.1 uch */ 54 1.1 uch cont: 55 1.1 uch c = *s++; 56 1.1 uch for (spanp = delim; (sc = *spanp++) != 0;) { 57 1.1 uch if (c == sc) 58 1.1 uch goto cont; 59 1.1 uch } 60 1.1 uch 61 1.1 uch if (c == 0) { /* no non-delimiter characters */ 62 1.1 uch last = NULL; 63 1.1 uch return (NULL); 64 1.1 uch } 65 1.1 uch tok = s - 1; 66 1.1 uch 67 1.1 uch /* 68 1.1 uch * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). 69 1.1 uch * Note that delim must have one NUL; we stop if we see that, too. 70 1.1 uch */ 71 1.1 uch for (;;) { 72 1.1 uch c = *s++; 73 1.1 uch spanp = delim; 74 1.1 uch do { 75 1.1 uch if ((sc = *spanp++) == c) { 76 1.1 uch if (c == 0) 77 1.1 uch s = NULL; 78 1.1 uch else 79 1.1 uch s[-1] = 0; 80 1.1 uch last = s; 81 1.1 uch return (tok); 82 1.1 uch } 83 1.1 uch } while (sc != 0); 84 1.1 uch } 85 1.1 uch /* NOTREACHED */ 86 1.1 uch } 87 1.1 uch __END_DECLS 88 1.1 uch LIBSA_NAMESPACE_END 89