8

(Note: This is not a duplicate question)

I'm using the libc function tmpnam, and getting the following warning:

warning: the use of 'tmpnam' is dangerous, better use 'mkstemp'

My question isn't "how to disable the warning", but rather "what function should I be using instead"? mkstemp doesn't help, because I'm not trying to create a temporary file - I'm creating a temporary directory. And AFAIK, there isn't an API function for that.

So if I'm not supposed to use tmpnam, what am I supposed to use?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Sod Almighty
  • 1,768
  • 1
  • 16
  • 29
  • 1
    If someone is looking to why the function is dangerous, see: [tmpnam warning saying it is dangerous](https://stackoverflow.com/questions/3299881/tmpnam-warning-saying-it-is-dangerous) – Evandro Coan Dec 29 '22 at 14:04

1 Answers1

8

You're looking for mkdtemp:

mkdtemp - create a unique temporary directory

e.g.,

#include <stdlib.h>
#include <string.h>
...
char templatebuf[80];
char *mkdirectory = mkdtemp(strcpy(templatebuf, "/tmp/mkprogXXXXXX"));

using strcpy to ensure the parameter passed to mkdtemp is writable (c89), or

#include <stdlib.h>
...
char templatebuf[] = "/tmp/mkprogXXXXXX";
char *mkdirectory = mkdtemp(templatebuf);

with c99.

Since the feature is "new" (only standardized within the past ten years, though provided in the mid-1990s on Linux), you need to turn the feature on in the header files with a preprocessor definition (which may differ from one platform to another). The simplest for Linux is to define _GNU_SOURCE, e.g.,

gcc -D_GNU_SOURCE -o foo foo.c
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Instead of `strcpy`, you can use `char templatebuf[] = "/tmp/mkprogXXXXXX";` – Barmar Feb 03 '16 at 22:18
  • How does c99 allow this constant assignment without it being non-writable? – Sod Almighty Feb 04 '16 at 05:34
  • 1
    It is an *initialization* of writable memory, not simply an *assignment*. To verify my understanding of the standard, I checked with gcc using `-Wcast-qual` and `-Wwrite-strings` (which at least proved that gcc agreed). – Thomas Dickey Feb 04 '16 at 09:20
  • You should really use `TMPDIR` instead of `/tmp/` as well. So there isn't really any easy way to switch out `tempnam` (which actually check `TMPDIR`). – csl Nov 21 '16 at 09:28