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