style(9)
Submitted by: Joseph Mallett <jmallett@xmach.org> Reviewed by: md5(1)
This commit is contained in:
parent
b8d6599e4c
commit
b87cdc2c64
@ -13,17 +13,13 @@
|
|||||||
*
|
*
|
||||||
* 1. Redistributions of source code must retain the above copyright
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
* notices, this list of conditions and the following disclaimer.
|
* notices, this list of conditions and the following disclaimer.
|
||||||
*
|
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
* notices, this list of conditions and the following disclaimer in the
|
* notices, this list of conditions and the following disclaimer in the
|
||||||
* documentation and/or other materials provided with the distribution.
|
* documentation and/or other materials provided with the distribution.
|
||||||
*
|
|
||||||
* 3. All advertising materials mentioning features or use of this software
|
* 3. All advertising materials mentioning features or use of this software
|
||||||
* must display the following acknowledgement:
|
* must display the following acknowledgement:
|
||||||
*
|
|
||||||
* This product includes software developed by Softweyr LLC, the
|
* This product includes software developed by Softweyr LLC, the
|
||||||
* University of California, Berkeley, and its contributors.
|
* University of California, Berkeley, and its contributors.
|
||||||
*
|
|
||||||
* 4. Neither the name of the University nor the names of its contributors
|
* 4. Neither the name of the University nor the names of its contributors
|
||||||
* may be used to endorse or promote products derived from this software
|
* may be used to endorse or promote products derived from this software
|
||||||
* without specific prior written permission.
|
* without specific prior written permission.
|
||||||
@ -50,117 +46,89 @@ __FBSDID("$FreeBSD$");
|
|||||||
char *
|
char *
|
||||||
strtok_r(char *s, const char *delim, char **last)
|
strtok_r(char *s, const char *delim, char **last)
|
||||||
{
|
{
|
||||||
char *spanp;
|
char *spanp;
|
||||||
int c, sc;
|
int c, sc;
|
||||||
char *tok;
|
char *tok;
|
||||||
|
|
||||||
if (s == NULL && (s = *last) == NULL)
|
if (s == NULL && (s = *last) == NULL)
|
||||||
{
|
return (NULL);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Skip (span) leading delimiters (s += strspn(s, delim), sort of).
|
* Skip (span) leading delimiters (s += strspn(s, delim), sort of).
|
||||||
*/
|
*/
|
||||||
cont:
|
cont:
|
||||||
c = *s++;
|
|
||||||
for (spanp = (char *)delim; (sc = *spanp++) != 0; )
|
|
||||||
{
|
|
||||||
if (c == sc)
|
|
||||||
{
|
|
||||||
goto cont;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c == 0) /* no non-delimiter characters */
|
|
||||||
{
|
|
||||||
*last = NULL;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
tok = s - 1;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
|
|
||||||
* Note that delim must have one NUL; we stop if we see that, too.
|
|
||||||
*/
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
c = *s++;
|
c = *s++;
|
||||||
spanp = (char *)delim;
|
for (spanp = (char *)delim; (sc = *spanp++) != 0;)
|
||||||
do
|
if (c == sc)
|
||||||
{
|
goto cont;
|
||||||
if ((sc = *spanp++) == c)
|
|
||||||
{
|
if (c == 0) { /* no non-delimiter characters */
|
||||||
if (c == 0)
|
*last = NULL;
|
||||||
{
|
return (NULL);
|
||||||
s = NULL;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char *w = s - 1;
|
|
||||||
*w = '\0';
|
|
||||||
}
|
|
||||||
*last = s;
|
|
||||||
return tok;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
while (sc != 0);
|
tok = s - 1;
|
||||||
}
|
|
||||||
/* NOTREACHED */
|
/*
|
||||||
|
* Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
|
||||||
|
* Note that delim must have one NUL; we stop if we see that, too.
|
||||||
|
*/
|
||||||
|
for (;;) {
|
||||||
|
c = *s++;
|
||||||
|
spanp = (char *)delim;
|
||||||
|
do {
|
||||||
|
if ((sc = *spanp++) == c) {
|
||||||
|
if (c == 0)
|
||||||
|
s = NULL;
|
||||||
|
else {
|
||||||
|
char *w = s - 1;
|
||||||
|
*w = '\0';
|
||||||
|
}
|
||||||
|
*last = s;
|
||||||
|
return (tok);
|
||||||
|
}
|
||||||
|
} while (sc != 0);
|
||||||
|
}
|
||||||
|
/* NOTREACHED */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
char *
|
char *
|
||||||
strtok(char *s, const char *delim)
|
strtok(char *s, const char *delim)
|
||||||
{
|
{
|
||||||
static char *last;
|
static char *last;
|
||||||
|
|
||||||
return strtok_r(s, delim, &last);
|
return (strtok_r(s, delim, &last));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#if defined(DEBUG_STRTOK)
|
#ifdef DEBUG_STRTOK
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Test the tokenizer.
|
* Test the tokenizer.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
main()
|
main(void)
|
||||||
{
|
{
|
||||||
char test[80], blah[80];
|
char test[80], blah[80];
|
||||||
char *sep = "\\/:;=-";
|
char *sep = "\\/:;=-";
|
||||||
char *word, *phrase, *brkt, *brkb;
|
char *word, *phrase, *brkt, *brkb;
|
||||||
|
|
||||||
printf("String tokenizer test:\n");
|
printf("String tokenizer test:\n");
|
||||||
|
strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
|
||||||
|
for (word = strtok(test, sep); word; word = strtok(NULL, sep))
|
||||||
|
printf("Next word is \"%s\".\n", word);
|
||||||
|
phrase = "foo";
|
||||||
|
strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
|
||||||
|
|
||||||
strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
|
for (word = strtok_r(test, sep, &brkt); word;
|
||||||
|
word = strtok_r(NULL, sep, &brkt)) {
|
||||||
|
strcpy(blah, "blah:blat:blab:blag");
|
||||||
|
|
||||||
for (word = strtok(test, sep);
|
for (phrase = strtok_r(blah, sep, &brkb); phrase;
|
||||||
word;
|
phrase = strtok_r(NULL, sep, &brkb))
|
||||||
word = strtok(NULL, sep))
|
printf("So far we're at %s:%s\n", word, phrase);
|
||||||
{
|
|
||||||
printf("Next word is \"%s\".\n", word);
|
|
||||||
}
|
|
||||||
|
|
||||||
phrase = "foo";
|
|
||||||
|
|
||||||
strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
|
|
||||||
|
|
||||||
for (word = strtok_r(test, sep, &brkt);
|
|
||||||
word;
|
|
||||||
word = strtok_r(NULL, sep, &brkt))
|
|
||||||
{
|
|
||||||
strcpy(blah, "blah:blat:blab:blag");
|
|
||||||
|
|
||||||
for (phrase = strtok_r(blah, sep, &brkb);
|
|
||||||
phrase;
|
|
||||||
phrase = strtok_r(NULL, sep, &brkb))
|
|
||||||
{
|
|
||||||
printf("So far we're at %s:%s\n", word, phrase);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* DEBUG_STRTOK */
|
#endif /* DEBUG_STRTOK */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user