For the task of creating a temporary directory in /tmp
,
how would one choose between mkdtemp
, mkstemp
, etc., for portable code?
I presume you need to create a temporary directory inside a directory where other users may have write permission.
As an administrator, you should set things up so that each user has its own TMPDIR
(e.g. with pam-tmpdir — or even better with per-process namespaces, but that takes more setup). As an application writer, however, you can't assume this, so you need to cope with a world-writable /tmp
.
The right function here is mkdtemp
, since mkstemp
can only create regular files. mkdtemp
was only introduced in POSIX.1 2008, so in principle it might not be available on all POSIX platforms yet. However, it has been available on major platforms for a long time:
So in practice, you can safely go with mkdtemp
. If you need a fallback, include the OpenBSD implementation in your source.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With