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