Commit | Line | Data |
---|---|---|
39423523 DD |
1 | /* |
2 | ||
3 | @deftypefn Supplemental char* tmpnam (char *@var{s}) | |
4 | ||
5 | This function attempts to create a name for a temporary file, which | |
6 | will be a valid file name yet not exist when @code{tmpnam} checks for | |
7 | it. @var{s} must point to a buffer of at least @code{L_tmpnam} bytes, | |
99b58139 | 8 | or be @code{NULL}. Use of this function creates a security risk, and it must |
39423523 DD |
9 | not be used in new projects. Use @code{mkstemp} instead. |
10 | ||
11 | @end deftypefn | |
12 | ||
13 | */ | |
14 | ||
252b5132 RH |
15 | #include <stdio.h> |
16 | ||
17 | #ifndef L_tmpnam | |
e2eaf477 | 18 | #define L_tmpnam 100 |
252b5132 RH |
19 | #endif |
20 | #ifndef P_tmpdir | |
21 | #define P_tmpdir "/usr/tmp" | |
22 | #endif | |
23 | ||
24 | static char tmpnam_buffer[L_tmpnam]; | |
25 | static int tmpnam_counter; | |
26 | ||
49b1fae4 | 27 | extern int getpid (void); |
252b5132 RH |
28 | |
29 | char * | |
49b1fae4 | 30 | tmpnam (char *s) |
252b5132 RH |
31 | { |
32 | int pid = getpid (); | |
33 | ||
34 | if (s == NULL) | |
35 | s = tmpnam_buffer; | |
36 | ||
37 | /* Generate the filename and make sure that there isn't one called | |
38 | it already. */ | |
39 | ||
40 | while (1) | |
41 | { | |
42 | FILE *f; | |
43 | sprintf (s, "%s/%s%x.%x", P_tmpdir, "t", pid, tmpnam_counter); | |
44 | f = fopen (s, "r"); | |
45 | if (f == NULL) | |
46 | break; | |
47 | tmpnam_counter++; | |
48 | fclose (f); | |
49 | } | |
50 | ||
51 | return s; | |
52 | } |