home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / useful / dist / gnu / fileutils / fileutils-3.9-amiga / src / chgrp.c next >
Encoding:
C/C++ Source or Header  |  1993-12-13  |  7.4 KB  |  326 lines

  1. /* chgrp -- change group ownership 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. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
  19.  
  20. #ifdef HAVE_CONFIG_H
  21. #if defined (CONFIG_BROKETS)
  22. /* We use <config.h> instead of "config.h" so that a compilation
  23.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  24.    (which it would do because it found this file in $srcdir).  */
  25. #include <config.h>
  26. #else
  27. #include "config.h"
  28. #endif
  29. #endif
  30.  
  31. #include <stdio.h>
  32. #include <ctype.h>
  33. #include <sys/types.h>
  34. #include <grp.h>
  35. #include <getopt.h>
  36. #include "system.h"
  37. #include "version.h"
  38.  
  39. #if !defined (isascii) || defined (STDC_HEADERS)
  40. #undef isascii
  41. #define isascii(c) 1
  42. #endif
  43.  
  44. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  45.  
  46. #ifndef _POSIX_VERSION
  47. struct group *getgrnam ();
  48. #endif
  49.  
  50. #ifdef _POSIX_SOURCE
  51. #define endgrent()
  52. #endif
  53.  
  54. int lstat ();
  55.  
  56. char *savedir ();
  57. char *xmalloc ();
  58. char *xrealloc ();
  59. void error ();
  60.  
  61. static int change_file_group ();
  62. static int change_dir_group ();
  63. static int isnumber ();
  64. static void describe_change ();
  65. static void parse_group ();
  66. static void usage ();
  67.  
  68. /* The name the program was run with. */
  69. char *program_name;
  70.  
  71. /* If nonzero, change the ownership of directories recursively. */
  72. static int recurse;
  73.  
  74. /* If nonzero, force silence (no error messages). */
  75. static int force_silent;
  76.  
  77. /* If nonzero, describe the files we process. */
  78. static int verbose;
  79.  
  80. /* If nonzero, describe only owners or groups that change. */
  81. static int changes_only;
  82.  
  83. /* The name of the group to which ownership of the files is being given. */
  84. static char *groupname;
  85.  
  86. /* If non-zero, display usage information and exit.  */
  87. static int show_help;
  88.  
  89. /* If non-zero, print the version on standard output and exit.  */
  90. static int show_version;
  91.  
  92. static struct option const long_options[] =
  93. {
  94.   {"recursive", no_argument, 0, 'R'},
  95.   {"changes", no_argument, 0, 'c'},
  96.   {"silent", no_argument, 0, 'f'},
  97.   {"quiet", no_argument, 0, 'f'},
  98.   {"verbose", no_argument, 0, 'v'},
  99.   {"help", no_argument, &show_help, 1},
  100.   {"version", no_argument, &show_version, 1},
  101.   {0, 0, 0, 0}
  102. };
  103.  
  104. main (argc, argv)
  105.      int argc;
  106.      char **argv;
  107. {
  108.   int group;
  109.   int errors = 0;
  110.   int optc;
  111.  
  112.   program_name = argv[0];
  113.   recurse = force_silent = verbose = changes_only = 0;
  114.  
  115.   while ((optc = getopt_long (argc, argv, "Rcfv", long_options, (int *) 0))
  116.      != EOF)
  117.     {
  118.       switch (optc)
  119.     {
  120.     case 0:
  121.       break;
  122.     case 'R':
  123.       recurse = 1;
  124.       break;
  125.     case 'c':
  126.       verbose = 1;
  127.       changes_only = 1;
  128.       break;
  129.     case 'f':
  130.       force_silent = 1;
  131.       break;
  132.     case 'v':
  133.       verbose = 1;
  134.       break;
  135.     default:
  136.       usage (1);
  137.     }
  138.     }
  139.  
  140.   if (show_version)
  141.     {
  142.       printf ("%s\n", version_string);
  143.       exit (0);
  144.     }
  145.  
  146.   if (show_help)
  147.     usage (0);
  148.  
  149.   if (optind >= argc - 1)
  150.     usage (1);
  151.  
  152.   parse_group (argv[optind++], &group);
  153.  
  154.   for (; optind < argc; ++optind)
  155.     errors |= change_file_group (argv[optind], group);
  156.  
  157.   exit (errors);
  158. }
  159.  
  160. /* Set *G according to NAME. */
  161.  
  162. static void
  163. parse_group (name, g)
  164.      char *name;
  165.      int *g;
  166. {
  167.   struct group *grp;
  168.  
  169.   groupname = name;
  170.   if (*name == '\0')
  171.     error (1, 0, "can not change to null group");
  172.  
  173.   grp = getgrnam (name);
  174.   if (grp == NULL)
  175.     {
  176.       if (!isnumber (name))
  177.     error (1, 0, "invalid group `%s'", name);
  178.       *g = atoi (name);
  179.     }
  180.   else
  181.     *g = grp->gr_gid;
  182.   endgrent ();        /* Save a file descriptor. */
  183. }
  184.  
  185. /* Change the ownership of FILE to GID GROUP.
  186.    If it is a directory and -R is given, recurse.
  187.    Return 0 if successful, 1 if errors occurred. */
  188.  
  189. static int
  190. change_file_group (file, group)
  191.      char *file;
  192.      int group;
  193. {
  194.   struct stat file_stats;
  195.   int errors = 0;
  196.  
  197.   if (lstat (file, &file_stats))
  198.     {
  199.       if (force_silent == 0)
  200.     error (0, errno, "%s", file);
  201.       return 1;
  202.     }
  203.  
  204.   if (group != file_stats.st_gid)
  205.     {
  206.       if (verbose)
  207.     describe_change (file, 1);
  208.       if (chown (file, file_stats.st_uid, group))
  209.     {
  210.       if (force_silent == 0)
  211.         error (0, errno, "%s", file);
  212.       errors = 1;
  213.     }
  214.     }
  215.   else if (verbose && changes_only == 0)
  216.     describe_change (file, 0);
  217.  
  218.   if (recurse && S_ISDIR (file_stats.st_mode))
  219.     errors |= change_dir_group (file, group, &file_stats);
  220.   return errors;
  221. }
  222.  
  223. /* Recursively change the ownership of the files in directory DIR
  224.    to GID GROUP.
  225.    STATP points to the results of lstat on DIR.
  226.    Return 0 if successful, 1 if errors occurred. */
  227.  
  228. static int
  229. change_dir_group (dir, group, statp)
  230.      char *dir;
  231.      int group;
  232.      struct stat *statp;
  233. {
  234.   char *name_space, *namep;
  235.   char *path;            /* Full path of each entry to process. */
  236.   unsigned dirlength;        /* Length of `dir' and '\0'. */
  237.   unsigned filelength;        /* Length of each pathname to process. */
  238.   unsigned pathlength;        /* Bytes allocated for `path'. */
  239.   int errors = 0;
  240.  
  241.   errno = 0;
  242.   name_space = savedir (dir, statp->st_size);
  243.   if (name_space == NULL)
  244.     {
  245.       if (errno)
  246.     {
  247.       if (force_silent == 0)
  248.         error (0, errno, "%s", dir);
  249.       return 1;
  250.     }
  251.       else
  252.     error (1, 0, "virtual memory exhausted");
  253.     }
  254.  
  255.   dirlength = strlen (dir) + 1;    /* + 1 is for the trailing '/'. */
  256.   pathlength = dirlength + 1;
  257.   /* Give `path' a dummy value; it will be reallocated before first use. */
  258.   path = xmalloc (pathlength);
  259.   strcpy (path, dir);
  260.   path[dirlength - 1] = '/';
  261.  
  262.   for (namep = name_space; *namep; namep += filelength - dirlength)
  263.     {
  264.       filelength = dirlength + strlen (namep) + 1;
  265.       if (filelength > pathlength)
  266.     {
  267.       pathlength = filelength * 2;
  268.       path = xrealloc (path, pathlength);
  269.     }
  270.       strcpy (path + dirlength, namep);
  271.       errors |= change_file_group (path, group);
  272.     }
  273.   free (path);
  274.   free (name_space);
  275.   return errors;
  276. }
  277.  
  278. /* Tell the user the group name to which ownership of FILE
  279.    has been given; if CHANGED is zero, FILE was that group already. */
  280.  
  281. static void
  282. describe_change (file, changed)
  283.      char *file;
  284.      int changed;
  285. {
  286.   if (changed)
  287.     printf ("group of %s changed to %s\n", file, groupname);
  288.   else
  289.     printf ("group of %s retained as %s\n", file, groupname);
  290. }
  291.  
  292. /* Return nonzero if STR represents an unsigned decimal integer,
  293.    otherwise return 0. */
  294.  
  295. static int
  296. isnumber (str)
  297.      char *str;
  298. {
  299.   for (; *str; str++)
  300.     if (!ISDIGIT (*str))
  301.       return 0;
  302.   return 1;
  303. }
  304.  
  305. static void
  306. usage (status)
  307.      int status;
  308. {
  309.   if (status != 0)
  310.     fprintf (stderr, "Try `%s --help' for more information.\n",
  311.          program_name);
  312.   else
  313.     {
  314.       printf ("Usage: %s [OPTION]... GROUP FILE...\n", program_name);
  315.       printf ("\
  316. \n\
  317.   -c, --changes           like verbose but report only when a change is made\n\
  318.   -f, --silent, --quiet   suppress most error messages\n\
  319.   -v, --verbose           output a diagnostic for every file processed\n\
  320.   -R, --recursive         change files and directories recursively\n\
  321.       --help              display this help and exit\n\
  322.       --version           output version information and exit\n");
  323.     }
  324.   exit (status);
  325. }
  326.