tmpnam(3)

πŸ’– tmpnam(3) β€” the "make me a temp filename" page πŸ’–

okay bestie, gather round, because this one is a Library Functions Manual classic and also, like, a cautionary tale 🫢. We're in tmpnam(3) territory: tmpnam, tmpnam_r β€” the functions that make names for temporary files but not, like, actual temporary files. It's giving "I made you a reservation but didn't book the table." πŸ’€


✨ NAME ✨

tmpnam, tmpnam_r β€” create a name for a temporary file

πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc)

🌸 SYNOPSIS 🌸

#include <stdio.h>

[[deprecated]] char *tmpnam(char *s);
[[deprecated]] char *tmpnam_r(char *s);

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

tmpnam_r()
    Since glibc 2.19:
        _DEFAULT_SOURCE
    Up to and including glibc 2.19:
        _BSD_SOURCE || _SVID_SOURCE

πŸ¦‹ DESCRIPTION πŸ¦‹

🚨 Note: avoid using these functions; use mkstemp(3) or tmpfile(3) instead. 🚨

The tmpnam() function returns a pointer to a string that is a valid filename, and such that a file with this name did not exist at some point in time β€” which is literally the loophole of the century, because it means naive programmers may think it a suitable name for a temporary file. πŸ™ˆ "It didn't exist a second ago, so surely it's mine now!" bestie. No. That's not how the filesystem works.

If the argument s is NULL, this name is generated in an internal static buffer and may be overwritten by the next call to tmpnam(). (Yes, it's the shared-group-chat-profile-picture situation: everyone writing to the same slot, chaos ensues. 😭)

If s is not NULL, the name is copied to the character array (of length at least L_tmpnam) pointed to by s and the value s is returned in case of success.

The created pathname has a directory prefix P_tmpdir. (Both L_tmpnam and P_tmpdir are defined in <stdio.h>, just like the TMP_MAX mentioned below β€” they're all hanging out in the same header, iconic.)

The tmpnam_r() function performs the same task as tmpnam(), but returns NULL (to indicate an error) if s is NULL. She simply refuses. Boundaries! πŸ’…

πŸŽ€ RETURN VALUE πŸŽ€

These functions return a pointer to a unique temporary filename, or NULL if a unique name cannot be generated.

πŸ’œ ERRORS πŸ’œ

No errors are defined. (Not a single one. Nothing to report. The error handling said "not my problem." 😌)

πŸ‘‘ ATTRIBUTES πŸ‘‘

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

  • Interface: tmpnam() β€” Attribute: Thread safety β€” Value: MT-Unsafe race:tmpnam/!s
  • Interface: tmpnam_r() β€” Attribute: Thread safety β€” Value: MT-Safe

(Translation for the girlies: tmpnam() is out here racing when s is NULL, because everyone's fighting over that one static buffer. tmpnam_r() said "I don't do shared buffers," and is therefore MT-Safe. Slay.)

🌈 STANDARDS 🌈

tmpnam()

  • C11, POSIX.1-2008.

tmpnam_r()

  • None. She's unstandardized, she's mysterious, she's ✨self-taught✨.

πŸ“œ HISTORY πŸ“œ

tmpnam()

  • SVr4, 4.3BSD, C89, POSIX.1-2001. Obsolete in POSIX.1-2008.

tmpnam_r()

  • Solaris.

πŸ’… NOTES πŸ’…

The tmpnam() function generates a different string each time it is called, up to TMP_MAX times. If it is called more than TMP_MAX times, the behavior is implementation defined. (So after that many calls, all bets are off, and honestly? Fair.)

Although these functions generate names that are difficult to guess, it is nevertheless possible that between the time that the pathname is returned and the time that the program opens it, another program might create that pathname using open(2), or create it as a symbolic link. This can lead to security holes. 😭

To avoid such possibilities, use the open(2) O_EXCL flag to open the pathname. Or better yet, use mkstemp(3) or tmpfile(3).

Portable applications that use threads cannot call tmpnam() with a NULL argument if either _POSIX_THREADS or _POSIX_THREAD_SAFE_FUNCTIONS is defined. (Threads said "we do not consent to that static buffer." And they're right!)

πŸ’€ BUGS πŸ’€

Never use these functions. Use mkstemp(3) or tmpfile(3) instead.

Like. Periodt. That's the whole bug report. The bug is the function. 🫠


πŸ”₯ SEE ALSO πŸ”₯

mkstemp(3), mktemp(3), tempnam(3), tmpfile(3)

(tl;dr bestie: go read mkstemp(3). She's the main character. tmpnam is the ex who keeps texting.)

πŸ’– stay safe out there and never trust a filename that "definitely wasn't there a second ago" πŸ’–


this page came to you from Linux man-pages 6.20, dated 2026-02-08, and yes it's literally called tmpnam(3) β€” go forth and use mkstemp instead, 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