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 notXXXXXX. 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
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms