NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |STANDARDS |HISTORY |NOTES |BUGS |SEE ALSO |COLOPHON | |
tempnam(3) Library Functions Manualtempnam(3)tempnam - create a name for a temporary file
Standard C library (libc,-lc)
#include <stdio.h>char *tempnam(const char *dir, const char *pfx); Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):tempnam(): Since glibc 2.19: _DEFAULT_SOURCE glibc 2.19 and earlier: _BSD_SOURCE || _SVID_SOURCE
Never use this function. Usemkstemp(3) ortmpfile(3) instead. Thetempnam() function returns a pointer to a string that is a valid filename, and such that a file with this name did not exist whentempnam() checked. The filename suffix of the pathname generated will start withpfx in casepfx 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). Attempts to find an appropriate directory go through the following steps: (1) In case the environment variableTMPDIRexists and contains the name of an appropriate directory, that is used. (2) Otherwise, if thedir 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. The string returned bytempnam() is allocated usingmalloc(3) and hence should be freed byfree(3).
On success, thetempnam() function returns a pointer to a unique temporary filename. It returns NULL if a unique name cannot be generated, witherrno set to indicate the error.
ENOMEMAllocation of storage failed.
For an explanation of the terms used in this section, seeattributes(7). ┌──────────────────────────────────┬───────────────┬─────────────┐ │Interface│Attribute│Value│ ├──────────────────────────────────┼───────────────┼─────────────┤ │tempnam() │ Thread safety │ MT-Safe env │ └──────────────────────────────────┴───────────────┴─────────────┘
POSIX.1-2008.
SVr4, 4.3BSD, POSIX.1-2001. Obsoleted in POSIX.1-2008.
Althoughtempnam() generates names that are difficult to guess, it is nevertheless possible that between the time thattempnam() returns a pathname, and the time that the program opens it, another program might create that pathname usingopen(2), or create it as a symbolic link. This can lead to security holes. To avoid such possibilities, use theopen(2)O_EXCLflag to open the pathname. Or better yet, usemkstemp(3) ortmpfile(3). SUSv2 does not mention the use ofTMPDIR; 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). Because it dynamically allocates memory used to return the pathname,tempnam() is reentrant, and thus thread safe, unliketmpnam(3). Thetempnam() function generates a different string each time it is called, up toTMP_MAX(defined in<stdio.h>) times. If it is called more thanTMP_MAXtimes, the behavior is implementation defined.tempnam() uses at most the first five bytes frompfx. The glibc implementation oftempnam() fails with the errorEEXIST upon failure to find a unique name.
The precise meaning of "appropriate" is undefined; it is unspecified how accessibility of a directory is determined.
mkstemp(3),mktemp(3),tmpfile(3),tmpnam(3)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17tempnam(3)Pages that refer to this page:getpid(2), mkdtemp(3), mkstemp(3), mktemp(3), stdio(3), tmpfile(3), tmpnam(3), environ(7)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |