Home | History | Annotate | Line # | Download | only in pax
ar_io.c revision 1.8
      1 /*	$NetBSD: ar_io.c,v 1.8 1998/03/06 09:13:01 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1992 Keith Muller.
      5  * Copyright (c) 1992, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * Keith Muller of the University of California, San Diego.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "@(#)ar_io.c	8.2 (Berkeley) 4/18/94";
     44 #else
     45 __RCSID("$NetBSD: ar_io.c,v 1.8 1998/03/06 09:13:01 mrg Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 #include <sys/types.h>
     50 #include <sys/time.h>
     51 #include <sys/stat.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/mtio.h>
     54 #include <sys/param.h>
     55 #include <signal.h>
     56 #include <string.h>
     57 #include <fcntl.h>
     58 #include <unistd.h>
     59 #include <stdio.h>
     60 #include <ctype.h>
     61 #include <errno.h>
     62 #include <stdlib.h>
     63 #include <err.h>
     64 #include "pax.h"
     65 #include "extern.h"
     66 
     67 /*
     68  * Routines which deal directly with the archive I/O device/file.
     69  */
     70 
     71 #define DMOD		0666		/* default mode of created archives */
     72 #define EXT_MODE	O_RDONLY	/* open mode for list/extract */
     73 #define AR_MODE		(O_WRONLY | O_CREAT | O_TRUNC)	/* mode for archive */
     74 #define APP_MODE	O_RDWR		/* mode for append */
     75 #define STDO		"<STDOUT>"	/* psuedo name for stdout */
     76 #define STDN		"<STDIN>"	/* psuedo name for stdin */
     77 static int arfd = -1;			/* archive file descriptor */
     78 static int artyp = ISREG;		/* archive type: file/FIFO/tape */
     79 static int arvol = 1;			/* archive volume number */
     80 static int lstrval = -1;		/* return value from last i/o */
     81 static int io_ok;			/* i/o worked on volume after resync */
     82 static int did_io;			/* did i/o ever occur on volume? */
     83 static int done;			/* set via tty termination */
     84 static struct stat arsb;		/* stat of archive device at open */
     85 static int invld_rec;			/* tape has out of spec record size */
     86 static int wr_trail = 1;		/* trailer was rewritten in append */
     87 static int can_unlnk = 0;		/* do we unlink null archives?  */
     88 char *arcname;                  	/* printable name of archive */
     89 char *gzip_program;			/* name of gzip program */
     90 
     91 static int get_phys __P((void));
     92 extern sigset_t s_mask;
     93 static void ar_start_gzip __P((int));
     94 
     95 /*
     96  * ar_open()
     97  *	Opens the next archive volume. Determines the type of the device and
     98  *	sets up block sizes as required by the archive device and the format.
     99  *	Note: we may be called with name == NULL on the first open only.
    100  * Return:
    101  *	-1 on failure, 0 otherwise
    102  */
    103 
    104 #if __STDC__
    105 int
    106 ar_open(char *name)
    107 #else
    108 int
    109 ar_open(name)
    110 	char *name;
    111 #endif
    112 {
    113         struct mtget mb;
    114 	static int minusCfd = -1, curdirfd = -1;
    115 
    116 	/*
    117 	 * change back to the current directory (for now).
    118 	 */
    119 	if (curdirfd != -1)
    120 		fchdir(curdirfd);
    121 
    122 	if (arfd != -1)
    123 		(void)close(arfd);
    124 	arfd = -1;
    125 	can_unlnk = did_io = io_ok = invld_rec = 0;
    126 	artyp = ISREG;
    127 	flcnt = 0;
    128 
    129 	/*
    130 	 * open based on overall operation mode
    131 	 */
    132 	switch (act) {
    133 	case LIST:
    134 	case EXTRACT:
    135 		if (name == NULL) {
    136 			arfd = STDIN_FILENO;
    137 			arcname = STDN;
    138 		} else if ((arfd = open(name, EXT_MODE, DMOD)) < 0)
    139 			syswarn(0, errno, "Failed open to read on %s", name);
    140 		if (zflag)
    141 			ar_start_gzip(arfd);
    142 		break;
    143 	case ARCHIVE:
    144 		if (name == NULL) {
    145 			arfd = STDOUT_FILENO;
    146 			arcname = STDO;
    147 		} else if ((arfd = open(name, AR_MODE, DMOD)) < 0)
    148 			syswarn(0, errno, "Failed open to write on %s", name);
    149 		else
    150 			can_unlnk = 1;
    151 		if (zflag)
    152 			ar_start_gzip(arfd);
    153 		break;
    154 	case APPND:
    155 		if (zflag)
    156 			err(1, "can not gzip while appending");
    157 		if (name == NULL) {
    158 			arfd = STDOUT_FILENO;
    159 			arcname = STDO;
    160 		} else if ((arfd = open(name, APP_MODE, DMOD)) < 0)
    161 			syswarn(0, errno, "Failed open to read/write on %s",
    162 				name);
    163 		break;
    164 	case COPY:
    165 		/*
    166 		 * arfd not used in COPY mode
    167 		 */
    168 		arcname = "<NONE>";
    169 		lstrval = 1;
    170 		return(0);
    171 	}
    172 	if (arfd < 0)
    173 		return(-1);
    174 
    175 	/*
    176 	 * set up is based on device type
    177 	 */
    178 	if (fstat(arfd, &arsb) < 0) {
    179 		syswarn(0, errno, "Failed stat on %s", arcname);
    180 		(void)close(arfd);
    181 		arfd = -1;
    182 		can_unlnk = 0;
    183 		return(-1);
    184 	}
    185 	if (S_ISDIR(arsb.st_mode)) {
    186 		tty_warn(0, "Cannot write an archive on top of a directory %s",
    187 		    arcname);
    188 		(void)close(arfd);
    189 		arfd = -1;
    190 		can_unlnk = 0;
    191 		return(-1);
    192 	}
    193 
    194 	if (S_ISCHR(arsb.st_mode))
    195 		artyp = ioctl(arfd, MTIOCGET, &mb) ? ISCHR : ISTAPE;
    196 	else if (S_ISBLK(arsb.st_mode))
    197 		artyp = ISBLK;
    198 	else if ((lseek(arfd, (off_t)0L, SEEK_CUR) == -1) && (errno == ESPIPE))
    199 		artyp = ISPIPE;
    200 	else
    201 		artyp = ISREG;
    202 
    203 	/*
    204 	 * make sure we beyond any doubt that we only can unlink regular files
    205 	 * we created
    206 	 */
    207 	if (artyp != ISREG)
    208 		can_unlnk = 0;
    209 	/*
    210 	 * if we are writing, we are done
    211 	 */
    212 	if (act == ARCHIVE) {
    213 		blksz = rdblksz = wrblksz;
    214 		lstrval = 1;
    215 		return(0);
    216 	}
    217 
    218 	/*
    219 	 * change directory if necessary
    220 	 */
    221 	if (chdir_dir) {
    222 		if (curdirfd == -1)
    223 			curdirfd = open(".", O_RDONLY);
    224 		if (curdirfd < 1) {
    225 			syswarn(0, errno, "failed to open directory .");
    226 			return (-1);
    227 		}
    228 
    229 		if (minusCfd == -1)
    230 			minusCfd = open(chdir_dir, O_RDONLY);
    231 		if (minusCfd < 1) {
    232 			syswarn(0, errno, "failed to open directory %s",
    233 			    chdir_dir);
    234 			return (-1);
    235 		}
    236 		fchdir(minusCfd);
    237 	}
    238 
    239 	/*
    240 	 * set default blksz on read. APPNDs writes rdblksz on the last volume
    241 	 * On all new archive volumes, we shift to wrblksz (if the user
    242 	 * specified one, otherwize we will continue to use rdblksz). We
    243 	 * must to set blocksize based on what kind of device the archive is
    244 	 * stored.
    245 	 */
    246 	switch(artyp) {
    247 	case ISTAPE:
    248 		/*
    249 		 * Tape drives come in at least two flavors. Those that support
    250 		 * variable sized records and those that have fixed sized
    251 		 * records. They must be treated differently. For tape drives
    252 		 * that support variable sized records, we must make large
    253 		 * reads to make sure we get the entire record, otherwise we
    254 		 * will just get the first part of the record (up to size we
    255 		 * asked). Tapes with fixed sized records may or may not return
    256 		 * multiple records in a single read. We really do not care
    257 		 * what the physical record size is UNLESS we are going to
    258 		 * append. (We will need the physical block size to rewrite
    259 		 * the trailer). Only when we are appending do we go to the
    260 		 * effort to figure out the true PHYSICAL record size.
    261 		 */
    262 		blksz = rdblksz = MAXBLK;
    263 		break;
    264 	case ISPIPE:
    265 	case ISBLK:
    266 	case ISCHR:
    267 		/*
    268 		 * Blocksize is not a major issue with these devices (but must
    269 		 * be kept a multiple of 512). If the user specified a write
    270 		 * block size, we use that to read. Under append, we must
    271 		 * always keep blksz == rdblksz. Otherwise we go ahead and use
    272 		 * the device optimal blocksize as (and if) returned by stat
    273 		 * and if it is within pax specs.
    274 		 */
    275 		if ((act == APPND) && wrblksz) {
    276 			blksz = rdblksz = wrblksz;
    277 			break;
    278 		}
    279 
    280 		if ((arsb.st_blksize > 0) && (arsb.st_blksize < MAXBLK) &&
    281 		    ((arsb.st_blksize % BLKMULT) == 0))
    282 			rdblksz = arsb.st_blksize;
    283 		else
    284 			rdblksz = DEVBLK;
    285 		/*
    286 		 * For performance go for large reads when we can without harm
    287 		 */
    288 		if ((act == APPND) || (artyp == ISCHR))
    289 			blksz = rdblksz;
    290 		else
    291 			blksz = MAXBLK;
    292 		break;
    293 	case ISREG:
    294 		/*
    295 		 * if the user specified wrblksz works, use it. Under appends
    296 		 * we must always keep blksz == rdblksz
    297 		 */
    298 		if ((act == APPND) && wrblksz && ((arsb.st_size%wrblksz)==0)){
    299 			blksz = rdblksz = wrblksz;
    300 			break;
    301 		}
    302 		/*
    303 		 * See if we can find the blocking factor from the file size
    304 		 */
    305 		for (rdblksz = MAXBLK; rdblksz > 0; rdblksz -= BLKMULT)
    306 			if ((arsb.st_size % rdblksz) == 0)
    307 				break;
    308 		/*
    309 		 * When we cannont find a match, we may have a flawed archive.
    310 		 */
    311 		if (rdblksz <= 0)
    312 			rdblksz = FILEBLK;
    313 		/*
    314 		 * for performance go for large reads when we can
    315 		 */
    316 		if (act == APPND)
    317 			blksz = rdblksz;
    318 		else
    319 			blksz = MAXBLK;
    320 		break;
    321 	default:
    322 		/*
    323 		 * should never happen, worse case, slow...
    324 		 */
    325 		blksz = rdblksz = BLKMULT;
    326 		break;
    327 	}
    328 	lstrval = 1;
    329 	return(0);
    330 }
    331 
    332 /*
    333  * ar_close()
    334  *	closes archive device, increments volume number, and prints i/o summary
    335  */
    336 #if __STDC__
    337 void
    338 ar_close(void)
    339 #else
    340 void
    341 ar_close()
    342 #endif
    343 {
    344 	FILE *outf;
    345 
    346 	if (arfd < 0) {
    347 		did_io = io_ok = flcnt = 0;
    348 		return;
    349 	}
    350 
    351 	if (act == LIST)
    352 		outf = stdout;
    353 	else
    354 		outf = stderr;
    355 
    356 	/*
    357 	 * Close archive file. This may take a LONG while on tapes (we may be
    358 	 * forced to wait for the rewind to complete) so tell the user what is
    359 	 * going on (this avoids the user hitting control-c thinking pax is
    360 	 * broken).
    361 	 */
    362 	if (vflag && (artyp == ISTAPE)) {
    363 		if (vfpart)
    364 			(void)putc('\n', outf);
    365 		(void)fprintf(outf,
    366 			"%s: Waiting for tape drive close to complete...",
    367 			argv0);
    368 		(void)fflush(outf);
    369 	}
    370 
    371 	/*
    372 	 * if nothing was written to the archive (and we created it), we remove
    373 	 * it
    374 	 */
    375 	if (can_unlnk && (fstat(arfd, &arsb) == 0) && (S_ISREG(arsb.st_mode)) &&
    376 	    (arsb.st_size == 0)) {
    377 		(void)unlink(arcname);
    378 		can_unlnk = 0;
    379 	}
    380 
    381 	(void)close(arfd);
    382 
    383 	if (vflag && (artyp == ISTAPE)) {
    384 		(void)fputs("done.\n", outf);
    385 		vfpart = 0;
    386 		(void)fflush(outf);
    387 	}
    388 	arfd = -1;
    389 
    390 	if (!io_ok && !did_io) {
    391 		flcnt = 0;
    392 		return;
    393 	}
    394 	did_io = io_ok = 0;
    395 
    396 	/*
    397 	 * The volume number is only increased when the last device has data
    398 	 * and we have already determined the archive format.
    399 	 */
    400 	if (frmt != NULL)
    401 		++arvol;
    402 
    403 	if (!vflag) {
    404 		flcnt = 0;
    405 		return;
    406 	}
    407 
    408 	/*
    409 	 * Print out a summary of I/O for this archive volume.
    410 	 */
    411 	if (vfpart) {
    412 		(void)putc('\n', outf);
    413 		vfpart = 0;
    414 	}
    415 
    416 	/*
    417 	 * If we have not determined the format yet, we just say how many bytes
    418 	 * we have skipped over looking for a header to id. there is no way we
    419 	 * could have written anything yet.
    420 	 */
    421 	if (frmt == NULL) {
    422 #	ifdef NET2_STAT
    423 		(void)fprintf(outf, "%s: unknown format, %lu bytes skipped.\n",
    424 #	else
    425 		(void)fprintf(outf, "%s: unknown format, %qu bytes skipped.\n",
    426 #	endif
    427 		    argv0, (unsigned long long) rdcnt);
    428 		(void)fflush(outf);
    429 		flcnt = 0;
    430 		return;
    431 	}
    432 
    433 	(void)fprintf(outf,
    434 #	ifdef NET2_STAT
    435 	    "%s: %s vol %d, %lu files, %lu bytes read, %lu bytes written.\n",
    436 #	else
    437 	    "%s: %s vol %d, %lu files, %qu bytes read, %qu bytes written.\n",
    438 #	endif
    439 	    argv0, frmt->name, arvol-1, flcnt,
    440 	    (unsigned long long) rdcnt,
    441 	    (unsigned long long) wrcnt);
    442 	(void)fflush(outf);
    443 	flcnt = 0;
    444 }
    445 
    446 /*
    447  * ar_drain()
    448  *	drain any archive format independent padding from an archive read
    449  *	from a socket or a pipe. This is to prevent the process on the
    450  *	other side of the pipe from getting a SIGPIPE (pax will stop
    451  *	reading an archive once a format dependent trailer is detected).
    452  */
    453 #if __STDC__
    454 void
    455 ar_drain(void)
    456 #else
    457 void
    458 ar_drain()
    459 #endif
    460 {
    461 	int res;
    462 	char drbuf[MAXBLK];
    463 
    464 	/*
    465 	 * we only drain from a pipe/socket. Other devices can be closed
    466 	 * without reading up to end of file. We sure hope that pipe is closed
    467 	 * on the other side so we will get an EOF.
    468 	 */
    469 	if ((artyp != ISPIPE) || (lstrval <= 0))
    470 		return;
    471 
    472 	/*
    473 	 * keep reading until pipe is drained
    474 	 */
    475 	while ((res = read(arfd, drbuf, sizeof(drbuf))) > 0)
    476 		;
    477 	lstrval = res;
    478 }
    479 
    480 /*
    481  * ar_set_wr()
    482  *	Set up device right before switching from read to write in an append.
    483  *	device dependent code (if required) to do this should be added here.
    484  *	For all archive devices we are already positioned at the place we want
    485  *	to start writing when this routine is called.
    486  * Return:
    487  *	0 if all ready to write, -1 otherwise
    488  */
    489 
    490 #if __STDC__
    491 int
    492 ar_set_wr(void)
    493 #else
    494 int
    495 ar_set_wr()
    496 #endif
    497 {
    498 	off_t cpos;
    499 
    500 	/*
    501 	 * we must make sure the trailer is rewritten on append, ar_next()
    502 	 * will stop us if the archive containing the trailer was not written
    503 	 */
    504 	wr_trail = 0;
    505 
    506 	/*
    507 	 * Add any device dependent code as required here
    508 	 */
    509 	if (artyp != ISREG)
    510 		return(0);
    511 	/*
    512 	 * Ok we have an archive in a regular file. If we were rewriting a
    513 	 * file, we must get rid of all the stuff after the current offset
    514 	 * (it was not written by pax).
    515 	 */
    516 	if (((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) ||
    517 	    (ftruncate(arfd, cpos) < 0)) {
    518 		syswarn(1, errno, "Unable to truncate archive file");
    519 		return(-1);
    520 	}
    521 	return(0);
    522 }
    523 
    524 /*
    525  * ar_app_ok()
    526  *	check if the last volume in the archive allows appends. We cannot check
    527  *	this until we are ready to write since there is no spec that says all
    528  *	volumes in a single archive have to be of the same type...
    529  * Return:
    530  *	0 if we can append, -1 otherwise.
    531  */
    532 
    533 #if __STDC__
    534 int
    535 ar_app_ok(void)
    536 #else
    537 int
    538 ar_app_ok()
    539 #endif
    540 {
    541 	if (artyp == ISPIPE) {
    542 		tty_warn(1,
    543 		    "Cannot append to an archive obtained from a pipe.");
    544 		return(-1);
    545 	}
    546 
    547 	if (!invld_rec)
    548 		return(0);
    549 	tty_warn(1,
    550 	    "Cannot append, device record size %d does not support %s spec",
    551 	    rdblksz, argv0);
    552 	return(-1);
    553 }
    554 
    555 /*
    556  * ar_read()
    557  *	read up to a specified number of bytes from the archive into the
    558  *	supplied buffer. When dealing with tapes we may not always be able to
    559  *	read what we want.
    560  * Return:
    561  *	Number of bytes in buffer. 0 for end of file, -1 for a read error.
    562  */
    563 
    564 #if __STDC__
    565 int
    566 ar_read(char *buf, int cnt)
    567 #else
    568 int
    569 ar_read(buf, cnt)
    570 	char *buf;
    571 	int cnt;
    572 #endif
    573 {
    574 	int res = 0;
    575 
    576 	/*
    577 	 * if last i/o was in error, no more reads until reset or new volume
    578 	 */
    579 	if (lstrval <= 0)
    580 		return(lstrval);
    581 
    582 	/*
    583 	 * how we read must be based on device type
    584 	 */
    585 	switch (artyp) {
    586 	case ISTAPE:
    587 		if ((res = read(arfd, buf, cnt)) > 0) {
    588 			/*
    589 			 * CAUTION: tape systems may not always return the same
    590 			 * sized records so we leave blksz == MAXBLK. The
    591 			 * physical record size that a tape drive supports is
    592 			 * very hard to determine in a uniform and portable
    593 			 * manner.
    594 			 */
    595 			io_ok = 1;
    596 			if (res != rdblksz) {
    597 				/*
    598 				 * Record size changed. If this is happens on
    599 				 * any record after the first, we probably have
    600 				 * a tape drive which has a fixed record size
    601 				 * we are getting multiple records in a single
    602 				 * read). Watch out for record blocking that
    603 				 * violates pax spec (must be a multiple of
    604 				 * BLKMULT).
    605 				 */
    606 				rdblksz = res;
    607 				if (rdblksz % BLKMULT)
    608 					invld_rec = 1;
    609 			}
    610 			return(res);
    611 		}
    612 		break;
    613 	case ISREG:
    614 	case ISBLK:
    615 	case ISCHR:
    616 	case ISPIPE:
    617 	default:
    618 		/*
    619 		 * Files are so easy to deal with. These other things cannot
    620 		 * be trusted at all. So when we are dealing with character
    621 		 * devices and pipes we just take what they have ready for us
    622 		 * and return. Trying to do anything else with them runs the
    623 		 * risk of failure.
    624 		 */
    625 		if ((res = read(arfd, buf, cnt)) > 0) {
    626 			io_ok = 1;
    627 			return(res);
    628 		}
    629 		break;
    630 	}
    631 
    632 	/*
    633 	 * We are in trouble at this point, something is broken...
    634 	 */
    635 	lstrval = res;
    636 	if (res < 0)
    637 		syswarn(1, errno, "Failed read on archive volume %d", arvol);
    638 	else
    639 		tty_warn(0, "End of archive volume %d reached", arvol);
    640 	return(res);
    641 }
    642 
    643 /*
    644  * ar_write()
    645  *	Write a specified number of bytes in supplied buffer to the archive
    646  *	device so it appears as a single "block". Deals with errors and tries
    647  *	to recover when faced with short writes.
    648  * Return:
    649  *	Number of bytes written. 0 indicates end of volume reached and with no
    650  *	flaws (as best that can be detected). A -1 indicates an unrecoverable
    651  *	error in the archive occured.
    652  */
    653 
    654 #if __STDC__
    655 int
    656 ar_write(char *buf, int bsz)
    657 #else
    658 int
    659 ar_write(buf, bsz)
    660 	char *buf;
    661 	int bsz;
    662 #endif
    663 {
    664 	int res;
    665 	off_t cpos;
    666 
    667 	/*
    668 	 * do not allow pax to create a "bad" archive. Once a write fails on
    669 	 * an archive volume prevent further writes to it.
    670 	 */
    671 	if (lstrval <= 0)
    672 		return(lstrval);
    673 
    674 	if ((res = write(arfd, buf, bsz)) == bsz) {
    675 		wr_trail = 1;
    676 		io_ok = 1;
    677 		return(bsz);
    678 	}
    679 	/*
    680 	 * write broke, see what we can do with it. We try to send any partial
    681 	 * writes that may violate pax spec to the next archive volume.
    682 	 */
    683 	if (res < 0)
    684 		lstrval = res;
    685 	else
    686 		lstrval = 0;
    687 
    688 	switch (artyp) {
    689 	case ISREG:
    690 		if ((res > 0) && (res % BLKMULT)) {
    691 			/*
    692 		 	 * try to fix up partial writes which are not BLKMULT
    693 			 * in size by forcing the runt record to next archive
    694 			 * volume
    695 		 	 */
    696 			if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0)
    697 				break;
    698 			cpos -= (off_t)res;
    699 			if (ftruncate(arfd, cpos) < 0)
    700 				break;
    701 			res = lstrval = 0;
    702 			break;
    703 		}
    704 		if (res >= 0)
    705 			break;
    706 		/*
    707 		 * if file is out of space, handle it like a return of 0
    708 		 */
    709 		if ((errno == ENOSPC) || (errno == EFBIG) || (errno == EDQUOT))
    710 			res = lstrval = 0;
    711 		break;
    712 	case ISTAPE:
    713 	case ISCHR:
    714 	case ISBLK:
    715 		if (res >= 0)
    716 			break;
    717 		if (errno == EACCES) {
    718 			tty_warn(0,
    719 			    "Write failed, archive is write protected.");
    720 			res = lstrval = 0;
    721 			return(0);
    722 		}
    723 		/*
    724 		 * see if we reached the end of media, if so force a change to
    725 		 * the next volume
    726 		 */
    727 		if ((errno == ENOSPC) || (errno == EIO) || (errno == ENXIO))
    728 			res = lstrval = 0;
    729 		break;
    730 	case ISPIPE:
    731 	default:
    732 		/*
    733 		 * we cannot fix errors to these devices
    734 		 */
    735 		break;
    736 	}
    737 
    738 	/*
    739 	 * Better tell the user the bad news...
    740 	 * if this is a block aligned archive format, we may have a bad archive
    741 	 * if the format wants the header to start at a BLKMULT boundry. While
    742 	 * we can deal with the mis-aligned data, it violates spec and other
    743 	 * archive readers will likely fail. if the format is not block
    744 	 * aligned, the user may be lucky (and the archive is ok).
    745 	 */
    746 	if (res >= 0) {
    747 		if (res > 0)
    748 			wr_trail = 1;
    749 		io_ok = 1;
    750 	}
    751 
    752 	/*
    753 	 * If we were trying to rewrite the trailer and it didn't work, we
    754 	 * must quit right away.
    755 	 */
    756 	if (!wr_trail && (res <= 0)) {
    757 		tty_warn(1,
    758 		    "Unable to append, trailer re-write failed. Quitting.");
    759 		return(res);
    760 	}
    761 
    762 	if (res == 0)
    763 		tty_warn(0, "End of archive volume %d reached", arvol);
    764 	else if (res < 0)
    765 		syswarn(1, errno, "Failed write to archive volume: %d", arvol);
    766 	else if (!frmt->blkalgn || ((res % frmt->blkalgn) == 0))
    767 		tty_warn(0,
    768 		    "WARNING: partial archive write. Archive MAY BE FLAWED");
    769 	else
    770 		tty_warn(1,"WARNING: partial archive write. Archive IS FLAWED");
    771 	return(res);
    772 }
    773 
    774 /*
    775  * ar_rdsync()
    776  *	Try to move past a bad spot on a flawed archive as needed to continue
    777  *	I/O. Clears error flags to allow I/O to continue.
    778  * Return:
    779  *	0 when ok to try i/o again, -1 otherwise.
    780  */
    781 
    782 #if __STDC__
    783 int
    784 ar_rdsync(void)
    785 #else
    786 int
    787 ar_rdsync()
    788 #endif
    789 {
    790 	long fsbz;
    791 	off_t cpos;
    792 	off_t mpos;
    793         struct mtop mb;
    794 
    795 	/*
    796 	 * Fail resync attempts at user request (done) or this is going to be
    797 	 * an update/append to a existing archive. if last i/o hit media end,
    798 	 * we need to go to the next volume not try a resync
    799 	 */
    800 	if ((done > 0) || (lstrval == 0))
    801 		return(-1);
    802 
    803 	if ((act == APPND) || (act == ARCHIVE)) {
    804 		tty_warn(1, "Cannot allow updates to an archive with flaws.");
    805 		return(-1);
    806 	}
    807 	if (io_ok)
    808 		did_io = 1;
    809 
    810 	switch(artyp) {
    811 	case ISTAPE:
    812 		/*
    813 		 * if the last i/o was a successful data transfer, we assume
    814 		 * the fault is just a bad record on the tape that we are now
    815 		 * past. If we did not get any data since the last resync try
    816 		 * to move the tape foward one PHYSICAL record past any
    817 		 * damaged tape section. Some tape drives are stubborn and need
    818 		 * to be pushed.
    819 		 */
    820 		if (io_ok) {
    821 			io_ok = 0;
    822 			lstrval = 1;
    823 			break;
    824 		}
    825 		mb.mt_op = MTFSR;
    826 		mb.mt_count = 1;
    827 		if (ioctl(arfd, MTIOCTOP, &mb) < 0)
    828 			break;
    829 		lstrval = 1;
    830 		break;
    831 	case ISREG:
    832 	case ISCHR:
    833 	case ISBLK:
    834 		/*
    835 		 * try to step over the bad part of the device.
    836 		 */
    837 		io_ok = 0;
    838 		if (((fsbz = arsb.st_blksize) <= 0) || (artyp != ISREG))
    839 			fsbz = BLKMULT;
    840 		if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0)
    841 			break;
    842 		mpos = fsbz - (cpos % (off_t)fsbz);
    843 		if (lseek(arfd, mpos, SEEK_CUR) < 0)
    844 			break;
    845 		lstrval = 1;
    846 		break;
    847 	case ISPIPE:
    848 	default:
    849 		/*
    850 		 * cannot recover on these archive device types
    851 		 */
    852 		io_ok = 0;
    853 		break;
    854 	}
    855 	if (lstrval <= 0) {
    856 		tty_warn(1, "Unable to recover from an archive read failure.");
    857 		return(-1);
    858 	}
    859 	tty_warn(0, "Attempting to recover from an archive read failure.");
    860 	return(0);
    861 }
    862 
    863 /*
    864  * ar_fow()
    865  *	Move the I/O position within the archive foward the specified number of
    866  *	bytes as supported by the device. If we cannot move the requested
    867  *	number of bytes, return the actual number of bytes moved in skipped.
    868  * Return:
    869  *	0 if moved the requested distance, -1 on complete failure, 1 on
    870  *	partial move (the amount moved is in skipped)
    871  */
    872 
    873 #if __STDC__
    874 int
    875 ar_fow(off_t sksz, off_t *skipped)
    876 #else
    877 int
    878 ar_fow(sksz, skipped)
    879 	off_t sksz;
    880 	off_t *skipped;
    881 #endif
    882 {
    883 	off_t cpos;
    884 	off_t mpos;
    885 
    886 	*skipped = 0;
    887 	if (sksz <= 0)
    888 		return(0);
    889 
    890 	/*
    891 	 * we cannot move foward at EOF or error
    892 	 */
    893 	if (lstrval <= 0)
    894 		return(lstrval);
    895 
    896 	/*
    897 	 * Safer to read forward on devices where it is hard to find the end of
    898 	 * the media without reading to it. With tapes we cannot be sure of the
    899 	 * number of physical blocks to skip (we do not know physical block
    900 	 * size at this point), so we must only read foward on tapes!
    901 	 */
    902 	if (artyp != ISREG)
    903 		return(0);
    904 
    905 	/*
    906 	 * figure out where we are in the archive
    907 	 */
    908 	if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) >= 0) {
    909 		/*
    910 	 	 * we can be asked to move farther than there are bytes in this
    911 		 * volume, if so, just go to file end and let normal buf_fill()
    912 		 * deal with the end of file (it will go to next volume by
    913 		 * itself)
    914 	 	 */
    915 		if ((mpos = cpos + sksz) > arsb.st_size) {
    916 			*skipped = arsb.st_size - cpos;
    917 			mpos = arsb.st_size;
    918 		} else
    919 			*skipped = sksz;
    920 		if (lseek(arfd, mpos, SEEK_SET) >= 0)
    921 			return(0);
    922 	}
    923 	syswarn(1, errno, "Foward positioning operation on archive failed");
    924 	lstrval = -1;
    925 	return(-1);
    926 }
    927 
    928 /*
    929  * ar_rev()
    930  *	move the i/o position within the archive backwards the specified byte
    931  *	count as supported by the device. With tapes drives we RESET rdblksz to
    932  *	the PHYSICAL blocksize.
    933  *	NOTE: We should only be called to move backwards so we can rewrite the
    934  *	last records (the trailer) of an archive (APPEND).
    935  * Return:
    936  *	0 if moved the requested distance, -1 on complete failure
    937  */
    938 
    939 #if __STDC__
    940 int
    941 ar_rev(off_t sksz)
    942 #else
    943 int
    944 ar_rev(sksz)
    945 	off_t sksz;
    946 #endif
    947 {
    948 	off_t cpos;
    949         struct mtop mb;
    950 	int phyblk;
    951 
    952 	/*
    953 	 * make sure we do not have try to reverse on a flawed archive
    954 	 */
    955 	if (lstrval < 0)
    956 		return(lstrval);
    957 
    958 	switch(artyp) {
    959 	case ISPIPE:
    960 		if (sksz <= 0)
    961 			break;
    962 		/*
    963 		 * cannot go backwards on these critters
    964 		 */
    965 		tty_warn(1, "Reverse positioning on pipes is not supported.");
    966 		lstrval = -1;
    967 		return(-1);
    968 	case ISREG:
    969 	case ISBLK:
    970 	case ISCHR:
    971 	default:
    972 		if (sksz <= 0)
    973 			break;
    974 
    975 		/*
    976 		 * For things other than files, backwards movement has a very
    977 		 * high probability of failure as we really do not know the
    978 		 * true attributes of the device we are talking to (the device
    979 		 * may not even have the ability to lseek() in any direction).
    980 		 * First we figure out where we are in the archive.
    981 		 */
    982 		if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) {
    983 			syswarn(1, errno,
    984 			   "Unable to obtain current archive byte offset");
    985 			lstrval = -1;
    986 			return(-1);
    987 		}
    988 
    989 		/*
    990 		 * we may try to go backwards past the start when the archive
    991 		 * is only a single record. If this hapens and we are on a
    992 		 * multi volume archive, we need to go to the end of the
    993 		 * previous volume and continue our movement backwards from
    994 		 * there.
    995 		 */
    996 		if ((cpos -= sksz) < (off_t)0L) {
    997 			if (arvol > 1) {
    998 				/*
    999 				 * this should never happen
   1000 				 */
   1001 				tty_warn(1,
   1002 				    "Reverse position on previous volume.");
   1003 				lstrval = -1;
   1004 				return(-1);
   1005 			}
   1006 			cpos = (off_t)0L;
   1007 		}
   1008 		if (lseek(arfd, cpos, SEEK_SET) < 0) {
   1009 			syswarn(1, errno, "Unable to seek archive backwards");
   1010 			lstrval = -1;
   1011 			return(-1);
   1012 		}
   1013 		break;
   1014 	case ISTAPE:
   1015 		/*
   1016 	 	 * Calculate and move the proper number of PHYSICAL tape
   1017 		 * blocks. If the sksz is not an even multiple of the physical
   1018 		 * tape size, we cannot do the move (this should never happen).
   1019 		 * (We also cannot handler trailers spread over two vols).
   1020 		 * get_phys() also makes sure we are in front of the filemark.
   1021 	 	 */
   1022 		if ((phyblk = get_phys()) <= 0) {
   1023 			lstrval = -1;
   1024 			return(-1);
   1025 		}
   1026 
   1027 		/*
   1028 		 * make sure future tape reads only go by physical tape block
   1029 		 * size (set rdblksz to the real size).
   1030 		 */
   1031 		rdblksz = phyblk;
   1032 
   1033 		/*
   1034 		 * if no movement is required, just return (we must be after
   1035 		 * get_phys() so the physical blocksize is properly set)
   1036 		 */
   1037 		if (sksz <= 0)
   1038 			break;
   1039 
   1040 		/*
   1041 		 * ok we have to move. Make sure the tape drive can do it.
   1042 		 */
   1043 		if (sksz % phyblk) {
   1044 			tty_warn(1,
   1045 			    "Tape drive unable to backspace requested amount");
   1046 			lstrval = -1;
   1047 			return(-1);
   1048 		}
   1049 
   1050 		/*
   1051 		 * move backwards the requested number of bytes
   1052 		 */
   1053 		mb.mt_op = MTBSR;
   1054 		mb.mt_count = sksz/phyblk;
   1055 		if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
   1056 			syswarn(1,errno, "Unable to backspace tape %d blocks.",
   1057 			    mb.mt_count);
   1058 			lstrval = -1;
   1059 			return(-1);
   1060 		}
   1061 		break;
   1062 	}
   1063 	lstrval = 1;
   1064 	return(0);
   1065 }
   1066 
   1067 /*
   1068  * get_phys()
   1069  *	Determine the physical block size on a tape drive. We need the physical
   1070  *	block size so we know how many bytes we skip over when we move with
   1071  *	mtio commands. We also make sure we are BEFORE THE TAPE FILEMARK when
   1072  *	return.
   1073  *	This is one really SLOW routine...
   1074  * Return:
   1075  *	physical block size if ok (ok > 0), -1 otherwise
   1076  */
   1077 
   1078 #if __STDC__
   1079 static int
   1080 get_phys(void)
   1081 #else
   1082 static int
   1083 get_phys()
   1084 #endif
   1085 {
   1086 	int padsz = 0;
   1087 	int res;
   1088 	int phyblk;
   1089 	struct mtop mb;
   1090 	char scbuf[MAXBLK];
   1091 
   1092 	/*
   1093 	 * move to the file mark, and then back up one record and read it.
   1094 	 * this should tell us the physical record size the tape is using.
   1095 	 */
   1096 	if (lstrval == 1) {
   1097 		/*
   1098 		 * we know we are at file mark when we get back a 0 from
   1099 		 * read()
   1100 		 */
   1101 		while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0)
   1102 			padsz += res;
   1103 		if (res < 0) {
   1104 			syswarn(1, errno, "Unable to locate tape filemark.");
   1105 			return(-1);
   1106 		}
   1107 	}
   1108 
   1109 	/*
   1110 	 * move backwards over the file mark so we are at the end of the
   1111 	 * last record.
   1112 	 */
   1113 	mb.mt_op = MTBSF;
   1114 	mb.mt_count = 1;
   1115 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
   1116 		syswarn(1, errno, "Unable to backspace over tape filemark.");
   1117 		return(-1);
   1118 	}
   1119 
   1120 	/*
   1121 	 * move backwards so we are in front of the last record and read it to
   1122 	 * get physical tape blocksize.
   1123 	 */
   1124 	mb.mt_op = MTBSR;
   1125 	mb.mt_count = 1;
   1126 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
   1127 		syswarn(1, errno, "Unable to backspace over last tape block.");
   1128 		return(-1);
   1129 	}
   1130 	if ((phyblk = read(arfd, scbuf, sizeof(scbuf))) <= 0) {
   1131 		syswarn(1, errno, "Cannot determine archive tape blocksize.");
   1132 		return(-1);
   1133 	}
   1134 
   1135 	/*
   1136 	 * read foward to the file mark, then back up in front of the filemark
   1137 	 * (this is a bit paranoid, but should be safe to do).
   1138 	 */
   1139 	while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0)
   1140 		;
   1141 	if (res < 0) {
   1142 		syswarn(1, errno, "Unable to locate tape filemark.");
   1143 		return(-1);
   1144 	}
   1145 	mb.mt_op = MTBSF;
   1146 	mb.mt_count = 1;
   1147 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
   1148 		syswarn(1, errno, "Unable to backspace over tape filemark.");
   1149 		return(-1);
   1150 	}
   1151 
   1152 	/*
   1153 	 * set lstrval so we know that the filemark has not been seen
   1154 	 */
   1155 	lstrval = 1;
   1156 
   1157 	/*
   1158 	 * return if there was no padding
   1159 	 */
   1160 	if (padsz == 0)
   1161 		return(phyblk);
   1162 
   1163 	/*
   1164 	 * make sure we can move backwards over the padding. (this should
   1165 	 * never fail).
   1166 	 */
   1167 	if (padsz % phyblk) {
   1168 		tty_warn(1, "Tape drive unable to backspace requested amount");
   1169 		return(-1);
   1170 	}
   1171 
   1172 	/*
   1173 	 * move backwards over the padding so the head is where it was when
   1174 	 * we were first called (if required).
   1175 	 */
   1176 	mb.mt_op = MTBSR;
   1177 	mb.mt_count = padsz/phyblk;
   1178 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
   1179 		syswarn(1,errno,"Unable to backspace tape over %d pad blocks",
   1180 		    mb.mt_count);
   1181 		return(-1);
   1182 	}
   1183 	return(phyblk);
   1184 }
   1185 
   1186 /*
   1187  * ar_next()
   1188  *	prompts the user for the next volume in this archive. For some devices
   1189  *	we may allow the media to be changed. Otherwise a new archive is
   1190  *	prompted for. By pax spec, if there is no controlling tty or an eof is
   1191  *	read on tty input, we must quit pax.
   1192  * Return:
   1193  *	0 when ready to continue, -1 when all done
   1194  */
   1195 
   1196 #if __STDC__
   1197 int
   1198 ar_next(void)
   1199 #else
   1200 int
   1201 ar_next()
   1202 #endif
   1203 {
   1204 	char buf[PAXPATHLEN+2];
   1205 	static int freeit = 0;
   1206 	sigset_t o_mask;
   1207 
   1208 	/*
   1209 	 * WE MUST CLOSE THE DEVICE. A lot of devices must see last close, (so
   1210 	 * things like writing EOF etc will be done) (Watch out ar_close() can
   1211 	 * also be called via a signal handler, so we must prevent a race.
   1212 	 */
   1213 	if (sigprocmask(SIG_BLOCK, &s_mask, &o_mask) < 0)
   1214 		syswarn(0, errno, "Unable to set signal mask");
   1215 	ar_close();
   1216 	if (sigprocmask(SIG_SETMASK, &o_mask, (sigset_t *)NULL) < 0)
   1217 		syswarn(0, errno, "Unable to restore signal mask");
   1218 
   1219 	if (done || !wr_trail)
   1220 		return(-1);
   1221 
   1222 	tty_prnt("\nATTENTION! %s archive volume change required.\n", argv0);
   1223 
   1224 	/*
   1225 	 * if i/o is on stdin or stdout, we cannot reopen it (we do not know
   1226 	 * the name), the user will be forced to type it in.
   1227 	 */
   1228 	if (strcmp(arcname, STDO) && strcmp(arcname, STDN) && (artyp != ISREG)
   1229 	    && (artyp != ISPIPE)) {
   1230 		if (artyp == ISTAPE) {
   1231 			tty_prnt("%s ready for archive tape volume: %d\n",
   1232 				arcname, arvol);
   1233 			tty_prnt("Load the NEXT TAPE on the tape drive");
   1234 		} else {
   1235 			tty_prnt("%s ready for archive volume: %d\n",
   1236 				arcname, arvol);
   1237 			tty_prnt("Load the NEXT STORAGE MEDIA (if required)");
   1238 		}
   1239 
   1240 		if ((act == ARCHIVE) || (act == APPND))
   1241 			tty_prnt(" and make sure it is WRITE ENABLED.\n");
   1242 		else
   1243 			tty_prnt("\n");
   1244 
   1245 		for(;;) {
   1246 			tty_prnt("Type \"y\" to continue, \".\" to quit %s,",
   1247 				argv0);
   1248 			tty_prnt(" or \"s\" to switch to new device.\nIf you");
   1249 			tty_prnt(" cannot change storage media, type \"s\"\n");
   1250 			tty_prnt("Is the device ready and online? > ");
   1251 
   1252 			if ((tty_read(buf,sizeof(buf))<0) || !strcmp(buf,".")){
   1253 				done = 1;
   1254 				lstrval = -1;
   1255 				tty_prnt("Quitting %s!\n", argv0);
   1256 				vfpart = 0;
   1257 				return(-1);
   1258 			}
   1259 
   1260 			if ((buf[0] == '\0') || (buf[1] != '\0')) {
   1261 				tty_prnt("%s unknown command, try again\n",buf);
   1262 				continue;
   1263 			}
   1264 
   1265 			switch (buf[0]) {
   1266 			case 'y':
   1267 			case 'Y':
   1268 				/*
   1269 				 * we are to continue with the same device
   1270 				 */
   1271 				if (ar_open(arcname) >= 0)
   1272 					return(0);
   1273 				tty_prnt("Cannot re-open %s, try again\n",
   1274 					arcname);
   1275 				continue;
   1276 			case 's':
   1277 			case 'S':
   1278 				/*
   1279 				 * user wants to open a different device
   1280 				 */
   1281 				tty_prnt("Switching to a different archive\n");
   1282 				break;
   1283 			default:
   1284 				tty_prnt("%s unknown command, try again\n",buf);
   1285 				continue;
   1286 			}
   1287 			break;
   1288 		}
   1289 	} else
   1290 		tty_prnt("Ready for archive volume: %d\n", arvol);
   1291 
   1292 	/*
   1293 	 * have to go to a different archive
   1294 	 */
   1295 	for (;;) {
   1296 		tty_prnt("Input archive name or \".\" to quit %s.\n", argv0);
   1297 		tty_prnt("Archive name > ");
   1298 
   1299 		if ((tty_read(buf, sizeof(buf)) < 0) || !strcmp(buf, ".")) {
   1300 			done = 1;
   1301 			lstrval = -1;
   1302 			tty_prnt("Quitting %s!\n", argv0);
   1303 			vfpart = 0;
   1304 			return(-1);
   1305 		}
   1306 		if (buf[0] == '\0') {
   1307 			tty_prnt("Empty file name, try again\n");
   1308 			continue;
   1309 		}
   1310                 if (!strcmp(buf, "..")) {
   1311                         tty_prnt("Illegal file name: .. try again\n");
   1312                         continue;
   1313                 }
   1314 		if (strlen(buf) > PAXPATHLEN) {
   1315 			tty_prnt("File name too long, try again\n");
   1316 			continue;
   1317 		}
   1318 
   1319 		/*
   1320 		 * try to open new archive
   1321 		 */
   1322 		if (ar_open(buf) >= 0) {
   1323 			if (freeit) {
   1324 				(void)free(arcname);
   1325 				freeit = 0;
   1326 			}
   1327 			if ((arcname = strdup(buf)) == NULL) {
   1328 				done = 1;
   1329 				lstrval = -1;
   1330 				tty_warn(0, "Cannot save archive name.");
   1331 				return(-1);
   1332 			}
   1333 			freeit = 1;
   1334 			break;
   1335 		}
   1336 		tty_prnt("Cannot open %s, try again\n", buf);
   1337 		continue;
   1338 	}
   1339 	return(0);
   1340 }
   1341 
   1342 /*
   1343  * ar_start_gzip()
   1344  * starts the gzip compression/decompression process as a child, using magic
   1345  * to keep the fd the same in the calling function (parent).
   1346  */
   1347 void
   1348 #ifdef __STDC__
   1349 ar_start_gzip(int fd)
   1350 #else
   1351 ar_start_gzip(fd)
   1352 	int fd;
   1353 #endif
   1354 {
   1355 	pid_t pid;
   1356 	int fds[2];
   1357 	char *gzip_flags;
   1358 
   1359 	if (pipe(fds) < 0)
   1360 		err(1, "could not pipe");
   1361 	pid = fork();
   1362 	if (pid < 0)
   1363 		err(1, "could not fork");
   1364 
   1365 	/* parent */
   1366 	if (pid) {
   1367 		switch (act) {
   1368 		case ARCHIVE:
   1369 			dup2(fds[1], fd);
   1370 			break;
   1371 		case LIST:
   1372 		case EXTRACT:
   1373 			dup2(fds[0], fd);
   1374 			break;
   1375 		default:
   1376 			errx(1, "ar_start_gzip:  impossible");
   1377 		}
   1378 		close(fds[0]);
   1379 		close(fds[1]);
   1380 	} else {
   1381 		switch (act) {
   1382 		case ARCHIVE:
   1383 			dup2(fds[0], STDIN_FILENO);
   1384 			dup2(fd, STDOUT_FILENO);
   1385 			gzip_flags = "-c";
   1386 			break;
   1387 		case LIST:
   1388 		case EXTRACT:
   1389 			dup2(fds[1], STDOUT_FILENO);
   1390 			dup2(fd, STDIN_FILENO);
   1391 			gzip_flags = "-dc";
   1392 			break;
   1393 		default:
   1394 			errx(1, "ar_start_gzip:  impossible");
   1395 		}
   1396 		close(fds[0]);
   1397 		close(fds[1]);
   1398 		if (execlp(gzip_program, gzip_program, gzip_flags, NULL) < 0)
   1399 			err(1, "could not exec");
   1400 		/* NOTREACHED */
   1401 	}
   1402 }
   1403