tempnam(3)
โจ tempnam(3) โจ โ Library Functions Manual
okay bestie, pull up a chair, we're doing tempnam(3) today ๐ซถ she lives in the Standard C library (libc, -lc) and honestly? her whole vibe is "please don't actually use me." But you asked, so we're gonna learn her thoroughly, no cap. ๐
๐ NAME ๐
tempnam โ create a name for a temporary file
๐ LIBRARY ๐
Standard C library (libc, -lc) โ yes that's the one you already linked, she's right there ๐
๐ง SYNOPSIS ๐ง
#include <stdio.h>
char *tempnam(const char *dir, const char *pfx);
๐ Feature Test Macro Requirements for glibc (see feature_test_macros(7)) ๐
Bestie, you gotta manifest these macros before glibc will even let you peek at her:
tempnam():
Since glibc 2.19:
_DEFAULT_SOURCE
glibc 2.19 and earlier:
_BSD_SOURCE || _SVID_SOURCE
๐ DESCRIPTION ๐
Never use this function. Use mkstemp(3) or tmpfile(3) instead. THAT is the entire headline. I need you to tattoo it somewhere. ๐จ
Okay but technically, here's what she does. The tempnam() function returns a pointer to a string that is a valid filename, and such that a file with this name did not exist when tempnam() checked. Checked. Past tense. Remember that word, it's gonna matter later. ๐
The filename suffix of the pathname generated will start with pfx in case pfx is a non-NULL string of at most five bytes. The directory prefix part of the pathname generated is required to be "appropriate" (often that at least implies writable). Quotes around appropriate are doing SO much heavy lifting there, we'll get to that in BUGS. ๐ซ
Attempts to find an appropriate directory go through the following steps:
- In case the environment variable
TMPDIRexists and contains the name of an appropriate directory, that is used. - Otherwise, if the
dirargument is non-NULLand appropriate, it is used. - Otherwise,
P_tmpdir(as defined in<stdio.h>) is used when appropriate. - Finally an implementation-defined directory may be used.
So she's out here checking four different spots in order like a girl checking four group chats for the aux cord. ๐ฑโจ
The string returned by tempnam() is allocated using malloc(3) and hence should be freed by free(3). Do not forget the free. That's your homework and I will be checking. ๐
๐ RETURN VALUE ๐
On success, the tempnam() function returns a pointer to a unique temporary filename. She returns NULL if a unique name cannot be generated, with errno set to indicate the error. ๐ญ
๐จ ERRORS ๐จ
ENOMEMโ Allocation of storage failed. She tried tomallocand it said no. ๐
๐ฆ ATTRIBUTES ๐ฆ
For an explanation of the terms used in this section, see attributes(7).
tempnam()โ Thread safety โMT-Safe env
๐ STANDARDS ๐
POSIX.1-2008.
๐ HISTORY ๐
SVr4, 4.3BSD, POSIX.1-2001. Obsoleted in POSIX.1-2008. She's an icon, but she's a retired icon, yk? ๐
๐ง NOTES ๐ง
Okay this is the part where we get real, bestie. ๐ธ
Although tempnam() generates names that are difficult to guess, it is nevertheless possible that between the time that tempnam() returns a pathname, 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). THE GAP BETWEEN CHECKING AND OPENING IS THE WHOLE PROBLEM. That's the plot twist of this entire manpage. ๐ซถ
SUSv2 does not mention the use of TMPDIR; glibc will use it only when the program is not set-user-ID. On SVr4, the directory used under (4) is /tmp (and this is what glibc does). Copy that.
Because it dynamically allocates memory used to return the pathname, tempnam() is reentrant, and thus thread safe, unlike tmpnam(3). slay, honestly. โจ
The tempnam() function generates a different string each time it is called, up to TMP_MAX (defined in <stdio.h>) times. If it is called more than TMP_MAX times, the behavior is implementation defined. Implementation defined = we simply do not know, and that's the tea. ๐
tempnam() uses at most the first five bytes from pfx. Only five. If you gave her more, she's ignoring them. Not sorry. ๐
The glibc implementation of tempnam() fails with the error EEXIST upon failure to find a unique name.
๐ BUGS ๐
The precise meaning of "appropriate" is undefined; it is unspecified how accessibility of a directory is determined. Yeah. The word the whole function hinges on? Nobody defined it. Iconic behavior, frankly. ๐ญ
๐ซถ SEE ALSO ๐ซถ
mkstemp(3), mktemp(3), tmpfile(3), tmpnam(3) โ go read mkstemp(3) first, I'm begging you ๐
k bye bestie, that's the whole page! ๐ This one came from Linux man-pages 6.20, dated 2026-02-08, and honestly the fact that she's still out here un-obsoleted in the docs while POSIX dropped her in 2008 is so real of her. Go use mkstemp(3). 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 1999, Andries E. Brouwer <aeb@cwi.nl>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms