'\" te .\" Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved. .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License. .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner] .TH MKTEMP 1 "Jan 10, 2008" .SH NAME mktemp \- make temporary filename .SH SYNOPSIS .LP .nf \fBmktemp\fR [\fB-dtqu\fR] [\fB-p\fR \fIdirectory\fR] [\fItemplate\fR] .fi .SH DESCRIPTION .sp .LP The \fBmktemp\fR utility makes a temporary filename. To do this, \fBmktemp\fR takes the specified filename template and overwrites a portion of it to create a unique filename. See \fBOPERANDS\fR. .sp .LP The template is passed to \fBmkdtemp\fR(3C) for directories or \fBmkstemp\fR(3C) for ordinary files. .sp .LP If \fBmktemp\fR can successfully generate a unique filename, the file (or directory) is created with file permissions such that it is only readable and writable by its owner (unless the \fB-u\fR flag is given) and the filename is printed to standard output. .sp .LP \fBmktemp\fR allows shell scripts to safely use temporary files. Traditionally, many shell scripts take the name of the program with the \fBPID\fR as a suffix and used that as a temporary filename. This kind of naming scheme is predictable and the race condition it creates is easy for an attacker to win. A safer, though still inferior approach is to make a temporary directory using the same naming scheme. While this guarantees that a temporary file is not subverted, it still allows a simple denial of service attack. Use \fBmktemp\fR instead. .SH OPTIONS .sp .LP The following options are supported: .sp .ne 2 .na \fB\fB-d\fR\fR .ad .RS 16n Make a directory instead of a file. .RE .sp .ne 2 .na \fB\fB-p\fR \fIdirectory\fR\fR .ad .RS 16n Use the specified directory as a prefix when generating the temporary filename. The directory is overridden by the user's TMPDIR environment variable if it is set. This option implies the \fB-t\fR flag. .RE .sp .ne 2 .na \fB\fB-q\fR\fR .ad .RS 16n Fail silently if an error occurs. This is useful if a script does not want error output to go to standard error. .RE .sp .ne 2 .na \fB\fB-t\fR\fR .ad .RS 16n Generate a path rooted in a temporary directory. This directory is chosen as follows: If the user's TMPDIR environment variable is set, the directory contained therein is used. Otherwise, if the \fB-p\fR flag was given the specified directory is used. If none of the above apply, \fB/tmp\fR is used. In this mode, the template (if specified) should be a directory component (as opposed to a full path) and thus should not contain any forward slashes. .RE .sp .ne 2 .na \fB\fB-u\fR\fR .ad .RS 16n Operate in unsafe mode. The temp file is unlinked before \fBmktemp\fR exits. This is slightly better than \fBmktemp\fR(3C), but still introduces a race condition. Use of this option is discouraged. .RE .SH OPERANDS .sp .LP The following operands are supported: .sp .ne 2 .na \fB\fItemplate\fR\fR .ad .RS 12n \fItemplate\fR can be any filename with one or more \fBX\fRs appended to it, for example \fB/tmp/tfile.XXXXXX\fR. .sp If \fItemplate\fR is not specified, a default of \fBtmp.XXXXXX\fR is used and the \fB-t\fR flag is implied. .RE .SH EXAMPLES .LP \fBExample 1 \fRUsing \fBmktemp\fR .sp .LP The following example illustrates a simple use of \fBmktemp\fR in a \fBsh\fR(1) script. In this example, the script quits if it cannot get a safe temporary file. .sp .in +2 .nf TMPFILE=`mktemp /tmp/example.XXXXXX` if [ -z "$TMPFILE" ]; then exit 1; fi echo "program output" >> $TMPFILE .fi .in -2 .sp .LP \fBExample 2 \fRUsing \fBmktemp\fR to Support \fBTMPDIR\fR .sp .LP The following example uses \fBmktemp\fR to support for a user's \fBTMPDIR\fR environment variable: .sp .in +2 .nf TMPFILE=`mktemp -t example.XXXXXX` if [ -z "$TMPFILE" ]; then exit 1; fi echo "program output" >> $TMPFILE .fi .in -2 .sp .LP \fBExample 3 \fRUsing \fBmktemp\fR Without Specifying the Name of the Temporary File .sp .LP The following example uses \fBmktemp\fR without specifying the name of the temporary file. In this case the \fB-t\fR flag is implied. .sp .in +2 .nf TMPFILE=`mktemp` if [ -z "$TMPFILE" ]; then exit 1; fi echo "program output" >> $TMPFILE .fi .in -2 .sp .LP \fBExample 4 \fRUsing \fBmktemp\fR with a Default Temporary Directory Other than \fB/tmp\fR .sp .LP The following example creates the temporary file in \fB/extra/tmp\fR unless the user's \fBTMPDIR\fR environment variable specifies otherwise: .sp .in +2 .nf TMPFILE=`mktemp -p /extra/tmp example.XXXXX` if [ -z "$TMPFILE" ]; then exit 1; fi echo "program output" >> $TMPFILE .fi .in -2 .sp .LP \fBExample 5 \fRUsing \fBmktemp\fR to Remove a File .sp .LP The following example attempts to create two temporary files. If creation of the second temporary file fails, \fBmktemp\fR removes the first file before exiting: .sp .in +2 .nf TMP1=`mktemp -t example.1.XXXXXX` if [ -z "$TMP1" ]; then exit 1; fi TMP2=`mktemp -t example.2.XXXXXX` if [ -z "$TMP2" ]; then rm -f $TMP1 exit 1 fi .fi .in -2 .sp .LP \fBExample 6 \fRUsing \fBmktemp\fR .sp .LP The following example does not exit if \fBmktemp\fR is unable to create the file. That part of the script has been protected. .sp .in +2 .nf TMPFILE=`mktemp -q -t example.XXXXXX` if [ ! -z "$TMPFILE" ] then # Safe to use $TMPFILE in this block echo data > $TMPFILE ... rm -f $TMPFILE fi .fi .in -2 .sp .SH ENVIRONMENT VARIABLES .sp .LP See \fBenviron\fR(5) for descriptions of the following environment variables that affect the execution of \fBmktemp\fR with the \fB-t\fR option: \fBTMPDIR.\fR .SH EXIT STATUS .sp .LP The following exit values are returned: .sp .ne 2 .na \fB\fB0\fR\fR .ad .RS 5n Successful completion. .RE .sp .ne 2 .na \fB\fB1\fR\fR .ad .RS 5n An error occurred. .RE .SH ATTRIBUTES .sp .LP See \fBattributes\fR(5) for descriptions of the following attributes: .sp .sp .TS box; c | c l | l . ATTRIBUTE TYPE ATTRIBUTE VALUE _ Interface Stability Committed .TE .SH SEE ALSO .sp .LP \fBsh\fR(1), \fBmkdtemp\fR(3C), \fBmkstemp\fR(3C), \fBattributes\fR(5), \fBenviron\fR(5) .SH NOTES .sp .LP The \fBmktemp\fR utility appeared in OpenBSD 2.1. The Solaris implementation uses only as many `Xs' as are significant for \fBmktemp\fR(3C) and \fBmkstemp\fR(3C).