MKTEMP(1)
MKTEMP(1)



NNAAMMEE
       mmkktteemmpp - make temporary filename (unique)

SSYYNNOOPPSSIISS
       mmkktteemmpp [--VV] | [--ddqqttuu] [--pp
       _d_i_r_e_c_t_o_r_y] [_t_e_m_p_l_a_t_e]

DDEESSCCRRIIPPTTIIOONN
       The  mmkktteemmpp  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
       is used and the --tt flag is implied (see below).

       The  trailing  `Xs'  are  replaced  with  a  combination
       of the current process number and random letters.
       The name chosen 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.  The number
       of unique filenames mmkktteemmpp  can  return
       depends  on the number of `Xs' provided; ten `Xs' will
       result in mmkktteemmpp testing roughly 26 **
       10 combinations.

       If mmkktteemmpp can successfully generate a unique
       filename,  the  file  (or directory)  is created with
       file permissions such that it is only read‐ able
       and writable by its owner (unless the --uu flag is
       given)  and  the filename is printed to standard output.

       mmkktteemmpp  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
       reasons it is suggested that mmkktteemmpp be
       used instead.

       The options are as follows:

       --VV     Print the version and exit.

       --dd     Make a directory instead of a file.

       --pp _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 generating the tem‐ porary
              filename.  The _d_i_r_e_c_t_o_r_y will
              be overridden by the user's TMPDIR  environment
              variable if it is set.  This option implies
              the --tt flag (see below).

       --qq     Fail silently if an error occurs.  This is
       useful  if  a  script
              does not want error output to go to standard
              error.

       --tt     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 --pp flag was
              given the specified direc‐
                     tory is used.

              ·      If none of the above apply, _/_t_m_p
              is used.

       In this mode, the _t_e_m_p_l_a_t_e (if
       specified) should be a directory  compo‐ nent  (as
       opposed to a full path) and thus should not contain
       any for‐ ward slashes.

       --uu     Operate in ``unsafe'' mode.  The  temp
       file  will  be  unlinked
              before mmkktteemmpp exits.  This is
              slightly better than mktemp(3) but still
              introduces a race condition.  Use of this
              option  is  not encouraged.

       The mmkktteemmpp utility exits with a value of
       0 on success or 1 on failure.

EEXXAAMMPPLLEESS
       The  following  sh(1) fragment illustrates a simple
       use of mmkktteemmpp 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
       environment 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 --tt flag is implied.

              TMPFILE=`mktemp` || exit 1 echo "program output"
              >> $TMPFILE

       In some cases, it may be desirable to use a default
       temporary directory other than _/_t_m_p_.
       In this example the temporary file will be created
       in _/_e_x_t_r_a_/_t_m_p unless the user's
       TMPDIR environment variable specifies oth‐ erwise.

              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
       mmkktteemmpp is unable to 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
              }


EENNVVIIRROONNMMEENNTT
       TMPDIR  directory in which to place the temporary file
       when in --tt mode

SSEEEE AALLSSOO
       mmkkddtteemmpp(3), mmkksstteemmpp(3),
       mmkktteemmpp(3)

HHIISSTTOORRYY
       The mmkktteemmpp utility appeared in OpenBSD 2.1.



                               30 September 2001
                               MKTEMP(1)
