From owner-freebsd-hackers Thu Jan 11 21:12:33 1996 Return-Path: owner-hackers Received: (from root@localhost) by freefall.freebsd.org (8.7.3/8.7.3) id VAA28653 for hackers-outgoing; Thu, 11 Jan 1996 21:12:33 -0800 (PST) Received: from rocky.sri.MT.net (rocky.sri.MT.net [204.182.243.10]) by freefall.freebsd.org (8.7.3/8.7.3) with SMTP id VAA28643 for ; Thu, 11 Jan 1996 21:12:25 -0800 (PST) Received: (from nate@localhost) by rocky.sri.MT.net (8.6.12/8.6.12) id WAA23403; Thu, 11 Jan 1996 22:14:59 -0700 Date: Thu, 11 Jan 1996 22:14:59 -0700 From: Nate Williams Message-Id: <199601120514.WAA23403@rocky.sri.MT.net> To: Josh MacDonald Cc: freebsd-hackers@freebsd.org Subject: Re: mktemp() sucks, why not fix it? In-Reply-To: <199601120419.UAA07014@paris.CS.Berkeley.EDU> References: <199601120419.UAA07014@paris.CS.Berkeley.EDU> Sender: owner-hackers@freebsd.org Precedence: bulk > I don't like how: > > main() > { > char buf1[] = "/tmp/foo.XXXXXX", buf2[] = "/tmp/foo.XXXXXX"; > mktemp(buf1); > symlink("/tmp/existsbutnotdirectory/foo", buf1); > if(mktemp(buf2) == NULL) > perror("mktemp() failed"); > printf("buf1 = %s\nbuf2 = %s\n", buf1, buf2); > } > > the second call to mktemp() fails, and they both end up > with the same value. I'll be the first call to mktemp fails as well, unless you happen to be compiling with -fwritable-strings. You have to pass in data to mktemp which can be modified, which neither of the above strings can be. Try this instead: int main() { char buf1[255], buf2[255]; strcpy(buf1, "/tmp/foo.XXXXXX"); strcpy(buf2, "/tmp/foo.XXXXXX"); if ( mktemp(buf1) == NULL ) perror("mktemp() failed"); symlink("/tmp/existsbutnotdirectory/foo", buf1); if(mktemp(buf2) == NULL) perror("mktemp() failed"); printf("buf1 = %s\nbuf2 = %s\n", buf1, buf2); } Nate