friedl@vsi.UUCP (Stephen J. Friedl) (02/24/88)
Netpeople, I'd like to relate a gotcha that could drive you crazy. We often use strtok(3) to run through a PATH-type variable to pick apart the individual pieces. Be careful when doing this with strings obtained from the environment because strtok(3) puts in little nul characters at the delimiters (modifying the string). If the program later forks a child, the child's environment gets a truncated version: BEFORE: PATH=/bin:/usr/bin:/etc\0 \0 is a nul AFTER: PATH=/bin\0/usr/bin\0/etc\0 The remedy is to copy the variable into a local or static buffer and run strtok(3) from that, leaving the environment alone. We had a large program that picked apart a $DBPATH variable (for the Informix database) but had no child processes until about six months later. At that point the child database opens were failing and we spent a lot of time suspecting file and data- base permissions, memory errors, etc. *Many* debug statements later we found it. Sigh. Steve -- Life : Stephen J. Friedl @ V-Systems Inc/Santa Ana, CA *Hi Mom* CSNet: friedl%vsi.uucp@kent.edu ARPA: friedl%vsi.uucp@uunet.uu.net uucp : {kentvax, uunet, attmail, ihnp4!amdcad!uport}!vsi!friedl
henry@utzoo.uucp (Henry Spencer) (02/25/88)
> The remedy is to copy the variable into a local or static > buffer and run strtok(3) from that, leaving the environment > alone. Better yet, actually, is to malloc space for it. That saves you from having to make assumptions about how long it can be. -- Those who do not understand Unix are | Henry Spencer @ U of Toronto Zoology condemned to reinvent it, poorly. | {allegra,ihnp4,decvax,utai}!utzoo!henry
karl@haddock.ISC.COM (Karl Heuer) (02/27/88)
In article <45@vsi.UUCP> friedl@vsi.UUCP (Stephen J. Friedl) writes: >I'd like to relate a gotcha that could drive you crazy. We often use >strtok(3) to run through a PATH-type variable to pick apart the individual >pieces. [But it scribbles in your array, which can be a problem.] Another problem is that strtok() doesn't distinguish between "/bin:/etc" and "/bin::/etc", so it's not really appropriate for parsing PATH or /etc/passwd. For the above and other reasons, I never use strtok(). Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint