19588ddcfSespie /* 29588ddcfSespie 39588ddcfSespie @deftypefn Supplemental char* tmpnam (char *@var{s}) 49588ddcfSespie 59588ddcfSespie This function attempts to create a name for a temporary file, which 69588ddcfSespie will be a valid file name yet not exist when @code{tmpnam} checks for 79588ddcfSespie it. @var{s} must point to a buffer of at least @code{L_tmpnam} bytes, 89588ddcfSespie or be @code{NULL}. Use of this function creates a security risk, and it must 99588ddcfSespie not be used in new projects. Use @code{mkstemp} instead. 109588ddcfSespie 119588ddcfSespie @end deftypefn 129588ddcfSespie 139588ddcfSespie */ 149588ddcfSespie 1500bf4279Sespie #include <stdio.h> 1600bf4279Sespie 1700bf4279Sespie #ifndef L_tmpnam 1800bf4279Sespie #define L_tmpnam 100 1900bf4279Sespie #endif 2000bf4279Sespie #ifndef P_tmpdir 2100bf4279Sespie #define P_tmpdir "/usr/tmp" 2200bf4279Sespie #endif 2300bf4279Sespie 2400bf4279Sespie static char tmpnam_buffer[L_tmpnam]; 2500bf4279Sespie static int tmpnam_counter; 2600bf4279Sespie 27*20fce977Smiod extern int getpid (void); 2800bf4279Sespie 2900bf4279Sespie char * tmpnam(char * s)30*20fce977Smiodtmpnam (char *s) 3100bf4279Sespie { 3200bf4279Sespie int pid = getpid (); 3300bf4279Sespie 3400bf4279Sespie if (s == NULL) 3500bf4279Sespie s = tmpnam_buffer; 3600bf4279Sespie 3700bf4279Sespie /* Generate the filename and make sure that there isn't one called 3800bf4279Sespie it already. */ 3900bf4279Sespie 4000bf4279Sespie while (1) 4100bf4279Sespie { 4200bf4279Sespie FILE *f; 4300bf4279Sespie sprintf (s, "%s/%s%x.%x", P_tmpdir, "t", pid, tmpnam_counter); 4400bf4279Sespie f = fopen (s, "r"); 4500bf4279Sespie if (f == NULL) 4600bf4279Sespie break; 4700bf4279Sespie tmpnam_counter++; 4800bf4279Sespie fclose (f); 4900bf4279Sespie } 5000bf4279Sespie 5100bf4279Sespie return s; 5200bf4279Sespie } 53