In defense of scanf() (Re: Re^2: scanf(..))

Stephen J. Friedl friedl at vsi.COM
Mon Jun 19 05:16:51 AEST 1989


In article <4563 at ficc.uu.net>, peter at ficc.uu.net (Peter da Silva) writes:
> I tend to stick with strspn() and strtok(), myself.

In article <824 at cbnewsl.ATT.COM>, mpl at cbnewsl.ATT.COM (michael.p.lindner) writes:
> Hope you never have to do anything complex.  If you call strtok on e string
> in the middle of a strtok of another string it trashes its state information
> on the first string (a little known feature), which can cause extremely
> elusive bugs.

In addition, strtok() considers multiple occurrences of the
separating token to be one, so you can't use it for obvious
things like parsing an /etc/passwd line.

One more thing.  If you use strtok to pick apart an environment
variable, be sure to copy the environment string somewhere before
you tear into it.  We had a program whose child processes were
always failing, and it drove us nuts until we realized that it
was strtok again.  We were picking apart $PATH earlier in the
program, and since strtok places NULs in the string, the
environment was getting corrupted for the child.

Neither of these are bugs -- they are documented parts of the
function -- but nevertheless we have been hit with these gotchas.

     Steve

-- 
Stephen J. Friedl / V-Systems, Inc. / Santa Ana, CA / +1 714 545 6442 
3B2-kind-of-guy   / friedl at vsi.com  / {attmail, uunet, etc}!vsi!friedl
                                          ---> vsi!bang!friedl <-- NEW
"Friends don't let friends run Xenix" - me



More information about the Comp.lang.c mailing list