home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fileutils-3.6 / src / chmod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-14  |  7.1 KB  |  303 lines

  1. /* chmod -- change permission modes of files
  2.    Copyright (C) 1989, 1990, 1991 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 2, 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. /* Options:
  19.    -R    Recursively change modes of directory contents.
  20.    -c    Verbosely describe only files whose modes actually change.
  21.    -f    Do not print error messages about files.
  22.    -v    Verbosely describe changed modes.
  23.  
  24.    David MacKenzie <djm@gnu.ai.mit.edu> */
  25.  
  26. #include <stdio.h>
  27. #include <getopt.h>
  28. #include <sys/types.h>
  29. #include "modechange.h"
  30. #include "system.h"
  31. #include "version.h"
  32.  
  33. int lstat ();
  34.  
  35. char *savedir ();
  36. char *xmalloc ();
  37. char *xrealloc ();
  38. void error ();
  39. void mode_string ();
  40.  
  41. static int change_file_mode ();
  42. static int change_dir_mode ();
  43. static void describe_change ();
  44. static void usage ();
  45.  
  46. /* The name the program was run with. */
  47. char *program_name;
  48.  
  49. /* If nonzero, change the modes of directories recursively. */
  50. static int recurse;
  51.  
  52. /* If nonzero, force silence (no error messages). */
  53. static int force_silent;
  54.  
  55. /* If nonzero, describe the modes we set. */
  56. static int verbose;
  57.  
  58. /* If nonzero, describe only modes that change. */
  59. static int changes_only;
  60.  
  61. /* If non-zero, display usage information and exit.  */
  62. static int flag_help;
  63.  
  64. /* If non-zero, print the version on standard error.  */
  65. static int flag_version;
  66.  
  67. static struct option const long_options[] =
  68. {
  69.   {"recursive", no_argument, 0, 'R'},
  70.   {"changes", no_argument, 0, 'c'},
  71.   {"silent", no_argument, 0, 'f'},
  72.   {"quiet", no_argument, 0, 'f'},
  73.   {"verbose", no_argument, 0, 'v'},
  74.   {"help", no_argument, &flag_help, 1},
  75.   {"version", no_argument, &flag_version, 1},
  76.   {0, 0, 0, 0}
  77. };
  78.  
  79. /* Parse the ASCII mode given on the command line into a linked list
  80.    of `struct mode_change' and apply that to each file argument. */
  81.  
  82. void
  83. main (argc, argv)
  84.      int argc;
  85.      char **argv;
  86. {
  87.   struct mode_change *changes;
  88.   int errors = 0;
  89.   int modeind = 0;        /* Index of the mode argument in `argv'. */
  90.   int thisind;
  91.   int c;
  92.  
  93.   program_name = argv[0];
  94.   recurse = force_silent = verbose = changes_only = 0;
  95.  
  96.   while (1)
  97.     {
  98.       thisind = optind ? optind : 1;
  99.  
  100.       c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options,
  101.                (int *) 0);
  102.       if (c == EOF)
  103.     break;
  104.  
  105.       switch (c)
  106.     {
  107.     case 0:
  108.       break;
  109.     case 'r':
  110.     case 'w':
  111.     case 'x':
  112.     case 'X':
  113.     case 's':
  114.     case 't':
  115.     case 'u':
  116.     case 'g':
  117.     case 'o':
  118.     case 'a':
  119.     case ',':
  120.     case '+':
  121.     case '-':
  122.     case '=':
  123.       if (modeind != 0 && modeind != thisind)
  124.         error (1, 0, "invalid mode");
  125.       modeind = thisind;
  126.       break;
  127.     case 'R':
  128.       recurse = 1;
  129.       break;
  130.     case 'c':
  131.       verbose = 1;
  132.       changes_only = 1;
  133.       break;
  134.     case 'f':
  135.       force_silent = 1;
  136.       break;
  137.     case 'v':
  138.       verbose = 1;
  139.       break;
  140.     default:
  141.       usage ();
  142.     }
  143.     }
  144.  
  145.   if (flag_version)
  146.     {
  147.       fprintf (stderr, "%s\n", version_string);
  148.       exit (0);
  149.     }
  150.  
  151.   if (flag_help)
  152.     usage ();
  153.  
  154.   if (modeind == 0)
  155.     modeind = optind++;
  156.  
  157.   if (optind >= argc)
  158.     usage ();
  159.  
  160.   changes = mode_compile (argv[modeind],
  161.               MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
  162.   if (changes == MODE_INVALID)
  163.     error (1, 0, "invalid mode");
  164.   else if (changes == MODE_MEMORY_EXHAUSTED)
  165.     error (1, 0, "virtual memory exhausted");
  166.  
  167.   for (; optind < argc; ++optind)
  168.     errors |= change_file_mode (argv[optind], changes);
  169.  
  170.   exit (errors);
  171. }
  172.  
  173. /* Change the mode of FILE according to the list of operations CHANGES.
  174.    Return 0 if successful, 1 if errors occurred. */
  175.  
  176. static int
  177. change_file_mode (file, changes)
  178.      char *file;
  179.      struct mode_change *changes;
  180. {
  181.   struct stat file_stats;
  182.   unsigned short newmode;
  183.   int errors = 0;
  184.  
  185.   if (lstat (file, &file_stats))
  186.     {
  187.       if (force_silent == 0)
  188.     error (0, errno, "%s", file);
  189.       return 1;
  190.     }
  191. #ifdef S_ISLNK
  192.   if (S_ISLNK (file_stats.st_mode))
  193.     return 0;
  194. #endif
  195.  
  196.   newmode = mode_adjust (file_stats.st_mode, changes);
  197.  
  198.   if (newmode != (file_stats.st_mode & 07777))
  199.     {
  200.       if (verbose)
  201.     describe_change (file, newmode, 1);
  202.       if (chmod (file, (int) newmode))
  203.     {
  204.       if (force_silent == 0)
  205.         error (0, errno, "%s", file);
  206.       errors = 1;
  207.     }
  208.     }
  209.   else if (verbose && changes_only == 0)
  210.     describe_change (file, newmode, 0);
  211.  
  212.   if (recurse && S_ISDIR (file_stats.st_mode))
  213.     errors |= change_dir_mode (file, changes, &file_stats);
  214.   return errors;
  215. }
  216.  
  217. /* Recursively change the modes of the files in directory DIR
  218.    according to the list of operations CHANGES.
  219.    STATP points to the results of lstat on DIR.
  220.    Return 0 if successful, 1 if errors occurred. */
  221.  
  222. static int
  223. change_dir_mode (dir, changes, statp)
  224.      char *dir;
  225.      struct mode_change *changes;
  226.      struct stat *statp;
  227. {
  228.   char *name_space, *namep;
  229.   char *path;            /* Full path of each entry to process. */
  230.   unsigned dirlength;        /* Length of DIR and '\0'. */
  231.   unsigned filelength;        /* Length of each pathname to process. */
  232.   unsigned pathlength;        /* Bytes allocated for `path'. */
  233.   int errors = 0;
  234.  
  235.   errno = 0;
  236.   name_space = savedir (dir, statp->st_size);
  237.   if (name_space == NULL)
  238.     {
  239.       if (errno)
  240.     {
  241.       if (force_silent == 0)
  242.         error (0, errno, "%s", dir);
  243.       return 1;
  244.     }
  245.       else
  246.     error (1, 0, "virtual memory exhausted");
  247.     }
  248.  
  249.   dirlength = strlen (dir) + 1;    /* + 1 is for the trailing '/'. */
  250.   pathlength = dirlength + 1;
  251.   /* Give `path' a dummy value; it will be reallocated before first use. */
  252.   path = xmalloc (pathlength);
  253.   strcpy (path, dir);
  254.   path[dirlength - 1] = '/';
  255.  
  256.   for (namep = name_space; *namep; namep += filelength - dirlength)
  257.     {
  258.       filelength = dirlength + strlen (namep) + 1;
  259.       if (filelength > pathlength)
  260.     {
  261.       pathlength = filelength * 2;
  262.       path = xrealloc (path, pathlength);
  263.     }
  264.       strcpy (path + dirlength, namep);
  265.       errors |= change_file_mode (path, changes);
  266.     }
  267.   free (path);
  268.   free (name_space);
  269.   return errors;
  270. }
  271.  
  272. /* Tell the user the mode MODE that file FILE has been set to;
  273.    if CHANGED is zero, FILE had that mode already. */
  274.  
  275. static void
  276. describe_change (file, mode, changed)
  277.      char *file;
  278.      unsigned short mode;
  279.      int changed;
  280. {
  281.   char perms[11];        /* "-rwxrwxrwx" ls-style modes. */
  282.  
  283.   mode_string (mode, perms);
  284.   perms[10] = '\0';        /* `mode_string' does not null terminate. */
  285.   if (changed)
  286.     printf ("mode of %s changed to %04o (%s)\n",
  287.         file, mode & 07777, &perms[1]);
  288.   else
  289.     printf ("mode of %s retained as %04o (%s)\n",
  290.         file, mode & 07777, &perms[1]);
  291. }
  292.  
  293. static void
  294. usage ()
  295. {
  296.   fprintf (stderr, "\
  297. Usage: %s [-Rcfv] [--recursive] [--changes] [--silent] [--quiet]\n\
  298.        [--verbose] [--help] [--version] mode file...\n\
  299.        mode is [ugoa...][[+-=][rwxXstugo...]...][,...] or octal number\n",
  300.        program_name);
  301.   exit (1);
  302. }
  303.