summaryrefslogtreecommitdiff
path: root/wrapper.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2019-10-09 14:01:00 +0900
committerJunio C Hamano <gitster@pobox.com>2019-10-09 14:01:00 +0900
commit6e12570822a904e2be554b05755c08f4d24be7e9 (patch)
treeb6d276fd5205c5adb7a02c7bac6ba201b3ab3ac3 /wrapper.c
parent772cad0afb834b9af8ee56511dc3df4d6e2f011d (diff)
parent53d687bf5f8008abd52b92120c7e22d4d81bdc71 (diff)
downloadgit-6e12570822a904e2be554b05755c08f4d24be7e9.tar.gz
Merge branch 'ah/cleanups'
Miscellaneous code clean-ups. * ah/cleanups: git_mkstemps_mode(): replace magic numbers with computed value wrapper: use a loop instead of repetitive statements diffcore-break: use a goto instead of a redundant if statement commit-graph: remove a duplicate assignment
Diffstat (limited to 'wrapper.c')
-rw-r--r--wrapper.c21
1 files changed, 11 insertions, 10 deletions
diff --git a/wrapper.c b/wrapper.c
index c55d7722d7..e1eaef2e16 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -441,7 +441,9 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
- static const int num_letters = 62;
+ static const int num_letters = ARRAY_SIZE(letters) - 1;
+ static const char x_pattern[] = "XXXXXX";
+ static const int num_x = ARRAY_SIZE(x_pattern) - 1;
uint64_t value;
struct timeval tv;
char *filename_template;
@@ -450,12 +452,12 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
len = strlen(pattern);
- if (len < 6 + suffix_len) {
+ if (len < num_x + suffix_len) {
errno = EINVAL;
return -1;
}
- if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
+ if (strncmp(&pattern[len - num_x - suffix_len], x_pattern, num_x)) {
errno = EINVAL;
return -1;
}
@@ -466,16 +468,15 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
*/
gettimeofday(&tv, NULL);
value = ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
- filename_template = &pattern[len - 6 - suffix_len];
+ filename_template = &pattern[len - num_x - suffix_len];
for (count = 0; count < TMP_MAX; ++count) {
uint64_t v = value;
+ int i;
/* Fill in the random bits. */
- filename_template[0] = letters[v % num_letters]; v /= num_letters;
- filename_template[1] = letters[v % num_letters]; v /= num_letters;
- filename_template[2] = letters[v % num_letters]; v /= num_letters;
- filename_template[3] = letters[v % num_letters]; v /= num_letters;
- filename_template[4] = letters[v % num_letters]; v /= num_letters;
- filename_template[5] = letters[v % num_letters]; v /= num_letters;
+ for (i = 0; i < num_x; i++) {
+ filename_template[i] = letters[v % num_letters];
+ v /= num_letters;
+ }
fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
if (fd >= 0)