Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!eecae!netnews.upenn.edu!rutgers!ucsd!ucsdhub!hp-sdd!ncr-sd!ncrlnk!uunet!mcvax!hp4nl!philmds!leo From: leo@philmds.UUCP (Leo de Wit) Newsgroups: comp.unix.wizards Subject: Re: replacement for putenv() Message-ID: <948@philmds.UUCP> Date: 11 Feb 89 19:12:21 GMT References: <164@bds-ny.UUCP> <549@marob.MASA.COM> Reply-To: leo@philmds.UUCP (Leo de Wit) Organization: Philips I&E DTS Eindhoven Lines: 32 In article <549@marob.MASA.COM> rogol@marob.masa.com (Fred Buck) writes: [] | /* see if this variable is already in environment */ | i = (-1); | while (environ[++i]) { | if (strncmp(string,environ[i],namelen)==0) { /* It's there */ | /* if we can just patch it, do so and return */ | if (strlen(string)<=strlen(environ[i])) { | strcpy(environ[i],string); return(0); | } | else break; | } | } | /* OK, allocate a spot for 'string' and copy it there */ | cptr = malloc(strlen(string)+1); | if (cptr==0) return(-1); /* can't malloc */ | strcpy(cptr,string); | /* no env at all; or else var exists but's too small to hold 'string' */ | if (i==0 || environ[i]) { | environ[i] = cptr; return(0); /* ok, done */ | } If i == 0 (no env at all), you end up overwriting the terminating null ptr (environ[0]). So the last three lines should read: if (environ[i] != (char *)0) { /* or simply: if (environ[i]) */ environ[i] = cptr; return(0); /* ok, done */ } Now for i == 0 a array for two char ptrs is allocated in the code that follows. Leo.