Add Book to My BookshelfPurchase This Book Online

Chapter 2 - Utility Routines

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Manipulating Temporary Files
When a program needs to create a temporary file, it is usually desirable to use a filename that is not likely to be used by another program or by another invocation of the current program. For example, if the C compiler always used the temporary file /tmp/c-compile, then only one program could be compiled on the system at a time. If two people tried to compile programs simultaneously, they would both be writing to the same temporary file, and neither would get anything useful out of the experience. For this reason, UNIX offers several functions for creating temporary files with unique names.
The most often-used function is mktemp. Although it is not specified by the ANSI C standard, it is nevertheless available on almost all modern UNIX platforms:
      #include <stdlib.h>
      char *mktemp(char *template);
(In HP-UX 10.x, mktemp is declared in unistd.h instead of stdlib.h.)
The template parameter points to a character string that contains a prototype temporary filename; this prototype must include six trailing X characters, which are replaced with a unique identifier (usually based on the process ID number). Because mktemp modifies the string pointed to by template in place, you cannot use constant strings as defined in ANSI C. In other words, do not use code like this:
      #include <stdlib.h>
          char *tempf;
          tempf = mktemp("/tmp/mytempXXXXXX");
Instead, use code like this:
      #include <stdlib.h>
          char *tempf;
          char template[32];
          strcpy(template, "/tmp/mytempXXXXXX");
          tempf = mktemp(template);
If mktemp cannot construct a unique filename, it assigns the empty string to template.
The ANSI C standard specifies two different functions, tmpnam and tempnam, for creating temporary files:
      #include <stdio.h>
      char *tmpnam(char *s);
      char *tempnam(const char *dir, constr char *pfx);
These functions also exist in most versions of System V UNIX, but are not usually present in BSD versions. tmpnam places its result in the character array pointed to by s; if s is null then the result is left in an internal area that is overwritten with each call. If s is not null, then it must point to an array of at least L_tmpnam (defined in stdio.h) bytes. The temporary filename generated by tmpnam always has the path prefix defined as P_tmpdir in stdio.h; on SVR4 systems it is defined as /tmp/.
tempnam allows you to control the directory in which you create the temporary file by passing it in as dir. If dir is null, the path defined as P_tmpdir in stdio.h is used. The pfx string allows you to choose a prefix for the filenames generated by tempnam; if it is null, no prefix is used. If the environment variable TMPDIR is set, its value overrides any value specified by dir.
A fourth function for creating a temporary file, also specified by the ANSI C standard, is called tmpfile:
      #include <stdio.h>
      FILE *tmpfile(void);
This function uses tmpnam to create a temporary filename and then opens the file for reading and writing. It returns a Standard I/O Library file pointer (see Chapter 4, The Standard I/O Library) for the file.
Porting Notes
The most portable of these functions is probably mktemp. Although it is not specified by the ANSI C standard, it has existed in UNIX for the longest time, and is likely to be present on almost any system.
BSD UNIX provides one other function, called mkstemp:
   int mkstemp(char *template);
Use the template parameter as described for mktemp, previously. After the temporary filename is obtained, mkstemp opens the file for reading and writing and returns a low-level I/O file descriptor (see Chapter 3, Low-Level I/O Routines) for the file. When porting programs that use this function to SVR4 systems, use the following compatibility routine:
#include <sys/types.h>
#include <stdlib.h>
#include <fcntl.h>
int
mkstemp(char *template)
{
    char *tempf;
tempf = mktemp(template);
if (strlen(template) == 0)
        return(-1);
   
    return(open(tempf, O_RDWR | O_CREAT | O_TRUNC, 0666));
}

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback