From 07fc05974df6ec335447f994e9b1df774ab364c3 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Sun, 28 Mar 1993 19:25:12 +0000 Subject: GNU file utilities --- lib/backupfile.c | 29 +++++++++++++--------------- lib/dirname.c | 4 +++- lib/fnmatch.c | 58 ++++++++++++++++++++++++++++++++------------------------ lib/fnmatch.h | 26 ++++++++++++------------- lib/idcache.c | 2 +- lib/makepath.c | 4 ++-- lib/mountlist.c | 2 +- lib/savedir.c | 32 ++++++++++++++++--------------- lib/strdup.c | 4 ---- lib/stripslash.c | 2 +- lib/userspec.c | 2 +- lib/xstrdup.c | 2 +- 12 files changed, 85 insertions(+), 82 deletions(-) diff --git a/lib/backupfile.c b/lib/backupfile.c index c6d7914a67..94bb55a269 100644 --- 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 . +/* David MacKenzie . Some algorithms adapted from GNU Emacs. */ #include #include #include #include "backupfile.h" -#if defined(USG) || defined(STDC_HEADERS) +#if defined(STDC_HEADERS) || defined(HAVE_STRING_H) #include #define index strchr #define rindex strrchr @@ -30,25 +30,22 @@ #include #endif -#ifdef DIRENT +#if defined(DIRENT) || defined(_POSIX_VERSION) #include -#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 -#else /* !SYSNDIR */ -#include -#endif /* !SYSNDIR */ -#else /* !USG */ +#endif /* SYSNDIR */ +#ifdef SYSDIR #include -#endif /* !USG */ -#endif /* !DIRENT */ +#endif /* SYSDIR */ +#ifdef NDIR +#include +#endif /* NDIR */ +#endif /* DIRENT or _POSIX_VERSION */ #ifdef VOID_CLOSEDIR /* Fake a return value. */ @@ -138,7 +135,7 @@ max_backup_version (file, dir) char *file, *dir; { DIR *dirp; - struct direct *dp; + struct dirent *dp; int highest_version; int this_version; int file_name_length; diff --git a/lib/dirname.c b/lib/dirname.c index 82deea7b4a..5a92ce557f 100644 --- 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 +#ifndef rindex #define rindex strrchr +#endif #else #include #endif diff --git a/lib/fnmatch.c b/lib/fnmatch.c index 525e6a85e8..be662d9d25 100644 --- 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 @@ not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include -#include "fnmatch.h" +#include +#include #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 @@ fnmatch (pattern, string, flags) 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 @@ fnmatch (pattern, string, flags) 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 @@ fnmatch (pattern, string, flags) 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 @@ fnmatch (pattern, string, flags) 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 @@ fnmatch (pattern, string, flags) 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 @@ fnmatch (pattern, string, flags) 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 @@ fnmatch (pattern, string, flags) 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; diff --git a/lib/fnmatch.h b/lib/fnmatch.h index 0b3588a6e0..2289f6345d 100644 --- 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 @@ Cambridge, MA 02139, USA. */ #define _FNMATCH_H 1 #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif #if defined (__cplusplus) || (defined (__STDC__) && __STDC__) @@ -35,14 +34,14 @@ extern "C" #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) - -#if !defined (_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 2 || defined (_BSD_SOURCE) -#define FNM_FILE_NAME FNM_PATHNAME +#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 (_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 @@ extern "C" /* 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 */ diff --git a/lib/idcache.c b/lib/idcache.c index 3e5f134ae7..dd9c366b16 100644 --- a/lib/idcache.c +++ b/lib/idcache.c @@ -20,7 +20,7 @@ #include #include -#if defined(USG) || defined(STDC_HEADERS) +#if defined(STDC_HEADERS) || defined(HAVE_STRING_H) #include #else #include diff --git a/lib/makepath.c b/lib/makepath.c index 5c6112414c..12da458cf2 100644 --- a/lib/makepath.c +++ b/lib/makepath.c @@ -49,7 +49,7 @@ char *alloca (); extern int errno; #endif -#if defined(USG) || defined(STDC_HEADERS) +#if defined(STDC_HEADERS) || defined(HAVE_STRING_H) #include #define index strchr #else @@ -127,7 +127,7 @@ make_path (argpath, mode, parent_mode, owner, group, verbose_fmt_string) slash = dirpath; while (*slash == '/') slash++; - while (slash = index (slash, '/')) + while ((slash = index (slash, '/'))) { *slash = '\0'; if (stat (dirpath, &stats)) diff --git a/lib/mountlist.c b/lib/mountlist.c index 78955f709d..88fda7a615 100644 --- 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 #else #include diff --git a/lib/savedir.c b/lib/savedir.c index fce61da0d6..d89adc0a09 100644 --- 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 . */ +/* Written by David MacKenzie . */ #include -#ifdef DIRENT -#include -#ifdef direct -#undef direct + +#ifdef HAVE_UNISTD_H +#include #endif -#define direct dirent + +#if defined(DIRENT) || defined(_POSIX_VERSION) +#include #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 -#else -#include -#endif -#else +#endif /* SYSNDIR */ +#ifdef SYSDIR #include -#endif -#endif +#endif /* SYSDIR */ +#ifdef NDIR +#include +#endif /* NDIR */ +#endif /* DIRENT or _POSIX_VERSION */ #ifdef VOID_CLOSEDIR /* Fake a return value. */ @@ -71,7 +73,7 @@ savedir (dir, name_size) unsigned name_size; { DIR *dirp; - struct direct *dp; + struct dirent *dp; char *name_space; char *namep; diff --git a/lib/strdup.c b/lib/strdup.c index 078c69b6eb..b137db2d95 100644 --- a/lib/strdup.c +++ b/lib/strdup.c @@ -23,10 +23,6 @@ char *malloc (); char *strcpy (); #endif -#if !__STDC__ -#define const -#endif - /* Return a newly allocated copy of STR, or 0 if out of memory. */ diff --git a/lib/stripslash.c b/lib/stripslash.c index 802204f25f..2971d4ced0 100644 --- 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 #else #include diff --git a/lib/userspec.c b/lib/userspec.c index 60c6ecc1aa..a417cca4ce 100644 --- a/lib/userspec.c +++ b/lib/userspec.c @@ -22,7 +22,7 @@ #include #include -#if defined(USG) || defined(STDC_HEADERS) +#if defined(STDC_HEADERS) || defined(HAVE_STRING_H) #include #define index strchr #else diff --git a/lib/xstrdup.c b/lib/xstrdup.c index da39db3e37..9588bc78d1 100644 --- 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 #else #include -- cgit v1.2.1