* src/browser.c (browser_refresh): Take the distant possibility of
terabyte files into account, and in the bargain get rid of the need
to calculate the number of digits in UINT_MAX.
+ * src/files.c (get_next_filename): Limit the number of backup files
+ to one hundred thousand -- which should be far more than enough --
+ before finding a unused filename takes an annoying amount of time.
+ * src/utils.c (digits): Delete this now unneeded function.
2015-05-03 Benno Schulenberg <bensberg@justemail.net>
* src/browser.c (browser_refresh): Display an ellipsis only when the
* extension exists, we return "". */
char *get_next_filename(const char *name, const char *suffix)
{
- static int ulmax_digits = -1;
unsigned long i = 0;
char *buf;
- size_t namelen, suffixlen;
+ size_t wholenamelen;
assert(name != NULL && suffix != NULL);
- if (ulmax_digits == -1)
- ulmax_digits = digits(ULONG_MAX);
+ wholenamelen = strlen(name) + strlen(suffix);
- namelen = strlen(name);
- suffixlen = strlen(suffix);
-
- buf = charalloc(namelen + suffixlen + ulmax_digits + 2);
+ /* Reserve space for: the name plus the suffix plus a dot plus
+ * possibly five digits plus a null byte. */
+ buf = charalloc(wholenamelen + 7);
sprintf(buf, "%s%s", name, suffix);
while (TRUE) {
if (stat(buf, &fs) == -1)
return buf;
- if (i == ULONG_MAX)
+
+ /* Limit the number of backup files to a hundred thousand. */
+ if (++i == 100000)
break;
- i++;
- sprintf(buf + namelen + suffixlen, ".%lu", i);
+ sprintf(buf + wholenamelen, ".%lu", i);
}
/* We get here only if there is no possible save file. Blank out
void do_verbatim_input(void);
/* All functions in utils.c. */
-int digits(size_t n);
void get_homedir(void);
bool parse_num(const char *str, ssize_t *val);
bool parse_line_column(const char *str, ssize_t *line, ssize_t *column);
#include <ctype.h>
#include <errno.h>
-/* Return the number of decimal digits in n. */
-int digits(size_t n)
-{
- int i;
-
- if (n == 0)
- i = 1;
- else {
- for (i = 0; n != 0; n /= 10, i++)
- ;
- }
-
- return i;
-}
-
/* Return the user's home directory. We use $HOME, and if that fails,
* we fall back on the home directory of the effective user ID. */
void get_homedir(void)