(ISDIGIT): Replace with smaller, faster edition
[gnulib.git] / lib / backupfile.c
index f15e530..2ca5125 100644 (file)
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 /* David MacKenzie <djm@gnu.ai.mit.edu>.
    Some algorithms adapted from GNU Emacs. */
 
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
 #include <stdio.h>
 #include <ctype.h>
 #include <sys/types.h>
 #include "backupfile.h"
-#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
-#include <string.h>
-#ifndef index
-#define index strchr
-#endif
-#ifndef rindex
-#define rindex strrchr
-#endif
+#ifdef HAVE_STRING_H
+# include <string.h>
 #else
-#include <strings.h>
+# include <strings.h>
 #endif
 
-#if defined(DIRENT) || defined(_POSIX_VERSION)
-#include <dirent.h>
-#define NLENGTH(direct) (strlen((direct)->d_name))
-#else /* not (DIRENT or _POSIX_VERSION) */
-#define dirent direct
-#define NLENGTH(direct) ((direct)->d_namlen)
-#ifdef SYSNDIR
-#include <sys/ndir.h>
-#endif /* SYSNDIR */
-#ifdef SYSDIR
-#include <sys/dir.h>
-#endif /* SYSDIR */
-#ifdef NDIR
-#include <ndir.h>
-#endif /* NDIR */
-#endif /* DIRENT or _POSIX_VERSION */
-
-#ifdef VOID_CLOSEDIR
+#ifdef HAVE_DIRENT_H
+# include <dirent.h>
+# define NLENGTH(direct) (strlen((direct)->d_name))
+#else /* not HAVE_DIRENT_H */
+# define dirent direct
+# define NLENGTH(direct) ((direct)->d_namlen)
+# ifdef HAVE_SYS_NDIR_H
+#  include <sys/ndir.h>
+# endif /* HAVE_SYS_NDIR_H */
+# ifdef HAVE_SYS_DIR_H
+#  include <sys/dir.h>
+# endif /* HAVE_SYS_DIR_H */
+# ifdef HAVE_NDIR_H
+#  include <ndir.h>
+# endif /* HAVE_NDIR_H */
+#endif /* HAVE_DIRENT_H */
+
+#ifdef CLOSEDIR_VOID
 /* Fake a return value. */
-#define CLOSEDIR(d) (closedir (d), 0)
+# define CLOSEDIR(d) (closedir (d), 0)
 #else
-#define CLOSEDIR(d) closedir (d)
+# define CLOSEDIR(d) closedir (d)
 #endif
 
 #ifdef STDC_HEADERS
-#include <stdlib.h>
+# include <stdlib.h>
 #else
 char *malloc ();
 #endif
 
-#if !defined (isascii) || defined (STDC_HEADERS)
-#undef isascii
-#define isascii(c) 1
+#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
+# define IN_CTYPE_DOMAIN(c) 1
+#else
+# define IN_CTYPE_DOMAIN(c) isascii(c)
 #endif
 
-#define ISDIGIT(c) (isascii ((unsigned char ) c) \
-                   && isdigit ((unsigned char) (c)))
+#define ISDIGIT_LOCALE(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))
+
+/* ISDIGIT differs from ISDIGIT_LOCALE, as follows:
+   - Its arg may be any int or unsigned int; it need not be an unsigned char.
+   - It's guaranteed to evaluate its argument exactly once.
+   - It's typically faster.
+   Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
+   only '0' through '9' are digits.  Prefer ISDIGIT to ISDIGIT_LOCALE unless
+   it's important to use the locale's definition of `digit' even when the
+   host does not conform to Posix.  */
+#define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
 
 #if defined (HAVE_UNISTD_H)
 #include <unistd.h>
@@ -79,9 +87,9 @@ char *malloc ();
 #if defined (_POSIX_VERSION)
 /* POSIX does not require that the d_ino field be present, and some
    systems do not provide it. */
-#define REAL_DIR_ENTRY(dp) 1
+# define REAL_DIR_ENTRY(dp) 1
 #else
-#define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
+# define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
 #endif
 
 /* Which type of backup file names are generated. */
@@ -106,7 +114,7 @@ static int version_number ();
 
 char *
 find_backup_file_name (file)
-     char *file;
+     const char *file;
 {
   char *dir;
   char *base_versions;
@@ -138,18 +146,19 @@ find_backup_file_name (file)
 
 static int
 max_backup_version (file, dir)
-     char *file, *dir;
+     const char *file;
+     const char *dir;
 {
   DIR *dirp;
   struct dirent *dp;
   int highest_version;
   int this_version;
-  int file_name_length;
-  
+  size_t file_name_length;
+
   dirp = opendir (dir);
   if (!dirp)
     return 0;
-  
+
   highest_version = 0;
   file_name_length = strlen (file);
 
@@ -157,7 +166,7 @@ max_backup_version (file, dir)
     {
       if (!REAL_DIR_ENTRY (dp) || NLENGTH (dp) <= file_name_length)
        continue;
-      
+
       this_version = version_number (file, dp->d_name, file_name_length);
       if (this_version > highest_version)
        highest_version = this_version;
@@ -172,7 +181,7 @@ max_backup_version (file, dir)
 
 static char *
 make_version_name (file, version)
-     char *file;
+     const char *file;
      int version;
 {
   char *backup_name;
@@ -190,13 +199,13 @@ make_version_name (file, version)
 
 static int
 version_number (base, backup, base_length)
-     char *base;
-     char *backup;
+     const char *base;
+     const char *backup;
      int base_length;
 {
   int version;
-  char *p;
-  
+  const char *p;
+
   version = 0;
   if (!strncmp (base, backup, base_length) && ISDIGIT (backup[base_length]))
     {
@@ -213,10 +222,11 @@ version_number (base, backup, base_length)
 
 static char *
 concat (str1, str2)
-     char *str1, *str2;
+     const char *str1;
+     const char *str2;
 {
   char *newstr;
-  char str1_length = strlen (str1);
+  int str1_length = strlen (str1);
 
   newstr = malloc (str1_length + strlen (str2) + 1);
   if (newstr == 0)