]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/tempnam.c
5 /* A reasonably functional tmpnam. */
7 /* Originally by Tom Hageman, tom@basil.icce.rug.nl */
10 * This tmpnam() was changed by Gerben_Wierda@RnA.nl to serve as
11 * tempnam() for squid-1.1.6. It ignores the directory parameter, every
12 * temp file is written in /tmp.
24 #include <sys/types.h>
36 #define lengthof_tmp 5
39 #define LONG_BIT (CHAR_BIT * 4) /* assume sizeof(long) == 4 */
42 #define L_tmpmin (lengthof_tmp + 5) /* 5 chars for pid. */
44 #if (L_tmpnam > L_tmpmin)
45 #if (L_tmpnam > L_tmpmin + LONG_BIT / 6) /* base 64 */
46 #define TMP_MAX ULONG_MAX
48 #define TMP_MAX ((1L << (6 * (L_tmpnam - L_tmpmin))) - 1)
52 #error "tmpnam: L_tmpnam undefined"
54 #error "tmpnam: L_tmpnam too small"
62 static const char digits
[] =
63 #if (L_tmpnam >= L_tmpmin + LONG_BIT / 4)
67 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-";
70 static unsigned long lastcount
= 0;
71 static char buffer
[L_tmpnam
+ 1];
73 unsigned long count
= lastcount
;
76 if (sizeof(_tmp
) - 1 != lengthof_tmp
)
77 abort(); /* Consistency error. */
80 register int i
= L_tmpnam
;
81 register unsigned long c
;
82 register unsigned int p
;
84 /* Build filename. (the hard way) */
88 c
= (count
== TMP_MAX
) ? 0 : ++count
;
90 *--s
= digits
[c
% TMP_BASE
];
92 } while (--i
> L_tmpmin
);
94 p
= (unsigned int) pid
;
96 *--s
= digits
[p
% 10];
98 } while (--i
> lengthof_tmp
);
104 /* Check that the file doesn't exist. */
105 if (access(s
, 0) != 0)
108 /* It exists; retry unless we tried them all. */
109 if (count
== lastcount
) {
121 tempnam(const char *dir
, const char *pfx
)
132 while ((t
= tempnam(NULL
, NULL
))) {