1 #include "../git-compat-util.h"
3 /* Adapted from libiberty's mkstemp.c. */
8 int gitmkstemps(char *pattern
, int suffix_len
)
10 static const char letters
[] =
11 "abcdefghijklmnopqrstuvwxyz"
12 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
14 static const int num_letters
= 62;
21 len
= strlen(pattern
);
23 if (len
< 6 + suffix_len
) {
28 if (strncmp(&pattern
[len
- 6 - suffix_len
], "XXXXXX", 6)) {
34 * Replace pattern's XXXXXX characters with randomness.
35 * Try TMP_MAX different filenames.
37 gettimeofday(&tv
, NULL
);
38 value
= ((size_t)(tv
.tv_usec
<< 16)) ^ tv
.tv_sec
^ getpid();
39 template = &pattern
[len
- 6 - suffix_len
];
40 for (count
= 0; count
< TMP_MAX
; ++count
) {
42 /* Fill in the random bits. */
43 template[0] = letters
[v
% num_letters
]; v
/= num_letters
;
44 template[1] = letters
[v
% num_letters
]; v
/= num_letters
;
45 template[2] = letters
[v
% num_letters
]; v
/= num_letters
;
46 template[3] = letters
[v
% num_letters
]; v
/= num_letters
;
47 template[4] = letters
[v
% num_letters
]; v
/= num_letters
;
48 template[5] = letters
[v
% num_letters
]; v
/= num_letters
;
50 fd
= open(pattern
, O_CREAT
| O_EXCL
| O_RDWR
, 0600);
54 * Fatal error (EPERM, ENOSPC etc).
55 * It doesn't make sense to loop.
60 * This is a random value. It is only necessary that
61 * the next TMP_MAX values generated by adding 7777 to
62 * VALUE are different with (module 2^32).
66 /* We return the null string if we can't find a unique file name. */