-#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
-
-/* 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)
-
-/* 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. */
+/* Convert OCTAL, which uses one of the traditional octal values, to
+ an internal mode_t value. */
+static mode_t
+octal_to_mode (unsigned int octal)
+{
+ /* Help the compiler optimize the usual case where mode_t uses
+ the traditional octal representation. */
+ return ((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
+ : (mode_t) ((octal & SUID ? S_ISUID : 0)
+ | (octal & SGID ? S_ISGID : 0)
+ | (octal & SVTX ? S_ISVTX : 0)
+ | (octal & RUSR ? S_IRUSR : 0)
+ | (octal & WUSR ? S_IWUSR : 0)
+ | (octal & XUSR ? S_IXUSR : 0)
+ | (octal & RGRP ? S_IRGRP : 0)
+ | (octal & WGRP ? S_IWGRP : 0)
+ | (octal & XGRP ? S_IXGRP : 0)
+ | (octal & ROTH ? S_IROTH : 0)
+ | (octal & WOTH ? S_IWOTH : 0)
+ | (octal & XOTH ? S_IXOTH : 0)));
+}
+
+/* Special operations flags. */
+enum
+ {
+ /* For the sentinel at the end of the mode changes array. */
+ MODE_DONE,
+
+ /* The typical case. */
+ MODE_ORDINARY_CHANGE,
+
+ /* In addition to the typical case, affect the execute bits if at
+ least one execute bit is set already, or if the file is a
+ directory. */
+ MODE_X_IF_ANY_X,
+
+ /* Instead of the typical case, copy some existing permissions for
+ u, g, or o onto the other two. Which of u, g, or o is copied
+ is determined by which bits are set in the `value' field. */
+ MODE_COPY_EXISTING
+ };
+
+/* Description of a mode change. */
+struct mode_change
+{
+ char op; /* One of "=+-". */
+ char flag; /* Special operations flag. */
+ mode_t affected; /* Set for u, g, o, or a. */
+ mode_t value; /* Bits to add/remove. */
+ mode_t mentioned; /* Bits explicitly mentioned. */
+};
+
+/* Return a mode_change array with the specified `=ddd'-style
+ mode change operation, where NEW_MODE is `ddd' and MENTIONED
+ contains the bits explicitly mentioned in the mode are MENTIONED. */