strsep.c

00001 /*      $NetBSD: strsep.c,v 1.14 2003/08/07 16:43:52 agc Exp $  */
00002 
00003 /*-
00004  * Copyright (c) 1990, 1993
00005  *      The Regents of the University of California.  All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 3. Neither the name of the University nor the names of its contributors
00016  *    may be used to endorse or promote products derived from this software
00017  *    without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00020  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00023  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00025  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00026  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00027  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00028  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00029  * SUCH DAMAGE.
00030  */
00031 
00032 #include <u/missing/strsep.h>
00033 
00034 #ifndef HAVE_STRSEP
00035 
00036 #include <stdio.h>
00037 
00038 /*
00039  * Get next token from string *stringp, where tokens are possibly-empty
00040  * strings separated by characters from delim.  
00041  *
00042  * Writes NULs into the string at *stringp to end tokens.
00043  * delim need not remain constant from call to call.
00044  * On return, *stringp points past the last NUL written (if there might
00045  * be further tokens), or is NULL (if there are definitely no more tokens).
00046  *
00047  * If *stringp is NULL, strsep returns NULL.
00048  */
00049 char *strsep(char **stringp, const char *delim) 
00050 {
00051         char *s;
00052         const char *spanp;
00053         int c, sc;
00054         char *tok;
00055 
00056         if ((s = *stringp) == NULL)
00057                 return (NULL);
00058 
00059         for (tok = s;;) {
00060                 c = *s++;
00061                 spanp = delim;
00062                 do {
00063                         if ((sc = *spanp++) == c) {
00064                                 if (c == 0)
00065                                         s = NULL;
00066                                 else
00067                                         s[-1] = 0;
00068                                 *stringp = s;
00069                                 return (tok);
00070                         }
00071                 } while (sc != 0);
00072         }
00073         /* NOTREACHED */
00074 }
00075 
00076 #else   /* HAVE_STRSEP */
00077 char *strsep(char **, const char *);
00078 #endif  /* !HAVE_STRSEP */
00079 

←Products
© 2005-2012 - KoanLogic S.r.l. - All rights reserved