home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / fileutils-3.12-src.lha / fileutils-3.12 / src / install.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-12  |  13.5 KB  |  564 lines

  1. /* install - copy files and set attributes
  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. /* Copy files and set their permission modes and, if possible,
  19.    their owner and group.  Used similarly to `cp'; typically
  20.    used in Makefiles to copy programs into their destination
  21.    directories.  It can also be used to create the destination
  22.    directories and any leading directories, and to set the final
  23.    directory's modes.  It refuses to copy files onto themselves.
  24.  
  25.    Options:
  26.    -g, --group=GROUP
  27.     Set the group ownership of the installed file or directory
  28.     to the group ID of GROUP (default is process's current
  29.     group).  GROUP may also be a numeric group ID.
  30.  
  31.    -m, --mode=MODE
  32.     Set the permission mode for the installed file or directory
  33.     to MODE, which is an octal number (default is 0755).
  34.  
  35.    -o, --owner=OWNER
  36.     If run as root, set the ownership of the installed file to
  37.     the user ID of OWNER (default is root).  OWNER may also be
  38.     a numeric user ID.
  39.  
  40.    -c    No effect.  For compatibility with old Unix versions of install.
  41.  
  42.    -s, --strip
  43.     Strip the symbol tables from installed files.
  44.  
  45.    -d, --directory
  46.     Create a directory and its leading directories, if they
  47.     do not already exist.  Set the owner, group and mode
  48.     as given on the command line.  Any leading directories
  49.     that are created are also given those attributes.
  50.     This is different from the SunOS 4.0 install, which gives
  51.     directories that it creates the default attributes.
  52.  
  53.    David MacKenzie <djm@gnu.ai.mit.edu> */
  54.  
  55. #include <config.h>
  56. #include <stdio.h>
  57. #include <getopt.h>
  58. #include <ctype.h>
  59. #include <sys/types.h>
  60. #include <pwd.h>
  61. #include <grp.h>
  62. #include "system.h"
  63. #include "version.h"
  64. #include "safe-stat.h"
  65. #include "modechange.h"
  66. #include "makepath.h"
  67.  
  68. #if !defined (isascii) || defined (STDC_HEADERS)
  69. #undef isascii
  70. #define isascii(c) 1
  71. #endif
  72.  
  73. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  74.  
  75. #ifdef _POSIX_VERSION
  76. #include <sys/wait.h>
  77. #else
  78. struct passwd *getpwnam ();
  79. struct group *getgrnam ();
  80. uid_t getuid ();
  81. gid_t getgid ();
  82. int wait ();
  83. #endif
  84.  
  85. #ifdef _POSIX_SOURCE
  86. #define endgrent()
  87. #define endpwent()
  88. #endif
  89.  
  90. /* True if C is an ASCII octal digit. */
  91. #define isodigit(c) ((c) >= '0' && c <= '7')
  92.  
  93. /* Number of bytes of a file to copy at a time. */
  94. #define READ_SIZE (32 * 1024)
  95.  
  96. char *basename ();
  97. char *stpcpy ();
  98. char *xmalloc ();
  99. void error ();
  100. int safe_read ();
  101. int full_write ();
  102. int isdir ();
  103.  
  104. static int change_attributes ();
  105. static int copy_file ();
  106. static int install_file_in_dir ();
  107. static int install_file_in_file ();
  108. static int isnumber ();
  109. static void get_ids ();
  110. static void strip ();
  111. static void usage ();
  112.  
  113. /* The name this program was run with, for error messages. */
  114. char *program_name;
  115.  
  116. /* The user name that will own the files, or NULL to make the owner
  117.    the current user ID. */
  118. static char *owner_name;
  119.  
  120. /* The user ID corresponding to `owner_name'. */
  121. static uid_t owner_id;
  122.  
  123. /* The group name that will own the files, or NULL to make the group
  124.    the current group ID. */
  125. static char *group_name;
  126.  
  127. /* The group ID corresponding to `group_name'. */
  128. static gid_t group_id;
  129.  
  130. /* The permissions to which the files will be set.  The umask has
  131.    no effect. */
  132. static int mode;
  133.  
  134. /* If nonzero, strip executable files after copying them. */
  135. static int strip_files;
  136.  
  137. /* If nonzero, install a directory instead of a regular file. */
  138. static int dir_arg;
  139.  
  140. /* If non-zero, display usage information and exit.  */
  141. static int show_help;
  142.  
  143. /* If non-zero, print the version on standard output and exit.  */
  144. static int show_version;
  145.  
  146. static struct option const long_options[] =
  147. {
  148.   {"strip", no_argument, NULL, 's'},
  149.   {"directory", no_argument, NULL, 'd'},
  150.   {"group", required_argument, NULL, 'g'},
  151.   {"mode", required_argument, NULL, 'm'},
  152.   {"owner", required_argument, NULL, 'o'},
  153.   {"help", no_argument, &show_help, 1},
  154.   {"version", no_argument, &show_version, 1},
  155.   {NULL, 0, NULL, 0}
  156. };
  157.  
  158. main (argc, argv)
  159.      int argc;
  160.      char **argv;
  161. {
  162.   int optc;
  163.   int errors = 0;
  164.   char *symbolic_mode = NULL;
  165.  
  166.   program_name = argv[0];
  167.   owner_name = NULL;
  168.   group_name = NULL;
  169.   mode = 0755;
  170.   strip_files = 0;
  171.   dir_arg = 0;
  172.   umask (0);
  173.  
  174.   while ((optc = getopt_long (argc, argv, "csdg:m:o:", long_options,
  175.                   (int *) 0)) != EOF)
  176.     {
  177.       switch (optc)
  178.     {
  179.     case 0:
  180.       break;
  181.     case 'c':
  182.       break;
  183.     case 's':
  184.       strip_files = 1;
  185.       break;
  186.     case 'd':
  187.       dir_arg = 1;
  188.       break;
  189.     case 'g':
  190.       group_name = optarg;
  191.       break;
  192.     case 'm':
  193.       symbolic_mode = optarg;
  194.       break;
  195.     case 'o':
  196.       owner_name = optarg;
  197.       break;
  198.     default:
  199.       usage (1);
  200.     }
  201.     }
  202.  
  203.   if (show_version)
  204.     {
  205.       printf ("%s\n", version_string);
  206.       exit (0);
  207.     }
  208.  
  209.   if (show_help)
  210.     usage (0);
  211.  
  212.   /* Check for invalid combinations of arguments. */
  213.   if (dir_arg && strip_files)
  214.     error (1, 0,
  215.        "the strip option may not be used when installing a directory");
  216.  
  217.   if (optind == argc || (optind == argc - 1 && !dir_arg))
  218.     {
  219.       error (0, 0, "too few arguments");
  220.       usage (1);
  221.     }
  222.  
  223.   if (symbolic_mode)
  224.     {
  225.       struct mode_change *change = mode_compile (symbolic_mode, 0);
  226.       if (change == MODE_INVALID)
  227.     error (1, 0, "invalid mode `%s'", symbolic_mode);
  228.       else if (change == MODE_MEMORY_EXHAUSTED)
  229.     error (1, 0, "virtual memory exhausted");
  230.       mode = mode_adjust (0, change);
  231.     }
  232.  
  233.   get_ids ();
  234.  
  235.   if (dir_arg)
  236.     {
  237.       for (; optind < argc; ++optind)
  238.     {
  239.       errors |=
  240.         make_path (argv[optind], mode, mode, owner_id, group_id, 0, NULL);
  241.     }
  242.     }
  243.   else
  244.     {
  245.       if (optind == argc - 2)
  246.     {
  247.       if (!isdir (argv[argc - 1]))
  248.         errors = install_file_in_file (argv[argc - 2], argv[argc - 1]);
  249.       else
  250.         errors = install_file_in_dir (argv[argc - 2], argv[argc - 1]);
  251.     }
  252.       else
  253.     {
  254.       if (!isdir (argv[argc - 1]))
  255.         usage (1);
  256.       for (; optind < argc - 1; ++optind)
  257.         {
  258.           errors |= install_file_in_dir (argv[optind], argv[argc - 1]);
  259.         }
  260.     }
  261.     }
  262.  
  263.   exit (errors);
  264. }
  265.  
  266. /* Copy file FROM onto file TO and give TO the appropriate
  267.    attributes.
  268.    Return 0 if successful, 1 if an error occurs. */
  269.  
  270. static int
  271. install_file_in_file (from, to)
  272.      char *from;
  273.      char *to;
  274. {
  275.   int to_created;
  276.   int no_need_to_chown;
  277.  
  278.   if (copy_file (from, to, &to_created))
  279.     return 1;
  280.   if (strip_files)
  281.     strip (to);
  282.   no_need_to_chown = (to_created
  283.               && owner_name == NULL
  284.               && group_name == NULL);
  285.   return change_attributes (to, no_need_to_chown);
  286. }
  287.  
  288. /* Copy file FROM into directory TO_DIR, keeping its same name,
  289.    and give the copy the appropriate attributes.
  290.    Return 0 if successful, 1 if not. */
  291.  
  292. static int
  293. install_file_in_dir (from, to_dir)
  294.      char *from;
  295.      char *to_dir;
  296. {
  297.   char *from_base;
  298.   char *to;
  299.   int ret;
  300.  
  301.   from_base = basename (from);
  302.   to = xmalloc ((unsigned) (strlen (to_dir) + strlen (from_base) + 2));
  303.   stpcpy (stpcpy (stpcpy (to, to_dir), "/"), from_base);
  304.   ret = install_file_in_file (from, to);
  305.   free (to);
  306.   return ret;
  307. }
  308.  
  309. /* A chunk of a file being copied. */
  310. static char buffer[READ_SIZE];
  311.  
  312. /* Copy file FROM onto file TO, creating TO if necessary.
  313.    Return 0 if the copy is successful, 1 if not.  If the copy is
  314.    successful, set *TO_CREATED to non-zero if TO was created (if it did
  315.    not exist or did, but was unlinked) and to zero otherwise.  If the
  316.    copy fails, don't modify *TO_CREATED.  */
  317.  
  318. static int
  319. copy_file (from, to, to_created)
  320.      char *from;
  321.      char *to;
  322.      int *to_created;
  323. {
  324.   int fromfd, tofd;
  325.   int bytes;
  326.   int ret = 0;
  327.   struct stat from_stats, to_stats;
  328.   int target_created = 1;
  329.  
  330.   if (SAFE_STAT (from, &from_stats))
  331.     {
  332.       error (0, errno, "%s", from);
  333.       return 1;
  334.     }
  335.   if (!S_ISREG (from_stats.st_mode))
  336.     {
  337.       error (0, 0, "`%s' is not a regular file", from);
  338.       return 1;
  339.     }
  340.   if (SAFE_STAT (to, &to_stats) == 0)
  341.     {
  342.       if (!S_ISREG (to_stats.st_mode))
  343.     {
  344.       error (0, 0, "`%s' is not a regular file", to);
  345.       return 1;
  346.     }
  347.       if (from_stats.st_dev == to_stats.st_dev
  348.       && from_stats.st_ino == to_stats.st_ino)
  349.     {
  350.       error (0, 0, "`%s' and `%s' are the same file", from, to);
  351.       return 1;
  352.     }
  353.       /* If unlink fails, try to proceed anyway.  */
  354.       if (unlink (to))
  355.     target_created = 0;
  356.     }
  357.  
  358.   fromfd = open (from, O_RDONLY, 0);
  359.   if (fromfd == -1)
  360.     {
  361.       error (0, errno, "%s", from);
  362.       return 1;
  363.     }
  364.  
  365.   /* Make sure to open the file in a mode that allows writing. */
  366.   tofd = open (to, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  367.   if (tofd == -1)
  368.     {
  369.       error (0, errno, "%s", to);
  370.       close (fromfd);
  371.       return 1;
  372.     }
  373.  
  374.   while ((bytes = safe_read (fromfd, buffer, READ_SIZE)) > 0)
  375.     if (full_write (tofd, buffer, bytes) < 0)
  376.       {
  377.     error (0, errno, "%s", to);
  378.     goto copy_error;
  379.       }
  380.  
  381.   if (bytes == -1)
  382.     {
  383.       error (0, errno, "%s", from);
  384.       goto copy_error;
  385.     }
  386.  
  387.   if (close (fromfd) < 0)
  388.     {
  389.       error (0, errno, "%s", from);
  390.       ret = 1;
  391.     }
  392.   if (close (tofd) < 0)
  393.     {
  394.       error (0, errno, "%s", to);
  395.       ret = 1;
  396.     }
  397.   if (ret == 0)
  398.     *to_created = target_created;
  399.   return ret;
  400.  
  401.  copy_error:
  402.   close (fromfd);
  403.   close (tofd);
  404.   return 1;
  405. }
  406.  
  407. /* Set the attributes of file or directory PATH.
  408.    If NO_NEED_TO_CHOWN is non-zero, don't call chown.
  409.    Return 0 if successful, 1 if not. */
  410.  
  411. static int
  412. change_attributes (path, no_need_to_chown)
  413.      char *path;
  414.      int no_need_to_chown;
  415. {
  416.   int err = 0;
  417.  
  418.   /* chown must precede chmod because on some systems,
  419.      chown clears the set[ug]id bits for non-superusers,
  420.      resulting in incorrect permissions.
  421.      On System V, users can give away files with chown and then not
  422.      be able to chmod them.  So don't give files away.
  423.  
  424.      We don't pass -1 to chown to mean "don't change the value"
  425.      because SVR3 and earlier non-BSD systems don't support that.
  426.  
  427.      We don't normally ignore errors from chown because the idea of
  428.      the install command is that the file is supposed to end up with
  429.      precisely the attributes that the user specified (or defaulted).
  430.      If the file doesn't end up with the group they asked for, they'll
  431.      want to know.  But AFS returns EPERM when you try to change a
  432.      file's group; thus the kludge.  */
  433.  
  434.   if (!no_need_to_chown && chown (path, owner_id, group_id)
  435. #ifdef AFS
  436.       && errno != EPERM
  437. #endif
  438.       )
  439.     err = errno;
  440.   if (chmod (path, mode))
  441.     err = errno;
  442.   if (err)
  443.     {
  444.       error (0, err, "%s", path);
  445.       return 1;
  446.     }
  447.   return 0;
  448. }
  449.  
  450. /* Strip the symbol table from the file PATH.
  451.    We could dig the magic number out of the file first to
  452.    determine whether to strip it, but the header files and
  453.    magic numbers vary so much from system to system that making
  454.    it portable would be very difficult.  Not worth the effort. */
  455.  
  456. static void
  457. strip (path)
  458.      char *path;
  459. {
  460.   int pid, status;
  461.  
  462.   pid = vfork ();
  463.   switch (pid)
  464.     {
  465.     case -1:
  466.       error (1, errno, "cannot fork");
  467.       break;
  468.     case 0:            /* Child. */
  469.       execlp ("strip", "strip", path, (char *) NULL);
  470.       error (1, errno, "cannot run strip");
  471.       break;
  472.     default:            /* Parent. */
  473.       /* Parent process. */
  474.       while (pid != wait (&status))    /* Wait for kid to finish. */
  475.     /* Do nothing. */ ;
  476.       break;
  477.     }
  478. }
  479.  
  480. /* Initialize the user and group ownership of the files to install. */
  481.  
  482. static void
  483. get_ids ()
  484. {
  485.   struct passwd *pw;
  486.   struct group *gr;
  487.  
  488.   if (owner_name)
  489.     {
  490.       pw = getpwnam (owner_name);
  491.       if (pw == NULL)
  492.     {
  493.       if (!isnumber (owner_name))
  494.         error (1, 0, "invalid user `%s'", owner_name);
  495.       owner_id = atoi (owner_name);
  496.     }
  497.       else
  498.     owner_id = pw->pw_uid;
  499.       endpwent ();
  500.     }
  501.   else
  502.     owner_id = getuid ();
  503.  
  504.   if (group_name)
  505.     {
  506.       gr = getgrnam (group_name);
  507.       if (gr == NULL)
  508.     {
  509.       if (!isnumber (group_name))
  510.         error (1, 0, "invalid group `%s'", group_name);
  511.       group_id = atoi (group_name);
  512.     }
  513.       else
  514.     group_id = gr->gr_gid;
  515.       endgrent ();
  516.     }
  517.   else
  518.     group_id = getgid ();
  519. }
  520.  
  521. /* Return nonzero if STR is an ASCII representation of a nonzero
  522.    decimal integer, zero if not. */
  523.  
  524. static int
  525. isnumber (str)
  526.      char *str;
  527. {
  528.   if (*str == 0)
  529.     return 0;
  530.   for (; *str; str++)
  531.     if (!ISDIGIT (*str))
  532.       return 0;
  533.   return 1;
  534. }
  535.  
  536. static void
  537. usage (status)
  538.      int status;
  539. {
  540.   if (status != 0)
  541.     fprintf (stderr, "Try `%s --help' for more information.\n",
  542.          program_name);
  543.   else
  544.     {
  545.       printf ("\
  546. Usage: %s [OPTION]... SOURCE DEST           (1st format)\n\
  547.   or:  %s [OPTION]... SOURCE... DIRECTORY   (2nd format)\n\
  548.   or:  %s [OPTION]... DIRECTORY...          (3nd format)\n\
  549. ",
  550.           program_name, program_name, program_name);
  551.       printf ("\
  552. \n\
  553.   -c                  (ignored)\n\
  554.   -d, --directory     create [leading] directories, mandatory for 3rd format\n\
  555.   -g, --group=GROUP   set group ownership, instead of process' current group\n\
  556.   -m, --mode=MODE     set permission mode (as in chmod), instead of 0755\n\
  557.   -o, --owner=OWNER   set ownership (super-user only)\n\
  558.   -s, --strip         strip symbol tables, only for 1st and 2nd formats\n\
  559.       --help          display this help and exit\n\
  560.       --version       output version information and exit\n");
  561.     }
  562.   exit (status);
  563. }
  564.