Bug 18175

Summary: strtok(3) example doesn't work.
Product: Base System Reporter: Pekka Savola <pekkas>
Component: miscAssignee: ru <ru>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 3.4-STABLE   
Hardware: Any   
OS: Any   

Description Pekka Savola 2000-04-23 13:40:01 UTC
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.

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.
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).
Comment 1 Poul-Henning Kamp freebsd_committer freebsd_triage 2001-06-02 10:05:06 UTC
Responsible Changed
From-To: freebsd-bugs->ru

See if Ruslan can fix this...
Comment 2 Peter Pentchev 2001-06-02 10:19:17 UTC
On Sat, Jun 02, 2001 at 02:05:27AM -0700, phk@FreeBSD.org wrote:
> Synopsis: strtok(3) example doesn't work.
> 
> Responsible-Changed-From-To: freebsd-bugs->ru
> Responsible-Changed-By: phk
> Responsible-Changed-When: Sat Jun 2 02:05:06 PDT 2001
> Responsible-Changed-Why: 
> See if Ruslan can fix this...

Actually, I believe all these issues have been fixed already :)

G'luck,
Peter

-- 
This sentence contains exactly threee erors.
Comment 3 ru freebsd_committer freebsd_triage 2001-06-04 14:55:26 UTC
State Changed
From-To: open->closed

The problem was already fixed a long ago in revision 1.4 of strtok.3.