]>
git.ipfire.org Git - thirdparty/squid.git/blob - compat/tempnam.c
41d19f978198a45073aac00a79bfc04e9142b273
2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* A reasonably functional tmpnam. */
11 /* Originally by Tom Hageman, tom@basil.icce.rug.nl */
14 * This tmpnam() was changed by Gerben_Wierda@RnA.nl to serve as
15 * tempnam() for squid-1.1.6. It ignores the directory parameter, every
16 * temp file is written in /tmp.
20 #include "compat/tempnam.h"
35 #define lengthof_tmp 5
38 #define LONG_BIT (CHAR_BIT * 4) /* assume sizeof(long) == 4 */
41 #define L_tmpmin (lengthof_tmp + 5) /* 5 chars for pid. */
43 #if (L_tmpnam > L_tmpmin)
44 #if (L_tmpnam > L_tmpmin + LONG_BIT / 6) /* base 64 */
45 #define TMP_MAX ULONG_MAX
47 #define TMP_MAX ((1L << (6 * (L_tmpnam - L_tmpmin))) - 1)
51 #error "tmpnam: L_tmpnam undefined"
53 #error "tmpnam: L_tmpnam too small"
60 static const char digits
[] =
61 #if (L_tmpnam >= L_tmpmin + LONG_BIT / 4)
65 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-";
68 static unsigned long lastcount
= 0;
69 static char buffer
[L_tmpnam
+ 1];
71 unsigned long count
= lastcount
;
74 if (sizeof(_tmp
) - 1 != lengthof_tmp
)
75 abort(); /* Consistency error. */
78 register int i
= L_tmpnam
;
79 register unsigned long c
;
80 register unsigned int p
;
82 /* Build filename. (the hard way) */
86 c
= (count
== TMP_MAX
) ? 0 : ++count
;
88 *--s
= digits
[c
% TMP_BASE
];
90 } while (--i
> L_tmpmin
);
92 p
= (unsigned int) pid
;
94 *--s
= digits
[p
% 10];
96 } while (--i
> lengthof_tmp
);
102 /* Check that the file doesn't exist. */
103 if (access(s
, 0) != 0)
106 /* It exists; retry unless we tried them all. */
107 if (count
== lastcount
) {
119 tempnam(const char *dir
, const char *pfx
)
130 while ((t
= tempnam(NULL
, NULL
))) {