1 /*
2 * Copyright (C) 1996 Free Software Foundation, Inc
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 */
19
20 #define NULL 0
21
22 #define size_t int
23
24 #ifdef NO_STRTOK
25
26 /* Find the first ocurrence in S of any character in ACCEPT. */
27 char *
strpbrk(char * s,char * accept)28 strpbrk(char *s, char *accept)
29 {
30 while (*s != '\0')
31 if (strchr(accept, *s) == NULL)
32 ++s;
33 else
34 return (char *) s;
35
36 return NULL;
37 }
38
39
40 static char *olds = NULL;
41
42 char *
strtok(char * s,char * delim)43 strtok(char *s, char *delim)
44 {
45 char *token;
46
47 if (s == NULL)
48 {
49 if (olds == NULL)
50 {
51 /*errno = EINVAL; Wonder where errno is defined....*/
52 return NULL;
53 }
54 else
55 s = olds;
56 }
57
58 /* Scan leading delimiters. */
59 s += strspn(s, delim);
60 if (*s == '\0')
61 {
62 olds = NULL;
63 return NULL;
64 }
65
66 /* Find the end of the token. */
67 token = s;
68 s = strpbrk(token, delim);
69 if (s == NULL)
70 /* This token finishes the string. */
71 olds = NULL;
72 else
73 {
74 /* Terminate the token and make OLDS point past it. */
75 *s = '\0';
76 olds = s + 1;
77 }
78 return token;
79 }
80
81
82 #endif /* NO_STRTOK */
83
84 #ifdef NO_STRSPN
85
86 /* Return the length of the maximum initial segment
87 of S which contains only characters in ACCEPT. */
88 size_t
strspn(char * s,char * accept)89 strspn(char *s, char *accept)
90 {
91 register char *p;
92 register char *a;
93 register size_t count = 0;
94
95 for (p = s; *p != '\0'; ++p)
96 {
97 for (a = accept; *a != '\0'; ++a)
98 if (*p == *a)
99 break;
100 if (*a == '\0')
101 return count;
102 else
103 ++count;
104 }
105
106 return count;
107 }
108
109 #endif NO_STRSPN
110