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