From owner-freebsd-bugs Sun Apr 23 5:40: 6 2000 Delivered-To: freebsd-bugs@freebsd.org Received: from freefall.freebsd.org (freefall.FreeBSD.ORG [204.216.27.21]) by hub.freebsd.org (Postfix) with ESMTP id A905137B76C for ; Sun, 23 Apr 2000 05:40:01 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.9.3/8.9.2) id FAA24502; Sun, 23 Apr 2000 05:40:01 -0700 (PDT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.FreeBSD.ORG [204.216.27.21]) by hub.freebsd.org (Postfix) with ESMTP id 2BE2B37B622 for ; 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 To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-1.0 Subject: misc/18175: strtok(3) example doesn't work. Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org >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