home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 367_01 / futi14as.zoo / cp.c < prev    next >
C/C++ Source or Header  |  1992-02-22  |  23KB  |  954 lines

  1. /*  cp.c  -- file copying (main routines)
  2.     Copyright (C) 1989, 1990 Free Software Foundation.
  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.     Written by Torbjorn Granlund and David MacKenzie. */
  19.  
  20. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  21.    This port is also distributed under the terms of the
  22.    GNU General Public License as published by the
  23.    Free Software Foundation.
  24.  
  25.    Please note that this file is not identical to the
  26.    original GNU release, you should have received this
  27.    code as patch to the official release.  */
  28.  
  29. static char RCS_Id[] =
  30.   "$Header: e:/gnu/fileutil/RCS/cp.c 1.4.0.2 90/09/19 11:18:06 tho Exp $";
  31.  
  32. static char Program_Id[] = "cp";
  33. static char RCS_Revision[] = "$Revision: 1.4.0.2 $";
  34.  
  35. #define VERSION \
  36.   "GNU %s, Version %.*s (compiled %s %s for MS-DOS)\n", Program_Id, \
  37.   (sizeof RCS_Revision - 14), (RCS_Revision + 11), __DATE__, __TIME__
  38.  
  39. #define COPYING \
  40.   "This is free software, distributed under the terms of the\n" \
  41.   "GNU General Public License.  For details, see the file COPYING.\n"
  42.  
  43. /* Yet to be done:
  44.  
  45.  * Symlink translation. */
  46.  
  47. #include <stdio.h>
  48. #include <getopt.h>
  49. #include "cp.h"
  50. #include "backupfile.h"
  51.  
  52. #ifdef MSDOS
  53. #include <assert.h>
  54.  
  55. extern enum backup_type get_version (char *version);
  56. extern char *savedir (char *dir, unsigned name_size);
  57.  
  58. #define strip_trailing_slashes(path)    strip_trailing_slashes (&path)
  59. #define is_ancestor(statb, ancestors)    0
  60. #define hash_init(module, size)
  61. #define remember_copied(path, ino, dev)    NULL
  62. #define remember_created(path)        0
  63. #define forget_all()
  64. char new_file;
  65.  
  66. #endif
  67.  
  68. #ifdef MKFIFO_MISSING
  69. /* This definition assumes that MODE has the S_IFIFO bit set. */
  70. #define mkfifo(path, mode) (mknod ((path), (mode), 0))
  71. #endif
  72.  
  73. enum backup_type get_version ();
  74. int eaccess_stat ();
  75.  
  76. /* Initial number of entries in each hash table entry's table of inodes.  */
  77. #define INITIAL_HASH_MODULE 100
  78.  
  79. /* Initial number of entries in the inode hash table.  */
  80. #define INITIAL_ENTRY_TAB_SIZE 70
  81.  
  82. /* A pointer to either lstat or stat, depending on
  83.    whether dereferencing of symlinks is done.  */
  84. #ifdef MSDOS
  85. int (*xstat) (char *, struct stat *);
  86. #else
  87. int (*xstat) ();
  88. #endif
  89.  
  90. /* The invocation name of this program.  */
  91. char *program_name;
  92.  
  93. /* If nonzero, copy all files except directories and, if not dereferencing
  94.    them, symbolic links, as if they were regular files. */
  95. int flag_copy_as_regular = 1;
  96.  
  97. /* If nonzero, dereference symbolic links (copy the files they point to). */
  98. int flag_dereference = 1;
  99.  
  100. /* If nonzero, remove existing target nondirectories. */
  101. int flag_force = 0;
  102.  
  103. /* If nonzero, query before overwriting existing targets with regular files. */
  104. int flag_interactive = 0;
  105.  
  106. /* If nonzero, give the copies the original files' permissions,
  107.    ownership, and timestamps. */
  108. int flag_preserve = 0;
  109.  
  110. /* If nonzero, copy directories recursively and copy special files
  111.    as themselves rather than copying their contents. */
  112. int flag_recursive = 0;
  113.  
  114. /* If nonzero, when copying recursively, skip any subdirectories that are
  115.    on different filesystems from the one we started on. */
  116. int flag_one_file_system = 0;
  117.  
  118. /* If nonzero, do not copy a nondirectory that has an existing destination
  119.    with the same or newer modification time. */
  120. int flag_update = 0;
  121.  
  122. /* If nonzero, display the names of the files before copying them. */
  123. int flag_verbose = 0;
  124.  
  125. /* The error code to return to the system. */
  126. int exit_status = 0;
  127.  
  128. /* The bits to preserve in created files' modes. */
  129. int umask_kill;
  130.  
  131. struct option long_opts[] =
  132. {
  133. #ifdef MSDOS
  134.   {"copying", 0, NULL, 30},
  135.   {"version", 0, NULL, 31},
  136. #endif
  137.   {"backup", 0, NULL, 'b'},
  138.   {"force", 0, NULL, 'f'},
  139.   {"interactive", 0, NULL, 'i'},
  140.   {"no-dereference", 0, &flag_dereference, 0},
  141.   {"one-file-system", 0, &flag_one_file_system, 1},
  142.   {"preserve", 0, &flag_preserve, 1},
  143.   {"recursive", 0, NULL, 'R'},
  144.   {"suffix", 1, NULL, 'S'},
  145.   {"update", 0, &flag_update, 1},
  146.   {"verbose", 0, &flag_verbose, 1},
  147.   {"version-control", 1, NULL, 'V'},
  148.   {NULL, 0, NULL, 0}
  149. };
  150.  
  151. void
  152. main (argc, argv)
  153.      int argc;
  154.      char *argv[];
  155. {
  156.   int c;
  157.   int ind;
  158.   int make_backups = 0;
  159.   char *version;
  160.  
  161.   program_name = argv[0];
  162.  
  163.   version = getenv ("SIMPLE_BACKUP_SUFFIX");
  164.   if (version)
  165.     simple_backup_suffix = version;
  166.   version = getenv ("VERSION_CONTROL");
  167.  
  168.   /* Find out the current file creation mask, to knock the right bits
  169.      when using chmod.  The creation mask is set to to be liberal, so
  170.      that created directories can be written, even if it would not
  171.      have been allowed with the mask this process was started with.  */
  172.  
  173. #ifdef MSDOS            /* not 100%ly correct ... */
  174.   umask_kill = 07777 ^ umask (0);
  175. #else
  176.   umask_kill = 0777777 ^ umask (0);
  177. #endif
  178.  
  179.   while ((c = getopt_long (argc, argv, "bdfipruvxRS:V:", long_opts, &ind))
  180.      != EOF)
  181.     {
  182.       switch (c)
  183.     {
  184.     case 0:
  185.       break;
  186.  
  187.     case 'b':
  188.       make_backups = 1;
  189.       break;
  190.  
  191.     case 'd':
  192.       flag_dereference = 0;
  193.       break;
  194.  
  195.     case 'f':
  196.       flag_force = 1;
  197.       flag_interactive = 0;
  198.       break;
  199.  
  200.     case 'i':
  201.       flag_force = 0;
  202.       flag_interactive = 1;
  203.       break;
  204.  
  205.     case 'p':
  206.       flag_preserve = 1;
  207.       break;
  208.  
  209.     case 'r':
  210.       flag_recursive = 1;
  211.       flag_copy_as_regular = 1;
  212.       break;
  213.  
  214.     case 'R':
  215.       flag_recursive = 1;
  216.       flag_copy_as_regular = 0;
  217.       break;
  218.  
  219.     case 'u':
  220.       flag_update = 1;
  221.       break;
  222.  
  223.     case 'v':
  224.       flag_verbose = 1;
  225.       break;
  226.  
  227.     case 'x':
  228.       flag_one_file_system = 1;
  229.       break;
  230.  
  231.     case 'S':
  232.       simple_backup_suffix = optarg;
  233.       break;
  234.  
  235.     case 'V':
  236.       version = optarg;
  237.       break;
  238.  
  239. #ifdef MSDOS
  240.     case 30:
  241.       fprintf (stderr, COPYING);
  242.       exit (0);
  243.       break;
  244.  
  245.     case 31:
  246.       fprintf (stderr, VERSION);
  247.       exit (0);
  248.       break;
  249. #endif
  250.  
  251.     default:
  252.       usage ((char *) 0);
  253.     }
  254.     }
  255.  
  256.   if (make_backups)
  257.     backup_type = get_version (version);
  258.  
  259.   if (flag_preserve == 1)
  260. #ifdef MSDOS            /* not 100%ly correct ... */
  261.     umask_kill = 07777;
  262. #else
  263.     umask_kill = 0777777;
  264. #endif
  265.  
  266.   /* The key difference between -d (+no-dereference) and not is the version
  267.      of `stat' to call.  */
  268.  
  269.   if (flag_dereference)
  270.     xstat = stat;
  271.   else
  272.     xstat = lstat;
  273.  
  274.   /* Allocate space for remembering copied and created files.  */
  275.  
  276.   hash_init (INITIAL_HASH_MODULE, INITIAL_ENTRY_TAB_SIZE);
  277.  
  278.   exit_status |= do_copy (argc, argv);
  279.  
  280.   exit (exit_status);
  281. }
  282.  
  283. /* Scan the arguments, and copy each by calling copy.
  284.    Return 0 if successful, 1 if any errors occur. */
  285.  
  286. int
  287. do_copy (argc, argv)
  288.      int argc;
  289.      char *argv[];
  290. {
  291.   char *target;
  292.   struct stat sb;
  293.   int new_dst = 0;
  294.   int ret = 0;
  295.  
  296.   if (optind >= argc)
  297.     usage ("missing file arguments");
  298.   if (optind >= argc - 1)
  299.     usage ("missing file argument");
  300.  
  301.   target = argv[argc - 1];
  302.  
  303.   strip_trailing_slashes (target);
  304.  
  305.   if (lstat (target, &sb))
  306.     {
  307.       if (errno != ENOENT)
  308.     {
  309.       error (0, errno, "%s", target);
  310.       return 1;
  311.     }
  312.       else
  313.     new_dst = 1;
  314.     }
  315.   else
  316.     {
  317.       struct stat sbx;
  318.  
  319.       /* If `target' is not a symlink to a nonexistent file, use
  320.      the results of stat instead of lstat, so we can copy files
  321.      into symlinks to directories. */
  322.       if (stat (target, &sbx) == 0)
  323.     sb = sbx;
  324.     }
  325.  
  326.   if (!new_dst && (sb.st_mode & S_IFMT) == S_IFDIR)
  327.     {
  328.       /* cp e_file_1...e_file_n e_dir
  329.      copy the files `e_file_1' through `e_file_n'
  330.      to the existing directory `e_dir'. */
  331.  
  332.       for (;;)
  333.     {
  334.       char *arg;
  335.       char *ap;
  336.       char *dst_path;
  337.  
  338.       arg = argv[optind];
  339.  
  340.       strip_trailing_slashes (arg);
  341.  
  342.       /* Append the last component of `arg' to `target'.  */
  343.  
  344.       ap = rindex (arg, '/');
  345.       if (ap == 0)
  346.         ap = arg;
  347.       else
  348.         ap++;
  349.       dst_path = xmalloc (strlen (target) + strlen (ap) + 2);
  350. #ifdef MSDOS
  351.       /* Here a trailing slash might still be present (needed for
  352.          stat()'ing root directories), take care of that. */
  353.       if (target[strlen(target) - 1] == '/')
  354.         stpcpy (stpcpy (dst_path, target), ap);
  355.       else
  356. #endif /* MSDOS */
  357.       stpcpy (stpcpy (stpcpy (dst_path, target), "/"), ap);
  358.  
  359.       ret |= copy (arg, dst_path, new_dst, 0, (struct dir_list *) 0);
  360.       forget_all ();
  361.  
  362.       ++optind;
  363.       if (optind == argc - 1)
  364.         break;
  365.     }
  366.       return ret;
  367.     }
  368.   else if (argc - optind == 2)
  369.     return copy (argv[optind], target, new_dst, 0, (struct dir_list *) 0);
  370.   else
  371.     usage ("when copying multiple files, last argument must be a directory");
  372. }
  373.  
  374. /* Copy the file SRC_PATH to the file DST_PATH.  The files may be of
  375.    any type.  NEW_DST should be non-zero if the file DST_PATH cannot
  376.    exist because its parent directory was just created; NEW_DST should
  377.    be zero if DST_PATH might already exist.  DEVICE is the device
  378.    number of the parent directory, or 0 if the parent of this file is
  379.    not known.  ANCESTORS points to a linked, null terminated list of
  380.    devices and inodes of parent directories of SRC_PATH.
  381.    Return 0 if successful, 1 if an error occurs. */
  382.  
  383. int
  384. copy (src_path, dst_path, new_dst, device, ancestors)
  385.      char *src_path;
  386.      char *dst_path;
  387.      int new_dst;
  388.      dev_t device;
  389.      struct dir_list *ancestors;
  390. {
  391.   struct stat src_sb;
  392.   struct stat dst_sb;
  393.   int src_mode;
  394. #ifdef MSDOS
  395.   unsigned int src_type;
  396. #else /* not MSDOS */
  397.   int src_type;
  398. #endif /* not MSDOS */
  399.   char *earlier_file;
  400.   char *dst_backup = NULL;
  401.   int dir_mode_changed = 0;
  402.  
  403.   if ((*xstat) (src_path, &src_sb))
  404.     {
  405.       error (0, errno, "%s", src_path);
  406.       return 1;
  407.     }
  408.  
  409.   /* Are we crossing a file system boundary?  */
  410.   if (flag_one_file_system && device != 0 && device != src_sb.st_dev)
  411.     return 0;
  412.  
  413.   /* We wouldn't insert a node unless nlink > 1, except that we need to
  414.      find created files so as to not copy infinitely if a directory is
  415.      copied into itself.  */
  416.  
  417.   earlier_file = remember_copied (dst_path, src_sb.st_ino, src_sb.st_dev);
  418.  
  419.   /* Did we just create this file?  */
  420.  
  421.   if (earlier_file == &new_file)
  422.     return 0;
  423.  
  424.   src_mode = src_sb.st_mode;
  425.   src_type = src_sb.st_mode & S_IFMT;
  426.   if (flag_copy_as_regular && src_type != S_IFDIR
  427. #ifdef S_IFLNK
  428.       && src_type != S_IFLNK
  429. #endif
  430.       )
  431.     src_type = S_IFREG;
  432.  
  433.   if (src_type == S_IFDIR && !flag_recursive)
  434.     {
  435.       error (0, 0, "%s: omitting directory", src_path);
  436.       return 1;
  437.     }
  438.  
  439.   if (!new_dst)
  440.     {
  441.       if ((*xstat) (dst_path, &dst_sb))
  442.     {
  443.       if (errno != ENOENT)
  444.         {
  445.           error (0, errno, "%s", dst_path);
  446.           return 1;
  447.         }
  448.       else
  449.         new_dst = 1;
  450.     }
  451.       else
  452.     {
  453.       /* The file exists already.  */
  454. #ifdef MSDOS
  455.       if (strcmp (dst_path, src_path) == 0)
  456.         {
  457.           error (0, 0, "`%s': can't copy file to itself", src_path);
  458.           return 1;
  459.         }
  460. #else /* not MSDOS */
  461.       if (src_sb.st_ino == dst_sb.st_ino && src_sb.st_dev == dst_sb.st_dev)
  462.         {
  463.           error (0, 0, "`%s' and `%s' are the same file",
  464.              src_path, dst_path);
  465.           return 1;
  466.         }
  467. #endif /* not MSDOS */
  468.  
  469.       if (src_type != S_IFDIR)
  470.         {
  471.           if ((dst_sb.st_mode & S_IFMT) == S_IFDIR)
  472.         {
  473.           error (0, 0,
  474.              "%s: cannot overwrite directory with non-directory",
  475.              dst_path);
  476.           return 1;
  477.         }
  478.  
  479.           if (flag_update && src_sb.st_mtime <= dst_sb.st_mtime)
  480.         return 0;
  481.         }
  482.  
  483.       if (src_type == S_IFREG && !flag_force)
  484.         {
  485.           /* Treat the file as nonwritable if it lacks write permission
  486.          bits, even if we are root.  */
  487.           if (eaccess_stat (&dst_sb, W_OK) != 0
  488.           || (dst_sb.st_mode & 0222) == 0)
  489.         {
  490.           error (0, 0, "%s: Permission denied", dst_path);
  491.           return 1;
  492.         }
  493.  
  494.           if (flag_interactive)
  495.         {
  496.           fprintf (stderr, "%s: overwrite `%s'? ", program_name,
  497.                dst_path);
  498.           if (!yesno ())
  499.             return 0;
  500.         }
  501.         }
  502.  
  503.       if (backup_type != none && (dst_sb.st_mode & S_IFMT) != S_IFDIR)
  504.         {
  505.           dst_backup = find_backup_file_name (dst_path);
  506.           if (dst_backup == NULL)
  507.         error (1, 0, "virtual memory exhausted");
  508.           if (rename (dst_path, dst_backup))
  509.         {
  510.           if (errno != ENOENT)
  511.             {
  512.               error (0, errno, "cannot backup `%s'", dst_path);
  513.               free (dst_backup);
  514.               return 1;
  515.             }
  516.           else
  517.             {
  518.               free (dst_backup);
  519.               dst_backup = NULL;
  520.             }
  521.         }
  522.           new_dst = 1;
  523.         }
  524.       else if (flag_force)
  525.         {
  526.           if ((dst_sb.st_mode & S_IFMT) == S_IFDIR)
  527.         {
  528.           /* Temporarily change mode to allow overwriting. */
  529.           if (eaccess_stat (&dst_sb, W_OK | X_OK) != 0)
  530.             {
  531.               if (chmod (dst_path, 0700))
  532.             {
  533.               error (0, errno, "%s", dst_path);
  534.               return 1;
  535.             }
  536.               else
  537.             dir_mode_changed = 1;
  538.             }
  539.         }
  540.           else
  541.         {
  542.           if (unlink (dst_path) && errno != ENOENT)
  543.             {
  544.               error (0, errno, "cannot remove old link to `%s'",
  545.                  dst_path);
  546.               return 1;
  547.             }
  548.           new_dst = 1;
  549.         }
  550.         }
  551.     }
  552.     }
  553.  
  554.   if (flag_verbose)
  555.     printf ("%s -> %s\n", src_path, dst_path);
  556.  
  557.   /* Did we copy this inode somewhere else (in this command line argument)
  558.      and therefore this is a second hard link to the inode?  */
  559.  
  560.   if (!flag_dereference && src_sb.st_nlink > 1 && earlier_file)
  561.     {
  562.       if (link (earlier_file, dst_path))
  563.     {
  564.       error (0, errno, "%s", dst_path);
  565.       goto un_backup;
  566.     }
  567.       if (dst_backup)
  568.     free (dst_backup);
  569.       return 0;
  570.     }
  571.  
  572.   switch (src_type)
  573.     {
  574. #ifdef S_IFIFO
  575.     case S_IFIFO:
  576.       if (mkfifo (dst_path, src_mode & umask_kill))
  577.     {
  578.       error (0, errno, "cannot make fifo `%s'", dst_path);
  579.       goto un_backup;
  580.     }
  581.       break;
  582. #endif
  583.  
  584. #ifndef MSDOS
  585.     case S_IFBLK:
  586.     case S_IFCHR:
  587. #ifdef S_IFSOCK
  588.     case S_IFSOCK:
  589. #endif
  590.       if (mknod (dst_path, src_mode & umask_kill, src_sb.st_rdev))
  591.     {
  592.       error (0, errno, "cannot create special file `%s'", dst_path);
  593.       goto un_backup;
  594.     }
  595.       break;
  596. #endif /* not MSDOS */
  597.  
  598.     case S_IFDIR:
  599.       {
  600.     struct dir_list *dir;
  601.  
  602.     /* If this directory has been copied before during the
  603.            recursion, there is a symbolic link to an ancestor
  604.            directory of the symbolic link.  It is impossible to
  605.            continue to copy this, unless we've got an infinite disk.  */
  606.  
  607.     if (is_ancestor (&src_sb, ancestors))
  608.       {
  609.         error (0, 0, "%s: cannot copy cyclic symbolic link", src_path);
  610.         goto un_backup;
  611.       }
  612.  
  613.     /* Insert the current directory in the list of parents.  */
  614.  
  615.     dir = (struct dir_list *) alloca (sizeof (struct dir_list));
  616. #ifdef MSDOS            /* always short of stack space ... */
  617.     if (!dir)
  618.       error (1, 0, "%s: stack overflow", src_path);
  619. #endif
  620.  
  621.     dir->parent = ancestors;
  622.     dir->ino = src_sb.st_ino;
  623.     dir->dev = src_sb.st_dev;
  624.  
  625.     if (new_dst || (dst_sb.st_mode & S_IFMT) != S_IFDIR)
  626.       {
  627.         /* Create the new directory writable and searchable, so
  628.            we can create new entries in it.  */
  629.  
  630.         if (mkdir (dst_path, 0700))
  631.           {
  632.         error (0, errno, "cannot create directory `%s'", dst_path);
  633.         goto un_backup;
  634.           }
  635.  
  636.         /* Insert the created directory's inode and device
  637.            numbers into the search structure, so that we can
  638.            avoid copying it again.  */
  639.  
  640.         if (remember_created (dst_path))
  641.           goto un_backup;
  642.       }
  643.  
  644.     /* Copy the contents of the directory.  */
  645.  
  646.     if (copy_dir (src_path, dst_path, new_dst, &src_sb, dir))
  647.       goto err_return;
  648.       }
  649.       break;
  650.  
  651.     case S_IFREG:
  652.       if (copy_reg (src_path, dst_path))
  653.     goto un_backup;
  654.       break;
  655.  
  656. #ifdef S_IFLNK
  657.     case S_IFLNK:
  658.       {
  659.     char *link_val = (char *) alloca (src_sb.st_size + 1);
  660.  
  661.     if (readlink (src_path, link_val, src_sb.st_size) < 0)
  662.       {
  663.         error (0, errno, "cannot read symbolic link `%s'", src_path);
  664.         goto un_backup;
  665.       }
  666.     link_val[src_sb.st_size] = '\0';
  667.  
  668.     if (symlink (link_val, dst_path))
  669.       {
  670.         error (0, errno, "cannot create symbolic link `%s'", dst_path);
  671.         goto un_backup;
  672.       }
  673.       }
  674.       return 0;
  675. #endif
  676.  
  677.     default:
  678.       error (0, 0, "%s: unknown file type", src_path);
  679.       goto un_backup;
  680.     }
  681.  
  682.   if ((flag_preserve || new_dst)
  683.       && (src_type == S_IFREG || src_type == S_IFDIR))
  684.     {
  685.       if (chmod (dst_path, src_mode & umask_kill))
  686.     {
  687.       error (0, errno, "%s", dst_path);
  688.       goto err_return;
  689.     }
  690.     }
  691.   else if (dir_mode_changed)
  692.     {
  693.       /* Reset the temporarily changed mode.  */
  694.       if (chmod (dst_path, dst_sb.st_mode))
  695.     {
  696.       error (0, errno, "%s", dst_path);
  697.       goto err_return;
  698.     }
  699.     }
  700.  
  701.   /* Adjust the times (and if possible, ownership) for the copy. */
  702.  
  703.   if (flag_preserve)
  704.     {
  705.       struct utimbuf utb;
  706.  
  707.       utb.actime = src_sb.st_atime;
  708.       utb.modtime = src_sb.st_mtime;
  709.  
  710.       if (utime (dst_path, &utb))
  711.     {
  712.       error (0, errno, "%s", dst_path);
  713.       goto err_return;
  714.     }
  715.  
  716.       if (chown (dst_path, src_sb.st_uid, src_sb.st_gid) && errno != EPERM)
  717.     {
  718.       error (0, errno, "%s", dst_path);
  719.       goto err_return;
  720.     }
  721.     }
  722.  
  723.   if (dst_backup)
  724.     free (dst_backup);
  725.   return 0;
  726.  
  727. err_return:
  728.   if (dst_backup)
  729.     free (dst_backup);
  730.   return 1;
  731.  
  732. un_backup:
  733.   if (dst_backup)
  734.     {
  735.       if (rename (dst_backup, dst_path))
  736.     error (0, errno, "cannot un-backup `%s'", dst_path);
  737.       free (dst_backup);
  738.     }
  739.   return 1;
  740. }
  741.  
  742. /* Read the contents of the directory SRC_PATH_IN, and recursively
  743.    copy the contents to DST_PATH_IN.  NEW_DST is non-zero if
  744.    DST_PATH_IN is a directory that was created previously in the
  745.    recursion.   SRC_SB and ANCESTORS describe SRC_PATH_IN.
  746.    Return 0 if successful, -1 if an error occurs. */
  747.  
  748. int
  749. copy_dir (src_path_in, dst_path_in, new_dst, src_sb, ancestors)
  750.      char *src_path_in;
  751.      char *dst_path_in;
  752.      int new_dst;
  753.      struct stat *src_sb;
  754.      struct dir_list *ancestors;
  755. {
  756.   char *name_space;
  757.   char *namep;
  758.   char *src_path;
  759.   char *dst_path;
  760.   int ret = 0;
  761.  
  762.   errno = 0;
  763. #ifdef MSDOS
  764.   assert (src_sb->st_size < 0xffffL);
  765.   name_space = savedir (src_path_in, (size_t) src_sb->st_size);
  766. #else
  767.   name_space = savedir (src_path_in, src_sb->st_size);
  768. #endif
  769.   if (name_space == 0)
  770.     {
  771.       if (errno)
  772.     {
  773.       error (0, errno, "%s", src_path_in);
  774.       return -1;
  775.     }
  776.       else
  777.     error (1, 0, "virtual memory exhausted");
  778.     }
  779.  
  780.   namep = name_space;
  781.   while (*namep != '\0')
  782.     {
  783.       int fn_length = strlen (namep) + 1;
  784.  
  785.       dst_path = xmalloc (strlen (dst_path_in) + fn_length + 1);
  786.       src_path = xmalloc (strlen (src_path_in) + fn_length + 1);
  787.  
  788.       stpcpy (stpcpy (stpcpy (src_path, src_path_in), "/"), namep);
  789.       stpcpy (stpcpy (stpcpy (dst_path, dst_path_in), "/"), namep);
  790.  
  791.       ret |= copy (src_path, dst_path, new_dst, src_sb->st_dev, ancestors);
  792.  
  793.       /* Free the memory for `src_path'.  The memory for `dst_path'
  794.      cannot be deallocated, since it is used to create multiple
  795.      hard links.  */
  796.  
  797.       free (src_path);
  798.  
  799.       namep += fn_length;
  800.     }
  801.   free (name_space);
  802.   return -ret;
  803. }
  804.  
  805. /* Copy a regular file from SRC_PATH to DST_PATH.  Large blocks of zeroes,
  806.    as well as holes in the source file, are made into holes in the
  807.    target file.  (Holes are read as zeroes by the `read' system call.)
  808.    Return 0 if successful, -1 if an error occurred. */
  809.  
  810. int
  811. copy_reg (src_path, dst_path)
  812.      char *src_path;
  813.      char *dst_path;
  814. {
  815.   char *buf;
  816.   int buf_size;
  817.   int target_desc;
  818.   int source_desc;
  819.   int n_read;
  820.   int n_written;
  821.   struct stat sb;
  822.   int return_val = 0;
  823.   long n_read_total = 0;
  824.   char *cp;
  825.   int *ip;
  826.   int last_write_made_hole = 0;
  827.  
  828. #ifdef MSDOS
  829.   source_desc = open (src_path, O_BINARY | O_RDONLY);
  830. #else
  831.   source_desc = open (src_path, O_RDONLY);
  832. #endif
  833.   if (source_desc < 0)
  834.     {
  835.       error (0, errno, "%s", src_path);
  836.       return -1;
  837.     }
  838.  
  839.   /* Create the new regular file with small permissions initially,
  840.      to not create a security hole.  */
  841.  
  842. #ifdef MSDOS
  843.   target_desc = open (dst_path, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0600);
  844. #else
  845.   target_desc = open (dst_path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  846. #endif
  847.   if (target_desc < 0)
  848.     {
  849.       error (0, errno, "cannot create regular file `%s'", dst_path);
  850.       return_val = -1;
  851.       goto ret2;
  852.     }
  853.  
  854.   /* Find out the appropriate buffer length.  */
  855.  
  856.   if (fstat (target_desc, &sb))
  857.     {
  858.       error (0, errno, "%s", dst_path);
  859.       return_val = -1;
  860.       goto ret;
  861.     }
  862.  
  863.   buf_size = ST_BLKSIZE (sb);
  864.  
  865.   /* Make a buffer with space for a sentinel at the end.  */
  866.  
  867.   buf = (char *) alloca (buf_size + sizeof (int));
  868. #ifdef MSDOS
  869.   if (!buf)
  870.     error (2, 0, "%s: stack overflow", src_path);
  871. #endif
  872.  
  873.   for (;;)
  874.     {
  875.       n_read = read (source_desc, buf, buf_size);
  876.       if (n_read < 0)
  877.     {
  878.       error (0, errno, "%s", src_path);
  879.       return_val = -1;
  880.       goto ret;
  881.     }
  882.       if (n_read == 0)
  883.     break;
  884.  
  885. #ifdef MSDOS
  886.       n_read_total += (long) n_read;
  887. #else
  888.       n_read_total += n_read;
  889. #endif
  890.  
  891.       buf[n_read] = 1;        /* Sentinel to stop loop.  */
  892.  
  893.       /* Find first non-zero *word*, or the word with the sentinel.  */
  894.  
  895.       ip = (int *) buf;
  896.       while (*ip++ == 0)
  897.     ;
  898.  
  899.       /* Find the first non-zero *byte*, or the sentinel.  */
  900.  
  901.       cp = (char *) (ip - 1);
  902.       while (*cp++ == 0)
  903.     ;
  904.  
  905.       /* If we found the sentinel, the whole input block was zero,
  906.      and we can make a hole.  */
  907.  
  908.       if (cp > buf + n_read)
  909.     {
  910.       /* Make a hole.  */
  911.       if (lseek (target_desc, (off_t) n_read, L_INCR) < 0L)
  912.         {
  913.           error (0, errno, "%s", dst_path);
  914.           return_val = -1;
  915.           goto ret;
  916.         }
  917.       last_write_made_hole = 1;
  918.     }
  919.       else
  920.     {
  921.       n_written = write (target_desc, buf, n_read);
  922.       if (n_written < n_read)
  923.         {
  924.           error (0, errno, "%s", dst_path);
  925.           return_val = -1;
  926.           goto ret;
  927.         }
  928.       last_write_made_hole = 0;
  929.     }
  930.     }
  931.  
  932.   /* If the file ends with a `hole', something needs to be written at
  933.      the end.  Otherwise the kernel would truncate the file at the end
  934.      of the last write operation.  */
  935.  
  936.   if (last_write_made_hole)
  937.     {
  938.       /* Seek backwards one character and write a null.  */
  939.       if (lseek (target_desc, (off_t) -1, L_INCR) < 0L
  940.       || write (target_desc, "", 1) != 1)
  941.     {
  942.       error (0, errno, "%s", dst_path);
  943.       return_val = -1;
  944.     }
  945.     }
  946.  
  947. ret:
  948.   close (target_desc);
  949. ret2:
  950.   close (source_desc);
  951.  
  952.   return return_val;
  953. }
  954.