diff -Nru libtrash-2.6/src/Makefile libtrash-2.6-/src/Makefile --- libtrash-2.6/src/Makefile 2007-02-09 19:07:26.000000000 +0100 +++ libtrash-2.6-/src/Makefile 2007-05-01 14:26:30.000000000 +0200 @@ -23,7 +23,7 @@ perl -w ../scripts/genheader.pl perl -w ../scripts/genconf.pl $(CC) $(CFLAGS) $(SRC) -nostartfiles -shared -fPIC -Wl,-soname,libtrash.so.${MAJOR} \ - -o libtrash.so.${VERSION} -ldl -lm + -o libtrash.so.${VERSION} -ldl install-libtrash: TRASH_OFF=YES install libtrash.so.${VERSION} ${INSTLIBDIR}/libtrash.so.${VERSION} diff -Nru libtrash-2.6/src/helpers.c libtrash-2.6-/src/helpers.c --- libtrash-2.6/src/helpers.c 2007-02-19 04:52:24.000000000 +0100 +++ libtrash-2.6-/src/helpers.c 2007-05-01 16:31:31.000000000 +0200 @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include @@ -62,6 +61,19 @@ /* ------------------------------------------------------------------------ */ +static inline int +ilog10 (int n) +{ + int result = 1; + + while (n /= 10) + result++; + + return result; +} + +/* ------------------------------------------------------------------------ */ + /* This function returns 1 if the given path points to a file or directory under one * of the directories listed in the semi-colon separated dir_list, 0 otherwise. We don't * use strtok_r() because doing so would involve creating both a copy of dir_list and an @@ -519,6 +531,9 @@ int digits_number = DEF_DIGITS; + int left = 0; + int right = -1; + /* This memory will be free()d either after having stored a new path in *new_path or * inside the calling function (graft_files()), depending on whether first_null is * NULL or not. @@ -536,13 +551,30 @@ errno = 0; - while (!(error && errno == ENOENT)) + while (1) { - sprintf(possible_path, "%s[%d]", *new_path, n++); + sprintf(possible_path, "%s[%d]", *new_path, n); - error = stat(possible_path, &stat_var); - - if (first_null != NULL && /* if we have been asked to invent a new a _dir_ name */ + error = stat(possible_path, &stat_var); + + if (error && errno == ENOENT){ + if (left == n - 1) + break; + right = n; + n = (n + left) / 2; + } else { + left = n; + if (right == -1) + n *= 2; + else if (right > n + 1) + n = (n + right) / 2; + else { + n++; + right = -1; + } + } + + if (first_null != NULL && /* if we have been asked to invent a new a _dir_ name */ !error && /* and if something with this name (possible_path) already exists */ S_ISDIR(stat_var.st_mode) && /* and this something is a directory */ !access(possible_path, W_OK | X_OK) ) /* to which you have write- and search-permission */ @@ -550,7 +582,7 @@ /* Do we need more memory? */ - if (log10(n) == digits_number) + if (ilog10(n) >= digits_number) { char *tmp = NULL;