home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tar-1.11.8-src.tgz / tar.out / fsf / tar / src / buffer.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  38KB  |  1,545 lines

  1. /* Buffer management for tar.
  2.    Copyright (C) 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GNU Tar.
  5.  
  6.    GNU Tar is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GNU Tar is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GNU Tar; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Buffer management for tar.
  21.    Written by John Gilmore, on 25 August 1985.  */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #ifdef HAVE_VFORK
  28. #define fork vfork
  29. #endif
  30.  
  31. #include "system.h"
  32.  
  33. #include <signal.h>
  34. #include <time.h>
  35. time_t time ();
  36.  
  37. #ifdef    __MSDOS__
  38. #include <process.h>
  39. #endif
  40.  
  41. #ifdef XENIX
  42. #include <sys/inode.h>
  43. #endif
  44.  
  45. #if WITH_REGEX
  46. # include <regex.h>
  47. #else
  48. # include <rx.h>
  49. #endif
  50.  
  51. #include "rmt.h"
  52. #include "tar.h"
  53.  
  54. /* Where we write messages (standard messages, not errors) to.  Stdout
  55.    unless we're writing a pipe, in which case stderr.  */
  56. FILE *stdlis;
  57.  
  58. #define    STDIN    0        /* standard input  file descriptor */
  59. #define    STDOUT    1        /* standard output file descriptor */
  60.  
  61. #define    PREAD    0        /* read  file descriptor from pipe() */
  62. #define    PWRITE    1        /* write file descriptor from pipe() */
  63.  
  64. static int backspace_output __P ((void));
  65. static int new_volume __P ((int));
  66. static void writeerror __P ((int));
  67. static void readerror __P ((void));
  68.  
  69. #ifndef __MSDOS__
  70. /* Obnoxious test to see if dimwit is trying to dump the archive */
  71. dev_t ar_dev;
  72. ino_t ar_ino;
  73. #endif
  74.  
  75. /* The record pointed to by save_rec should not be overlaid when reading
  76.    in a new tape block.  Copy it to record_save_area first, and change
  77.    the pointer in *save_rec to point to record_save_area.  Saved_recno
  78.    records the record number at the time of the save.  This is used by
  79.    annofile() to print the record number of a file's header record.  */
  80. static union record **save_rec;
  81. union record record_save_area;
  82. static long saved_recno;
  83.  
  84. /* PID of child program, if flag_compress or remote archive access.  */
  85. static int childpid = 0;
  86.  
  87. /* Record number of the start of this block of records  */
  88. long baserec;
  89.  
  90. /* Error recovery stuff  */
  91. static int r_error_count;
  92.  
  93. /* Have we hit EOF yet?  */
  94. static int hit_eof;
  95.  
  96. /* Checkpointing counter */
  97. static int checkpoint;
  98.  
  99. /* JF we're reading, but we just read the last record and its time to update */
  100. extern time_to_start_writing;
  101. int file_to_switch_to = -1;    /* if remote update, close archive, and use
  102.                    this descriptor to write to */
  103.  
  104. static int volno = 1;        /* JF which volume of a multi-volume tape
  105.                    we're on */
  106. static int global_volno = 1;    /* volume number to print in external messages */
  107.  
  108. char *save_name;        /* name of the file we are currently writing */
  109. long save_totsize;        /* total size of file we are writing, only
  110.                    valid if save_name is non NULL */
  111. long save_sizeleft;        /* where we are in the file we are writing,
  112.                    only valid if save_name is non-zero */
  113.  
  114. int write_archive_to_stdout;
  115.  
  116. /* Used by fl_read and fl_write to store the real info about saved names */
  117. static char real_s_name[NAMSIZ];
  118. static long real_s_totsize;
  119. static long real_s_sizeleft;
  120.  
  121. /*---------------------------------------------------------.
  122. | Reset the EOF flag (if set), and re-set ar_record, etc.  |
  123. `---------------------------------------------------------*/
  124.  
  125. void
  126. reset_eof (void)
  127. {
  128.   if (hit_eof)
  129.     {
  130.       hit_eof = 0;
  131.       ar_record = ar_block;
  132.       ar_last = ar_block + blocking;
  133.       ar_reading = 0;
  134.     }
  135. }
  136.  
  137. /*-------------------------------------------------------------------------.
  138. | Return the location of the next available input or output record.       |
  139. | Return NULL for EOF.  Once we have returned NULL, we just keep returning |
  140. | it, to avoid accidentally going on to the next file on the "tape".       |
  141. `-------------------------------------------------------------------------*/
  142.  
  143. union record *
  144. findrec (void)
  145. {
  146.   if (ar_record == ar_last)
  147.     {
  148.       if (hit_eof)
  149.     return NULL;
  150.       flush_archive ();
  151.       if (ar_record == ar_last)
  152.     {
  153.       hit_eof++;
  154.       return NULL;
  155.     }
  156.     }
  157.   return ar_record;
  158. }
  159.  
  160. /*----------------------------------------------------------------------.
  161. | Indicate that we have used all records up thru the argument.  (should |
  162. | the arg have an off-by-1?  FIXME)                        |
  163. `----------------------------------------------------------------------*/
  164.  
  165. void
  166. userec (union record *rec)
  167. {
  168.   while (rec >= ar_record)
  169.     ar_record++;
  170.  
  171.   /* Do *not* flush the archive here.  If we do, the same argument to
  172.      userec() could mean the next record (if the input block is exactly
  173.      one record long), which is not what is intended.  */
  174.  
  175.   if (ar_record > ar_last)
  176.     abort ();
  177. }
  178.  
  179. /*----------------------------------------------------------------------.
  180. | Return a pointer to the end of the current records buffer.  All the   |
  181. | space between findrec() and endofrecs() is available for filling with |
  182. | data, or taking data from.                            |
  183. `----------------------------------------------------------------------*/
  184.  
  185. union record *
  186. endofrecs (void)
  187. {
  188.   return ar_last;
  189. }
  190.  
  191. /*--------------------------------------------------------------------.
  192. | Duplicate a file descriptor into a certain slot.  Equivalent to BSD |
  193. | "dup2" with error reporting.                          |
  194. `--------------------------------------------------------------------*/
  195.  
  196. static void
  197. dupto (int from, int to, const char *msg)
  198. {
  199.   int err;
  200.  
  201.   if (from != to)
  202.     {
  203.       err = close (to);
  204.       if (err < 0 && errno != EBADF)
  205.     ERROR ((TAREXIT_FAILURE, errno, _("Cannot close descriptor %d"), to));
  206.       err = dup (from);
  207.       if (err != to)
  208.     ERROR ((TAREXIT_FAILURE, errno, _("Cannot dup %s"), msg));
  209.       ck_close (from);
  210.     }
  211. }
  212.  
  213. /*---.
  214. | ?  |
  215. `---*/
  216.  
  217. #ifdef __MSDOS__
  218. static void
  219. child_open (void)
  220. {
  221.   ERROR ((TAREXIT_FAILURE, 0, _("Cannot use compressed or remote archives")))
  222. }
  223.  
  224. #else
  225. static void
  226. child_open (void)
  227. {
  228.   int local_pipe[2];
  229.   int err = 0;
  230.  
  231.   int kidpipe[2];
  232.   int kidchildpid;
  233.  
  234. #define READ    0
  235. #define WRITE    1
  236.  
  237.   ck_pipe (local_pipe);
  238.  
  239.   childpid = fork ();
  240.   if (childpid < 0)
  241.     ERROR ((TAREXIT_FAILURE, errno, _("Cannot fork")));
  242.  
  243.   if (childpid > 0)
  244.     {
  245.  
  246.       /* We're the parent.  Clean up and be happy.  This, at least, is
  247.      easy.  */
  248.  
  249.       if (ar_reading)
  250.     {
  251.       flag_reblock++;
  252.       archive = local_pipe[READ];
  253.       ck_close (local_pipe[WRITE]);
  254.     }
  255.       else
  256.     {
  257.       archive = local_pipe[WRITE];
  258.       ck_close (local_pipe[READ]);
  259.     }
  260.       return;
  261.     }
  262.  
  263.   /* We're the kid.  */
  264.  
  265.   if (ar_reading)
  266.     {
  267.       dupto (local_pipe[WRITE], STDOUT, _("(child) Pipe to stdout"));
  268.       ck_close (local_pipe[READ]);
  269.     }
  270.   else
  271.     {
  272.       dupto (local_pipe[READ], STDIN, _("(child) Pipe to stdin"));
  273.       ck_close (local_pipe[WRITE]);
  274.     }
  275.  
  276.   /* We need a child tar only if
  277.      1: we're reading/writing stdin/out (to force reblocking),
  278.      2: the file is to be accessed by rmt (compress doesn't know how),
  279.      3: the file is not a plain file.  */
  280.  
  281.   if (strcmp (archive_name_array[0], "-") != 0
  282.       && !_remdev (archive_name_array[0])
  283.       && isfile (archive_name_array[0]))
  284.     {
  285.  
  286.       /* We don't need a child tar.  Open the archive.  */
  287.  
  288.       if (ar_reading)
  289.     {
  290.       archive = open (archive_name_array[0], O_RDONLY | O_BINARY, 0666);
  291.       if (archive < 0)
  292.         ERROR ((TAREXIT_FAILURE, errno, _("Cannot open archive %s"),
  293.             archive_name_array[0]));
  294.       dupto (archive, STDIN, _("Archive to stdin"));
  295. #if 0
  296.       close (archive);
  297. #endif
  298.     }
  299.       else
  300.     {
  301.       archive = creat (archive_name_array[0], 0666);
  302.       if (archive < 0)
  303.         ERROR ((TAREXIT_FAILURE, errno, _("Cannot open archive %s"),
  304.             archive_name_array[0]));
  305.       dupto (archive, STDOUT, _("Archive to stdout"));
  306. #if 0
  307.       close (archive);
  308. #endif
  309.     }
  310.     }
  311. /* Circumvent the blocking factor of tar, which needs a true fork */
  312. #ifndef __amigaos__
  313.   else
  314.     {
  315.  
  316.       /* We need a child tar.  */
  317.  
  318.       ck_pipe (kidpipe);
  319.  
  320.       kidchildpid = fork ();
  321.       if (kidchildpid < 0)
  322.     ERROR ((TAREXIT_FAILURE, errno, _("Child cannot fork")));
  323.  
  324.       if (kidchildpid > 0)
  325.     {
  326.  
  327.       /* About to exec compress:  set up the files.  */
  328.  
  329.       if (ar_reading)
  330.         {
  331.           dupto (kidpipe[READ], STDIN, _("((child)) Pipe to stdin"));
  332.           ck_close (kidpipe[WRITE]);
  333. #if 0
  334.           dup2 (local_pipe[WRITE], STDOUT);
  335. #endif
  336.         }
  337.       else
  338.         {
  339. #if 0
  340.           dup2 (local_pipe[READ], STDIN);
  341. #endif
  342.           dupto (kidpipe[WRITE], STDOUT, _("((child)) Pipe to stdout"));
  343.           ck_close (kidpipe[READ]);
  344.         }
  345. #if 0
  346.       ck_close (local_pipe[READ]);
  347.       ck_close (local_pipe[WRITE]);
  348.       ck_close (kidpipe[READ]);
  349.       ck_close (kidpipe[WRITE]);
  350. #endif
  351.     }
  352.       else
  353.     {
  354.  
  355.       /* Grandchild.  Do the right thing, namely sit here and
  356.          read/write the archive, and feed stuff back to compress.  */
  357.  
  358.       program_name = _("tar (child)");
  359.       if (ar_reading)
  360.         {
  361.           dupto (kidpipe[WRITE], STDOUT, _("[child] Pipe to stdout"));
  362.           ck_close (kidpipe[READ]);
  363.         }
  364.       else
  365.         {
  366.           dupto (kidpipe[READ], STDIN, _("[child] Pipe to stdin"));
  367.           ck_close (kidpipe[WRITE]);
  368.         }
  369.  
  370.       if (strcmp (archive_name_array[0], "-") == 0)
  371.         {
  372.           if (ar_reading)
  373.         archive = STDIN;
  374.           else
  375.         archive = STDOUT;
  376.         }
  377.       else
  378. #if 0
  379.         /* This can't happen.  */
  380.  
  381.         if (ar_reading==2)
  382.            archive
  383.          = rmtopen (archive_name_array[0], O_RDWR|O_CREAT | O_BINARY,
  384.                 0666, flag_rsh_command);
  385.         else
  386. #endif
  387.           if (ar_reading)
  388.         archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
  389.                    0666, flag_rsh_command);
  390.           else
  391.         archive = rmtcreat (archive_name_array[0], 0666, flag_rsh_command);
  392.  
  393.       if (archive < 0)
  394.         ERROR ((TAREXIT_FAILURE, errno, _("Cannot open archive %s"),
  395.             archive_name_array[0]));
  396.  
  397.       if (ar_reading)
  398.         {
  399.           while (1)
  400.         {
  401.           char *ptr;
  402.           int max, count;
  403.  
  404.           r_error_count = 0;
  405.  
  406.         error_loop:
  407.           err = rmtread (archive, ar_block->charptr,
  408.                  (unsigned int) (blocksize));
  409.           if (err < 0)
  410.             {
  411.               readerror ();
  412.               goto error_loop;
  413.             }
  414.           if (err == 0)
  415.             break;
  416.           ptr = ar_block->charptr;
  417.           max = err;
  418.           while (max)
  419.             {
  420.               count = (max < RECORDSIZE) ? max : RECORDSIZE;
  421.               err = write (STDOUT, ptr, (size_t) count);
  422.               if (err < 0)
  423.             ERROR ((TAREXIT_FAILURE, errno, _("\
  424. Cannot write to compression program")));
  425.               
  426.               if (err != count)
  427.             {
  428.               ERROR ((0, 0, _("\
  429. Write to compression program short %d bytes"),
  430.                      count - err));
  431.               count = err;
  432.             }
  433.  
  434.               ptr += count;
  435.               max -= count;
  436.             }
  437.         }
  438.         }
  439.       else
  440.         {
  441.           while (1)
  442.         {
  443.           int n;
  444.           char *ptr;
  445.  
  446.           n = blocksize;
  447.           ptr = ar_block->charptr;
  448.           while (n)
  449.             {
  450.               err
  451.             = read (STDIN, ptr,
  452.                 (size_t) ((n < RECORDSIZE) ? n : RECORDSIZE));
  453.               if (err <= 0)
  454.             break;
  455.               n -= err;
  456.               ptr += err;
  457.             }
  458.  
  459.           /* EOF */
  460.  
  461.           if (err == 0)
  462.             {
  463.               if (!flag_compress_block)
  464.             blocksize -= n;
  465.               else
  466.             memset (ar_block->charptr + blocksize - n, 0, (size_t) n);
  467.               err = rmtwrite (archive, ar_block->charptr,
  468.                       (unsigned int) blocksize);
  469.               if (err != (blocksize))
  470.             writeerror (err);
  471.               if (!flag_compress_block)
  472.             blocksize += n;
  473.               break;
  474.             }
  475.  
  476.           if (n)
  477.             ERROR ((TAREXIT_FAILURE, errno,
  478.                    _("Cannot read from compression program")));
  479.  
  480.           err = rmtwrite (archive, ar_block->charptr,
  481.                   (unsigned int) blocksize);
  482.           if (err != blocksize)
  483.             writeerror (err);
  484.         }
  485.         }
  486.  
  487. #if 0
  488.       close_archive ();
  489. #endif
  490.       exit (exit_status);
  491.     }
  492.     }
  493. #endif /* #ifndef __amigaos__ */
  494.  
  495.   /* So we should exec compress (-d).  */
  496.  
  497.   if (ar_reading)
  498.     execlp (flag_compressprog, flag_compressprog, "-d", (char *) 0);
  499.   else
  500.     execlp (flag_compressprog, flag_compressprog, (char *) 0);
  501.  
  502.   /* In a previous tar version, _exit was used here instead of exit.  */
  503.   ERROR ((TAREXIT_FAILURE, errno, _("Cannot exec %s"), flag_compressprog));
  504. }
  505.  
  506. /*--------------------------------------------------.
  507. | Return non-zero if P is the name of a directory.  |
  508. `--------------------------------------------------*/
  509.  
  510. int
  511. isfile (const char *p)
  512. {
  513.   struct stat stbuf;
  514.  
  515.   if (stat (p, &stbuf) < 0)
  516.     return 1;
  517.   if (S_ISREG (stbuf.st_mode))
  518.     return 1;
  519.   return 0;
  520. }
  521.  
  522. #endif
  523.  
  524. /*------------------------------------------------------------------------.
  525. | Open an archive file.  The argument specifies whether we are reading or |
  526. | writing.                                  |
  527. `------------------------------------------------------------------------*/
  528.  
  529. /* JF if the arg is 2, open for reading and writing.  */
  530.  
  531. void
  532. open_archive (int reading)
  533. {
  534.   stdlis = flag_exstdout ? stderr : stdout;
  535.  
  536.   if (blocksize == 0)
  537.     ERROR ((TAREXIT_FAILURE, 0, _("Invalid value for blocksize")));
  538.  
  539.   if (archive_names == 0)
  540.     ERROR ((TAREXIT_FAILURE, 0,
  541.         _("No archive name given, what should I do?")));
  542.  
  543.   current_file_name = NULL;
  544.   current_link_name = NULL;
  545.   save_name = NULL;
  546.  
  547.   if (flag_multivol)
  548.     {
  549.       ar_block
  550.     = (union record *) valloc ((unsigned) (blocksize + (2 * RECORDSIZE)));
  551.       if (ar_block)
  552.     ar_block += 2;
  553.     }
  554.   else
  555.     ar_block = (union record *) valloc ((unsigned) blocksize);
  556.   if (!ar_block)
  557.     ERROR ((TAREXIT_FAILURE, 0,
  558.         _("Could not allocate memory for blocking factor %d"), blocking));
  559.  
  560.   ar_record = ar_block;
  561.   ar_last = ar_block + blocking;
  562.   ar_reading = reading;
  563.  
  564.   if (flag_multivol && flag_verify)
  565.     ERROR ((TAREXIT_FAILURE, 0, _("Cannot verify multi-volume archives")));
  566.  
  567.   if (flag_compressprog)
  568.     {
  569.       if (reading == 2 || flag_verify)
  570.     ERROR ((TAREXIT_FAILURE, 0,
  571.         _("Cannot update or verify compressed archives")));
  572.       if (flag_multivol)
  573.     ERROR ((TAREXIT_FAILURE, 0,
  574.         _("Cannot use multi-volume compressed archives")));
  575.       child_open ();
  576.       if (!reading && strcmp (archive_name_array[0], "-") == 0)
  577.     stdlis = stderr;
  578. #if 0
  579.       child_open (rem_host, rem_file);
  580. #endif
  581.     }
  582.   else if (strcmp (archive_name_array[0], "-") == 0)
  583.     {
  584.       flag_reblock++;        /* could be a pipe, be safe */
  585.       if (flag_verify)
  586.     ERROR ((TAREXIT_FAILURE, 0, _("Cannot verify stdin/stdout archive")));
  587.       if (reading == 2)
  588.     {
  589.       archive = STDIN;
  590.       stdlis = stderr;
  591.       write_archive_to_stdout++;
  592.     }
  593.       else if (reading)
  594.     archive = STDIN;
  595.       else
  596.     {
  597.       archive = STDOUT;
  598.       stdlis = stderr;
  599.     }
  600.     }
  601.   else if (reading == 2 || flag_verify)
  602.     archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
  603.                0666, flag_rsh_command);
  604.   else if (reading)
  605.     archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY, 0666,
  606.                flag_rsh_command);
  607.   else
  608.     archive = rmtcreat (archive_name_array[0], 0666, flag_rsh_command);
  609.  
  610.   if (archive < 0)
  611.     ERROR ((TAREXIT_FAILURE, errno, _("Cannot open %s"),
  612.         archive_name_array[0]));
  613.  
  614. #ifndef __MSDOS__
  615.   if (!_isrmt (archive))
  616.     {
  617.       struct stat tmp_stat;
  618.  
  619.       fstat (archive, &tmp_stat);
  620.       if (S_ISREG (tmp_stat.st_mode))
  621.     {
  622.       ar_dev = tmp_stat.st_dev;
  623.       ar_ino = tmp_stat.st_ino;
  624.     }
  625.     }
  626. #endif
  627.  
  628. #ifdef    __MSDOS__
  629.   setmode (archive, O_BINARY);
  630. #endif
  631.  
  632.   if (reading)
  633.     {
  634.       ar_last = ar_block;    /* set up for 1st block = # 0 */
  635.       findrec ();        /* read it in, check for EOF */
  636.  
  637.       if (flag_volhdr)
  638.     {
  639.       union record *label;
  640. #if 0
  641.       char *ptr;
  642.  
  643.       if (flag_multivol)
  644.         {
  645.           ptr = xmalloc (strlen (flag_volhdr) + 20);
  646.           sprintf (ptr, "%s Volume %d", flag_volhdr, 1);
  647.         }
  648.       else
  649.         ptr = flag_volhdr;
  650. #endif
  651.       label = findrec ();
  652.       if (!label)
  653.         ERROR ((TAREXIT_FAILURE, 0, _("Archive not labelled to match %s"),
  654.             flag_volhdr));
  655.       if (re_match (label_pattern, label->header.arch_name,
  656.             (int) strlen (label->header.arch_name), 0, 0)
  657.           < 0)
  658.         ERROR ((TAREXIT_FAILURE, 0, _("Volume mismatch!  %s!=%s"),
  659.             flag_volhdr, label->header.arch_name));
  660. #if 0
  661.       if (strcmp (ptr, label->header.name))
  662.           ERROR ((TAREXIT_FAILURE, 0, _("Volume mismatch!  %s!=%s"),
  663.               ptr, label->header.name));
  664.       if (ptr != flag_volhdr)
  665.         free (ptr);
  666. #endif
  667.     }
  668.     }
  669.   else if (flag_volhdr)
  670.     {
  671.       memset ((void *) ar_block, 0, RECORDSIZE);
  672.       if (flag_multivol)
  673.     sprintf (ar_block->header.arch_name, "%s Volume 1", flag_volhdr);
  674.       else
  675.     strcpy (ar_block->header.arch_name, flag_volhdr);
  676.  
  677.       assign_string (¤t_file_name, ar_block->header.arch_name);
  678.  
  679.       ar_block->header.linkflag = LF_VOLHDR;
  680.       to_oct (time (0), 1 + 12, ar_block->header.mtime);
  681.       finish_header (ar_block);
  682. #if 0
  683.       ar_record++;
  684. #endif
  685.     }
  686. }
  687.  
  688. /*------------------------------------------------------------------------.
  689. | Remember a union record * as pointing to something that we need to keep |
  690. | when reading onward in the file.  Only one such thing can be remembered |
  691. | at once, and it only works when reading an archive.              |
  692. `------------------------------------------------------------------------*/
  693.  
  694. /* We calculate "offset" then add it because some compilers end up adding
  695.    (baserec+ar_record), doing a 9-bit shift of baserec, then subtracting
  696.    ar_block from that, shifting it back, losing the top 9 bits.  */
  697.  
  698. void
  699. saverec (union record **pointer)
  700. {
  701.   long offset;
  702.  
  703.   save_rec = pointer;
  704.   offset = ar_record - ar_block;
  705.   saved_recno = baserec + offset;
  706. }
  707.  
  708. /*--------------------------------------.
  709. | Perform a write to flush the buffer.  |
  710. `--------------------------------------*/
  711.  
  712. #if 0
  713.    send_buffer_to_file ();
  714.    if (new_volume)
  715.      {
  716.        deal_with_new_volume_stuff ();
  717.        send_buffer_to_file ();
  718.      }
  719. #endif
  720.  
  721. void
  722. fl_write (void)
  723. {
  724.   int err;
  725.   int copy_back;
  726.   static long bytes_written = 0;
  727.  
  728.   if (flag_checkpoint && !(++checkpoint % 10))
  729.     WARN ((0, 0, _("Write checkpoint %d"), checkpoint));
  730.  
  731.   if (tape_length && bytes_written >= tape_length * 1024)
  732.     {
  733.       errno = ENOSPC;
  734.       err = 0;
  735.     }
  736.   else
  737.     err = rmtwrite (archive, ar_block->charptr, (unsigned int) blocksize);
  738.   if (err != blocksize && !flag_multivol)
  739.     writeerror (err);
  740.   else if (flag_totals)
  741.     tot_written += blocksize;
  742.  
  743.   if (err > 0)
  744.     bytes_written += err;
  745.   if (err == blocksize)
  746.     {
  747.       if (flag_multivol)
  748.     {
  749.       char *cursor;
  750.  
  751.       if (!save_name)
  752.         {
  753.           real_s_name[0] = '\0';
  754.           real_s_totsize = 0;
  755.           real_s_sizeleft = 0;
  756.           return;
  757.         }
  758.  
  759.       cursor = save_name;
  760. #ifdef __MSDOS__
  761.       if (cursor[1] == ':')
  762.         cursor += 2;
  763. #endif
  764.       while (*cursor == '/')
  765.         cursor++;
  766.  
  767.       strcpy (real_s_name, cursor);
  768.       real_s_totsize = save_totsize;
  769.       real_s_sizeleft = save_sizeleft;
  770.     }
  771.       return;
  772.     }
  773.  
  774.   /* We're multivol.  Panic if we didn't get the right kind of response.  */
  775.  
  776.   /* ENXIO is for the UNIX PC.  */
  777.   if (err < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
  778.     writeerror (err);
  779.  
  780.   /* If error indicates a short write, we just move to the next tape.  */
  781.  
  782.   if (new_volume (0) < 0)
  783.     return;
  784.   bytes_written = 0;
  785.   if (flag_volhdr && real_s_name[0])
  786.     {
  787.       copy_back = 2;
  788.       ar_block -= 2;
  789.     }
  790.   else if (flag_volhdr || real_s_name[0])
  791.     {
  792.       copy_back = 1;
  793.       ar_block--;
  794.     }
  795.   else
  796.     copy_back = 0;
  797.   if (flag_volhdr)
  798.     {
  799.       memset ((void *) ar_block, 0, RECORDSIZE);
  800.       sprintf (ar_block->header.arch_name, "%s Volume %d", flag_volhdr, volno);
  801.       to_oct (time (0), 1 + 12, ar_block->header.mtime);
  802.       ar_block->header.linkflag = LF_VOLHDR;
  803.       finish_header (ar_block);
  804.     }
  805.   if (real_s_name[0])
  806.     {
  807.       int tmp;
  808.  
  809.       if (flag_volhdr)
  810.     ar_block++;
  811.       memset ((void *) ar_block, 0, RECORDSIZE);
  812.       strcpy (ar_block->header.arch_name, real_s_name);
  813.       ar_block->header.linkflag = LF_MULTIVOL;
  814.       to_oct ((long) real_s_sizeleft, 1 + 12,
  815.           ar_block->header.size);
  816.       to_oct ((long) real_s_totsize - real_s_sizeleft,
  817.           1 + 12, ar_block->header.offset);
  818.       tmp = flag_verbose;
  819.       flag_verbose = 0;
  820.       finish_header (ar_block);
  821.       flag_verbose = tmp;
  822.       if (flag_volhdr)
  823.     ar_block--;
  824.     }
  825.  
  826.   err = rmtwrite (archive, ar_block->charptr, (unsigned int) blocksize);
  827.   if (err != blocksize)
  828.     writeerror (err);
  829.   else if (flag_totals)
  830.     tot_written += blocksize;
  831.  
  832.  
  833.   bytes_written = blocksize;
  834.   if (copy_back)
  835.     {
  836.       ar_block += copy_back;
  837.       memcpy ((void *) ar_record,
  838.           (void *) (ar_block + blocking - copy_back),
  839.           (size_t) (copy_back * RECORDSIZE));
  840.       ar_record += copy_back;
  841.  
  842.       if (real_s_sizeleft >= copy_back * RECORDSIZE)
  843.     real_s_sizeleft -= copy_back * RECORDSIZE;
  844.       else if ((real_s_sizeleft + RECORDSIZE - 1) / RECORDSIZE <= copy_back)
  845.     real_s_name[0] = '\0';
  846.       else
  847.     {
  848.       char *cursor = save_name;
  849.       
  850. #ifdef __MSDOS__
  851.       if (cursor[1] == ':')
  852.         cursor += 2;
  853. #endif
  854.       while (*cursor == '/')
  855.         cursor++;
  856.  
  857.       strcpy (real_s_name, cursor);
  858.       real_s_sizeleft = save_sizeleft;
  859.       real_s_totsize = save_totsize;
  860.     }
  861.       copy_back = 0;
  862.     }
  863. }
  864.  
  865. /*---------------------------------------------------------------------.
  866. | Handle write errors on the archive.  Write errors are always fatal.  |
  867. | Hitting the end of a volume does not cause a write error unless the  |
  868. | write was the first block of the volume.                   |
  869. `---------------------------------------------------------------------*/
  870.  
  871. static void
  872. writeerror (int err)
  873. {
  874.   if (err < 0)
  875.     ERROR ((TAREXIT_FAILURE, errno, _("Cannot write to %s"),
  876.         *archive_name_cursor));
  877.   else
  878.     ERROR ((TAREXIT_FAILURE, 0, _("Only wrote %u of %u bytes to %s"),
  879.         err, blocksize, *archive_name_cursor));
  880. }
  881.  
  882. /*-------------------------------------------------------------------.
  883. | Handle read errors on the archive.  If the read should be retried, |
  884. | readerror() returns to the caller.                     |
  885. `-------------------------------------------------------------------*/
  886.  
  887. static void
  888. readerror (void)
  889. {
  890. #define    READ_ERROR_MAX    10
  891.  
  892.   read_error_flag++;        /* tell callers */
  893.  
  894.   WARN ((0, errno, _("Read error on %s"), *archive_name_cursor));
  895.  
  896.   if (baserec == 0)
  897.     ERROR ((TAREXIT_FAILURE, 0, _("At beginning of tape, quitting now")));
  898.  
  899.   /* Read error in mid archive.  We retry up to READ_ERROR_MAX times and
  900.      then give up on reading the archive.  We set read_error_flag for our
  901.      callers, so they can cope if they want.  */
  902.  
  903.   if (r_error_count++ > READ_ERROR_MAX)
  904.     ERROR ((TAREXIT_FAILURE, 0, _("Too many errors, quitting")));
  905.   return;
  906. }
  907.  
  908. /*-------------------------------------.
  909. | Perform a read to flush the buffer.  |
  910. `-------------------------------------*/
  911.  
  912. void
  913. fl_read (void)
  914. {
  915.   int err;            /* result from system call */
  916.   int left;            /* bytes left */
  917.   char *more;            /* pointer to next byte to read */
  918.  
  919.   if (flag_checkpoint && !(++checkpoint % 10))
  920.     WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
  921.  
  922.   /* Clear the count of errors.  This only applies to a single call to
  923.      fl_read.  We leave read_error_flag alone; it is only turned off by
  924.      higher level software.  */
  925.  
  926.   r_error_count = 0;        /* clear error count */
  927.  
  928.   /* If we are about to wipe out a record that somebody needs to keep,
  929.      copy it out to a holding area and adjust somebody's pointer to it.  */
  930.  
  931.   if (save_rec &&
  932.       *save_rec >= ar_record &&
  933.       *save_rec < ar_last)
  934.     {
  935.       record_save_area = **save_rec;
  936.       *save_rec = &record_save_area;
  937.     }
  938.   if (write_archive_to_stdout && baserec != 0)
  939.     {
  940.       err = rmtwrite (1, ar_block->charptr, (unsigned int) blocksize);
  941.       if (err != blocksize)
  942.     writeerror (err);
  943.     }
  944.   if (flag_multivol)
  945.     if (save_name)
  946.       {
  947.     if (save_name != real_s_name)
  948.       {
  949. #ifdef __MSDOS__
  950.         if (save_name[1] == ':')
  951.           save_name += 2;
  952. #endif
  953.         while (*save_name == '/')
  954.           save_name++;
  955.  
  956.         strcpy (real_s_name, save_name);
  957.         save_name = real_s_name;
  958.       }
  959.     real_s_totsize = save_totsize;
  960.     real_s_sizeleft = save_sizeleft;
  961.       }
  962.     else
  963.       {
  964.     real_s_name[0] = '\0';
  965.     real_s_totsize = 0;
  966.     real_s_sizeleft = 0;
  967.       }
  968.  
  969. error_loop:
  970.   err = rmtread (archive, ar_block->charptr, (unsigned int) blocksize);
  971.   if (err == blocksize)
  972.     return;
  973.  
  974.   if ((err == 0 || (err < 0 && errno == ENOSPC) || (err > 0 && !flag_reblock))
  975.       && flag_multivol)
  976.     {
  977.       union record *cursor;
  978.  
  979.     try_volume:
  980.       if (new_volume ((command_mode == COMMAND_APPEND
  981.                || command_mode == COMMAND_CAT
  982.                || command_mode == COMMAND_UPDATE) ? 2 : 1)
  983.       < 0)
  984.     return;
  985.  
  986.     vol_error:
  987.       err = rmtread (archive, ar_block->charptr, (unsigned int) blocksize);
  988.       if (err < 0)
  989.     {
  990.       readerror ();
  991.       goto vol_error;
  992.     }
  993.       if (err != blocksize)
  994.     goto short_read;
  995.  
  996.       cursor = ar_block;
  997.  
  998.       if (cursor->header.linkflag == LF_VOLHDR)
  999.     {
  1000.       if (flag_volhdr)
  1001.         {
  1002. #if 0
  1003.           char *ptr;
  1004.  
  1005.           ptr = (char *) xmalloc (strlen (flag_volhdr) + 20);
  1006.           sprintf (ptr, "%s Volume %d", flag_volhdr, volno);
  1007. #endif
  1008.           if (re_match (label_pattern, cursor->header.arch_name,
  1009.                 (int) strlen (cursor->header.arch_name),
  1010.                 0, 0) < 0)
  1011.         {
  1012.           WARN ((0, 0, _("Volume mismatch!  %s!=%s"),
  1013.              flag_volhdr, cursor->header.arch_name));
  1014.           volno--;
  1015.           global_volno--;
  1016.           goto try_volume;
  1017.         }
  1018.  
  1019. #if 0
  1020.           if (strcmp (ptr, cursor->header.name))
  1021.         {
  1022.           WARN ((0, 0, _("Volume mismatch!  %s!=%s"),
  1023.              ptr, cursor->header.name));
  1024.           volno--;
  1025.           global_volno--;
  1026.           free (ptr);
  1027.           goto try_volume;
  1028.         }
  1029.           free (ptr);
  1030. #endif
  1031.         }
  1032.       if (flag_verbose)
  1033.         fprintf (stdlis, _("Reading %s\n"), cursor->header.arch_name);
  1034.       cursor++;
  1035.     }
  1036.       else if (flag_volhdr)
  1037.     WARN ((0, 0, _("WARNING: No volume header")));
  1038.  
  1039.       if (real_s_name[0])
  1040.     {
  1041.       if (cursor->header.linkflag != LF_MULTIVOL
  1042.           || strcmp (cursor->header.arch_name, real_s_name))
  1043.         {
  1044.           WARN ((0, 0, _("%s is not continued on this volume"),
  1045.              real_s_name));
  1046.           volno--;
  1047.           global_volno--;
  1048.           goto try_volume;
  1049.         }
  1050.       if (real_s_totsize != (from_oct (1 + 12, cursor->header.size)
  1051.                  + from_oct (1 + 12, cursor->header.offset)))
  1052.         {
  1053.           WARN ((0, 0, _("%s is the wrong size (%ld != %ld + %ld)"),
  1054.              cursor->header.arch_name, save_totsize,
  1055.              from_oct (1 + 12, cursor->header.size),
  1056.              from_oct (1 + 12, cursor->header.offset)));
  1057.           volno--;
  1058.           global_volno--;
  1059.           goto try_volume;
  1060.         }
  1061.       if (real_s_totsize - real_s_sizeleft
  1062.           != from_oct (1 + 12, cursor->header.offset))
  1063.         {
  1064.           WARN ((0, 0, _("This volume is out of sequence")));
  1065.           volno--;
  1066.           global_volno--;
  1067.           goto try_volume;
  1068.         }
  1069.       cursor++;
  1070.     }
  1071.       ar_record = cursor;
  1072.       return;
  1073.     }
  1074.   else if (err < 0)
  1075.     {
  1076.       readerror ();
  1077.       goto error_loop;        /* try again */
  1078.     }
  1079.  
  1080. short_read:
  1081.   more = ar_block->charptr + err;
  1082.   left = blocksize - err;
  1083.  
  1084. again:
  1085.   if ((unsigned) left % RECORDSIZE == 0)
  1086.     {
  1087.  
  1088.       /* FIXME, for size=0, multi vol support.  On the first block, warn
  1089.      about the problem.  */
  1090.  
  1091.       if (!flag_reblock && baserec == 0 && flag_verbose && err > 0)
  1092.     WARN ((0, 0, _("Blocksize = %d records"), err / RECORDSIZE));
  1093.  
  1094.       ar_last = ar_block + ((unsigned) (blocksize - left)) / RECORDSIZE;
  1095.       return;
  1096.     }
  1097.   if (flag_reblock)
  1098.     {
  1099.  
  1100.       /* User warned us about this.  Fix up.  */
  1101.  
  1102.       if (left > 0)
  1103.     {
  1104.     error2loop:
  1105.       err = rmtread (archive, more, (unsigned int) left);
  1106.       if (err < 0)
  1107.         {
  1108.           readerror ();
  1109.           goto error2loop;    /* try again */
  1110.         }
  1111.       if (err == 0)
  1112.         ERROR ((TAREXIT_FAILURE, 0,
  1113.             _("Archive %s EOF not on block boundary"),
  1114.             *archive_name_cursor));
  1115.       left -= err;
  1116.       more += err;
  1117.       goto again;
  1118.     }
  1119.     }
  1120.   else
  1121.     ERROR ((TAREXIT_FAILURE, 0, _("Only read %d bytes from archive %s"),
  1122.         err, *archive_name_cursor));
  1123. }
  1124.  
  1125. /*-----------------------------------------------.
  1126. | Flush the current buffer to/from the archive.     |
  1127. `-----------------------------------------------*/
  1128.  
  1129. void
  1130. flush_archive (void)
  1131. {
  1132.   int c;
  1133.  
  1134.   baserec += ar_last - ar_block; /* keep track of block #s */
  1135.   ar_record = ar_block;        /* restore pointer to start */
  1136.   ar_last = ar_block + blocking; /* restore pointer to end */
  1137.  
  1138.   if (ar_reading)
  1139.     {
  1140.       if (time_to_start_writing)
  1141.     {
  1142.       time_to_start_writing = 0;
  1143.       ar_reading = 0;
  1144.  
  1145.       if (file_to_switch_to >= 0)
  1146.         {
  1147.           if (c = rmtclose (archive), c < 0)
  1148.         WARN ((0, errno, _("WARNING: Cannot close %s (%d, %d)"),
  1149.                *archive_name_cursor, archive, c));
  1150.  
  1151.           archive = file_to_switch_to;
  1152.         }
  1153.       else
  1154.         backspace_output ();
  1155.       fl_write ();
  1156.     }
  1157.       else
  1158.     fl_read ();
  1159.     }
  1160.   else
  1161.     {
  1162.       fl_write ();
  1163.     }
  1164. }
  1165.  
  1166. /*-------------------------------------------------------------------------.
  1167. | Backspace the archive descriptor by one blocks worth.  If its a tape,       |
  1168. | MTIOCTOP will work.  If its something else, we try to seek on it.  If we |
  1169. | can't seek, we lose!                               |
  1170. `-------------------------------------------------------------------------*/
  1171.  
  1172. static int
  1173. backspace_output (void)
  1174. {
  1175.   off_t cur;
  1176. #if 0
  1177.   int er;
  1178. #endif
  1179.  
  1180. #ifdef MTIOCTOP
  1181.   struct mtop t;
  1182.  
  1183.   t.mt_op = MTBSR;
  1184.   t.mt_count = 1;
  1185.   if (rmtioctl (archive, MTIOCTOP, (char *) &t) >= 0)
  1186.     return 1;
  1187.   if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &t) >= 0)
  1188.     return 1;
  1189. #endif
  1190.  
  1191.   cur = rmtlseek (archive, 0L, 1);
  1192.   cur -= blocksize;
  1193.  
  1194.   /* Seek back to the beginning of this block and start writing there.  */
  1195.  
  1196.   if (rmtlseek (archive, cur, 0) != cur)
  1197.     {
  1198.  
  1199.       /* Lseek failed.  Try a different method.  */
  1200.  
  1201.       WARN ((0, 0, _("\
  1202. Could not backspace archive file; it may be unreadable without -i")));
  1203.  
  1204.       /* Replace the first part of the block with nulls.  */
  1205.  
  1206.       if (ar_block->charptr != output_start)
  1207.     memset (ar_block->charptr, 0,
  1208.         (size_t) (output_start - ar_block->charptr));
  1209.       return 2;
  1210.     }
  1211.   return 3;
  1212. }
  1213.  
  1214. /*-------------------------.
  1215. | Close the archive file.  |
  1216. `-------------------------*/
  1217.  
  1218. void
  1219. close_archive (void)
  1220. {
  1221.   int child;
  1222.   WAIT_T status;
  1223.   int c;
  1224.  
  1225.   if (time_to_start_writing || !ar_reading)
  1226.     flush_archive ();
  1227.   if (command_mode == COMMAND_DELETE)
  1228.     {
  1229.       off_t pos;
  1230.  
  1231.       pos = rmtlseek (archive, 0L, 1);
  1232. #ifndef __MSDOS__
  1233.       ftruncate (archive, (size_t) pos);
  1234. #else
  1235.       rmtwrite (archive, "", 0);
  1236. #endif
  1237.     }
  1238.   if (flag_verify)
  1239.     verify_volume ();
  1240.  
  1241.   if (c = rmtclose (archive), c < 0)
  1242.     WARN ((0, errno, _("WARNING: Cannot close %s (%d, %d)"),
  1243.        *archive_name_cursor, archive, c));
  1244.  
  1245. #ifndef    __MSDOS__
  1246.  
  1247.   if (childpid)
  1248.     {
  1249.  
  1250.       /* Loop waiting for the right child to die, or for no more kids.  */
  1251.  
  1252.       while ((child = wait ((int *) &status), child != childpid) && child != -1)
  1253.     ;
  1254.  
  1255.       if (child != -1)
  1256.     if (WIFSIGNALED (status)
  1257. #if 0
  1258.         && !WIFSTOPPED (status)
  1259. #endif
  1260.         )
  1261.       {
  1262.  
  1263.         /* SIGPIPE is OK, everything else is a problem.  */
  1264.  
  1265.         if (WTERMSIG (status) != SIGPIPE)
  1266.           ERROR ((0, 0, _("Child died with signal %d%s"),
  1267.               WTERMSIG (status),
  1268.               WCOREDUMP (status) ? _(" (core dumped)") : ""));
  1269.       }
  1270.     else
  1271.       {
  1272.  
  1273.         /* Child voluntarily terminated -- but why?  /bin/sh returns
  1274.            SIGPIPE + 120 if its child, then do nothing.  */
  1275.  
  1276.         if (WEXITSTATUS (status) != (SIGPIPE + 128)
  1277.         && WEXITSTATUS (status))
  1278.           ERROR ((0, 0, _("Child returned status %d"),
  1279.               WEXITSTATUS (status)));
  1280.       }
  1281.     }
  1282. #endif /* not __MSDOS__ */
  1283.  
  1284.   if (current_file_name)
  1285.     free (current_file_name);
  1286.   if (current_link_name)
  1287.     free (current_link_name);
  1288.   if (save_name)
  1289.     free (save_name);
  1290.   free (flag_multivol ? ar_block - 2 : ar_block);
  1291. }
  1292.  
  1293. /*------------------------------------------------.
  1294. | Called to initialize the global volume number.  |
  1295. `------------------------------------------------*/
  1296.  
  1297. void
  1298. init_volume_number (void)
  1299. {
  1300.   FILE *vf;
  1301.  
  1302.   vf = fopen (flag_volno_file, "r");
  1303.   if (!vf && errno != ENOENT)
  1304.     ERROR ((0, errno, "%s", flag_volno_file));
  1305.  
  1306.   if (vf)
  1307.     {
  1308.       fscanf (vf, "%d", &global_volno);
  1309.       fclose (vf);
  1310.     }
  1311. }
  1312.  
  1313. /*-------------------------------------------------------.
  1314. | Called to write out the closing global volume number.     |
  1315. `-------------------------------------------------------*/
  1316.  
  1317. void
  1318. closeout_volume_number (void)
  1319. {
  1320.   FILE *vf;
  1321.  
  1322.   vf = fopen (flag_volno_file, "w");
  1323.   if (!vf)
  1324.     ERROR ((0, errno, "%s", flag_volno_file));
  1325.   else
  1326.     {
  1327.       fprintf (vf, "%d\n", global_volno);
  1328.       fclose (vf);
  1329.     }
  1330. }
  1331.  
  1332. /*---------------------------------------------------------------------.
  1333. | We've hit the end of the old volume.  Close it and open the next one |
  1334. | Values for type: 0: writing 1: reading 2: updating               |
  1335. `---------------------------------------------------------------------*/
  1336.  
  1337. static int
  1338. new_volume (int type)
  1339. {
  1340.   int c;
  1341.   char inbuf[80];
  1342.   const char *p;
  1343.   static FILE *read_file = 0;
  1344.   static int looped = 0;
  1345.   WAIT_T status;
  1346.  
  1347.   if (!read_file && !flag_run_script_at_end)
  1348.     read_file = (archive == 0) ? fopen (TTY_NAME, "r") : stdin;
  1349.  
  1350.   if (now_verifying)
  1351.     return -1;
  1352.   if (flag_verify)
  1353.     verify_volume ();
  1354.   if (c = rmtclose (archive), c < 0)
  1355.     WARN ((0, errno, _("WARNING: Cannot close %s (%d, %d)"),
  1356.        *archive_name_cursor, archive, c));
  1357.  
  1358.   global_volno++;
  1359.   volno++;
  1360.   archive_name_cursor++;
  1361.   if (archive_name_cursor == archive_name_array + archive_names)
  1362.     {
  1363.       archive_name_cursor = archive_name_array;
  1364.       looped = 1;
  1365.     }
  1366.  
  1367. tryagain:
  1368.   if (looped)
  1369.     {
  1370.  
  1371.       /* We have to prompt from now on.  */
  1372.  
  1373.       if (flag_run_script_at_end)
  1374.     {
  1375.       closeout_volume_number ();
  1376.       system (info_script);
  1377.     }
  1378.       else
  1379.     while (1)
  1380.       {
  1381.         fprintf (stdlis,
  1382.              _("\007Prepare volume #%d for %s and hit return: "),
  1383.              global_volno, *archive_name_cursor);
  1384.         fflush (stdlis);
  1385.         if (fgets (inbuf, sizeof (inbuf), read_file) == 0)
  1386.           {
  1387.         fprintf (stdlis, _("EOF?  What does that mean?"));
  1388.  
  1389.         if (command_mode != COMMAND_EXTRACT
  1390.             && command_mode != COMMAND_LIST
  1391.             && command_mode != COMMAND_DIFF)
  1392.           WARN ((0, 0, _("WARNING: Archive is incomplete")));
  1393.  
  1394.         exit (TAREXIT_FAILURE);
  1395.           }
  1396.         if (inbuf[0] == '\n' || inbuf[0] == 'y' || inbuf[0] == 'Y')
  1397.           break;
  1398.  
  1399.         switch (inbuf[0])
  1400.           {
  1401.           case '?':
  1402.         {
  1403.           fprintf (stdlis, _("\
  1404.  n [name]   Give a new filename for the next (and subsequent) volume(s)\n\
  1405.  q          Abort tar\n\
  1406.  !          Spawn a subshell\n\
  1407.  ?          Print this list\n"));
  1408.         }
  1409.         break;
  1410.  
  1411.           case 'q':        /* quit */
  1412.         fprintf (stdlis, _("No new volume; exiting.\n"));
  1413.  
  1414.         if (command_mode != COMMAND_EXTRACT
  1415.             && command_mode != COMMAND_LIST
  1416.             && command_mode != COMMAND_DIFF)
  1417.           WARN ((0, 0, _("WARNING: Archive is incomplete")));
  1418.  
  1419.         exit (TAREXIT_FAILURE);
  1420.  
  1421.           case 'n':        /* get new file name */
  1422.         {
  1423.           char *q, *r;
  1424.  
  1425.           for (q = &inbuf[1]; *q == ' ' || *q == '\t'; q++)
  1426.             ;
  1427.           for (r = q; *r; r++)
  1428.             if (*r == '\n')
  1429.               *r = '\0';
  1430.           r = (char *) xmalloc ((size_t) (strlen (q) + 2));
  1431.           strcpy (r, q);
  1432.           p = r;
  1433.           *archive_name_cursor = p;
  1434.         }
  1435.         break;
  1436.  
  1437.           case '!':
  1438. #ifdef __MSDOS__
  1439.         spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
  1440. #else
  1441.         switch (fork ())
  1442.           {
  1443.           case -1:
  1444.             WARN ((0, errno, _("Cannot fork!")));
  1445.             break;
  1446.  
  1447.           case 0:
  1448.             p = getenv ("SHELL");
  1449.             if (p == 0)
  1450.               p = "/bin/sh";
  1451.             execlp (p, "-sh", "-i", 0);
  1452.             /* In a previous tar version, _exit was used here
  1453.                instead of exit.  */
  1454.             ERROR ((TAREXIT_FAILURE, errno,
  1455.                 _("Cannot exec a shell %s"), p));
  1456.  
  1457.           default:
  1458.             wait ((int *) &status);
  1459.             break;
  1460.           }
  1461.  
  1462.         /* FIXME: I'm not sure if that's all that has to be done
  1463.            here.  (jk)  */
  1464.  
  1465. #endif
  1466.         break;
  1467.           }
  1468.       }
  1469.     }
  1470.  
  1471.   if (type == 2 || flag_verify)
  1472.     archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, 0666,
  1473.                flag_rsh_command);
  1474.   else if (type == 1)
  1475.     archive = rmtopen (*archive_name_cursor, O_RDONLY, 0666, flag_rsh_command);
  1476.   else if (type == 0)
  1477.     archive = rmtcreat (*archive_name_cursor, 0666, flag_rsh_command);
  1478.   else
  1479.     archive = -1;
  1480.  
  1481.   if (archive < 0)
  1482.     {
  1483.       WARN ((0, errno, _("Cannot open %s"), *archive_name_cursor));
  1484.       goto tryagain;
  1485.     }
  1486. #ifdef __MSDOS__
  1487.   setmode (archive, O_BINARY);
  1488. #endif
  1489.   return 0;
  1490. }
  1491.  
  1492. /*-------------------------------------------------------------------------.
  1493. | This is a useless function that takes a buffer returned by wantbytes and |
  1494. | does nothing with it.  If the function called by wantbytes returns an       |
  1495. | error indicator (non-zero), this function is called for the rest of the  |
  1496. | file.                                       |
  1497. `-------------------------------------------------------------------------*/
  1498.  
  1499. /* Yes, I know.  SIZE and DATA are unused in this function, and this
  1500.    might even trigger some compiler warnings.  That's OK!  Relax.  */
  1501.  
  1502. int
  1503. no_op (int size, char *data)
  1504. {
  1505.   return 0;
  1506. }
  1507.  
  1508. /*-----------------------------------------------------------------------.
  1509. | Some other routine wants SIZE bytes in the archive.  For each chunk of |
  1510. | the archive, call FUNC with the size of the chunk, and the address of     |
  1511. | the chunk it can work with.                         |
  1512. `-----------------------------------------------------------------------*/
  1513.  
  1514. int
  1515. wantbytes (long size, int (*func) ())
  1516. {
  1517.   char *data;
  1518.   long data_size;
  1519.  
  1520.   if (flag_multivol)
  1521.     save_sizeleft = size;
  1522.   while (size)
  1523.     {
  1524.       data = findrec ()->charptr;
  1525.       if (data == NULL)
  1526.     {
  1527.  
  1528.       /* Check it.  */
  1529.  
  1530.       ERROR ((0, 0, _("Unexpected EOF on archive file")));
  1531.       return -1;
  1532.     }
  1533.       data_size = endofrecs ()->charptr - data;
  1534.       if (data_size > size)
  1535.     data_size = size;
  1536.       if ((*func) (data_size, data))
  1537.     func = no_op;
  1538.       userec ((union record *) (data + data_size - 1));
  1539.       size -= data_size;
  1540.       if (flag_multivol)
  1541.     save_sizeleft -= data_size;
  1542.     }
  1543.   return 0;
  1544. }
  1545.