]>
git.ipfire.org Git - thirdparty/squid.git/blob - compat/tempnam.c
1 /* A reasonably functional tmpnam. */
3 /* Originally by Tom Hageman, tom@basil.icce.rug.nl */
6 * This tmpnam() was changed by Gerben_Wierda@RnA.nl to serve as
7 * tempnam() for squid-1.1.6. It ignores the directory parameter, every
8 * temp file is written in /tmp.
12 #include "compat/tempnam.h"
27 #define lengthof_tmp 5
30 #define LONG_BIT (CHAR_BIT * 4) /* assume sizeof(long) == 4 */
33 #define L_tmpmin (lengthof_tmp + 5) /* 5 chars for pid. */
35 #if (L_tmpnam > L_tmpmin)
36 #if (L_tmpnam > L_tmpmin + LONG_BIT / 6) /* base 64 */
37 #define TMP_MAX ULONG_MAX
39 #define TMP_MAX ((1L << (6 * (L_tmpnam - L_tmpmin))) - 1)
43 #error "tmpnam: L_tmpnam undefined"
45 #error "tmpnam: L_tmpnam too small"
53 static const char digits
[] =
54 #if (L_tmpnam >= L_tmpmin + LONG_BIT / 4)
58 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-";
61 static unsigned long lastcount
= 0;
62 static char buffer
[L_tmpnam
+ 1];
64 unsigned long count
= lastcount
;
67 if (sizeof(_tmp
) - 1 != lengthof_tmp
)
68 abort(); /* Consistency error. */
71 register int i
= L_tmpnam
;
72 register unsigned long c
;
73 register unsigned int p
;
75 /* Build filename. (the hard way) */
79 c
= (count
== TMP_MAX
) ? 0 : ++count
;
81 *--s
= digits
[c
% TMP_BASE
];
83 } while (--i
> L_tmpmin
);
85 p
= (unsigned int) pid
;
87 *--s
= digits
[p
% 10];
89 } while (--i
> lengthof_tmp
);
95 /* Check that the file doesn't exist. */
96 if (access(s
, 0) != 0)
99 /* It exists; retry unless we tried them all. */
100 if (count
== lastcount
) {
112 tempnam(const char *dir
, const char *pfx
)
123 while ((t
= tempnam(NULL
, NULL
))) {