mktemp(3)

πŸ’– mktemp(3) β€” the "make a unique temporary filename" page πŸ’–

okay bestie, sit down, we need to talk. this one is a Library Functions Manual page for the function called mktemp, and it's lowkey a cautionary tale. no cap. 🫢


✨ NAME ✨

mktemp β€” make a unique temporary filename

πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc) β€” she lives in libc, queen πŸ’…

🌸 SYNOPSIS 🌸

#include <stdlib.h>

char *mktemp(char *template);

🧚 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

mktemp():

Since glibc 2.12:
    (_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200112L)
        || /* glibc >= 2.19: */ _DEFAULT_SOURCE
        || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
Before glibc 2.12:
    _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 500

πŸ’€ DESCRIPTION πŸ’€

Never use this function; see BUGS. literally. that's the whole vibe. πŸ™ˆ

The mktemp() function generates a unique temporary filename from template. The last six characters of template must be XXXXXX and these are replaced with a string that makes the filename unique. And because it's gonna get modified, template must not be a string constant β€” declare it as a character array instead, bestie. do not pass it a literal, she will not survive it. 😭


πŸ” RETURN VALUE πŸ”

The mktemp() function always returns template. always always. If a unique name was created, the last six bytes of template will have been modified in such a way that the resulting name is unique (i.e., does not exist already). If a unique name could not be created, template is made an empty string, and errno is set to indicate the error. so like… check errno, okay? πŸ‘‘


🚨 ERRORS 🚨

  • EINVAL β€” The last six characters of template were not XXXXXX. that's it, that's the whole list. she's picky about her X's fr fr.

🌈 ATTRIBUTES 🌈

For an explanation of the terms used in this section, see attributes(7).

The way that this table is giving "one row, no notes":

  • Interface: mktemp()
  • Attribute: Thread safety
  • Value: MT-Safe

MT-Safe and iconic, periodt. ✨


πŸ“œ STANDARDS πŸ“œ

None.

that's right. none. not a single one claims her. πŸ’€


πŸ•°οΈ HISTORY πŸ•°οΈ

4.3BSD, POSIX.1-2001. Removed in POSIX.1-2008. she got βœ‚οΈ removed, bestie. they said "we don't do that anymore."


πŸ›‘ BUGS πŸ›‘

Never use mktemp(). like, genuinely, for real for real. πŸ™…β€β™€οΈ

Some implementations follow 4.3BSD and replace XXXXXX by the current process ID and a single letter, so that at most 26 different names can be returned. twenty-six. that's it. that's the whole namespace. 😭

Since on the one hand the names are easy to guess, and on the other hand there is a race between testing whether the name exists and opening the file, every use of mktemp() is a security risk. a whole race condition. a TOCTOU situation. not the cute kind of race. πŸƒβ€β™€οΈπŸ’¨

The race is avoided by mkstemp(3) and mkdtemp(3). those are your girls. use those girls instead. πŸ’–


πŸ‘€ SEE ALSO πŸ‘€

mktemp(1), mkdtemp(3), mkstemp(3), tempnam(3), tmpfile(3), tmpnam(3)


and that's the page, bestie!! πŸŽ€ huge shoutout to the Linux man-pages 6.20, dated 2026-02-08, for keeping this fossil documented so we all know exactly which function to never ever call. mwah. πŸ’œβœ¨

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms