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