For the task of creating a temporary directory in /tmp,
how would one choose between mkdtemp, mkstemp, etc., for portable code?
For the task of creating a temporary directory in /tmp , how would one
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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, sincemkstempcan only create regular files.mkdtempwas 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.