diff options
| author | Paul Eggert | 2023-08-07 13:57:13 -0700 |
|---|---|---|
| committer | Paul Eggert | 2023-08-07 13:59:44 -0700 |
| commit | 4a2367d2f0199a95f25297ef2c5948768e2b01da (patch) | |
| tree | db3c863a5f364207afb2029ea7e180ec9e6edb81 /src | |
| parent | 7cd8236d35c033fefb7be742e6c3290c63eaf609 (diff) | |
| download | emacs-4a2367d2f0199a95f25297ef2c5948768e2b01da.tar.gz emacs-4a2367d2f0199a95f25297ef2c5948768e2b01da.zip | |
Simplify lockfile name calculation
* src/filelock.c (get_boot_time):
Move ‘counter’ decl to simplify #ifdef nesting.
(lock_file_1): Refactor two snprintf calls into one.
Don’t assume INT_MAX < SIZE_MAX.
Diffstat (limited to 'src')
| -rw-r--r-- | src/filelock.c | 27 |
1 files changed, 9 insertions, 18 deletions
diff --git a/src/filelock.c b/src/filelock.c index fea0044e219..66b8fd2ceac 100644 --- a/src/filelock.c +++ b/src/filelock.c | |||
| @@ -143,10 +143,6 @@ static void get_boot_time_1 (const char *, bool); | |||
| 143 | static time_t | 143 | static time_t |
| 144 | get_boot_time (void) | 144 | get_boot_time (void) |
| 145 | { | 145 | { |
| 146 | #if defined (BOOT_TIME) | ||
| 147 | int counter; | ||
| 148 | #endif | ||
| 149 | |||
| 150 | if (boot_time_initialized) | 146 | if (boot_time_initialized) |
| 151 | return boot_time; | 147 | return boot_time; |
| 152 | boot_time_initialized = 1; | 148 | boot_time_initialized = 1; |
| @@ -196,8 +192,9 @@ get_boot_time (void) | |||
| 196 | /* Try to get boot time from the current wtmp file. */ | 192 | /* Try to get boot time from the current wtmp file. */ |
| 197 | get_boot_time_1 (WTMP_FILE, 1); | 193 | get_boot_time_1 (WTMP_FILE, 1); |
| 198 | 194 | ||
| 199 | /* If we did not find a boot time in wtmp, look at wtmp, and so on. */ | 195 | /* If we did not find a boot time in wtmp, look at wtmp.1, |
| 200 | for (counter = 0; counter < 20 && ! boot_time; counter++) | 196 | wtmp.1.gz, wtmp.2, wtmp.2.gz, and so on. */ |
| 197 | for (int counter = 0; counter < 20 && ! boot_time; counter++) | ||
| 201 | { | 198 | { |
| 202 | Lisp_Object filename = Qnil; | 199 | Lisp_Object filename = Qnil; |
| 203 | bool delete_flag = false; | 200 | bool delete_flag = false; |
| @@ -442,18 +439,12 @@ lock_file_1 (Lisp_Object lfname, bool force) | |||
| 442 | char lock_info_str[MAX_LFINFO + 1]; | 439 | char lock_info_str[MAX_LFINFO + 1]; |
| 443 | intmax_t pid = getpid (); | 440 | intmax_t pid = getpid (); |
| 444 | 441 | ||
| 445 | if (boot) | 442 | char const *lock_info_fmt = (boot |
| 446 | { | 443 | ? "%s@%s.%"PRIdMAX":%"PRIdMAX |
| 447 | if (sizeof lock_info_str | 444 | : "%s@%s.%"PRIdMAX); |
| 448 | <= snprintf (lock_info_str, sizeof lock_info_str, | 445 | int len = snprintf (lock_info_str, sizeof lock_info_str, |
| 449 | "%s@%s.%"PRIdMAX":%"PRIdMAX, | 446 | lock_info_fmt, user_name, host_name, pid, boot); |
| 450 | user_name, host_name, pid, boot)) | 447 | if (! (0 <= len && len < sizeof lock_info_str)) |
| 451 | return ENAMETOOLONG; | ||
| 452 | } | ||
| 453 | else if (sizeof lock_info_str | ||
| 454 | <= snprintf (lock_info_str, sizeof lock_info_str, | ||
| 455 | "%s@%s.%"PRIdMAX, | ||
| 456 | user_name, host_name, pid)) | ||
| 457 | return ENAMETOOLONG; | 448 | return ENAMETOOLONG; |
| 458 | 449 | ||
| 459 | return create_lock_file (SSDATA (lfname), lock_info_str, force); | 450 | return create_lock_file (SSDATA (lfname), lock_info_str, force); |