int main (int argc, char *argv[]) { char buf[FILENAME_MAX]; char *fn; FILE *fp; int i; for (i = 0; i < 500; i++) { fn = __stdio_gen_tempname(buf, sizeof (buf), (const char *) NULL, "file", 0, (size_t *) NULL, (FILE **) NULL); if (fn == NULL) { printf ("__stdio_gen_tempname failed\n"); exit (1); } files[i] = strdup (fn); printf ("file: %s\n", fn); fp = fopen (fn, "w"); fclose (fp); } for (i = 0; i < 500; i++) remove (files[i]); exit (0); }
/* Generate a unique filename in P_tmpdir. If S is NULL return NULL. This makes this function thread safe. */ char * tmpnam_r (char *s) { if (s == NULL) return NULL; /* In the following call we use the buffer pointed to by S if non-NULL although we don't know the size. But we limit the size to L_tmpnam characters in any case. */ return __stdio_gen_tempname (s, L_tmpnam, (const char *) NULL, (const char *) NULL, 0, (size_t *) NULL, (FILE **) NULL); }
#include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* Generate a unique temporary filename using up to five characters of PFX if it is not NULL. The directory to put this file in is searched for as follows: First the environment variable "TMPDIR" is checked. If it contains the name of a writable directory, that directory is used. If not and if DIR is not NULL, that value is checked. If that fails, P_tmpdir is tried and finally "/tmp". The storage for the filename is allocated by `malloc'. */ char * DEFUN(tempnam, (dir, pfx), CONST char *dir AND CONST char *pfx) { size_t len; register char *s; register char *t = __stdio_gen_tempname(dir, pfx, 1, &len); if (t == NULL) return NULL; s = (char *) malloc(len); if (s == NULL) return NULL; (void) memcpy(s, t, len); return s; }