Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: How to use a temporary file in a portable way Date: Fri, 10 Apr 2015 08:29:49 -0700 Organization: None to speak of Lines: 30 Message-ID: <87d23cuhle.fsf@kst-u.example.com> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="9be37d943435275679235b07c7947eef"; logging-data="1839"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18oFk1oFg9VJzzJqL6+UDPm" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:HFfD1HJNwRxTwZKiZEYfqV2nrt8= sha1:OO5K2B4c4ZbcQTykKgHmfxIL548= Xref: csiph.com comp.lang.c:59882 Richard Damon writes: > On 4/9/15 1:22 AM, pozz wrote: >> How to choose the temporary file name? How to face the situation when >> the temporary file name already exist? > > Call tmpnam(). From the standard: > The tmpnam function generates a string that is a valid file name and > that is not the same as the name of an existing file. tmpnam() has potential problems. The Linux man page says: Although tmpnam() generates names that are difficult to guess, it is nevertheless possible that between the time that tmpnam() returns a pathname, and the time that the program opens it, another program might create that pathname using open(2), or create it as a symbolic link. This can lead to security holes. To avoid such possibilities, use the open(2) O_EXCL flag to open the pathname. Or better yet, use mkstemp(3) or tmpfile(3). ... BUGS Never use this function. Use mkstemp(3) or tmpfile(3) instead. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"