X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fmodechange.c;h=4dccdb0f305a08595cd07a19a9ce497ab8284859;hb=e29fbaee8afd6fd9b6c0291562740807ca11a0c6;hp=b09661d0c5ca68da99ad8a362b45ad02f0d432e0;hpb=d87c39464604e74f580c7fae835be31a4c125c36;p=gnulib.git diff --git a/lib/modechange.c b/lib/modechange.c index b09661d0c..4dccdb0f3 100644 --- a/lib/modechange.c +++ b/lib/modechange.c @@ -1,5 +1,5 @@ /* modechange.c -- file mode manipulation - Copyright (C) 1989, 1990 Free Software Foundation, Inc. + Copyright (C) 1989, 1990, 1997, 1998 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -12,8 +12,8 @@ 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. */ /* Written by David MacKenzie */ @@ -24,22 +24,30 @@ changing the mode of many files, this probably results in a performance gain. */ +#if HAVE_CONFIG_H +# include +#endif + #include #include #include "modechange.h" -#ifdef STDC_HEADERS -#include +#if STDC_HEADERS +# include #else char *malloc (); #endif #ifndef NULL -#define NULL 0 +# define NULL 0 +#endif + +#if STAT_MACROS_BROKEN +# undef S_ISDIR #endif -#ifndef S_ISDIR -#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +#if !defined(S_ISDIR) && defined(S_IFDIR) +# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) #endif /* Return newly allocated memory to hold one element of type TYPE. */ @@ -47,7 +55,22 @@ char *malloc (); #define isodigit(c) ((c) >= '0' && (c) <= '7') -static int oatoi (); +/* Return a positive integer containing the value of the ASCII + octal number S. If S is not an octal number, return -1. */ + +static int +oatoi (const char *s) +{ + register int i; + + if (*s == 0) + return -1; + for (i = 0; isodigit (*s); ++s) + i = i * 8 + *s - '0'; + if (*s) + return -1; + return i; +} /* Return a linked list of file mode change operations created from MODE_STRING, an ASCII string that contains either an octal number @@ -63,9 +86,7 @@ static int oatoi (); return MODE_MEMORY_EXHAUSTED if there is insufficient memory. */ struct mode_change * -mode_compile (mode_string, masked_ops) - register char *mode_string; - unsigned masked_ops; +mode_compile (const char *mode_string, unsigned int masked_ops) { struct mode_change *head; /* First element of the linked list. */ struct mode_change *change; /* An element of the linked list. */ @@ -95,6 +116,9 @@ mode_compile (mode_string, masked_ops) umask (umask_value); /* Restore the old value. */ head = NULL; +#ifdef lint + change = NULL; +#endif --mode_string; /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */ @@ -225,15 +249,39 @@ invalid: return MODE_INVALID; } +/* Return a file mode change operation that sets permissions to match those + of REF_FILE. Return MODE_BAD_REFERENCE if REF_FILE can't be accessed. */ + +struct mode_change * +mode_create_from_ref (const char *ref_file) +{ + struct mode_change *change; /* the only change element */ + struct stat ref_stats; + + if (stat (ref_file, &ref_stats)) + return MODE_BAD_REFERENCE; + + change = talloc (struct mode_change); + + if (change == NULL) + return MODE_MEMORY_EXHAUSTED; + + change->op = '='; + change->flags = 0; + change->affected = 07777; + change->value = ref_stats.st_mode; + change->next = NULL; + + return change; +} + /* Return file mode OLDMODE, adjusted as indicated by the list of change operations CHANGES. If OLDMODE is a directory, the type `X' change affects it even if no execute bits were set in OLDMODE. The returned value has the S_IFMT bits cleared. */ unsigned short -mode_adjust (oldmode, changes) - unsigned oldmode; - register struct mode_change *changes; +mode_adjust (unsigned int oldmode, const struct mode_change *changes) { unsigned short newmode; /* The adjusted mode and one operand. */ unsigned short value; /* The other operand. */ @@ -298,8 +346,7 @@ mode_adjust (oldmode, changes) CHANGES. */ void -mode_free (changes) - register struct mode_change *changes; +mode_free (register struct mode_change *changes) { register struct mode_change *next; @@ -310,21 +357,3 @@ mode_free (changes) changes = next; } } - -/* Return a positive integer containing the value of the ASCII - octal number S. If S is not an octal number, return -1. */ - -static int -oatoi (s) - char *s; -{ - register int i; - - if (*s == 0) - return -1; - for (i = 0; isodigit (*s); ++s) - i = i * 8 + *s - '0'; - if (*s) - return -1; - return i; -}