* mktime.c (not_equal_tm): Remove redundant check.
[gnulib.git] / lib / modechange.c
index cbb1bb9..991235b 100644 (file)
@@ -1,5 +1,7 @@
 /* modechange.c -- file mode manipulation
-   Copyright (C) 1989, 1990, 1997, 1998, 1999 Free Software Foundation, Inc.
+
+   Copyright (C) 1989, 1990, 1997, 1998, 1999, 2001, 2003, 2004 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
 # include <config.h>
 #endif
 
-#include <sys/types.h>
-#include <sys/stat.h>
 #include "modechange.h"
-
-#if STDC_HEADERS
-# include <stdlib.h>
-#else
-char *malloc ();
-#endif
-
-#ifndef NULL
-# define NULL 0
-#endif
+#include <sys/stat.h>
+#include "xstrtol.h"
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdlib.h>
 
 #if STAT_MACROS_BROKEN
 # undef S_ISDIR
@@ -50,34 +45,80 @@ char *malloc ();
 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
 #endif
 
-/* Return newly allocated memory to hold one element of type TYPE. */
-#define talloc(type) ((type *) malloc (sizeof (type)))
-
-#define isodigit(c) ((c) >= '0' && (c) <= '7')
+/* The traditional octal values corresponding to each mode bit.  */
+#define SUID 04000
+#define SGID 02000
+#define SVTX 01000
+#define RUSR 00400
+#define WUSR 00200
+#define XUSR 00100
+#define RGRP 00040
+#define WGRP 00020
+#define XGRP 00010
+#define ROTH 00004
+#define WOTH 00002
+#define XOTH 00001
+#define ALLM 07777 /* all octal mode bits */
+
+#ifndef S_ISUID
+# define S_ISUID SUID
+#endif
+#ifndef S_ISGID
+# define S_ISGID SGID
+#endif
+#ifndef S_ISVTX
+# define S_ISVTX SVTX
+#endif
+#ifndef S_IRUSR
+# define S_IRUSR RUSR
+#endif
+#ifndef S_IWUSR
+# define S_IWUSR WUSR
+#endif
+#ifndef S_IXUSR
+# define S_IXUSR XUSR
+#endif
+#ifndef S_IRGRP
+# define S_IRGRP RGRP
+#endif
+#ifndef S_IWGRP
+# define S_IWGRP WGRP
+#endif
+#ifndef S_IXGRP
+# define S_IXGRP XGRP
+#endif
+#ifndef S_IROTH
+# define S_IROTH ROTH
+#endif
+#ifndef S_IWOTH
+# define S_IWOTH WOTH
+#endif
+#ifndef S_IXOTH
+# define S_IXOTH XOTH
+#endif
+#ifndef S_IRWXU
+# define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
+#endif
+#ifndef S_IRWXG
+# define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
+#endif
+#ifndef S_IRWXO
+# define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
+#endif
 
-/* Return a positive integer containing the value of the ASCII
-   octal number S.  If S is not an octal number, return -1.  */
+/* All the mode bits that can be affected by chmod.  */
+#define CHMOD_MODE_BITS \
+  (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
 
-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 newly allocated memory to hold one element of type TYPE. */
+#define talloc(type) ((type *) malloc (sizeof (type)))
 
 /* Create a mode_change entry with the specified `=ddd'-style
    mode change operation, where NEW_MODE is `ddd'.  Return the
    new entry, or NULL upon failure.  */
 
 static struct mode_change *
-make_node_op_equals (int new_mode)
+make_node_op_equals (mode_t new_mode)
 {
   struct mode_change *p;
   p = talloc (struct mode_change);
@@ -87,7 +128,7 @@ make_node_op_equals (int new_mode)
   p->op = '=';
   p->flags = 0;
   p->value = new_mode;
-  p->affected = 07777; /* Affect all permissions. */
+  p->affected = CHMOD_MODE_BITS;       /* Affect all permissions. */
   return p;
 }
 
@@ -126,21 +167,42 @@ mode_compile (const char *mode_string, unsigned int masked_ops)
 {
   struct mode_change *head;    /* First element of the linked list. */
   struct mode_change *tail;    /* An element of the linked list. */
-  int i;                       /* General purpose temporary. */
-  int umask_value;             /* The umask value (surprise). */
+  unsigned long octal_value;   /* The mode value, if octal.  */
+  mode_t umask_value;          /* The umask value (surprise). */
 
   head = NULL;
 #ifdef lint
   tail = NULL;
 #endif
 
-  i = oatoi (mode_string);
-  if (i >= 0)
+  if (xstrtoul (mode_string, NULL, 8, &octal_value, "") == LONGINT_OK)
     {
       struct mode_change *p;
-      if (i > 07777)
+      mode_t mode;
+      if (octal_value != (octal_value & ALLM))
        return MODE_INVALID;
-      p = make_node_op_equals (i);
+
+      /* Help the compiler optimize the usual case where mode_t uses
+        the traditional octal representation.  */
+      mode = ((S_ISUID == SUID && S_ISGID == SGID && S_ISVTX == SVTX
+              && S_IRUSR == RUSR && S_IWUSR == WUSR && S_IXUSR == XUSR
+              && S_IRGRP == RGRP && S_IWGRP == WGRP && S_IXGRP == XGRP
+              && S_IROTH == ROTH && S_IWOTH == WOTH && S_IXOTH == XOTH)
+             ? octal_value
+             : (mode_t) ((octal_value & SUID ? S_ISUID : 0)
+                         | (octal_value & SGID ? S_ISGID : 0)
+                         | (octal_value & SVTX ? S_ISVTX : 0)
+                         | (octal_value & RUSR ? S_IRUSR : 0)
+                         | (octal_value & WUSR ? S_IWUSR : 0)
+                         | (octal_value & XUSR ? S_IXUSR : 0)
+                         | (octal_value & RGRP ? S_IRGRP : 0)
+                         | (octal_value & WGRP ? S_IWGRP : 0)
+                         | (octal_value & XGRP ? S_IXGRP : 0)
+                         | (octal_value & ROTH ? S_IROTH : 0)
+                         | (octal_value & WOTH ? S_IWOTH : 0)
+                         | (octal_value & XOTH ? S_IXOTH : 0)));
+
+      p = make_node_op_equals (mode);
       if (p == NULL)
        return MODE_MEMORY_EXHAUSTED;
       mode_append_entry (&head, &tail, p);
@@ -149,37 +211,34 @@ mode_compile (const char *mode_string, unsigned int masked_ops)
 
   umask_value = umask (0);
   umask (umask_value);         /* Restore the old value. */
-  --mode_string;
 
   /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
-  do
+  for (;; mode_string++)
     {
       /* Which bits in the mode are operated on. */
-      unsigned short affected_bits = 0;
+      mode_t affected_bits = 0;
       /* `affected_bits' modified by umask. */
-      unsigned short affected_masked;
+      mode_t affected_masked;
       /* Operators to actually use umask on. */
-      unsigned ops_to_mask = 0;
+      unsigned int ops_to_mask = 0;
 
-      int who_specified_p;
+      bool who_specified_p;
 
-      affected_bits = 0;
-      ops_to_mask = 0;
       /* Turn on all the bits in `affected_bits' for each group given. */
-      for (++mode_string;; ++mode_string)
+      for (;; mode_string++)
        switch (*mode_string)
          {
          case 'u':
-           affected_bits |= 04700;
+           affected_bits |= S_ISUID | S_IRWXU;
            break;
          case 'g':
-           affected_bits |= 02070;
+           affected_bits |= S_ISGID | S_IRWXG;
            break;
          case 'o':
-           affected_bits |= 01007;
+           affected_bits |= S_ISVTX | S_IRWXO;
            break;
          case 'a':
-           affected_bits |= 07777;
+           affected_bits |= CHMOD_MODE_BITS;
            break;
          default:
            goto no_more_affected;
@@ -189,11 +248,11 @@ mode_compile (const char *mode_string, unsigned int masked_ops)
       /* If none specified, affect all bits, except perhaps those
         set in the umask. */
       if (affected_bits)
-       who_specified_p = 1;
+       who_specified_p = true;
       else
        {
-         who_specified_p = 0;
-         affected_bits = 07777;
+         who_specified_p = false;
+         affected_bits = CHMOD_MODE_BITS;
          ops_to_mask = masked_ops;
        }
 
@@ -238,31 +297,34 @@ mode_compile (const char *mode_string, unsigned int masked_ops)
            switch (*mode_string)
              {
              case 'r':
-               change->value |= 00444 & affected_masked;
+               change->value |= ((S_IRUSR | S_IRGRP | S_IROTH)
+                                 & affected_masked);
                break;
              case 'w':
-               change->value |= 00222 & affected_masked;
+               change->value |= ((S_IWUSR | S_IWGRP | S_IWOTH)
+                                 & affected_masked);
                break;
              case 'X':
                change->flags |= MODE_X_IF_ANY_X;
                /* Fall through. */
              case 'x':
-               change->value |= 00111 & affected_masked;
+               change->value |= ((S_IXUSR | S_IXGRP | S_IXOTH)
+                                 & affected_masked);
                break;
              case 's':
                /* Set the setuid/gid bits if `u' or `g' is selected. */
-               change->value |= 06000 & affected_masked;
+               change->value |= (S_ISUID | S_ISGID) & affected_masked;
                break;
              case 't':
                /* Set the "save text image" bit if `o' is selected. */
-               change->value |= 01000 & affected_masked;
+               change->value |= S_ISVTX & affected_masked;
                break;
              case 'u':
                /* Set the affected bits to the value of the `u' bits
                   on the same file.  */
                if (change->value)
                  goto invalid;
-               change->value = 00700;
+               change->value = S_IRWXU;
                change->flags |= MODE_COPY_EXISTING;
                break;
              case 'g':
@@ -270,7 +332,7 @@ mode_compile (const char *mode_string, unsigned int masked_ops)
                   on the same file.  */
                if (change->value)
                  goto invalid;
-               change->value = 00070;
+               change->value = S_IRWXG;
                change->flags |= MODE_COPY_EXISTING;
                break;
              case 'o':
@@ -278,7 +340,7 @@ mode_compile (const char *mode_string, unsigned int masked_ops)
                   on the same file.  */
                if (change->value)
                  goto invalid;
-               change->value = 00007;
+               change->value = S_IRWXO;
                change->flags |= MODE_COPY_EXISTING;
                break;
              default:
@@ -286,7 +348,11 @@ mode_compile (const char *mode_string, unsigned int masked_ops)
              }
        no_more_values:;
        }
-  } while (*mode_string == ',');
+
+      if (*mode_string != ',')
+       break;
+    }
+
   if (*mode_string == 0)
     return head;
 invalid:
@@ -313,7 +379,7 @@ mode_create_from_ref (const char *ref_file)
 
   change->op = '=';
   change->flags = 0;
-  change->affected = 07777;
+  change->affected = CHMOD_MODE_BITS;
   change->value = ref_stats.st_mode;
   change->next = NULL;
 
@@ -325,13 +391,13 @@ mode_create_from_ref (const char *ref_file)
    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 (unsigned int oldmode, const struct mode_change *changes)
+mode_t
+mode_adjust (mode_t oldmode, const struct mode_change *changes)
 {
-  unsigned short newmode;      /* The adjusted mode and one operand. */
-  unsigned short value;                /* The other operand. */
+  mode_t newmode;      /* The adjusted mode and one operand. */
+  mode_t value;                /* The other operand. */
 
-  newmode = oldmode & 07777;
+  newmode = oldmode & CHMOD_MODE_BITS;
 
   for (; changes; changes = changes->next)
     {
@@ -341,19 +407,25 @@ mode_adjust (unsigned int oldmode, const struct mode_change *changes)
             the mask `changes->value'. */
          value = newmode & changes->value;
 
-         if (changes->value & 00700)
+         if (changes->value & S_IRWXU)
            /* Copy `u' permissions onto `g' and `o'. */
-           value |= (value >> 3) | (value >> 6);
-         else if (changes->value & 00070)
+           value |= (  (value & S_IRUSR ? S_IRGRP | S_IROTH : 0)
+                     | (value & S_IWUSR ? S_IWGRP | S_IWOTH : 0)
+                     | (value & S_IXUSR ? S_IXGRP | S_IXOTH : 0));
+         else if (changes->value & S_IRWXG)
            /* Copy `g' permissions onto `u' and `o'. */
-           value |= (value << 3) | (value >> 3);
+           value |= (  (value & S_IRGRP ? S_IRUSR | S_IROTH : 0)
+                     | (value & S_IWGRP ? S_IWUSR | S_IWOTH : 0)
+                     | (value & S_IXGRP ? S_IXUSR | S_IXOTH : 0));
          else
            /* Copy `o' permissions onto `u' and `g'. */
-           value |= (value << 3) | (value << 6);
+           value |= (  (value & S_IROTH ? S_IRUSR | S_IRGRP : 0)
+                     | (value & S_IWOTH ? S_IWUSR | S_IWGRP : 0)
+                     | (value & S_IXOTH ? S_IXUSR | S_IXGRP : 0));
 
          /* In order to change only `u', `g', or `o' permissions,
             or some combination thereof, clear unselected bits.
-            This can not be done in mode_compile because the value
+            This cannot be done in mode_compile because the value
             to which the `changes->affected' mask is applied depends
             on the old mode of each file. */
          value &= changes->affected;
@@ -365,8 +437,9 @@ mode_adjust (unsigned int oldmode, const struct mode_change *changes)
             directory and no execute bits are already set. */
          if ((changes->flags & MODE_X_IF_ANY_X)
              && !S_ISDIR (oldmode)
-             && (newmode & 00111) == 0)
-           value &= ~00111;    /* Clear the execute bits. */
+             && (newmode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
+           /* Clear the execute bits. */
+           value &= ~ (S_IXUSR | S_IXGRP | S_IXOTH);
        }
 
       switch (changes->op)