strtok (was: In defense of scanf)

Steve Summit scs at adam.pika.mit.edu
Mon Jul 3 02:24:52 AEST 1989


In article <824 at cbnewsl.ATT.COM>, mpl at cbnewsl.ATT.COM (michael.p.lindner) (I think) 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.

This shouldn't be a "little known feature;" it is obvious from
the man page that strtok must keep private state, and the first
thing any good programmer should worry about when "private state"
is mentioned is "non-reentrancy" or "non-concurrency."

strtok is a horrid routine, precisely because of this aspect; I
can't believe SYSIII or SYSV (or wherever it originated) adopted
it, and then of course X3J11 had to go and further legitimize it.
(The rest of the "new" str* functions -- strspn, strpbrk, etc. --
aren't much better.  Can you say orthogonal?  "Orthghnpgtphl!"
Hmm, not very good.  The (r)index to str(r)chr change still
rankles, too.)

                                            Steve Summit
                                            scs at adam.pika.mit.edu



More information about the Comp.lang.c mailing list