home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / a / bin / fileutil.12 / fileutil / fileutils-3.12 / src / chgrp.c next >
Encoding:
C/C++ Source or Header  |  1994-10-07  |  7.6 KB  |  333 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. void
  95. main (argc, argv)
  96.      int argc;
  97.      char **argv;
  98. {
  99.   int group;
  100.   int errors = 0;
  101.   int optc;
  102.  
  103.   program_name = argv[0];
  104.   recurse = force_silent = verbose = changes_only = 0;
  105.  
  106.   while ((optc = getopt_long (argc, argv, "Rcfv", long_options, (int *) 0))
  107.      != EOF)
  108.     {
  109.       switch (optc)
  110.     {
  111.     case 0:
  112.       break;
  113.     case 'R':
  114.       recurse = 1;
  115.       break;
  116.     case 'c':
  117.       verbose = 1;
  118.       changes_only = 1;
  119.       break;
  120.     case 'f':
  121.       force_silent = 1;
  122.       break;
  123.     case 'v':
  124.       verbose = 1;
  125.       break;
  126.     default:
  127.       usage (1);
  128.     }
  129.     }
  130.  
  131.   if (show_version)
  132.     {
  133.       printf ("%s\n", version_string);
  134.       exit (0);
  135.     }
  136.  
  137.   if (show_help)
  138.     usage (0);
  139.  
  140.   if (argc - optind <= 1)
  141.     {
  142.       error (0, 0, "too few arguments");
  143.       usage (1);
  144.     }
  145.  
  146.   parse_group (argv[optind++], &group);
  147.  
  148.   for (; optind < argc; ++optind)
  149.     errors |= change_file_group (argv[optind], group);
  150.  
  151.   exit (errors);
  152. }
  153.  
  154. /* Set *G according to NAME. */
  155.  
  156. static void
  157. parse_group (name, g)
  158.      char *name;
  159.      int *g;
  160. {
  161.   struct group *grp;
  162.  
  163.   groupname = name;
  164.   if (*name == '\0')
  165.     error (1, 0, "can not change to null group");
  166.  
  167.   grp = getgrnam (name);
  168.   if (grp == NULL)
  169.     {
  170.       if (!isnumber (name))
  171.     error (1, 0, "invalid group `%s'", name);
  172.       *g = atoi (name);
  173.     }
  174.   else
  175.     *g = grp->gr_gid;
  176.   endgrent ();        /* Save a file descriptor. */
  177. }
  178.  
  179. /* Change the ownership of FILE to GID GROUP.
  180.    If it is a directory and -R is given, recurse.
  181.    Return 0 if successful, 1 if errors occurred. */
  182.  
  183. static int
  184. change_file_group (file, group)
  185.      char *file;
  186.      int group;
  187. {
  188.   struct stat file_stats;
  189.   int errors = 0;
  190.  
  191.   if (SAFE_LSTAT (file, &file_stats))
  192.     {
  193.       if (force_silent == 0)
  194.     error (0, errno, "%s", file);
  195.       return 1;
  196.     }
  197.  
  198.   if (group != file_stats.st_gid)
  199.     {
  200.       if (verbose)
  201.     describe_change (file, 1);
  202.       if (chown (file, file_stats.st_uid, group))
  203.     {
  204.       errors = 1;
  205.       if (force_silent == 0)
  206.         {
  207.           /* Give a more specific message.  Some systems set errno
  208.          to EPERM for both `inaccessible file' and `user not a member
  209.          of the specified group' errors.  */
  210.           if (errno == EPERM && !group_member (group))
  211.         {
  212.           error (0, errno, "you are not a member of group `%s'",
  213.              groupname);
  214.         }
  215.           else
  216.         {
  217.           error (0, errno, "%s", file);
  218.         }
  219.         }
  220.     }
  221.     }
  222.   else if (verbose && changes_only == 0)
  223.     describe_change (file, 0);
  224.  
  225.   if (recurse && S_ISDIR (file_stats.st_mode))
  226.     errors |= change_dir_group (file, group, &file_stats);
  227.   return errors;
  228. }
  229.  
  230. /* Recursively change the ownership of the files in directory DIR
  231.    to GID GROUP.
  232.    STATP points to the results of lstat on DIR.
  233.    Return 0 if successful, 1 if errors occurred. */
  234.  
  235. static int
  236. change_dir_group (dir, group, statp)
  237.      char *dir;
  238.      int group;
  239.      struct stat *statp;
  240. {
  241.   char *name_space, *namep;
  242.   char *path;            /* Full path of each entry to process. */
  243.   unsigned dirlength;        /* Length of `dir' and '\0'. */
  244.   unsigned filelength;        /* Length of each pathname to process. */
  245.   unsigned pathlength;        /* Bytes allocated for `path'. */
  246.   int errors = 0;
  247.  
  248.   errno = 0;
  249.   name_space = savedir (dir, statp->st_size);
  250.   if (name_space == NULL)
  251.     {
  252.       if (errno)
  253.     {
  254.       if (force_silent == 0)
  255.         error (0, errno, "%s", dir);
  256.       return 1;
  257.     }
  258.       else
  259.     error (1, 0, "virtual memory exhausted");
  260.     }
  261.  
  262.   dirlength = strlen (dir) + 1;    /* + 1 is for the trailing '/'. */
  263.   pathlength = dirlength + 1;
  264.   /* Give `path' a dummy value; it will be reallocated before first use. */
  265.   path = xmalloc (pathlength);
  266.   strcpy (path, dir);
  267.   path[dirlength - 1] = '/';
  268.  
  269.   for (namep = name_space; *namep; namep += filelength - dirlength)
  270.     {
  271.       filelength = dirlength + strlen (namep) + 1;
  272.       if (filelength > pathlength)
  273.     {
  274.       pathlength = filelength * 2;
  275.       path = xrealloc (path, pathlength);
  276.     }
  277.       strcpy (path + dirlength, namep);
  278.       errors |= change_file_group (path, group);
  279.     }
  280.   free (path);
  281.   free (name_space);
  282.   return errors;
  283. }
  284.  
  285. /* Tell the user the group name to which ownership of FILE
  286.    has been given; if CHANGED is zero, FILE was that group already. */
  287.  
  288. static void
  289. describe_change (file, changed)
  290.      char *file;
  291.      int changed;
  292. {
  293.   if (changed)
  294.     printf ("group of %s changed to %s\n", file, groupname);
  295.   else
  296.     printf ("group of %s retained as %s\n", file, groupname);
  297. }
  298.  
  299. /* Return nonzero if STR represents an unsigned decimal integer,
  300.    otherwise return 0. */
  301.  
  302. static int
  303. isnumber (str)
  304.      char *str;
  305. {
  306.   for (; *str; str++)
  307.     if (!ISDIGIT (*str))
  308.       return 0;
  309.   return 1;
  310. }
  311.  
  312. static void
  313. usage (status)
  314.      int status;
  315. {
  316.   if (status != 0)
  317.     fprintf (stderr, "Try `%s --help' for more information.\n",
  318.          program_name);
  319.   else
  320.     {
  321.       printf ("Usage: %s [OPTION]... GROUP FILE...\n", program_name);
  322.       printf ("\
  323. \n\
  324.   -c, --changes           like verbose but report only when a change is made\n\
  325.   -f, --silent, --quiet   suppress most error messages\n\
  326.   -v, --verbose           output a diagnostic for every file processed\n\
  327.   -R, --recursive         change files and directories recursively\n\
  328.       --help              display this help and exit\n\
  329.       --version           output version information and exit\n");
  330.     }
  331.   exit (status);
  332. }
  333.