1 0 stevel /* 2 7478 Vladimir * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 3 7478 Vladimir * Use is subject to license terms. 4 0 stevel */ 5 0 stevel 6 0 stevel /* $OpenBSD: strsep.c,v 1.3 1997/08/20 04:28:14 millert Exp $ */ 7 0 stevel 8 0 stevel /* 9 0 stevel * Copyright (c) 1990, 1993 10 0 stevel * The Regents of the University of California. All rights reserved. 11 0 stevel * 12 0 stevel * Redistribution and use in source and binary forms, with or without 13 0 stevel * modification, are permitted provided that the following conditions 14 0 stevel * are met: 15 0 stevel * 1. Redistributions of source code must retain the above copyright 16 0 stevel * notice, this list of conditions and the following disclaimer. 17 0 stevel * 2. Redistributions in binary form must reproduce the above copyright 18 0 stevel * notice, this list of conditions and the following disclaimer in the 19 0 stevel * documentation and/or other materials provided with the distribution. 20 0 stevel * 3. All advertising materials mentioning features or use of this software 21 0 stevel * must display the following acknowledgement: 22 0 stevel * This product includes software developed by the University of 23 0 stevel * California, Berkeley and its contributors. 24 0 stevel * 4. Neither the name of the University nor the names of its contributors 25 0 stevel * may be used to endorse or promote products derived from this software 26 0 stevel * without specific prior written permission. 27 0 stevel * 28 0 stevel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 0 stevel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 0 stevel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 0 stevel * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 0 stevel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 0 stevel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 0 stevel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 0 stevel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 0 stevel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 0 stevel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 0 stevel * SUCH DAMAGE. 39 0 stevel */ 40 0 stevel 41 7478 Vladimir #include "lint.h" 42 0 stevel #include <string.h> 43 0 stevel #include <stdio.h> 44 0 stevel 45 0 stevel #if defined(LIBC_SCCS) && !defined(lint) 46 0 stevel #if 0 47 0 stevel static char sccsid[] = "@(#)strsep.c 8.1 (Berkeley) 6/4/93"; 48 0 stevel #else 49 0 stevel static char *rcsid = 50 0 stevel "$OpenBSD: strsep.c,v 1.3 1997/08/20 04:28:14 millert Exp $"; 51 0 stevel #endif 52 0 stevel #endif /* LIBC_SCCS and not lint */ 53 0 stevel 54 0 stevel /* 55 0 stevel * Get next token from string *stringp, where tokens are possibly-empty 56 0 stevel * strings separated by characters from delim. 57 0 stevel * 58 0 stevel * Writes NULs into the string at *stringp to end tokens. 59 0 stevel * delim need not remain constant from call to call. 60 0 stevel * On return, *stringp points past the last NUL written (if there might 61 0 stevel * be further tokens), or is NULL (if there are definitely no more tokens). 62 0 stevel * 63 0 stevel * If *stringp is NULL, strsep returns NULL. 64 0 stevel */ 65 0 stevel char * 66 0 stevel strsep(char **stringp, const char *delim) 67 0 stevel { 68 0 stevel register char *s; 69 0 stevel register const char *spanp; 70 0 stevel register int c, sc; 71 0 stevel char *tok; 72 0 stevel 73 0 stevel if ((s = *stringp) == NULL) 74 0 stevel return (NULL); 75 0 stevel for (tok = s; ; ) { 76 0 stevel c = *s++; 77 0 stevel spanp = delim; 78 0 stevel do { 79 0 stevel if ((sc = *spanp++) == c) { 80 0 stevel if (c == 0) 81 0 stevel s = NULL; 82 0 stevel else 83 0 stevel s[-1] = 0; 84 0 stevel *stringp = s; 85 0 stevel return (tok); 86 0 stevel } 87 0 stevel } while (sc != 0); 88 0 stevel } 89 0 stevel /* NOTREACHED */ 90 0 stevel } 91