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(s, delim) 54 char *s; 55 const char *delim; 56 { 57 const char *spanp; 58 int c, sc; 59 char *tok; 60 static char *last; 61 62 63 if (s == NULL && (s = last) == NULL) 64 return (NULL); 65 66 /* 67 * Skip (span) leading delimiters (s += strspn(s, delim), sort of). 68 */ 69 #ifdef OLD 70 cont: 71 c = *s++; 72 for (spanp = delim; (sc = *spanp++) != 0;) { 73 if (c == sc) 74 goto cont; 75 } 76 77 if (c == 0) { /* no non-delimiter characters */ 78 last = NULL; 79 return (NULL); 80 } 81 tok = s - 1; 82 #else 83 tok = s; 84 #endif 85 86 /* 87 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). 88 * Note that delim must have one NUL; we stop if we see that, too. 89 */ 90 for (;;) { 91 c = *s++; 92 spanp = delim; 93 do { 94 if ((sc = *spanp++) == c) { 95 if (c == 0) 96 s = NULL; 97 else 98 s[-1] = 0; 99 last = s; 100 return (tok); 101 } 102 } while (sc != 0); 103 } 104 /* NOTREACHED */ 105 } 106