Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

mkstemp64() is the Linux Standard Base (LSB) large-file variant of mkstemp(): it takes a writable filename template ending in XXXXXX, creates and opens a unique temporary file, and returns a file descriptor. It is chiefly a legacy or compatibility interface—not a function you can assume every modern Unix-like system provides. For new code, use the supported mkstemp() interface unless a target ABI specifically requires mkstemp64().

What “BaseLib” means here

In this context, BaseLib is the Linux Standard Base’s classification for a base-library interface. It does not mean that mkstemp64() belongs to a separate, universally installable “BaseLib” package. The implementation comes from the target system’s C library or compatibility environment.

The LSB Core Specification 4.1 entry documents the function as a large-file version of mkstemp().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Signature and basic behavior

#include <stdio.h>
#include <stdlib.h>

int mkstemp64(char *template);

The function expects a writable character array whose final six characters are exactly XXXXXX. It replaces those characters with a unique suffix, creates and opens the resulting file, and returns the open file descriptor. It changes the template in place, so after success the array contains the generated pathname.

char template[] = "/tmp/demo-XXXXXX";
int fd = mkstemp64(template);

Do not pass a string literal: the function modifies its argument, and string literals are not writable.

int fd = mkstemp64("/tmp/demo-XXXXXX");  /* Do not do this */

The template determines the directory; the function does not select one for you. Choose a directory appropriate for the application and deployment environment rather than assuming /tmp is always the right location.

Example with error handling and cleanup

This example is for a target whose headers declare mkstemp64() and whose C library exports it. That availability is not universal.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    char template[] = "/tmp/demo-XXXXXX";
    int fd = mkstemp64(template);

    if (fd == -1) {
        perror("mkstemp64");
        return EXIT_FAILURE;
    }

    printf("Created: %sn", template);

    /* Read from or write to fd here. */

    if (close(fd) == -1) {
        perror("close");
        return EXIT_FAILURE;
    }
    if (unlink(template) == -1) {
        perror("unlink");
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

The caller is responsible for closing the descriptor and, if the file should not remain in the directory, removing its pathname with unlink(). A process crash or early return can leave a named temporary file behind if cleanup is not arranged for every exit path.

If the program needs the file only through its open descriptor, it can unlink the pathname immediately after creation. The file remains accessible through the descriptor and is ordinarily reclaimed when the last descriptor closes. Check the result of unlink(); if it fails, handle the remaining named file deliberately.

Return value and errors

  • Success: a nonnegative file descriptor; the template array has been changed to the generated pathname.
  • Failure: -1, with the reason reported through errno.

Possible failures include an invalid template (the final six characters are not XXXXXX, commonly reported as EINVAL), inability to create a unique file (which can result in EEXIST), an absent or unwritable directory, and exhaustion of available file descriptors. On Linux, the current mkstemp(3) documentation notes that template contents may be undefined after an EEXIST failure; do not rely on the original pathname remaining intact.

If you need to perform logging or cleanup before reporting the error, save errno first: intervening calls may change it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why the name ends in “64”

In the LSB entry, the “64” marks the large-file variant: it uses open64() rather than open(). It does not mean the descriptor is 64-bit, that the filename has 64 characters, or that the function guarantees a particular maximum file size. Large-file behavior depends on the platform’s ABI, file-offset type, C library, build configuration, and filesystem.

Security and file lifecycle

The security advantage of a mkstemp-style API is that it creates and opens the file as part of the operation, rather than asking the caller to guess a name and then open it. Current Linux documentation describes creation with exclusive semantics (O_EXCL), which prevents another process from winning a race between name selection and file creation. It documents mode 0600 for current Linux behavior. Do not assume every historical or non-Linux implementation used identical permissions: the man page records that glibc 2.06 and earlier used mode 0666 subject to umask.

Keep using the returned descriptor instead of reopening the generated path. Avoid exposing the pathname unnecessarily, and account for the directory’s permissions and mount behavior in the security model. Atomic creation does not make every later pathname operation safe.

mkstemp64() has no flags parameter. If a descriptor must not survive an exec(), use an implementation-supported close-on-exec option or set FD_CLOEXEC promptly with fcntl(). On systems providing the GNU extension, mkostemp() accepts selected flags such as O_CLOEXEC:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#include <fcntl.h>
#include <stdlib.h>

char template[] = "/tmp/demo-XXXXXX";
int fd = mkostemp(template, O_CLOEXEC);

mkostemp() is not a universally portable drop-in replacement; check the target system’s documentation.

Best Value
Sale
C Pocket Reference
  • Used Book in Good Condition
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choosing among related interfaces

Interface Use Portability note
mkstemp() Create and open a uniquely named temporary file. The usual portable baseline; documented as POSIX.1-2001 by current Linux man-pages.
mkstemp64() LSB large-file variant, documented to use open64(). Legacy or implementation-dependent; verify the target ABI, headers, and library.
mkostemp() Create a temporary file while supplying selected open flags. GNU extension on Linux; not universal.
mkstemps() Create a temporary file with a suffix after the six placeholders. Extension available on some systems, including glibc; verify locally.
mkdtemp() Create a uniquely named temporary directory. A separate API for directories, not files.
tmpfile() Obtain a temporary stream where FILE * I/O is appropriate. A different abstraction; confirm its lifetime and cleanup behavior for the target.

Do not substitute insecure name-generation patterns such as sprintf(path, "/tmp/file-%d", getpid()) followed by open(), or mktemp() followed by a separate open. Separating the choice of a name from its creation leaves a race window. Use an API that creates the file atomically.

Portability: what to check before using it

The LSB reference is versioned documentation for LSB Core 4.1, not a statement that every current POSIX system implements mkstemp64(). Current Linux man-pages document mkstemp() and related interfaces but do not list mkstemp64() among the current documented interfaces. Availability can differ among operating systems, C libraries, ABIs, headers, and compatibility layers.

  • Check whether the target headers declare mkstemp64() and whether the linker provides the symbol.
  • Consult that implementation’s documentation for any feature-test macro requirements; there is no universal macro requirement to assume.
  • Confirm whether the target’s ordinary mkstemp() already uses the required large-file behavior.
  • Check the target ABI’s file-offset model and any compatibility requirement for the specific mkstemp64 symbol.
  • Decide how to handle close-on-exec, temporary-directory selection, and cleanup.

For new portable code, prefer mkstemp() when it meets the target’s requirements. Use mkstemp64() when an explicit legacy LSB or platform ABI requirement calls for it, and build against that platform’s own headers and libraries.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.