Date: Thu, 11 Jan 1996 22:14:59 -0700 From: Nate Williams <nate@sri.MT.net> To: Josh MacDonald <jmacd@CS.Berkeley.EDU> Cc: freebsd-hackers@freebsd.org Subject: Re: mktemp() sucks, why not fix it? Message-ID: <199601120514.WAA23403@rocky.sri.MT.net> In-Reply-To: <199601120419.UAA07014@paris.CS.Berkeley.EDU> References: <199601120419.UAA07014@paris.CS.Berkeley.EDU>
next in thread | previous in thread | raw e-mail | index | archive | help
> 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199601120514.WAA23403>