home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / find12as.zip / MODECHAN.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  10KB  |  345 lines

  1. /* modechange.c -- file mode manipulation
  2.    Copyright (C) 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie <djm@ai.mit.edu> */
  19.  
  20. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  21.    This port is also distributed under the terms of the
  22.    GNU General Public License as published by the
  23.    Free Software Foundation.
  24.  
  25.    Please note that this file is not identical to the
  26.    original GNU release, you should have received this
  27.    code as patch to the official release.
  28.  
  29.    $Header: e:/gnu/find/RCS/modechan.c 1.2.0.3 90/09/23 16:09:43 tho Exp $
  30.  */
  31.  
  32. /* The ASCII mode string is compiled into a linked list of `struct
  33.    modechange', which can then be applied to each file to be changed.
  34.    We do this instead of re-parsing the ASCII string for each file
  35.    because the compiled form requires less computation to use; when
  36.    changing the mode of many files, this probably results in a
  37.    performance gain. */
  38.  
  39. #include <sys/types.h>
  40. #include <sys/stat.h>
  41. #include "modechange.h"
  42.  
  43. #ifdef STDC_HEADERS
  44. #include <stdlib.h>
  45. #else
  46. char *malloc ();
  47.  
  48. #ifndef NULL
  49. #define NULL 0
  50. #endif
  51. #endif
  52.  
  53. #ifdef MSDOS
  54. #include <stdio.h>
  55. #include <io.h>
  56. static    int oatoi (char *s);
  57. #endif /* MSDOS */
  58.  
  59. /* Return newly allocated memory to hold one element of type TYPE. */
  60. #define talloc(type) ((type *) malloc (sizeof (type)))
  61.  
  62. #define isodigit(c) ((c) >= '0' && (c) <= '7')
  63.  
  64. static int oatoi ();
  65.  
  66. /* Return a linked list of file mode change operations created from
  67.    MODE_STRING, an ASCII string that contains either an octal number
  68.    specifying an absolute mode, or symbolic mode change operations with
  69.    the form:
  70.    [ugoa...][[+-=][rwxXstugo...]...][,...]
  71.    MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
  72.    should not affect bits set in the umask when no users are given.
  73.    Operators not selected in MASKED_OPS ignore the umask.
  74.  
  75.    Return MODE_INVALID if `mode_string' does not contain a valid
  76.    representation of file mode change operations;
  77.    return MODE_MEMORY_EXHAUSTED if there is insufficient memory. */
  78.  
  79. struct mode_change *
  80. mode_compile (mode_string, masked_ops)
  81.      register char *mode_string;
  82.      unsigned masked_ops;
  83. {
  84.   struct mode_change *head;    /* First element of the linked list. */
  85.   struct mode_change *change;    /* An element of the linked list. */
  86.   int i;            /* General purpose temporary. */
  87.   int umask_value;        /* The umask value (surprise). */
  88.   unsigned short affected_bits;    /* Which bits in the mode are operated on. */
  89.   unsigned short affected_masked; /* `affected_bits' modified by umask. */
  90.   unsigned ops_to_mask;        /* Operators to actually use umask on. */
  91.  
  92.   i = oatoi (mode_string);
  93.   if (i >= 0)
  94.     {
  95.       if (i > 07777)
  96.     return MODE_INVALID;
  97.       head = talloc (struct mode_change);
  98.       if (head == NULL)
  99.     return MODE_MEMORY_EXHAUSTED;
  100.       head->next = NULL;
  101.       head->op = '=';
  102.       head->flags = 0;
  103.       head->value = i;
  104.       head->affected = 07777;    /* Affect all permissions. */
  105.       return head;
  106.     }
  107.  
  108.   umask_value = umask (0);
  109.   umask (umask_value);        /* Restore the old value. */
  110.  
  111.   head = NULL;
  112.   --mode_string;
  113.  
  114.   /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
  115.   do
  116.     {
  117.       affected_bits = 0;
  118.       ops_to_mask = 0;
  119.       /* Turn on all the bits in `affected_bits' for each group given. */
  120.       for (++mode_string;; ++mode_string)
  121.     switch (*mode_string)
  122.       {
  123.       case 'u':
  124.         affected_bits |= 04700;
  125.         break;
  126.       case 'g':
  127.         affected_bits |= 02070;
  128.         break;
  129.       case 'o':
  130.         affected_bits |= 01007;
  131.         break;
  132.       case 'a':
  133.         affected_bits |= 07777;
  134.         break;
  135.       default:
  136.         goto no_more_affected;
  137.       }
  138.  
  139.     no_more_affected:
  140.       /* If none specified, affect all bits, except perhaps those
  141.      set in the umask. */
  142.       if (affected_bits == 0)
  143.     {
  144.       affected_bits = 07777;
  145.       ops_to_mask = masked_ops;
  146.     }
  147.  
  148.       while (*mode_string == '=' || *mode_string == '+' || *mode_string == '-')
  149.     {
  150.       /* Add the element to the tail of the list, so the operations
  151.          are performed in the correct order. */
  152.       if (head == NULL)
  153.         {
  154.           head = talloc (struct mode_change);
  155.           if (head == NULL)
  156.         return MODE_MEMORY_EXHAUSTED;
  157.           change = head;
  158.         }
  159.       else
  160.         {
  161.           change->next = talloc (struct mode_change);
  162.           if (change->next == NULL)
  163.         {
  164.           mode_free (change);
  165.           return MODE_MEMORY_EXHAUSTED;
  166.         }
  167.           change = change->next;
  168.         }
  169.  
  170.       change->next = NULL;
  171.       change->op = *mode_string;    /* One of "=+-". */
  172.       affected_masked = affected_bits;
  173.       if (ops_to_mask & (*mode_string == '=' ? MODE_MASK_EQUALS
  174.                  : *mode_string == '+' ? MODE_MASK_PLUS
  175.                  : MODE_MASK_MINUS))
  176.         affected_masked &= ~umask_value;
  177.       change->affected = affected_masked;
  178.       change->value = 0;
  179.       change->flags = 0;
  180.  
  181.       /* Set `value' according to the bits set in `affected_masked'. */
  182.       for (++mode_string;; ++mode_string)
  183.         switch (*mode_string)
  184.           {
  185.           case 'r':
  186.         change->value |= 00444 & affected_masked;
  187.         break;
  188.           case 'w':
  189.         change->value |= 00222 & affected_masked;
  190.         break;
  191.           case 'X':
  192.         change->flags |= MODE_X_IF_ANY_X;
  193.         /* Fall through. */
  194.           case 'x':
  195.         change->value |= 00111 & affected_masked;
  196.         break;
  197.           case 's':
  198.         /* Set the setuid/gid bits if `u' or `g' is selected. */
  199.         change->value |= 06000 & affected_masked;
  200.         break;
  201.           case 't':
  202.         /* Set the "save text image" bit if `o' is selected. */
  203.         change->value |= 01000 & affected_masked;
  204.         break;
  205.           case 'u':
  206.         /* Set the affected bits to the value of the `u' bits
  207.            on the same file.  */
  208.         if (change->value)
  209.           goto invalid;
  210.         change->value = 00700;
  211.         change->flags |= MODE_COPY_EXISTING;
  212.         break;
  213.           case 'g':
  214.         /* Set the affected bits to the value of the `g' bits
  215.            on the same file.  */
  216.         if (change->value)
  217.           goto invalid;
  218.         change->value = 00070;
  219.         change->flags |= MODE_COPY_EXISTING;
  220.         break;
  221.           case 'o':
  222.         /* Set the affected bits to the value of the `o' bits
  223.            on the same file.  */
  224.         if (change->value)
  225.           goto invalid;
  226.         change->value = 00007;
  227.         change->flags |= MODE_COPY_EXISTING;
  228.         break;
  229.           default:
  230.         goto no_more_values;
  231.           }
  232.     no_more_values:;
  233.     }
  234.   } while (*mode_string == ',');
  235.   if (*mode_string == 0)
  236.     return head;
  237. invalid:
  238.   mode_free (head);
  239.   return MODE_INVALID;
  240. }
  241.  
  242. /* Return file mode OLDMODE, adjusted as indicated by the list of change
  243.    operations CHANGES.  If OLDMODE has the S_IFDIR bit set, the type `X'
  244.    change affects it even if no execute bits were set in OLDMODE.
  245.    The returned value has the S_IFMT bits cleared. */
  246.  
  247. unsigned short
  248. mode_adjust (oldmode, changes)
  249.      unsigned oldmode;
  250.      register struct mode_change *changes;
  251. {
  252.   unsigned short newmode;    /* The adjusted mode and one operand. */
  253.   unsigned short value;        /* The other operand. */
  254.  
  255.   newmode = oldmode & 07777;
  256.  
  257.   for (; changes; changes = changes->next)
  258.     {
  259.       if (changes->flags & MODE_COPY_EXISTING)
  260.     {
  261.       /* Isolate in `value' the bits in `newmode' to copy, given in
  262.          the mask `changes->value'. */
  263.       value = newmode & changes->value;
  264.  
  265.       if (changes->value & 00700)
  266.         /* Copy `u' permissions onto `g' and `o'. */
  267.         value |= (value >> 3) | (value >> 6);
  268.       else if (changes->value & 00070)
  269.         /* Copy `g' permissions onto `u' and `o'. */
  270.         value |= (value << 3) | (value >> 3);
  271.       else
  272.         /* Copy `o' permissions onto `u' and `g'. */
  273.         value |= (value << 3) | (value << 6);
  274.  
  275.       /* In order to change only `u', `g', or `o' permissions,
  276.          or some combination thereof, clear unselected bits.
  277.          This can not be done in mode_compile because the value
  278.          to which the `changes->affected' mask is applied depends
  279.          on the old mode of each file. */
  280.       value &= changes->affected;
  281.     }
  282.       else
  283.     {
  284.       value = changes->value;
  285.       /* If `X', do not affect the execute bits if the file is not a
  286.          directory and no execute bits are already set. */
  287.       if ((changes->flags & MODE_X_IF_ANY_X)
  288.           && (oldmode & S_IFMT) != S_IFDIR
  289.           && (newmode & 00111) == 0)
  290.         value &= ~00111;    /* Clear the execute bits. */
  291.     }
  292.  
  293.       switch (changes->op)
  294.     {
  295.     case '=':
  296.       /* Preserve the previous values in `newmode' of bits that are
  297.          not affected by this change operation. */
  298.       newmode = (newmode & ~changes->affected) | value;
  299.       break;
  300.     case '+':
  301.       newmode |= value;
  302.       break;
  303.     case '-':
  304.       newmode &= ~value;
  305.       break;
  306.     }
  307.     }
  308.   return newmode;
  309. }
  310.  
  311. /* Free the memory used by the list of file mode change operations
  312.    CHANGES. */
  313.  
  314. void
  315. mode_free (changes)
  316.      register struct mode_change *changes;
  317. {
  318.   register struct mode_change *next;
  319.  
  320.   while (changes)
  321.     {
  322.       next = changes->next;
  323.       free (changes);
  324.       changes = next;
  325.     }
  326. }
  327.  
  328. /* Return a positive integer containing the value of the ASCII
  329.    octal number S.  If S is not an octal number, return -1.  */
  330.  
  331. static int
  332. oatoi (s)
  333.      char *s;
  334. {
  335.   register int i;
  336.  
  337.   if (*s == 0)
  338.     return -1;
  339.   for (i = 0; isodigit (*s); ++s)
  340.     i = i * 8 + *s - '0';
  341.   if (*s)
  342.     return -1;
  343.   return i;
  344. }
  345.