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