Date: Sun, 23 Apr 2000 05:39:22 -0700 (PDT) From: pekkas@netcore.fi To: freebsd-gnats-submit@FreeBSD.org Subject: misc/18175: strtok(3) example doesn't work. Message-ID: <200004231239.FAA24453@freefall.freebsd.org>
next in thread | raw e-mail | index | archive | help
>Number: 18175
>Category: misc
>Synopsis: strtok(3) example doesn't work.
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: freebsd-bugs
>State: open
>Quarter:
>Keywords:
>Date-Required:
>Class: doc-bug
>Submitter-Id: current-users
>Arrival-Date: Sun Apr 23 05:40:01 PDT 2000
>Closed-Date:
>Last-Modified:
>Originator: Pekka Savola
>Release: 3.4-STABLE
>Organization:
-
>Environment:
FreeBSD gap.tky.hut.fi 3.4-STABLE FreeBSD 3.4-STABLE #2: Sat Jan 8 10:39:13 EET 2000 root@gap.tky.hut.fi:/usr/src/sys/compile/OW i386
>Description:
1) The code example in strtok(3) man page does not compile at all.
2) Also, this
strcpy(test, "This;is.a:test:of=the/string\tokenizer-function.");
should probably be
strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
3) Also, there is a problem with the idea behind it; code like:
---
for (word = strtok_r(test, sep, &brkt);
word;
word = strtok_r(NULL, sep, &brkt))
{
---
Just loops the first token forever.
>How-To-Repeat:
1)
Copy the code into a .c file and add a few includes (stdlib.h, string.h). Compiling it,
you get:
---
d.c:21: unterminated string or character constant
d.c:21: possible real start of unterminated constant
---
[ which is line 'printf("So far we're at %s:%s0, word, phrase);' ]
---
2) and later, after fixing that:
---
d.c:7: warning: unknown escape sequence `\/'
---
[ which is line 'char *sep = "\/:;=-";' ]
3) the output is like:
---
So far we're at This:blah0So far we're at This:blat0So far we're at This:blab0So far we're at This:blag0So far we're at is.a:blah0So
[ goes on ]
Which is probably not what we want ("This" is always the same).
>Fix:
1) change
printf("So far we're at %s:%s0, word, phrase)
to
printf("So far we're at %s:%s\n", word, phrase)
[0 also changed to \n for readability ]
change
char *sep = "\/:;=-";
to
char *sep = "\\/:;=-";
2) replace
strcpy(test, "This;is.a:test:of=the/string\tokenizer-function.");
with
strcpy(test, "This;is.a:test:of=the/string\\tokenizer-function.");
3) The whole example would probably have to be re-thought.
for-loops could be replaced with, for example, structure like:
-----
if ((word = strtok_r(test, sep, &brkb )) != NULL) {
while ((word = strtok_r(NULL, sep, &brkb )) != NULL) {
xxxx
}
}
However, I'm not a programmer, so I'm not sure if this is a working/best
approach.
>Release-Note:
>Audit-Trail:
>Unformatted:
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200004231239.FAA24453>
