MKTEMP(1) MKTEMP(1) NNAAMMEE mmkktteemmpp - make temporary filename (unique) SSYYNNOOPPSSIISS mmkktteemmpp [--VV] | [--ddqqTTttuu] [--pp _d_i_r_e_c_t_o_r_y] [_t_e_m_p_l_a_t_e] DDEESSCCRRIIPPTTIIOONN The mmkktteemmpp utility takes the given filename _t_e_m_p_l_a_t_e and overwrites a portion of it to create a unique filename. The _t_e_m_p_l_a_t_e may be any filename with some number of `Xs' appended to it, for example _/_t_m_p_/_t_f_i_l_e_._X_X_X_X_X_X_X_X_X_X_. If no _t_e_m_p_l_a_t_e is specified a default of _t_m_p_._X_X_X_X_X_X_X_X_X_X if LFN support is available, and _t_p_X_X_X_X_X_X_._X_X_X if LFN support is not available is used and the --tt flag is implied (see below). The trailing `Xs' are replaced with a combination of the current process number and random letters. The name cho- sen depends both on the number of `Xs' in the _t_e_m_p_l_a_t_e and the number of collisions with pre-existing files. If LFN support is not available, like on plain DOS, the number of `Xs' in the _t_e_m_p_l_a_t_e is restricted to 6 in the filename and 3 more in the extension. The number of unique file- names mmkktteemmpp can return depends on the number of `Xs' pro- vided; ten `Xs' will result in mmkktteemmpp testing roughly 26 ** 10 combinations. If mmkktteemmpp 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 --uu flag is given) and the filename is printed to stan- dard output. mmkktteemmpp is provided to allow shell scripts to safely use temporary files. Traditionally, many shell scripts take the name of the program with the PID as a suffix and use 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 does allow one to guarantee that a temporary file will not be subverted, it still allows a simple denial of service attack. For these rea- sons it is suggested that mmkktteemmpp be used instead. The options are as follows: --VV Print the version and exit. --dd Make a directory instead of a file. --pp _d_i_r_e_c_t_o_r_y Use the specified _d_i_r_e_c_t_o_r_y as a prefix when gener- ating the temporary filename. The _d_i_r_e_c_t_o_r_y will 30 September 2001 1 MKTEMP(1) MKTEMP(1) be overridden by the user's TMPDIR environment variable if it is set. If the environment variable has been set, it will be checked if the directory contained therein can be accessed. If this is not possible then the current working directory will be used as prefix when generating the temporary file- name. This option implies the --tt flag (see below). --qq Fail silently if an error occurs. This is useful if a script does not want error output to go to standard error. --tt Generate a path rooted in a temporary directory. This directory is chosen as follows: +o If the user's TMPDIR environment variable is set, the directory contained therein is used. +o Otherwise, if the --pp flag was given the specified directory is used. +o If none of the above apply, _/_t_m_p (on MSDOS and clones _._/ ) is used. --TT Generate a path rooted in a temporary directory on MSDOS and clones. This directory is chosen as fol- lows: +o If the user's TMPDIR environment variable is set, the directory contained therein is used. On MSDOS and clones also the environ- ment variables TMP and TEMP will be checked in that order. +o Otherwise, if the --pp flag was given the specified directory is used. +o If none of the above apply, _/_t_m_p (on MSDOS and clones _._/ ) is used. In these modes, the _t_e_m_p_l_a_t_e (if specified) should be a directory component (as opposed to a full path) and thus should not contain any forward slashes. On MSDOS and clones also neither backslashes nor drive letter prefix are allowed. --uu Operate in "unsafe" mode. The temp file will be unlinked before mmkktteemmpp exits. This is slightly better than mktemp(3) but still introduces a race condition. Use of this option is not encouraged. The mmkktteemmpp utility exits with a value of 0 on success or 1 on failure. 30 September 2001 2 MKTEMP(1) MKTEMP(1) EENNVVIIRROONNMMEENNTT TMPDIR directory in which to place the temporary file when in --tt mode TMPDIR or TMP and TEMP on MSDOS and clones directory in which to place the temporary file when in --TT mode EEXXAAMMPPLLEESS The following sh(1) fragment illustrates a simple use of mmkktteemmpp where the script should quit if it cannot get a safe temporary file. TMPFILE=`mktemp /tmp/example.XXXXXXXXXX` || exit 1 echo "program output" >> $TMPFILE The same fragment with support for a user's TMPDIR envi- ronment variable can be written as follows. TMPFILE=`mktemp -t example.XXXXXXXXXX` || exit 1 echo "program output" >> $TMPFILE This can be further simplified if we don't care about the actual name of the temporary file. In this case the --tt flag is implied. TMPFILE=`mktemp` || exit 1 echo "program output" >> $TMPFILE In some cases, it may be desirable to use a default tempo- rary directory other than _/_t_m_p_. In this example the tem- porary file will be created in _/_e_x_t_r_a_/_t_m_p unless the user's TMPDIR (or TMP and TEMP on MSDOS and clones) envi- ronment variable specifies otherwise. TMPFILE=`mktemp -p /extra/tmp example.XXXXXXXXXX` || exit 1 echo "program output" >> $TMPFILE In some cases, we want the script to catch the error. For instance, if we attempt to create two temporary files and the second one fails we need to remove the first before exiting. TMP1=`mktemp -t example.1.XXXXXXXXXX` || exit 1 TMP2=`mktemp -t example.2.XXXXXXXXXX` if [ $? -ne 0 ]; then rm -f $TMP1 exit 1 fi Or perhaps you don't want to exit if mmkktteemmpp is unable to 30 September 2001 3 MKTEMP(1) MKTEMP(1) create the file. In this case you can protect that part of the script thusly. TMPFILE=`mktemp -t example.XXXXXXXXXX` && { # Safe to use $TMPFILE in this block echo data > $TMPFILE ... rm -f $TMPFILE } SSEEEE AALLSSOO mmkkddtteemmpp(3), mmkksstteemmpp(3), mmkktteemmpp(3) HHIISSTTOORRYY The mmkktteemmpp utility appeared in OpenBSD 2.1. 30 September 2001 4