*nix Documentation Project
·  Home
 +   man pages
·  Linux HOWTOs
·  FreeBSD Tips
·  *niX Forums

  man pages->Tru64 Unix man pages -> mktemp (3)              
Title
Content
Arch
Section
 

mktemp(3)

Contents


NAME    [Toc]    [Back]

       mktemp,  mkstemp,  mkstemps,  mkdtemp - Construct a unique
       file name or directory

SYNOPSIS    [Toc]    [Back]

       #include <stdlib.h>

       char *mktemp(
               char *template ); int mkstemp(
               char *template ); int mkstemps(
               char *template,
               int suffixlen ); char *mkdtemp(
               char *template );

LIBRARY    [Toc]    [Back]

       Standard C Library (libc)

       System V Compatibility Library (libsys5)

STANDARDS    [Toc]    [Back]

       Interfaces documented on this reference  page  conform  to
       industry standards as follows:

       mktemp(), mkstemp():  XSH4.2, XSH5.0

       Refer to the standards(5) reference page for more information
 about industry standards and associated tags.

PARAMETERS    [Toc]    [Back]

       Points to a string to be modified to form  a  unique  file
       name. The string should be composed of characters from the
       portable character set. Elements in the string consist  of
       an  optional  prefix,  6  or  more X characters (which are
       replaced to make the file name unique), and -- in the case
       of  the  mkstemps()  function  --  a  suffix. The template
       string prior to the suffix element should look like a file
       name  with  six  or more trailing X characters. The string
       has the following  forms:  For  mktemp(),  mkstemp(),  and
       mkdtemp() functions:

              [prefix]XXXXXXX For the mkstemps() function:

              [prefix]XXXXXXsuffix


              The  prefix portion of the template string (often a
              directory specification and/or file name prefix) is
              optional,  the  string  of 6 or more Xs is replaced
              when the file name is  generated,  and  the  suffix
              portion  is the string to be appended when generating
 the file name. The  template  string  prior  to
              suffix  should  look  like  a file name with six or
              more trailing X characters.  The length of the suffix
 in the template string.








DESCRIPTION    [Toc]    [Back]

       The  mktemp()  function replaces, with a unique file name,
       the contents of the template string pointed to by the template
  parameter.  The  application  should initialize the
       template string to be a file name with six or more  trailing
 X characters. To generate a unique file name, mktemp()
       replaces the Xs in the  template  string  with  characters
       from the portable character set. Note that a possible race
       condition can exist when using the mktemp() function.  The
       mkstemp(),  mkstemps(), and mkdtemp() functions avoid this
       problem; see APPLICATION USAGE for details.

       For compatibility  with  SVID2,  the  libsys5  version  of
       mktemp()  uses  the  getpid() function when generating the
       file name. In addition to suffering  from  the  same  race
       condition mentioned above, the required use of getpid() by
       the libsys5 version of mktemp() results  in  significantly
       more  predictable  file  name  generation,  and  this predictability
 could be misused  by  a  hostile  process.  To
       avoid  these  problems,  use the mkstemp(), mkstemps(), or
       mkdtemp() functions (not specified by SVID2).

       The mkstemp() function performs the same  substitution  to
       the  template  name  as  mktemp(), but it also creates the
       file with mode 0600 and returns,  for  the  file,  a  file
       descriptor that is open for reading and writing. The function
 thus prevents any possible race condition that  could
       exist  in  the  interval  between testing whether the file
       exists and opening it for use.

       The mkstemps() function is similar to the mkstemp()  function,
  except  that it permits the inclusion of a trailing
       suffix in the template string. The suffix is  appended  to
       the generated file name.

       The  mkdtemp()  function is similar to the mkstemp() function,
 except that it creates a mode 0700 directory instead
       of  a file name and returns a pointer to a string containing
 the resulting directory path that has been created.

       The mkdtemp() and mkstemps() functions first  appeared  in
       OpenBSD.

RETURN VALUES    [Toc]    [Back]

       Upon  successful completion, the mktemp() function returns
       the address of the  string  pointed  to  by  the  template
       parameter. If the string pointed to by the template parameter
 does not contain trailing Xs or if the mktemp() function
  is unable to construct a unique file name, the first
       character of the template string is replaced with  a  null
       character and a null pointer is returned.

       Upon successful completion, the mkstemp() function returns
       an open file descriptor. If the string pointed to  by  the
       template  parameter  does not contain trailing Xs or if no
       suitable file can be created, -1 is returned.

       Upon  successful  completion,  the   mkstemps()   function
       returns  an open file descriptor. Under any of the following
 conditions, -1 is returned and errno is set  to  indicate
  the  error: If the string pointed to by the template
       parameter does not contain trailing Xs prior to the  start
       of  the  suffix If the suffix contains a slash (/) If suffixlen
 is larger than the length of  the  template  If  no
       suitable file can be created

       Upon successful completion, the mkdtemp() function returns
       the address of the  string  pointed  to  by  the  template
       parameter. If the string pointed to by the template parameter
 does not contain trailing Xs or if no suitable directory
  can be created, a null pointer is returned and errno
       is set to indicate the error.

ERRORS    [Toc]    [Back]

       No errors are defined for the mktemp() or mkstemp()  functions.


       The  mkstemps()  function may set errno to one of the following
 values or to any value specified by the  open()  or
       stat()  function:  The  string  pointed to by the template
       parameter does not contain trailing Xs prior to the  start
       of  the  suffix,  the suffix contains a slash (/), or suffixlen
 is larger than the length  of  the  template.   The
       pathname portion of the template is not an existing directory.


       The mkdtemp() function may set errno to one of the following
  values  or  to  any value specified by the stat() and
       mkdir() functions: The string pointed to by  the  template
       parameter does not contain trailing Xs.  The pathname portion
 of the template is not an existing directory.

APPLICATION USAGE    [Toc]    [Back]

       The use of mktemp() is discouraged due to a race condition
       that may exist between the time a file name is created and
       the time the file is opened. During this interval,  it  is
       possible  for  some other (potentially hostile) process to
       create  a  file  with  the  same  name.   The   mkstemp(),
       mkstemps(),  and mkdtemp() functions avoid this problem by
       creating the file name or directory in a way that  ensures
       that duplication cannot occur.

       It  is  important  that  the  template parameter passed to
       these functions does not point to a read-only string.  For
       example, a call such as:

       int retval; retval = mkstemp("/tmp/temp.XXXXXX");

       may  result  in  a  core  dump when the mkstemp() function
       attempts to modify a string constant.

EXAMPLES    [Toc]    [Back]

       The following example uses mkstemp() to create and open  a
       temp file:

       #include    <string.h>    #include   <stdlib.h>   #include
       <unistd.h> #include <stdio.h> main() {
            char template[16];
            int fd = -1;

            strcpy(template, "/tmp/tempXXXXXX");
            if ((fd = mkstemp(template)) != -1) {
               /* use 'fd' here */
               unlink(template);
               close(fd);
            } else
               fprintf(stderr, "Failed to create temp  file.\n");
       }


       The following example uses mkstemps() to create and open a
       temp file with a suffix:

       #include   <string.h>   #include    <stdlib.h>    #include
       <unistd.h> #include <stdio.h> #include <errno.h> main() {
            char template[20];
            int fd = -1;

            strcpy(template, "/tmp/tempXXXXXXX.xyz");
            if ((fd = mkstemps(template, 4)) != -1) {
               /* use 'fd' here */
               unlink(template);
               close(fd);
            } else
               fprintf(stderr,    "Failed    to    create    temp
       file.\n%s\n",
                       strerror(errno)); }



       The following example uses  mkdtemp()  to  create  a  temp
       directory  and, if successful, then uses mkstemp() to create
 a temp file within the directory:

       #include   <string.h>   #include    <stdlib.h>    #include
       <unistd.h> #include <stdio.h> #include <errno.h> main() {
            char template[35], dirpath[22], *dp;
            int fd = -1;

            strcpy(dirpath, "/tmp/tempdir.XXXXXXXX");
            if ((dp = mkdtemp(dirpath)) != NULL) {
             /* begin - example use of 'dp' */
               sprintf(template, "%s/%s", dp, "tempXXXXXXXX");
               if ((fd = mkstemp(template)) != -1) {
                  /* use 'fd' here */
                  unlink(template);
                  close(fd);
               } else
                  fprintf(stderr,    "Failed   to   create   temp
       file.\n%s\n",
                          strerror(errno));
             /* end - example use of 'dp' */
               rmdir(dirpath);
            } else
               fprintf(stderr,  "Failed  to  create  temp  directory.\n%s\n",

                       strerror(errno)); }


SEE ALSO    [Toc]    [Back]

      
      
       Functions:   tmpfile(3),   tmpnam(3),  mkdir(2),  open(2),
       stat(2)

       Standards: standards(5)



                                                        mktemp(3)
[ Back ]
 Similar pages
Name OS Title
mkdtemp NetBSD make unique temporary file or directory name
mkstemp NetBSD make unique temporary file or directory name
mktemp NetBSD make unique temporary file or directory name
newfs_msdos FreeBSD construct a new MS-DOS (FAT) file system
tmpnam Tru64 Construct the name for a temporary file
newfs_hfs HP-UX construct a new HFS file system
tempnam Tru64 Construct the name for a temporary file
newfs_msdos OpenBSD construct a new MS-DOS (FAT) file system
newfs OpenBSD construct a new file system
mkfs_hfs HP-UX construct an HFS file system
Copyright © 2004-2005 DeniX Solutions SRL
newsletter delivery service