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