MKTEMP(3)

NAME
mktemp, mkstemp, mkostemp, mkstemps, mkostemps, mkdtemp, _mktemp_r, _mkdtemp_r, _mkstemp_r, _mkstemps_r, _mkostemp_r, _mkostemps_r - generate unused file name, generate unused directory

SYNOPSIS

#include <stdlib.h>

char *mktemp(char *path);

char *mkdtemp(char *path);

int mkstemp(char *path);

int mkstemps(char *path, int suffixlen);

int mkostemp(char *path, int flags);

int mkostemps(char *path, int suffixlen, int flags);

char *_mktemp_r(struct _reent *reent, char *path);

char *_mkdtemp_r(struct _reent *reent, char *path);

int *_mkstemp_r(struct _reent *reent, char *path);

int *_mkstemps_r(struct _reent *reent, char *path, int len);

int *_mkostemp_r(struct _reent *reent, char *path, int flags);

int *_mkostemps_r(struct _reent *reent, char *path, int len, int flags);

DESCRIPTION

mktemp, mkstemp, and mkstemps attempt to generate a file name that is not yet in use for any existing file. mkstemp and mkstemps create the file and open it for reading and writing; mktemp simply generates the file name (making mktemp a security risk). mkostemp and mkostemps allow the addition of other open flags, such as O_CLOEXEC, O_APPEND, or O_SYNC. On platforms with a separate text mode, mkstemp forces O_BINARY, while mkostemp allows the choice between O_BINARY, O_TEXT, or 0 for default. mkdtemp attempts to create a directory instead of a file, with a permissions mask of 0700.

You supply a simple pattern for the generated file name, as the string at path. The pattern should be a valid filename (including path information if you wish) ending with at least six `X' characters. The generated filename will match the leading part of the name you supply, with the trailing `X' characters replaced by some combination of digits and letters. With mkstemps, the `X' characters end suffixlen bytes before the end of the string.

The alternate functions _mktemp_r, _mkdtemp_r, _mkstemp_r, _mkostemp_r, _mkostemps_r, and _mkstemps_r are reentrant versions. The extra argument reent is a pointer to a reentrancy structure.

RETURNS

mktemp returns the pointer path to the modified string representing an unused filename, unless it could not generate one, or the pattern you provided is not suitable for a filename; in that case, it returns NULL. Be aware that there is an inherent race between generating the name and attempting to create a file by that name; you are advised to use O_EXCL|O_CREAT.

mkdtemp returns the pointer path to the modified string if the directory was created, otherwise it returns NULL.

mkstemp, mkstemps, mkostemp, and mkostemps return a file descriptor to the newly created file, unless it could not generate an unused filename, or the pattern you provided is not suitable for a filename; in that case, it returns -1.

NOTES

Never use mktemp. The generated filenames are easy to guess and there's a race between the test if the file exists and the creation of the file. In combination this makes mktemp prone to attacks and using it is a security risk. Whenever possible use mkstemp instead. It doesn't suffer the race condition.

PORTABILITY

ANSI C does not require either mktemp or mkstemp; the System V Interface Definition requires mktemp as of Issue 2. POSIX 2001 requires mkstemp, and POSIX 2008 requires mkdtemp while deprecating mktemp. mkstemps, mkostemp, and mkostemps are not standardized.

Supporting OS subroutines required: getpid, mkdir, open, stat.

AUTHORS

Steve Chamberlain
    Author.

Roland Pesch
    Author.

Red Hat Support

Jeff Johnston
    Author.

COPYRIGHT

This file documents the ANSI C library.

Copyright (C) 1992, 1993, 1994-2014 Red Hat, Inc.

libc includes software developed by the University of California, Berkeley and its contributors.

libc includes software developed by Martin Jackson, Graham Haley and Steve Chamberlain of Tadpole Technology and released to Cygnus.

libc uses floating-point conversion software developed at AT&T, which includes this copyright information:

The author of this software is David M. Gay.

Copyright (c) 1991 by AT&T.

Permission to use, copy, modify, and distribute this software for any purpose without fee is hereby granted, provided that this entire notice is included in all copies of any software which is or includes a copy or modification of this software and in all copies of the supporting documentation for such software.

THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.

Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.

Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, subject to the terms of the GNU General Public License, which includes the provision that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.