changeset 20:395da10fb137

GNU file utilities
author Jim Meyering <jim@meyering.net>
date Sun, 28 Mar 1993 19:25:12 +0000
parents 4949c6c98e20
children 05adbdfa0f87
files lib/backupfile.c lib/dirname.c lib/fnmatch.c lib/fnmatch.h lib/idcache.c lib/makepath.c lib/mountlist.c lib/savedir.c lib/strdup.c lib/stripslash.c lib/userspec.c lib/xstrdup.c
diffstat 12 files changed, 84 insertions(+), 81 deletions(-) [+]
line wrap: on
line diff
--- a/lib/backupfile.c
+++ b/lib/backupfile.c
@@ -15,14 +15,14 @@
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
-/* David MacKenzie <djm@ai.mit.edu>.
+/* David MacKenzie <djm@gnu.ai.mit.edu>.
    Some algorithms adapted from GNU Emacs. */
 
 #include <stdio.h>
 #include <ctype.h>
 #include <sys/types.h>
 #include "backupfile.h"
-#if defined(USG) || defined(STDC_HEADERS)
+#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
 #include <string.h>
 #define index strchr
 #define rindex strrchr
@@ -30,25 +30,22 @@
 #include <strings.h>
 #endif
 
-#ifdef DIRENT
+#if defined(DIRENT) || defined(_POSIX_VERSION)
 #include <dirent.h>
-#ifdef direct
-#undef direct
-#endif
-#define direct dirent
 #define NLENGTH(direct) (strlen((direct)->d_name))
-#else /* !DIRENT */
+#else /* not (DIRENT or _POSIX_VERSION) */
+#define dirent direct
 #define NLENGTH(direct) ((direct)->d_namlen)
-#ifdef USG
 #ifdef SYSNDIR
 #include <sys/ndir.h>
-#else /* !SYSNDIR */
+#endif /* SYSNDIR */
+#ifdef SYSDIR
+#include <sys/dir.h>
+#endif /* SYSDIR */
+#ifdef NDIR
 #include <ndir.h>
-#endif /* !SYSNDIR */
-#else /* !USG */
-#include <sys/dir.h>
-#endif /* !USG */
-#endif /* !DIRENT */
+#endif /* NDIR */
+#endif /* DIRENT or _POSIX_VERSION */
 
 #ifdef VOID_CLOSEDIR
 /* Fake a return value. */
@@ -138,7 +135,7 @@
      char *file, *dir;
 {
   DIR *dirp;
-  struct direct *dp;
+  struct dirent *dp;
   int highest_version;
   int this_version;
   int file_name_length;
--- a/lib/dirname.c
+++ b/lib/dirname.c
@@ -20,9 +20,11 @@
 #else
 char *malloc ();
 #endif
-#if defined(USG) || defined(STDC_HEADERS)
+#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
 #include <string.h>
+#ifndef rindex
 #define rindex strrchr
+#endif
 #else
 #include <strings.h>
 #endif
--- a/lib/fnmatch.c
+++ b/lib/fnmatch.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public License as
@@ -16,16 +16,13 @@
 Cambridge, MA 02139, USA.  */
 
 #include <errno.h>
-#include "fnmatch.h"
+#include <fnmatch.h>
+#include <ctype.h>
 
 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
 extern int errno;
 #endif
 
-#if !__STDC__
-#define const
-#endif
-
 /* Match STRING against the filename pattern PATTERN, returning zero if
    it matches, nonzero if not.  */
 int
@@ -37,40 +34,42 @@
   register const char *p = pattern, *n = string;
   register char c;
 
-  if ((flags & ~__FNM_FLAGS) != 0)
-    {
-      errno = EINVAL;
-      return -1;
-    }
+/* Note that this evalutes C many times.  */
+#define FOLD(c)	((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c))
 
   while ((c = *p++) != '\0')
     {
+      c = FOLD (c);
+
       switch (c)
 	{
 	case '?':
 	  if (*n == '\0')
 	    return FNM_NOMATCH;
-	  else if ((flags & FNM_PATHNAME) && *n == '/')
+	  else if ((flags & FNM_FILE_NAME) && *n == '/')
 	    return FNM_NOMATCH;
 	  else if ((flags & FNM_PERIOD) && *n == '.' &&
-		   (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
+		   (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
 	    return FNM_NOMATCH;
 	  break;
 
 	case '\\':
 	  if (!(flags & FNM_NOESCAPE))
-	    c = *p++;
-	  if (*n != c)
+	    {
+	      c = *p++;
+	      c = FOLD (c);
+	    }
+	  if (FOLD (*n) != c)
 	    return FNM_NOMATCH;
 	  break;
 
 	case '*':
 	  if ((flags & FNM_PERIOD) && *n == '.' &&
-	      (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
+	      (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
 	    return FNM_NOMATCH;
 
 	  for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
-	    if (((flags & FNM_PATHNAME) && *n == '/') ||
+	    if (((flags & FNM_FILE_NAME) && *n == '/') ||
 		(c == '?' && *n == '\0'))
 	      return FNM_NOMATCH;
 
@@ -78,9 +77,9 @@
 	    return 0;
 
 	  {
-	    char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
+	    char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? FOLD (*p) : c;
 	    for (--p; *n != '\0'; ++n)
-	      if ((c == '[' || *n == c1) &&
+	      if ((c == '[' || FOLD (*n) == c1) &&
 		  fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
 		return 0;
 	    return FNM_NOMATCH;
@@ -95,7 +94,7 @@
 	      return FNM_NOMATCH;
 
 	    if ((flags & FNM_PERIOD) && *n == '.' &&
-		(n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
+		(n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
 	      return FNM_NOMATCH;
 
 	    not = (*p == '!' || *p == '^');
@@ -110,13 +109,16 @@
 		if (!(flags & FNM_NOESCAPE) && c == '\\')
 		  cstart = cend = *p++;
 
+		cstart = cend = FOLD (cstart);
+
 		if (c == '\0')
 		  /* [ (unterminated) loses.  */
 		  return FNM_NOMATCH;
 
 		c = *p++;
+		c = FOLD (c);
 
-		if ((flags & FNM_PATHNAME) && c == '/')
+		if ((flags & FNM_FILE_NAME) && c == '/')
 		  /* [/] can never match.  */
 		  return FNM_NOMATCH;
 
@@ -127,10 +129,12 @@
 		      cend = *p++;
 		    if (cend == '\0')
 		      return FNM_NOMATCH;
+		    cend = FOLD (cend);
+
 		    c = *p++;
 		  }
 
-		if (*n >= cstart && *n <= cend)
+		if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
 		  goto matched;
 
 		if (c == ']')
@@ -150,7 +154,7 @@
 
 		c = *p++;
 		if (!(flags & FNM_NOESCAPE) && c == '\\')
-		  /* 1003.2d11 is unclear if this is right.  %%% */
+		  /* XXX 1003.2d11 is unclear if this is right.  */
 		  ++p;
 	      }
 	    if (not)
@@ -159,14 +163,18 @@
 	  break;
 
 	default:
-	  if (c != *n)
+	  if (c != FOLD (*n))
 	    return FNM_NOMATCH;
 	}
 
       ++n;
     }
 
-  if (*n == '\0' || ((flags & FNM_TARPATH) && *n == '/'))
+  if (*n == '\0')
+    return 0;
+
+  if ((flags & FNM_LEADING_DIR) && *n == '/')
+    /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz".  */
     return 0;
 
   return FNM_NOMATCH;
--- a/lib/fnmatch.h
+++ b/lib/fnmatch.h
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public License as
@@ -20,8 +20,7 @@
 #define	_FNMATCH_H	1
 
 #ifdef	__cplusplus
-extern "C"
-{
+extern "C" {
 #endif
 
 #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
@@ -35,14 +34,14 @@
 #endif /* C++ or ANSI C.  */
 
 /* Bits set in the FLAGS argument to `fnmatch'.  */
-#define	FNM_PATHNAME	(1 << 0)/* No wildcard can ever match `/'.  */
-#define	FNM_NOESCAPE	(1 << 1)/* Backslashes don't quote special chars.  */
-#define	FNM_PERIOD	(1 << 2)/* Leading `.' is matched only explicitly.  */
-#define	FNM_TARPATH	(1 << 4)/* Ignore `/...' after a match.  */
-#define	__FNM_FLAGS	(FNM_PATHNAME|FNM_NOESCAPE|FNM_PERIOD|FNM_TARPATH)
+#define	FNM_PATHNAME	(1 << 0) /* No wildcard can ever match `/'.  */
+#define	FNM_NOESCAPE	(1 << 1) /* Backslashes don't quote special chars.  */
+#define	FNM_PERIOD	(1 << 2) /* Leading `.' is matched only explicitly.  */
 
-#if !defined (_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 2 || defined (_BSD_SOURCE)
-#define	FNM_FILE_NAME	FNM_PATHNAME
+#if !defined (_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 2 || defined (_GNU_SOURCE)
+#define	FNM_FILE_NAME	FNM_PATHNAME /* Preferred GNU name.  */
+#define	FNM_LEADING_DIR	(1 << 3) /* Ignore `/...' after a match.  */
+#define	FNM_CASEFOLD	(1 << 4) /* Compare without regard to case.  */
 #endif
 
 /* Value returned by `fnmatch' if STRING does not match PATTERN.  */
@@ -50,12 +49,11 @@
 
 /* Match STRING against the filename pattern PATTERN,
    returning zero if it matches, FNM_NOMATCH if not.  */
-  extern int fnmatch __P ((const char *__pattern, const char *__string,
-			   int __flags));
+extern int fnmatch __P ((const char *__pattern, const char *__string,
+			 int __flags));
 
 #ifdef	__cplusplus
 }
-
 #endif
 
 #endif /* fnmatch.h */
--- a/lib/idcache.c
+++ b/lib/idcache.c
@@ -20,7 +20,7 @@
 #include <pwd.h>
 #include <grp.h>
 
-#if defined(USG) || defined(STDC_HEADERS)
+#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
 #include <string.h>
 #else
 #include <strings.h>
--- a/lib/makepath.c
+++ b/lib/makepath.c
@@ -49,7 +49,7 @@
 extern int errno;
 #endif
 
-#if defined(USG) || defined(STDC_HEADERS)
+#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
 #include <string.h>
 #define index strchr
 #else
@@ -127,7 +127,7 @@
       slash = dirpath;
       while (*slash == '/')
 	slash++;
-      while (slash = index (slash, '/'))
+      while ((slash = index (slash, '/')))
 	{
 	  *slash = '\0';
 	  if (stat (dirpath, &stats))
--- a/lib/mountlist.c
+++ b/lib/mountlist.c
@@ -24,7 +24,7 @@
 #else
 void free ();
 #endif
-#if defined(USG) || defined(STDC_HEADERS)
+#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
 #include <string.h>
 #else
 #include <strings.h>
--- a/lib/savedir.c
+++ b/lib/savedir.c
@@ -15,28 +15,30 @@
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
-/* Written by David MacKenzie <djm@ai.mit.edu>. */
+/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
 
 #include <sys/types.h>
-#ifdef DIRENT
-#include <dirent.h>
-#ifdef direct
-#undef direct
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
 #endif
-#define direct dirent
+
+#if defined(DIRENT) || defined(_POSIX_VERSION)
+#include <dirent.h>
 #define NLENGTH(direct) (strlen((direct)->d_name))
-#else
+#else /* not (DIRENT or _POSIX_VERSION) */
+#define dirent direct
 #define NLENGTH(direct) ((direct)->d_namlen)
-#ifdef USG
 #ifdef SYSNDIR
 #include <sys/ndir.h>
-#else
+#endif /* SYSNDIR */
+#ifdef SYSDIR
+#include <sys/dir.h>
+#endif /* SYSDIR */
+#ifdef NDIR
 #include <ndir.h>
-#endif
-#else
-#include <sys/dir.h>
-#endif
-#endif
+#endif /* NDIR */
+#endif /* DIRENT or _POSIX_VERSION */
 
 #ifdef VOID_CLOSEDIR
 /* Fake a return value. */
@@ -71,7 +73,7 @@
      unsigned name_size;
 {
   DIR *dirp;
-  struct direct *dp;
+  struct dirent *dp;
   char *name_space;
   char *namep;
 
--- a/lib/strdup.c
+++ b/lib/strdup.c
@@ -23,10 +23,6 @@
 char *strcpy ();
 #endif
 
-#if !__STDC__
-#define const
-#endif
-
 /* Return a newly allocated copy of STR,
    or 0 if out of memory. */
 
--- a/lib/stripslash.c
+++ b/lib/stripslash.c
@@ -15,7 +15,7 @@
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
-#if defined(STDC_HEADERS) || defined(USG)
+#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
 #include <string.h>
 #else
 #include <strings.h>
--- a/lib/userspec.c
+++ b/lib/userspec.c
@@ -22,7 +22,7 @@
 #include <pwd.h>
 #include <grp.h>
 
-#if defined(USG) || defined(STDC_HEADERS)
+#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
 #include <string.h>
 #define index strchr
 #else
--- a/lib/xstrdup.c
+++ b/lib/xstrdup.c
@@ -15,7 +15,7 @@
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
-#if defined(USG) || defined(STDC_HEADERS)
+#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
 #include <string.h>
 #else
 #include <strings.h>