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