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_APPEND
  • O_CLOEXEC
  • O_SYNC
โš ๏ธ heads up though: when creating the file, mkostemp() includes the values O_RDWR, O_CREAT, and O_EXCL in the flags argument it gives to open(2). so including these values yourself in the flags you pass to mkostemp() 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 of template are undefined. yeah that's the scary one. it tried, it failed, and template is now just vibes and mystery.
  • EINVAL โ€” For mkstemp() and mkostemp(): The last six characters of template were not XXXXXX; now template is unchanged (at least she didn't mess with your buffer). For mkstemps() and mkostemps(): template is less than (6 + suffixlen) characters long, or the last 6 characters before the suffix in template were not XXXXXX.

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 permissions 0666 โ€” that's read and write for all users. This old behavior may be a security risk, especially since other UNIX flavors use 0600, and somebody might overlook this detail when porting programs. POSIX.1-2008 adds a requirement that the file be created with mode 0600. 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms