Xref: utzoo comp.unix.microport:2861 comp.unix.wizards:14766 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!gatech!hubcap!ncrcae!ncrlnk!uunet!auspex!guy From: guy@auspex.UUCP (Guy Harris) Newsgroups: comp.unix.microport,comp.unix.wizards Subject: Re: bug in putenv()! Keywords: bug putenv Message-ID: <1046@auspex.UUCP> Date: 18 Feb 89 11:20:49 GMT References: <204@tree.UUCP> Reply-To: guy@auspex.UUCP (Guy Harris) Organization: Auspex Systems, Santa Clara Lines: 43 >> char envbuf[100]; >> >> sprintf(envbuf,"USERNAME=%s","hi"); >> putenv(envbuf); To quote from the SunOS 4.0 PUTENV(3) man page (which probably describes most "putenv" implementations, since the code hasn't changed much as far as I know - I think the 4.0 one is just the S5R2 one with a Sun SCCS header stuck in...): DESCRIPTION 'string' points to a string of the form `name = value' putenv() makes the value of the environment variable name equal to value by altering an existing variable or creating a new one. In either case, the string pointed to by 'string' becomes part of the environment, so altering the string will change the environment. The space used by 'string' is no longer used once a new string-defining name is passed to putenv. WARNINGS ... A potential error is to call putenv() with an automatic variable as the argument, then exit the calling function while 'string' is still part of the environment. It sure looks as if you're handing "putenv" an automatic variable in your example; do you return from the function in which that automatic is declared before doing another "putenv" to modify "USERNAME"? If so, that's probably your problem. >But it works right if done like this... >> putenv("USERNAME=hi"); The argument is static, not automatic, so the string stays around after the function returns. >The strange part is it works with numbers when doing a sprintf... >> sprintf(envbuf,"USERNUM=%d",13); >> putenv(envbuf); Sheer luck, possibly.