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