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 (); 00028 00029 char * 00030 tmpnam (s) 00031 char *s; 00032 { 00033 int pid = getpid (); 00034 00035 if (s == NULL) 00036 s = tmpnam_buffer; 00037 00038 /* Generate the filename and make sure that there isn't one called 00039 it already. */ 00040 00041 while (1) 00042 { 00043 FILE *f; 00044 sprintf (s, "%s/%s%x.%x", P_tmpdir, "t", pid, tmpnam_counter); 00045 f = fopen (s, "r"); 00046 if (f == NULL) 00047 break; 00048 tmpnam_counter++; 00049 fclose (f); 00050 } 00051 00052 return s; 00053 }
1.5.6