1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved 4 * 5 */ 6 7 /* 8 * Copyright (c) 1988 Regents of the University of California. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms are permitted 12 * provided that: (1) source distributions retain this entire copyright 13 * notice and comment, and (2) distributions including binaries display 14 * the following acknowledgement: ``This product includes software 15 * developed by the University of California, Berkeley and its contributors'' 16 * in the documentation or other materials provided with the distribution 17 * and in all advertising materials mentioning features or use of this 18 * software. Neither the name of the University nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 24 */ 25 26 #include <stddef.h> 27 #include <string.h> 28 #include "nstrtok.h" 29 30 /* 31 * Function: nstrtok 32 * 33 * Purpose: the same as strtok ... just different. does not deal with 34 * multiple tokens in row. 35 * 36 * Arguments: 37 * s (input) string to scan 38 * delim (input) list of delimiters 39 * <return value> string or null on error. 40 * 41 * Requires: 42 * nuttin 43 * 44 * Effects: 45 * sets last to string 46 * 47 * Modifies: 48 * last 49 * 50 */ 51 52 char * 53 nstrtok(char *s, const char *delim) 54 { 55 const char *spanp; 56 int c, sc; 57 char *tok; 58 static char *last; 59 60 61 if (s == NULL && (s = last) == NULL) 62 return (NULL); 63 64 /* 65 * Skip (span) leading delimiters (s += strspn(s, delim), sort of). 66 */ 67 #ifdef OLD 68 cont: 69 c = *s++; 70 for (spanp = delim; (sc = *spanp++) != 0;) { 71 if (c == sc) 72 goto cont; 73 } 74 75 if (c == 0) { /* no non-delimiter characters */ 76 last = NULL; 77 return (NULL); 78 } 79 tok = s - 1; 80 #else 81 tok = s; 82 #endif 83 84 /* 85 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). 86 * Note that delim must have one NUL; we stop if we see that, too. 87 */ 88 for (;;) { 89 c = *s++; 90 spanp = delim; 91 do { 92 if ((sc = *spanp++) == c) { 93 if (c == 0) 94 s = NULL; 95 else 96 s[-1] = 0; 97 last = s; 98 return (tok); 99 } 100 } while (sc != 0); 101 } 102 /* NOTREACHED */ 103 } 104