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