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:

  1. In case the environment variable TMPDIR exists and contains the name of an appropriate directory, that is used.
  2. Otherwise, if the dir argument is non-NULL and appropriate, it is used.
  3. Otherwise, P_tmpdir (as defined in <stdio.h>) is used when appropriate.
  4. 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 to malloc and 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms