home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / useful / dist / gnu / fileutils / fileutils-3.9-amiga / src / chmod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-13  |  8.4 KB  |  342 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. #ifdef HAVE_CONFIG_H
  27. #if defined (CONFIG_BROKETS)
  28. /* We use <config.h> instead of "config.h" so that a compilation
  29.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  30.    (which it would do because it found this file in $srcdir).  */
  31. #include <config.h>
  32. #else
  33. #include "config.h"
  34. #endif
  35. #endif
  36.  
  37. #include <stdio.h>
  38. #include <getopt.h>
  39. #include <sys/types.h>
  40. #include "modechange.h"
  41. #include "system.h"
  42. #include "version.h"
  43.  
  44. int lstat ();
  45.  
  46. char *savedir ();
  47. char *xmalloc ();
  48. char *xrealloc ();
  49. void error ();
  50. void mode_string ();
  51.  
  52. static int change_file_mode ();
  53. static int change_dir_mode ();
  54. static void describe_change ();
  55. static void usage ();
  56.  
  57. /* The name the program was run with. */
  58. char *program_name;
  59.  
  60. /* If nonzero, change the modes of directories recursively. */
  61. static int recurse;
  62.  
  63. /* If nonzero, force silence (no error messages). */
  64. static int force_silent;
  65.  
  66. /* If nonzero, describe the modes we set. */
  67. static int verbose;
  68.  
  69. /* If nonzero, describe only modes that change. */
  70. static int changes_only;
  71.  
  72. /* If non-zero, display usage information and exit.  */
  73. static int show_help;
  74.  
  75. /* If non-zero, print the version on standard output and exit.  */
  76. static int show_version;
  77.  
  78. static struct option const long_options[] =
  79. {
  80.   {"recursive", no_argument, 0, 'R'},
  81.   {"changes", no_argument, 0, 'c'},
  82.   {"silent", no_argument, 0, 'f'},
  83.   {"quiet", no_argument, 0, 'f'},
  84.   {"verbose", no_argument, 0, 'v'},
  85.   {"help", no_argument, &show_help, 1},
  86.   {"version", no_argument, &show_version, 1},
  87.   {0, 0, 0, 0}
  88. };
  89.  
  90. /* Parse the ASCII mode given on the command line into a linked list
  91.    of `struct mode_change' and apply that to each file argument. */
  92.  
  93. main (argc, argv)
  94.      int argc;
  95.      char **argv;
  96. {
  97.   struct mode_change *changes;
  98.   int errors = 0;
  99.   int modeind = 0;        /* Index of the mode argument in `argv'. */
  100.   int thisind;
  101.   int c;
  102.  
  103.   program_name = argv[0];
  104.   recurse = force_silent = verbose = changes_only = 0;
  105.  
  106.   while (1)
  107.     {
  108.       thisind = optind ? optind : 1;
  109.  
  110.       c = getopt_long (argc, argv, "RcfvrwxXstugoa,+-=", long_options,
  111.                (int *) 0);
  112.       if (c == EOF)
  113.     break;
  114.  
  115.       switch (c)
  116.     {
  117.     case 0:
  118.       break;
  119.     case 'r':
  120.     case 'w':
  121.     case 'x':
  122.     case 'X':
  123.     case 's':
  124.     case 't':
  125.     case 'u':
  126.     case 'g':
  127.     case 'o':
  128.     case 'a':
  129.     case ',':
  130.     case '+':
  131.     case '-':
  132.     case '=':
  133.       if (modeind != 0 && modeind != thisind)
  134.         error (1, 0, "invalid mode");
  135.       modeind = thisind;
  136.       break;
  137.     case 'R':
  138.       recurse = 1;
  139.       break;
  140.     case 'c':
  141.       verbose = 1;
  142.       changes_only = 1;
  143.       break;
  144.     case 'f':
  145.       force_silent = 1;
  146.       break;
  147.     case 'v':
  148.       verbose = 1;
  149.       break;
  150.     default:
  151.       usage (1);
  152.     }
  153.     }
  154.  
  155.   if (show_version)
  156.     {
  157.       printf ("%s\n", version_string);
  158.       exit (0);
  159.     }
  160.  
  161.   if (show_help)
  162.     usage (0);
  163.  
  164.   if (modeind == 0)
  165.     modeind = optind++;
  166.  
  167.   if (optind >= argc)
  168.     usage (1);
  169.  
  170.   changes = mode_compile (argv[modeind],
  171.               MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
  172.   if (changes == MODE_INVALID)
  173.     error (1, 0, "invalid mode");
  174.   else if (changes == MODE_MEMORY_EXHAUSTED)
  175.     error (1, 0, "virtual memory exhausted");
  176.  
  177.   for (; optind < argc; ++optind)
  178.     errors |= change_file_mode (argv[optind], changes, 1);
  179.  
  180.   exit (errors);
  181. }
  182.  
  183. /* Change the mode of FILE according to the list of operations CHANGES.
  184.    If DEREF_SYMLINK is non-zero and FILE is a symbolic link, change the
  185.    mode of the referenced file.  If DEREF_SYMLINK is zero, ignore symbolic
  186.    links.  Return 0 if successful, 1 if errors occurred. */
  187.  
  188. static int
  189. change_file_mode (file, changes, deref_symlink)
  190.      char *file;
  191.      struct mode_change *changes;
  192.      int deref_symlink;
  193. {
  194.   struct stat file_stats;
  195.   unsigned short newmode;
  196.   int errors = 0;
  197.  
  198.   if (lstat (file, &file_stats))
  199.     {
  200.       if (force_silent == 0)
  201.     error (0, errno, "%s", file);
  202.       return 1;
  203.     }
  204. #ifdef S_ISLNK
  205.   if (S_ISLNK (file_stats.st_mode))
  206.     if (! deref_symlink)
  207.       return 0;
  208.     else 
  209.       if (stat (file, &file_stats))
  210.     {
  211.       if (force_silent == 0)
  212.         error (0, errno, "%s", file);
  213.       return 1;
  214.     }
  215. #endif
  216.  
  217.   newmode = mode_adjust (file_stats.st_mode, changes);
  218.  
  219.   if (newmode != (file_stats.st_mode & 07777))
  220.     {
  221.       if (verbose)
  222.     describe_change (file, newmode, 1);
  223.       if (chmod (file, (int) newmode))
  224.     {
  225.       if (force_silent == 0)
  226.         error (0, errno, "%s", file);
  227.       errors = 1;
  228.     }
  229.     }
  230.   else if (verbose && changes_only == 0)
  231.     describe_change (file, newmode, 0);
  232.  
  233.   if (recurse && S_ISDIR (file_stats.st_mode))
  234.     errors |= change_dir_mode (file, changes, &file_stats);
  235.   return errors;
  236. }
  237.  
  238. /* Recursively change the modes of the files in directory DIR
  239.    according to the list of operations CHANGES.
  240.    STATP points to the results of lstat on DIR.
  241.    Return 0 if successful, 1 if errors occurred. */
  242.  
  243. static int
  244. change_dir_mode (dir, changes, statp)
  245.      char *dir;
  246.      struct mode_change *changes;
  247.      struct stat *statp;
  248. {
  249.   char *name_space, *namep;
  250.   char *path;            /* Full path of each entry to process. */
  251.   unsigned dirlength;        /* Length of DIR and '\0'. */
  252.   unsigned filelength;        /* Length of each pathname to process. */
  253.   unsigned pathlength;        /* Bytes allocated for `path'. */
  254.   int errors = 0;
  255.  
  256.   errno = 0;
  257.   name_space = savedir (dir, statp->st_size);
  258.   if (name_space == NULL)
  259.     {
  260.       if (errno)
  261.     {
  262.       if (force_silent == 0)
  263.         error (0, errno, "%s", dir);
  264.       return 1;
  265.     }
  266.       else
  267.     error (1, 0, "virtual memory exhausted");
  268.     }
  269.  
  270.   dirlength = strlen (dir) + 1;    /* + 1 is for the trailing '/'. */
  271.   pathlength = dirlength + 1;
  272.   /* Give `path' a dummy value; it will be reallocated before first use. */
  273.   path = xmalloc (pathlength);
  274.   strcpy (path, dir);
  275.   path[dirlength - 1] = '/';
  276.  
  277.   for (namep = name_space; *namep; namep += filelength - dirlength)
  278.     {
  279.       filelength = dirlength + strlen (namep) + 1;
  280.       if (filelength > pathlength)
  281.     {
  282.       pathlength = filelength * 2;
  283.       path = xrealloc (path, pathlength);
  284.     }
  285.       strcpy (path + dirlength, namep);
  286.       errors |= change_file_mode (path, changes, 0);
  287.     }
  288.   free (path);
  289.   free (name_space);
  290.   return errors;
  291. }
  292.  
  293. /* Tell the user the mode MODE that file FILE has been set to;
  294.    if CHANGED is zero, FILE had that mode already. */
  295.  
  296. static void
  297. describe_change (file, mode, changed)
  298.      char *file;
  299.      unsigned short mode;
  300.      int changed;
  301. {
  302.   char perms[11];        /* "-rwxrwxrwx" ls-style modes. */
  303.  
  304.   mode_string (mode, perms);
  305.   perms[10] = '\0';        /* `mode_string' does not null terminate. */
  306.   if (changed)
  307.     printf ("mode of %s changed to %04o (%s)\n",
  308.         file, mode & 07777, &perms[1]);
  309.   else
  310.     printf ("mode of %s retained as %04o (%s)\n",
  311.         file, mode & 07777, &perms[1]);
  312. }
  313.  
  314. static void
  315. usage (status)
  316.      int status;
  317. {
  318.   if (status != 0)
  319.     fprintf (stderr, "Try `%s --help' for more information.\n",
  320.          program_name);
  321.   else
  322.     {
  323.       printf ("\
  324. Usage: %s [OPTION]... MODE[,MODE]... FILE...\n\
  325.   or:  %s [OPTION]... OCTAL_MODE FILE...\n\
  326. ",
  327.           program_name, program_name);
  328.       printf ("\
  329. \n\
  330.   -c, --changes           like verbose but report only when a change is made\n\
  331.   -f, --silent, --quiet   suppress most error messages\n\
  332.   -v, --verbose           output a diagnostic for every file processed\n\
  333.   -R, --recursive         change files and directories recursively\n\
  334.       --help              display this help and exit\n\
  335.       --version           output version information and exit\n\
  336. \n\
  337. Each MODE is one or more of the letters ugoa, one of the symbols +-= and\n\
  338. one or more of the letters rwxXstugo.\n");
  339.     }
  340.   exit (status);
  341. }
  342.