xref: /illumos-gate/usr/src/cmd/krb5/kadmin/dbutil/strtok.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
26 
27 #include <stddef.h>
28 #include <string.h>
29 #include "nstrtok.h"
30 
31 /*
32  * Function: nstrtok
33  *
34  * Purpose: the same as strtok ... just different. does not deal with
35  *	    multiple tokens in row.
36  *
37  * Arguments:
38  *	s	    (input) string to scan
39  *	delim	    (input) list of delimiters
40  * 	<return value> string or null on error.
41  *
42  * Requires:
43  *	nuttin
44  *
45  * Effects:
46  *	sets last to string
47  *
48  * Modifies:
49  *	last
50  *
51  */
52 
53 char *
54 nstrtok(s, delim)
55 	register char *s;
56 	register const char *delim;
57 {
58 	register const char *spanp;
59 	register int c, sc;
60 	char *tok;
61 	static char *last;
62 
63 
64 	if (s == NULL && (s = last) == NULL)
65 		return (NULL);
66 
67 	/*
68 	 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
69 	 */
70 #ifdef OLD
71 cont:
72 	c = *s++;
73 	for (spanp = delim; (sc = *spanp++) != 0;) {
74 		if (c == sc)
75 			goto cont;
76 	}
77 
78 	if (c == 0) {		/* no non-delimiter characters */
79 		last = NULL;
80 		return (NULL);
81 	}
82 	tok = s - 1;
83 #else
84 	tok = s;
85 #endif
86 
87 	/*
88 	 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
89 	 * Note that delim must have one NUL; we stop if we see that, too.
90 	 */
91 	for (;;) {
92 		c = *s++;
93 		spanp = delim;
94 		do {
95 			if ((sc = *spanp++) == c) {
96 				if (c == 0)
97 					s = NULL;
98 				else
99 					s[-1] = 0;
100 				last = s;
101 				return (tok);
102 			}
103 		} while (sc != 0);
104 	}
105 	/* NOTREACHED */
106 }
107 
108