mkstemp(3)
โจ mkstemp(3) โจ
okay bestie, pull up a chair because we're talking about mkstemp, mkostemp, mkstemps, and mkstemps's fancy cousin mkostemps โ the whole girl group of temporary-file-creating functions ๐
this is page 3 of the Library Functions Manual and it's literally about to save your life if you've ever been tempted to write your own temp file name generator (don't, that's how you get owned).
๐ธ NAME ๐ธ
mkstemp, mkostemp, mkstemps, mkstemps... no wait โ mkstemp, mkostemp, mkstemps, mkostemps โ they create a unique temporary file. that's the whole job. iconic behavior honestly.
๐ LIBRARY ๐
Standard C library (libc, link with -lc). yes the basic one. no extra packages required, we love a low-maintenance queen.
๐ SYNOPSIS ๐
#include <stdlib.h>
int mkstemp(char *template);
int mkostemp(char *template, int flags);
int mkstemps(char *template, int suffixlen);
int mkostemps(char *template, int suffixlen, int flags);
๐ Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
okay so before glibc will even look at you, you need these defined. yes it's giving bureaucratic but it's also giving "the compiler is protecting you."
mkstemp():
_XOPEN_SOURCE >= 500
|| /* glibc >= 2.12: */ _POSIX_C_SOURCE >= 200809L
|| /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
mkostemp():
_GNU_SOURCE
mkstemps():
/* glibc >= 2.19: */ _DEFAULT_SOURCE
|| /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
mkostemps():
_GNU_SOURCE
๐ DESCRIPTION ๐
okay so the main character here: mkstemp() generates a unique temporary filename from template, creates and opens the file, and hands you back an open file descriptor for it. that's it. that's the whole vibe. no cap.
The last six characters of template must be "XXXXXX" and these get replaced with a string that makes the filename unique. and listen โ since template will get modified, it must not be a string constant. declare it as a character array, bestie. a string literal is a one-way ticket to segfault city ๐ฉธ
The file is created with permissions 0600 โ that's read plus write for owner only, so nobody else can peek. the returned file descriptor gives you both read AND write access to the file. and it's opened with the open(2) O_EXCL flag, which guarantees that the caller is the process that creates the file. no race conditions, no drama, we love a secure queen ๐
mkostemp() is like mkstemp() but it lets you pass flags, and the following bits may be specified โ with the same meaning as for open(2):
O_APPENDO_CLOEXECO_SYNC
โ ๏ธ heads up though: when creating the file,mkostemp()includes the valuesO_RDWR,O_CREAT, andO_EXCLin theflagsargument it gives toopen(2). so including these values yourself in theflagsyou pass tomkostemp()is unnecessary, and it produces errors on some systems. don't do it. let her cook ๐
mkstemps() is like mkstemp() except the string in template contains a suffix of suffixlen characters. so template is of the form prefixXXXXXXsuffix, and the XXXXXX string is modified exactly like in mkstemp(). perfect for when you need myfileXXXXXX.txt energy.
mkostemps() is to mkstemps() as mkostemp() is to mkstemp(). yes. that sentence is real. read it twice, it'll click ๐
๐ฆ RETURN VALUE ๐ฆ
On success, these functions return the file descriptor of the temporary file. slay.
On error, -1 is returned, and errno is set to indicate the error. ๐ญ check your errno, bestie, she's always trying to tell you something.
๐ ERRORS ๐
EEXISTโ Could not create a unique temporary filename. Now the contents oftemplateare undefined. yeah that's the scary one. it tried, it failed, andtemplateis now just vibes and mystery.EINVALโ Formkstemp()andmkostemp(): The last six characters oftemplatewere notXXXXXX; nowtemplateis unchanged (at least she didn't mess with your buffer). Formkstemps()andmkostemps():templateis less than (6+suffixlen) characters long, or the last 6 characters before the suffix intemplatewere notXXXXXX.
These functions may also fail with any of the errors described for open(2). so go read that page too, iykyk.
๐ ATTRIBUTES ๐
For an explanation of the terms used in this section, see attributes(7).
- Interface:
mkstemp(),mkostemp(),mkstemps(),mkostemps() - Attribute: Thread safety
- Value: MT-Safe โจ
so yes you can call these from multiple threads and they won't betray you. group chat approved ๐ซถ
โญ STANDARDS โญ
mkstemp()โ POSIX.1-2001.mkstemps()โ BSD.mkostemp()/mkostemps()โ GNU.
๐ HISTORY ๐
mkstemp()โ4.3BSD, POSIX.1-2001.mkstemps()โ glibc 2.11. BSD, Mac OS X, Solaris, Tru64.mkostemp()โ glibc 2.7.mkostemps()โ glibc 2.11.
๐จ the ancient lore, and it's a cautionary tale: in glibc versions 2.06 and earlier, the file is created with permissions0666โ that's read and write for all users. This old behavior may be a security risk, especially since other UNIX flavors use0600, and somebody might overlook this detail when porting programs. POSIX.1-2008 adds a requirement that the file be created with mode0600. so like. if you're on something crusty, beware ๐
More generally, the POSIX specification of mkstemp() does not say anything about file modes, so the application should make sure its file mode creation mask (see umask(2)) is set appropriately before calling mkstemp() (and mkostemp()). set your umask first, bestie. it's giving "get ready before you go out" ๐
๐ฅ SEE ALSO ๐ฅ
mkdtemp(3), mktemp(3), tempnam(3), tmpfile(3), tmpnam(3)
okay that's the whole page, you're officially educated โจ this was Linux man-pages 6.20, dated 2026-02-08, brought to you by the mkstemp(3) page itself โ go forth and create temp files responsibly, no race conditions, no permission disasters, just clean secure file descriptors ๐๐๐ฆ๐ธ
๐ฆ 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>
- Copyright 2008, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms