home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / futi14as.zip / INSTALL.C < prev    next >
C/C++ Source or Header  |  1990-08-28  |  14KB  |  577 lines

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