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