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