Xref: utzoo comp.lang.c:7535 comp.databases:746 Path: utzoo!mnetor!uunet!vsi!friedl From: friedl@vsi.UUCP (Stephen J. Friedl) Newsgroups: comp.lang.c,comp.databases Subject: gotcha in strtok(3) Message-ID: <45@vsi.UUCP> Date: 24 Feb 88 07:49:57 GMT Distribution: comp Organization: V-Systems -- Santa Ana, CA Lines: 29 Keywords: strtok(3), getenv(3), gotcha, $PATH, $DBPATH 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