From nobody@FreeBSD.org  Sun Apr 23 05:39:22 2000
Return-Path: <nobody@FreeBSD.org>
Received: from freefall.freebsd.org (freefall.FreeBSD.ORG [204.216.27.21])
	by hub.freebsd.org (Postfix) with ESMTP id 2BE2B37B622
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 23 Apr 2000 05:39:22 -0700 (PDT)
	(envelope-from nobody@FreeBSD.org)
Received: (from nobody@localhost)
	by freefall.freebsd.org (8.9.3/8.9.2) id FAA24453;
	Sun, 23 Apr 2000 05:39:22 -0700 (PDT)
	(envelope-from nobody@FreeBSD.org)
Message-Id: <200004231239.FAA24453@freefall.freebsd.org>
Date: Sun, 23 Apr 2000 05:39:22 -0700 (PDT)
From: pekkas@netcore.fi
Sender: nobody@FreeBSD.org
To: freebsd-gnats-submit@FreeBSD.org
Subject: strtok(3) example doesn't work.
X-Send-Pr-Version: www-1.0

>Number:         18175
>Category:       misc
>Synopsis:       strtok(3) example doesn't work.
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    ru
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          doc-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Apr 23 05:40:01 PDT 2000
>Closed-Date:    Mon Jun 4 06:55:26 PDT 2001
>Last-Modified:  Mon Jun 04 06:56:07 PDT 2001
>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:
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... 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=18175 

From: Peter Pentchev <roam@orbitel.bg>
To: phk@FreeBSD.org
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: Re: misc/18175: strtok(3) example doesn't work.
Date: Sat, 2 Jun 2001 12:19:17 +0300

 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.
State-Changed-From-To: open->closed 
State-Changed-By: ru 
State-Changed-When: Mon Jun 4 06:55:26 PDT 2001 
State-Changed-Why:  
The problem was already fixed a long ago in revision 1.4 of strtok.3. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=18175 
>Unformatted:
