home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fileutils-3.6 / src / chown.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-14  |  7.2 KB  |  292 lines

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