Back to index

cell-binutils  2.17cvs20070401
tmpnam.c
Go to the documentation of this file.
00001 /*
00002 
00003 @deftypefn Supplemental char* tmpnam (char *@var{s})
00004 
00005 This function attempts to create a name for a temporary file, which
00006 will be a valid file name yet not exist when @code{tmpnam} checks for
00007 it.  @var{s} must point to a buffer of at least @code{L_tmpnam} bytes,
00008 or be @code{NULL}.  Use of this function creates a security risk, and it must
00009 not be used in new projects.  Use @code{mkstemp} instead.
00010 
00011 @end deftypefn
00012 
00013 */
00014 
00015 #include <stdio.h>
00016 
00017 #ifndef L_tmpnam
00018 #define L_tmpnam 100
00019 #endif
00020 #ifndef P_tmpdir
00021 #define P_tmpdir "/usr/tmp"
00022 #endif
00023 
00024 static char tmpnam_buffer[L_tmpnam];
00025 static int tmpnam_counter;
00026 
00027 extern int getpid (void);
00028 
00029 char *
00030 tmpnam (char *s)
00031 {
00032   int pid = getpid ();
00033 
00034   if (s == NULL)
00035     s = tmpnam_buffer;
00036 
00037   /*  Generate the filename and make sure that there isn't one called
00038       it already.  */
00039 
00040   while (1)
00041     {
00042       FILE *f;
00043       sprintf (s, "%s/%s%x.%x", P_tmpdir, "t", pid, tmpnam_counter);
00044       f = fopen (s, "r");
00045       if (f == NULL)
00046        break;
00047       tmpnam_counter++;
00048       fclose (f);
00049     }
00050 
00051   return s;
00052 }