Home | History | Annotate | Line # | Download | only in pax
ar_io.c revision 1.18
      1 /*	$NetBSD: ar_io.c,v 1.18 2000/03/30 17:53:04 thorpej 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.18 2000/03/30 17:53:04 thorpej 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>"	/* pseudo name for stdout */
     76 #define STDN		"<STDIN>"	/* pseudo 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 const char *arcname;			/* printable name of archive */
     89 const char *gzip_program;		/* name of gzip program */
     90 time_t starttime;			/* time the run started */
     91 int minusCfd = -1;			/* active -C directory */
     92 int curdirfd = -1;			/* original current directory */
     93 int force_one_volume;			/* 1 if we ignore volume changes */
     94 
     95 static int get_phys __P((void));
     96 extern sigset_t s_mask;
     97 static void ar_start_gzip __P((int));
     98 static const char *timefmt __P((char *, size_t, off_t, time_t));
     99 static const char *sizefmt __P((char *, size_t, off_t));
    100 
    101 /*
    102  * ar_open()
    103  *	Opens the next archive volume. Determines the type of the device and
    104  *	sets up block sizes as required by the archive device and the format.
    105  *	Note: we may be called with name == NULL on the first open only.
    106  * Return:
    107  *	-1 on failure, 0 otherwise
    108  */
    109 
    110 #if __STDC__
    111 int
    112 ar_open(const char *name)
    113 #else
    114 int
    115 ar_open(name)
    116 	const char *name;
    117 #endif
    118 {
    119 	struct mtget mb;
    120 
    121 	/*
    122 	 * change back to the current directory (for now).
    123 	 */
    124 	if (curdirfd != -1)
    125 		fchdir(curdirfd);
    126 
    127 	if (arfd != -1)
    128 		(void)close(arfd);
    129 	arfd = -1;
    130 	can_unlnk = did_io = io_ok = invld_rec = 0;
    131 	artyp = ISREG;
    132 	flcnt = 0;
    133 
    134 	/*
    135 	 * open based on overall operation mode
    136 	 */
    137 	switch (act) {
    138 	case LIST:
    139 	case EXTRACT:
    140 		if (name == NULL) {
    141 			arfd = STDIN_FILENO;
    142 			arcname = STDN;
    143 		} else if ((arfd = open(name, EXT_MODE, DMOD)) < 0)
    144 			syswarn(0, errno, "Failed open to read on %s", name);
    145 		if (zflag)
    146 			ar_start_gzip(arfd);
    147 		break;
    148 	case ARCHIVE:
    149 		if (name == NULL) {
    150 			arfd = STDOUT_FILENO;
    151 			arcname = STDO;
    152 		} else if ((arfd = open(name, AR_MODE, DMOD)) < 0)
    153 			syswarn(0, errno, "Failed open to write on %s", name);
    154 		else
    155 			can_unlnk = 1;
    156 		if (zflag)
    157 			ar_start_gzip(arfd);
    158 		break;
    159 	case APPND:
    160 		if (zflag)
    161 			err(1, "can not gzip while appending");
    162 		if (name == NULL) {
    163 			arfd = STDOUT_FILENO;
    164 			arcname = STDO;
    165 		} else if ((arfd = open(name, APP_MODE, DMOD)) < 0)
    166 			syswarn(0, errno, "Failed open to read/write on %s",
    167 				name);
    168 		break;
    169 	case COPY:
    170 		/*
    171 		 * arfd not used in COPY mode
    172 		 */
    173 		arcname = "<NONE>";
    174 		lstrval = 1;
    175 		return(0);
    176 	}
    177 	if (arfd < 0)
    178 		return(-1);
    179 
    180 	/*
    181 	 * set up is based on device type
    182 	 */
    183 	if (fstat(arfd, &arsb) < 0) {
    184 		syswarn(0, errno, "Failed stat on %s", arcname);
    185 		(void)close(arfd);
    186 		arfd = -1;
    187 		can_unlnk = 0;
    188 		return(-1);
    189 	}
    190 	if (S_ISDIR(arsb.st_mode)) {
    191 		tty_warn(0, "Cannot write an archive on top of a directory %s",
    192 		    arcname);
    193 		(void)close(arfd);
    194 		arfd = -1;
    195 		can_unlnk = 0;
    196 		return(-1);
    197 	}
    198 
    199 	if (S_ISCHR(arsb.st_mode))
    200 		artyp = ioctl(arfd, MTIOCGET, &mb) ? ISCHR : ISTAPE;
    201 	else if (S_ISBLK(arsb.st_mode))
    202 		artyp = ISBLK;
    203 	else if ((lseek(arfd, (off_t)0L, SEEK_CUR) == -1) && (errno == ESPIPE))
    204 		artyp = ISPIPE;
    205 	else
    206 		artyp = ISREG;
    207 
    208 	/*
    209 	 * make sure we beyond any doubt that we only can unlink regular files
    210 	 * we created
    211 	 */
    212 	if (artyp != ISREG)
    213 		can_unlnk = 0;
    214 
    215 	/*
    216 	 * change directory if necessary
    217 	 */
    218 	if (minusCfd != -1)
    219 		fchdir(minusCfd);
    220 
    221 	/*
    222 	 * if we are writing, we are done
    223 	 */
    224 	if (act == ARCHIVE) {
    225 		blksz = rdblksz = wrblksz;
    226 		lstrval = 1;
    227 		return(0);
    228 	}
    229 
    230 	/*
    231 	 * set default blksz on read. APPNDs writes rdblksz on the last volume
    232 	 * On all new archive volumes, we shift to wrblksz (if the user
    233 	 * specified one, otherwize we will continue to use rdblksz). We
    234 	 * must to set blocksize based on what kind of device the archive is
    235 	 * stored.
    236 	 */
    237 	switch(artyp) {
    238 	case ISTAPE:
    239 		/*
    240 		 * Tape drives come in at least two flavors. Those that support
    241 		 * variable sized records and those that have fixed sized
    242 		 * records. They must be treated differently. For tape drives
    243 		 * that support variable sized records, we must make large
    244 		 * reads to make sure we get the entire record, otherwise we
    245 		 * will just get the first part of the record (up to size we
    246 		 * asked). Tapes with fixed sized records may or may not return
    247 		 * multiple records in a single read. We really do not care
    248 		 * what the physical record size is UNLESS we are going to
    249 		 * append. (We will need the physical block size to rewrite
    250 		 * the trailer). Only when we are appending do we go to the
    251 		 * effort to figure out the true PHYSICAL record size.
    252 		 */
    253 		blksz = rdblksz = MAXBLK;
    254 		break;
    255 	case ISPIPE:
    256 	case ISBLK:
    257 	case ISCHR:
    258 		/*
    259 		 * Blocksize is not a major issue with these devices (but must
    260 		 * be kept a multiple of 512). If the user specified a write
    261 		 * block size, we use that to read. Under append, we must
    262 		 * always keep blksz == rdblksz. Otherwise we go ahead and use
    263 		 * the device optimal blocksize as (and if) returned by stat
    264 		 * and if it is within pax specs.
    265 		 */
    266 		if ((act == APPND) && wrblksz) {
    267 			blksz = rdblksz = wrblksz;
    268 			break;
    269 		}
    270 
    271 		if ((arsb.st_blksize > 0) && (arsb.st_blksize < MAXBLK) &&
    272 		    ((arsb.st_blksize % BLKMULT) == 0))
    273 			rdblksz = arsb.st_blksize;
    274 		else
    275 			rdblksz = DEVBLK;
    276 		/*
    277 		 * For performance go for large reads when we can without harm
    278 		 */
    279 		if ((act == APPND) || (artyp == ISCHR))
    280 			blksz = rdblksz;
    281 		else
    282 			blksz = MAXBLK;
    283 		break;
    284 	case ISREG:
    285 		/*
    286 		 * if the user specified wrblksz works, use it. Under appends
    287 		 * we must always keep blksz == rdblksz
    288 		 */
    289 		if ((act == APPND) && wrblksz && ((arsb.st_size%wrblksz)==0)){
    290 			blksz = rdblksz = wrblksz;
    291 			break;
    292 		}
    293 		/*
    294 		 * See if we can find the blocking factor from the file size
    295 		 */
    296 		for (rdblksz = MAXBLK; rdblksz > 0; rdblksz -= BLKMULT)
    297 			if ((arsb.st_size % rdblksz) == 0)
    298 				break;
    299 		/*
    300 		 * When we cannot find a match, we may have a flawed archive.
    301 		 */
    302 		if (rdblksz <= 0)
    303 			rdblksz = FILEBLK;
    304 		/*
    305 		 * for performance go for large reads when we can
    306 		 */
    307 		if (act == APPND)
    308 			blksz = rdblksz;
    309 		else
    310 			blksz = MAXBLK;
    311 		break;
    312 	default:
    313 		/*
    314 		 * should never happen, worse case, slow...
    315 		 */
    316 		blksz = rdblksz = BLKMULT;
    317 		break;
    318 	}
    319 	lstrval = 1;
    320 	return(0);
    321 }
    322 
    323 /*
    324  * ar_close()
    325  *	closes archive device, increments volume number, and prints i/o summary
    326  */
    327 #if __STDC__
    328 void
    329 ar_close(void)
    330 #else
    331 void
    332 ar_close()
    333 #endif
    334 {
    335 	FILE *outf;
    336 
    337 	if (arfd < 0) {
    338 		did_io = io_ok = flcnt = 0;
    339 		return;
    340 	}
    341 
    342 	if (act == LIST)
    343 		outf = stdout;
    344 	else
    345 		outf = stderr;
    346 
    347 	/*
    348 	 * Close archive file. This may take a LONG while on tapes (we may be
    349 	 * forced to wait for the rewind to complete) so tell the user what is
    350 	 * going on (this avoids the user hitting control-c thinking pax is
    351 	 * broken).
    352 	 */
    353 	if (vflag && (artyp == ISTAPE)) {
    354 		if (vfpart)
    355 			(void)putc('\n', outf);
    356 		(void)fprintf(outf,
    357 			"%s: Waiting for tape drive close to complete...",
    358 			argv0);
    359 		(void)fflush(outf);
    360 	}
    361 
    362 	/*
    363 	 * if nothing was written to the archive (and we created it), we remove
    364 	 * it
    365 	 */
    366 	if (can_unlnk && (fstat(arfd, &arsb) == 0) && (S_ISREG(arsb.st_mode)) &&
    367 	    (arsb.st_size == 0)) {
    368 		(void)unlink(arcname);
    369 		can_unlnk = 0;
    370 	}
    371 
    372 	(void)close(arfd);
    373 
    374 	if (vflag && (artyp == ISTAPE)) {
    375 		(void)fputs("done.\n", outf);
    376 		vfpart = 0;
    377 		(void)fflush(outf);
    378 	}
    379 	arfd = -1;
    380 
    381 	if (!io_ok && !did_io) {
    382 		flcnt = 0;
    383 		return;
    384 	}
    385 	did_io = io_ok = 0;
    386 
    387 	/*
    388 	 * The volume number is only increased when the last device has data
    389 	 * and we have already determined the archive format.
    390 	 */
    391 	if (frmt != NULL)
    392 		++arvol;
    393 
    394 	if (!vflag) {
    395 		flcnt = 0;
    396 		return;
    397 	}
    398 
    399 	/*
    400 	 * Print out a summary of I/O for this archive volume.
    401 	 */
    402 	if (vfpart) {
    403 		(void)putc('\n', outf);
    404 		vfpart = 0;
    405 	}
    406 
    407 	ar_summary(0);
    408 
    409 	(void)fflush(outf);
    410 	flcnt = 0;
    411 }
    412 
    413 /*
    414  * ar_drain()
    415  *	drain any archive format independent padding from an archive read
    416  *	from a socket or a pipe. This is to prevent the process on the
    417  *	other side of the pipe from getting a SIGPIPE (pax will stop
    418  *	reading an archive once a format dependent trailer is detected).
    419  */
    420 #if __STDC__
    421 void
    422 ar_drain(void)
    423 #else
    424 void
    425 ar_drain()
    426 #endif
    427 {
    428 	int res;
    429 	char drbuf[MAXBLK];
    430 
    431 	/*
    432 	 * we only drain from a pipe/socket. Other devices can be closed
    433 	 * without reading up to end of file. We sure hope that pipe is closed
    434 	 * on the other side so we will get an EOF.
    435 	 */
    436 	if ((artyp != ISPIPE) || (lstrval <= 0))
    437 		return;
    438 
    439 	/*
    440 	 * keep reading until pipe is drained
    441 	 */
    442 	while ((res = read_with_restart(arfd, drbuf, sizeof(drbuf))) > 0)
    443 		;
    444 	lstrval = res;
    445 }
    446 
    447 /*
    448  * ar_set_wr()
    449  *	Set up device right before switching from read to write in an append.
    450  *	device dependent code (if required) to do this should be added here.
    451  *	For all archive devices we are already positioned at the place we want
    452  *	to start writing when this routine is called.
    453  * Return:
    454  *	0 if all ready to write, -1 otherwise
    455  */
    456 
    457 #if __STDC__
    458 int
    459 ar_set_wr(void)
    460 #else
    461 int
    462 ar_set_wr()
    463 #endif
    464 {
    465 	off_t cpos;
    466 
    467 	/*
    468 	 * we must make sure the trailer is rewritten on append, ar_next()
    469 	 * will stop us if the archive containing the trailer was not written
    470 	 */
    471 	wr_trail = 0;
    472 
    473 	/*
    474 	 * Add any device dependent code as required here
    475 	 */
    476 	if (artyp != ISREG)
    477 		return(0);
    478 	/*
    479 	 * Ok we have an archive in a regular file. If we were rewriting a
    480 	 * file, we must get rid of all the stuff after the current offset
    481 	 * (it was not written by pax).
    482 	 */
    483 	if (((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) ||
    484 	    (ftruncate(arfd, cpos) < 0)) {
    485 		syswarn(1, errno, "Unable to truncate archive file");
    486 		return(-1);
    487 	}
    488 	return(0);
    489 }
    490 
    491 /*
    492  * ar_app_ok()
    493  *	check if the last volume in the archive allows appends. We cannot check
    494  *	this until we are ready to write since there is no spec that says all
    495  *	volumes in a single archive have to be of the same type...
    496  * Return:
    497  *	0 if we can append, -1 otherwise.
    498  */
    499 
    500 #if __STDC__
    501 int
    502 ar_app_ok(void)
    503 #else
    504 int
    505 ar_app_ok()
    506 #endif
    507 {
    508 	if (artyp == ISPIPE) {
    509 		tty_warn(1,
    510 		    "Cannot append to an archive obtained from a pipe.");
    511 		return(-1);
    512 	}
    513 
    514 	if (!invld_rec)
    515 		return(0);
    516 	tty_warn(1,
    517 	    "Cannot append, device record size %d does not support %s spec",
    518 	    rdblksz, argv0);
    519 	return(-1);
    520 }
    521 
    522 #ifdef SYS_NO_RESTART
    523 /*
    524  * read_with_restart()
    525  *	Equivalent to read() but does retry on signals.
    526  *	This function is not needed on 4.2BSD and later.
    527  * Return:
    528  *	Number of bytes written.  -1 indicates an error.
    529  */
    530 
    531 #if __STDC__
    532 int
    533 read_with_restart(int fd, void *buf, int bsz)
    534 #else
    535 int
    536 read_with_restart(fd, buf, bsz)
    537 	int fd;
    538 	void *buf;
    539 	int bsz;
    540 #endif
    541 {
    542 	int r;
    543 
    544 	while (((r = read(fd, buf, bsz)) < 0) && errno == EINTR)
    545 		;
    546 
    547 	return(r);
    548 }
    549 #endif
    550 
    551 /*
    552  * xread()
    553  *	Equivalent to read() but does retry on partial read, which may occur
    554  *	on signals.
    555  * Return:
    556  *	Number of bytes read.  0 for end of file, -1 for an error.
    557  */
    558 
    559 #if __STDC__
    560 int
    561 xread(int fd, void *buf, int bsz)
    562 #else
    563 int
    564 xread(fd, buf, bsz)
    565 	int fd;
    566 	void *buf;
    567 	int bsz;
    568 #endif
    569 {
    570 	char *b = buf;
    571 	int nread = 0;
    572 	int r;
    573 
    574 	do {
    575 		if ((r = read_with_restart(fd, b, bsz)) <= 0)
    576 			break;
    577 		b += r;
    578 		bsz -= r;
    579 		nread += r;
    580 	} while (bsz > 0);
    581 
    582 	return(nread ? nread : r);
    583 }
    584 
    585 #ifdef SYS_NO_RESTART
    586 /*
    587  * write_with_restart()
    588  *	Equivalent to write() but does retry on signals.
    589  *	This function is not needed on 4.2BSD and later.
    590  * Return:
    591  *	Number of bytes written.  -1 indicates an error.
    592  */
    593 
    594 #if __STDC__
    595 int
    596 write_with_restart(int fd, void *buf, int bsz)
    597 #else
    598 int
    599 write_with_restart(fd, buf, bsz)
    600 	int fd;
    601 	void *buf;
    602 	int bsz;
    603 #endif
    604 {
    605 	int r;
    606 
    607 	while (((r = write(fd, buf, bsz)) < 0) && errno == EINTR)
    608 		;
    609 
    610 	return(r);
    611 }
    612 #endif
    613 
    614 /*
    615  * xwrite()
    616  *	Equivalent to write() but does retry on partial write, which may occur
    617  *	on signals.
    618  * Return:
    619  *	Number of bytes written.  -1 indicates an error.
    620  */
    621 
    622 #if __STDC__
    623 int
    624 xwrite(int fd, void *buf, int bsz)
    625 #else
    626 int
    627 xwrite(fd, buf, bsz)
    628 	int fd;
    629 	void *buf;
    630 	int bsz;
    631 #endif
    632 {
    633 	char *b = buf;
    634 	int written = 0;
    635 	int r;
    636 
    637 	do {
    638 		if ((r = write_with_restart(fd, b, bsz)) <= 0)
    639 			break;
    640 		b += r;
    641 		bsz -= r;
    642 		written += r;
    643 	} while (bsz > 0);
    644 
    645 	return(written ? written : r);
    646 }
    647 
    648 /*
    649  * ar_read()
    650  *	read up to a specified number of bytes from the archive into the
    651  *	supplied buffer. When dealing with tapes we may not always be able to
    652  *	read what we want.
    653  * Return:
    654  *	Number of bytes in buffer. 0 for end of file, -1 for a read error.
    655  */
    656 
    657 #if __STDC__
    658 int
    659 ar_read(char *buf, int cnt)
    660 #else
    661 int
    662 ar_read(buf, cnt)
    663 	char *buf;
    664 	int cnt;
    665 #endif
    666 {
    667 	int res = 0;
    668 
    669 	/*
    670 	 * if last i/o was in error, no more reads until reset or new volume
    671 	 */
    672 	if (lstrval <= 0)
    673 		return(lstrval);
    674 
    675 	/*
    676 	 * how we read must be based on device type
    677 	 */
    678 	switch (artyp) {
    679 	case ISTAPE:
    680 		if ((res = read_with_restart(arfd, buf, cnt)) > 0) {
    681 			/*
    682 			 * CAUTION: tape systems may not always return the same
    683 			 * sized records so we leave blksz == MAXBLK. The
    684 			 * physical record size that a tape drive supports is
    685 			 * very hard to determine in a uniform and portable
    686 			 * manner.
    687 			 */
    688 			io_ok = 1;
    689 			if (res != rdblksz) {
    690 				/*
    691 				 * Record size changed. If this is happens on
    692 				 * any record after the first, we probably have
    693 				 * a tape drive which has a fixed record size
    694 				 * we are getting multiple records in a single
    695 				 * read). Watch out for record blocking that
    696 				 * violates pax spec (must be a multiple of
    697 				 * BLKMULT).
    698 				 */
    699 				rdblksz = res;
    700 				if (rdblksz % BLKMULT)
    701 					invld_rec = 1;
    702 			}
    703 			return(res);
    704 		}
    705 		break;
    706 	case ISREG:
    707 	case ISBLK:
    708 	case ISCHR:
    709 	case ISPIPE:
    710 	default:
    711 		/*
    712 		 * Files are so easy to deal with. These other things cannot
    713 		 * be trusted at all. So when we are dealing with character
    714 		 * devices and pipes we just take what they have ready for us
    715 		 * and return. Trying to do anything else with them runs the
    716 		 * risk of failure.
    717 		 */
    718 		if ((res = read_with_restart(arfd, buf, cnt)) > 0) {
    719 			io_ok = 1;
    720 			return(res);
    721 		}
    722 		break;
    723 	}
    724 
    725 	/*
    726 	 * We are in trouble at this point, something is broken...
    727 	 */
    728 	lstrval = res;
    729 	if (res < 0)
    730 		syswarn(1, errno, "Failed read on archive volume %d", arvol);
    731 	else if (!is_oldgnutar)
    732 		tty_warn(0, "End of archive volume %d reached", arvol);
    733 	return(res);
    734 }
    735 
    736 /*
    737  * ar_write()
    738  *	Write a specified number of bytes in supplied buffer to the archive
    739  *	device so it appears as a single "block". Deals with errors and tries
    740  *	to recover when faced with short writes.
    741  * Return:
    742  *	Number of bytes written. 0 indicates end of volume reached and with no
    743  *	flaws (as best that can be detected). A -1 indicates an unrecoverable
    744  *	error in the archive occured.
    745  */
    746 
    747 #if __STDC__
    748 int
    749 ar_write(char *buf, int bsz)
    750 #else
    751 int
    752 ar_write(buf, bsz)
    753 	char *buf;
    754 	int bsz;
    755 #endif
    756 {
    757 	int res;
    758 	off_t cpos;
    759 
    760 	/*
    761 	 * do not allow pax to create a "bad" archive. Once a write fails on
    762 	 * an archive volume prevent further writes to it.
    763 	 */
    764 	if (lstrval <= 0)
    765 		return(lstrval);
    766 
    767 	if ((res = xwrite(arfd, buf, bsz)) == bsz) {
    768 		wr_trail = 1;
    769 		io_ok = 1;
    770 		return(bsz);
    771 	}
    772 	/*
    773 	 * write broke, see what we can do with it. We try to send any partial
    774 	 * writes that may violate pax spec to the next archive volume.
    775 	 */
    776 	if (res < 0)
    777 		lstrval = res;
    778 	else
    779 		lstrval = 0;
    780 
    781 	switch (artyp) {
    782 	case ISREG:
    783 		if ((res > 0) && (res % BLKMULT)) {
    784 			/*
    785 			 * try to fix up partial writes which are not BLKMULT
    786 			 * in size by forcing the runt record to next archive
    787 			 * volume
    788 			 */
    789 			if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0)
    790 				break;
    791 			cpos -= (off_t)res;
    792 			if (ftruncate(arfd, cpos) < 0)
    793 				break;
    794 			res = lstrval = 0;
    795 			break;
    796 		}
    797 		if (res >= 0)
    798 			break;
    799 		/*
    800 		 * if file is out of space, handle it like a return of 0
    801 		 */
    802 		if ((errno == ENOSPC) || (errno == EFBIG) || (errno == EDQUOT))
    803 			res = lstrval = 0;
    804 		break;
    805 	case ISTAPE:
    806 	case ISCHR:
    807 	case ISBLK:
    808 		if (res >= 0)
    809 			break;
    810 		if (errno == EACCES) {
    811 			tty_warn(0,
    812 			    "Write failed, archive is write protected.");
    813 			res = lstrval = 0;
    814 			return(0);
    815 		}
    816 		/*
    817 		 * see if we reached the end of media, if so force a change to
    818 		 * the next volume
    819 		 */
    820 		if ((errno == ENOSPC) || (errno == EIO) || (errno == ENXIO))
    821 			res = lstrval = 0;
    822 		break;
    823 	case ISPIPE:
    824 	default:
    825 		/*
    826 		 * we cannot fix errors to these devices
    827 		 */
    828 		break;
    829 	}
    830 
    831 	/*
    832 	 * Better tell the user the bad news...
    833 	 * if this is a block aligned archive format, we may have a bad archive
    834 	 * if the format wants the header to start at a BLKMULT boundary. While
    835 	 * we can deal with the mis-aligned data, it violates spec and other
    836 	 * archive readers will likely fail. if the format is not block
    837 	 * aligned, the user may be lucky (and the archive is ok).
    838 	 */
    839 	if (res >= 0) {
    840 		if (res > 0)
    841 			wr_trail = 1;
    842 		io_ok = 1;
    843 	}
    844 
    845 	/*
    846 	 * If we were trying to rewrite the trailer and it didn't work, we
    847 	 * must quit right away.
    848 	 */
    849 	if (!wr_trail && (res <= 0)) {
    850 		tty_warn(1,
    851 		    "Unable to append, trailer re-write failed. Quitting.");
    852 		return(res);
    853 	}
    854 
    855 	if (res == 0)
    856 		tty_warn(0, "End of archive volume %d reached", arvol);
    857 	else if (res < 0)
    858 		syswarn(1, errno, "Failed write to archive volume: %d", arvol);
    859 	else if (!frmt->blkalgn || ((res % frmt->blkalgn) == 0))
    860 		tty_warn(0,
    861 		    "WARNING: partial archive write. Archive MAY BE FLAWED");
    862 	else
    863 		tty_warn(1,"WARNING: partial archive write. Archive IS FLAWED");
    864 	return(res);
    865 }
    866 
    867 /*
    868  * ar_rdsync()
    869  *	Try to move past a bad spot on a flawed archive as needed to continue
    870  *	I/O. Clears error flags to allow I/O to continue.
    871  * Return:
    872  *	0 when ok to try i/o again, -1 otherwise.
    873  */
    874 
    875 #if __STDC__
    876 int
    877 ar_rdsync(void)
    878 #else
    879 int
    880 ar_rdsync()
    881 #endif
    882 {
    883 	long fsbz;
    884 	off_t cpos;
    885 	off_t mpos;
    886 	struct mtop mb;
    887 
    888 	/*
    889 	 * Fail resync attempts at user request (done) or this is going to be
    890 	 * an update/append to a existing archive. if last i/o hit media end,
    891 	 * we need to go to the next volume not try a resync
    892 	 */
    893 	if ((done > 0) || (lstrval == 0))
    894 		return(-1);
    895 
    896 	if ((act == APPND) || (act == ARCHIVE)) {
    897 		tty_warn(1, "Cannot allow updates to an archive with flaws.");
    898 		return(-1);
    899 	}
    900 	if (io_ok)
    901 		did_io = 1;
    902 
    903 	switch(artyp) {
    904 	case ISTAPE:
    905 		/*
    906 		 * if the last i/o was a successful data transfer, we assume
    907 		 * the fault is just a bad record on the tape that we are now
    908 		 * past. If we did not get any data since the last resync try
    909 		 * to move the tape forward one PHYSICAL record past any
    910 		 * damaged tape section. Some tape drives are stubborn and need
    911 		 * to be pushed.
    912 		 */
    913 		if (io_ok) {
    914 			io_ok = 0;
    915 			lstrval = 1;
    916 			break;
    917 		}
    918 		mb.mt_op = MTFSR;
    919 		mb.mt_count = 1;
    920 		if (ioctl(arfd, MTIOCTOP, &mb) < 0)
    921 			break;
    922 		lstrval = 1;
    923 		break;
    924 	case ISREG:
    925 	case ISCHR:
    926 	case ISBLK:
    927 		/*
    928 		 * try to step over the bad part of the device.
    929 		 */
    930 		io_ok = 0;
    931 		if (((fsbz = arsb.st_blksize) <= 0) || (artyp != ISREG))
    932 			fsbz = BLKMULT;
    933 		if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0)
    934 			break;
    935 		mpos = fsbz - (cpos % (off_t)fsbz);
    936 		if (lseek(arfd, mpos, SEEK_CUR) < 0)
    937 			break;
    938 		lstrval = 1;
    939 		break;
    940 	case ISPIPE:
    941 	default:
    942 		/*
    943 		 * cannot recover on these archive device types
    944 		 */
    945 		io_ok = 0;
    946 		break;
    947 	}
    948 	if (lstrval <= 0) {
    949 		tty_warn(1, "Unable to recover from an archive read failure.");
    950 		return(-1);
    951 	}
    952 	tty_warn(0, "Attempting to recover from an archive read failure.");
    953 	return(0);
    954 }
    955 
    956 /*
    957  * ar_fow()
    958  *	Move the I/O position within the archive forward the specified number of
    959  *	bytes as supported by the device. If we cannot move the requested
    960  *	number of bytes, return the actual number of bytes moved in skipped.
    961  * Return:
    962  *	0 if moved the requested distance, -1 on complete failure, 1 on
    963  *	partial move (the amount moved is in skipped)
    964  */
    965 
    966 #if __STDC__
    967 int
    968 ar_fow(off_t sksz, off_t *skipped)
    969 #else
    970 int
    971 ar_fow(sksz, skipped)
    972 	off_t sksz;
    973 	off_t *skipped;
    974 #endif
    975 {
    976 	off_t cpos;
    977 	off_t mpos;
    978 
    979 	*skipped = 0;
    980 	if (sksz <= 0)
    981 		return(0);
    982 
    983 	/*
    984 	 * we cannot move forward at EOF or error
    985 	 */
    986 	if (lstrval <= 0)
    987 		return(lstrval);
    988 
    989 	/*
    990 	 * Safer to read forward on devices where it is hard to find the end of
    991 	 * the media without reading to it. With tapes we cannot be sure of the
    992 	 * number of physical blocks to skip (we do not know physical block
    993 	 * size at this point), so we must only read forward on tapes!
    994 	 */
    995 	if (artyp == ISTAPE || artyp == ISPIPE)
    996 		return(0);
    997 
    998 	/*
    999 	 * figure out where we are in the archive
   1000 	 */
   1001 	if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) >= 0) {
   1002 		/*
   1003 		 * we can be asked to move farther than there are bytes in this
   1004 		 * volume, if so, just go to file end and let normal buf_fill()
   1005 		 * deal with the end of file (it will go to next volume by
   1006 		 * itself)
   1007 		 */
   1008 		mpos = cpos + sksz;
   1009 		if (artyp == ISREG && mpos > arsb.st_size)
   1010 			mpos = arsb.st_size;
   1011 		if ((mpos = lseek(arfd, mpos, SEEK_SET)) >= 0) {
   1012 			*skipped = mpos - cpos;
   1013 			return(0);
   1014 		}
   1015 	} else {
   1016 		if (artyp != ISREG)
   1017 			return(0);		/* non-seekable device */
   1018 	}
   1019 	syswarn(1, errno, "Forward positioning operation on archive failed");
   1020 	lstrval = -1;
   1021 	return(-1);
   1022 }
   1023 
   1024 /*
   1025  * ar_rev()
   1026  *	move the i/o position within the archive backwards the specified byte
   1027  *	count as supported by the device. With tapes drives we RESET rdblksz to
   1028  *	the PHYSICAL blocksize.
   1029  *	NOTE: We should only be called to move backwards so we can rewrite the
   1030  *	last records (the trailer) of an archive (APPEND).
   1031  * Return:
   1032  *	0 if moved the requested distance, -1 on complete failure
   1033  */
   1034 
   1035 #if __STDC__
   1036 int
   1037 ar_rev(off_t sksz)
   1038 #else
   1039 int
   1040 ar_rev(sksz)
   1041 	off_t sksz;
   1042 #endif
   1043 {
   1044 	off_t cpos;
   1045 	struct mtop mb;
   1046 	int phyblk;
   1047 
   1048 	/*
   1049 	 * make sure we do not have try to reverse on a flawed archive
   1050 	 */
   1051 	if (lstrval < 0)
   1052 		return(lstrval);
   1053 
   1054 	switch(artyp) {
   1055 	case ISPIPE:
   1056 		if (sksz <= 0)
   1057 			break;
   1058 		/*
   1059 		 * cannot go backwards on these critters
   1060 		 */
   1061 		tty_warn(1, "Reverse positioning on pipes is not supported.");
   1062 		lstrval = -1;
   1063 		return(-1);
   1064 	case ISREG:
   1065 	case ISBLK:
   1066 	case ISCHR:
   1067 	default:
   1068 		if (sksz <= 0)
   1069 			break;
   1070 
   1071 		/*
   1072 		 * For things other than files, backwards movement has a very
   1073 		 * high probability of failure as we really do not know the
   1074 		 * true attributes of the device we are talking to (the device
   1075 		 * may not even have the ability to lseek() in any direction).
   1076 		 * First we figure out where we are in the archive.
   1077 		 */
   1078 		if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) {
   1079 			syswarn(1, errno,
   1080 			   "Unable to obtain current archive byte offset");
   1081 			lstrval = -1;
   1082 			return(-1);
   1083 		}
   1084 
   1085 		/*
   1086 		 * we may try to go backwards past the start when the archive
   1087 		 * is only a single record. If this hapens and we are on a
   1088 		 * multi volume archive, we need to go to the end of the
   1089 		 * previous volume and continue our movement backwards from
   1090 		 * there.
   1091 		 */
   1092 		if ((cpos -= sksz) < (off_t)0L) {
   1093 			if (arvol > 1) {
   1094 				/*
   1095 				 * this should never happen
   1096 				 */
   1097 				tty_warn(1,
   1098 				    "Reverse position on previous volume.");
   1099 				lstrval = -1;
   1100 				return(-1);
   1101 			}
   1102 			cpos = (off_t)0L;
   1103 		}
   1104 		if (lseek(arfd, cpos, SEEK_SET) < 0) {
   1105 			syswarn(1, errno, "Unable to seek archive backwards");
   1106 			lstrval = -1;
   1107 			return(-1);
   1108 		}
   1109 		break;
   1110 	case ISTAPE:
   1111 		/*
   1112 		 * Calculate and move the proper number of PHYSICAL tape
   1113 		 * blocks. If the sksz is not an even multiple of the physical
   1114 		 * tape size, we cannot do the move (this should never happen).
   1115 		 * (We also cannot handler trailers spread over two vols).
   1116 		 * get_phys() also makes sure we are in front of the filemark.
   1117 		 */
   1118 		if ((phyblk = get_phys()) <= 0) {
   1119 			lstrval = -1;
   1120 			return(-1);
   1121 		}
   1122 
   1123 		/*
   1124 		 * make sure future tape reads only go by physical tape block
   1125 		 * size (set rdblksz to the real size).
   1126 		 */
   1127 		rdblksz = phyblk;
   1128 
   1129 		/*
   1130 		 * if no movement is required, just return (we must be after
   1131 		 * get_phys() so the physical blocksize is properly set)
   1132 		 */
   1133 		if (sksz <= 0)
   1134 			break;
   1135 
   1136 		/*
   1137 		 * ok we have to move. Make sure the tape drive can do it.
   1138 		 */
   1139 		if (sksz % phyblk) {
   1140 			tty_warn(1,
   1141 			    "Tape drive unable to backspace requested amount");
   1142 			lstrval = -1;
   1143 			return(-1);
   1144 		}
   1145 
   1146 		/*
   1147 		 * move backwards the requested number of bytes
   1148 		 */
   1149 		mb.mt_op = MTBSR;
   1150 		mb.mt_count = sksz/phyblk;
   1151 		if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
   1152 			syswarn(1,errno, "Unable to backspace tape %ld blocks.",
   1153 			    (long) mb.mt_count);
   1154 			lstrval = -1;
   1155 			return(-1);
   1156 		}
   1157 		break;
   1158 	}
   1159 	lstrval = 1;
   1160 	return(0);
   1161 }
   1162 
   1163 /*
   1164  * get_phys()
   1165  *	Determine the physical block size on a tape drive. We need the physical
   1166  *	block size so we know how many bytes we skip over when we move with
   1167  *	mtio commands. We also make sure we are BEFORE THE TAPE FILEMARK when
   1168  *	return.
   1169  *	This is one really SLOW routine...
   1170  * Return:
   1171  *	physical block size if ok (ok > 0), -1 otherwise
   1172  */
   1173 
   1174 #if __STDC__
   1175 static int
   1176 get_phys(void)
   1177 #else
   1178 static int
   1179 get_phys()
   1180 #endif
   1181 {
   1182 	int padsz = 0;
   1183 	int res;
   1184 	int phyblk;
   1185 	struct mtop mb;
   1186 	char scbuf[MAXBLK];
   1187 
   1188 	/*
   1189 	 * move to the file mark, and then back up one record and read it.
   1190 	 * this should tell us the physical record size the tape is using.
   1191 	 */
   1192 	if (lstrval == 1) {
   1193 		/*
   1194 		 * we know we are at file mark when we get back a 0 from
   1195 		 * read()
   1196 		 */
   1197 		while ((res = read_with_restart(arfd, scbuf, sizeof(scbuf))) > 0)
   1198 			padsz += res;
   1199 		if (res < 0) {
   1200 			syswarn(1, errno, "Unable to locate tape filemark.");
   1201 			return(-1);
   1202 		}
   1203 	}
   1204 
   1205 	/*
   1206 	 * move backwards over the file mark so we are at the end of the
   1207 	 * last record.
   1208 	 */
   1209 	mb.mt_op = MTBSF;
   1210 	mb.mt_count = 1;
   1211 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
   1212 		syswarn(1, errno, "Unable to backspace over tape filemark.");
   1213 		return(-1);
   1214 	}
   1215 
   1216 	/*
   1217 	 * move backwards so we are in front of the last record and read it to
   1218 	 * get physical tape blocksize.
   1219 	 */
   1220 	mb.mt_op = MTBSR;
   1221 	mb.mt_count = 1;
   1222 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
   1223 		syswarn(1, errno, "Unable to backspace over last tape block.");
   1224 		return(-1);
   1225 	}
   1226 	if ((phyblk = read_with_restart(arfd, scbuf, sizeof(scbuf))) <= 0) {
   1227 		syswarn(1, errno, "Cannot determine archive tape blocksize.");
   1228 		return(-1);
   1229 	}
   1230 
   1231 	/*
   1232 	 * read forward to the file mark, then back up in front of the filemark
   1233 	 * (this is a bit paranoid, but should be safe to do).
   1234 	 */
   1235 	while ((res = read_with_restart(arfd, scbuf, sizeof(scbuf))) > 0)
   1236 		;
   1237 	if (res < 0) {
   1238 		syswarn(1, errno, "Unable to locate tape filemark.");
   1239 		return(-1);
   1240 	}
   1241 	mb.mt_op = MTBSF;
   1242 	mb.mt_count = 1;
   1243 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
   1244 		syswarn(1, errno, "Unable to backspace over tape filemark.");
   1245 		return(-1);
   1246 	}
   1247 
   1248 	/*
   1249 	 * set lstrval so we know that the filemark has not been seen
   1250 	 */
   1251 	lstrval = 1;
   1252 
   1253 	/*
   1254 	 * return if there was no padding
   1255 	 */
   1256 	if (padsz == 0)
   1257 		return(phyblk);
   1258 
   1259 	/*
   1260 	 * make sure we can move backwards over the padding. (this should
   1261 	 * never fail).
   1262 	 */
   1263 	if (padsz % phyblk) {
   1264 		tty_warn(1, "Tape drive unable to backspace requested amount");
   1265 		return(-1);
   1266 	}
   1267 
   1268 	/*
   1269 	 * move backwards over the padding so the head is where it was when
   1270 	 * we were first called (if required).
   1271 	 */
   1272 	mb.mt_op = MTBSR;
   1273 	mb.mt_count = padsz/phyblk;
   1274 	if (ioctl(arfd, MTIOCTOP, &mb) < 0) {
   1275 		syswarn(1,errno,"Unable to backspace tape over %ld pad blocks",
   1276 		    (long)mb.mt_count);
   1277 		return(-1);
   1278 	}
   1279 	return(phyblk);
   1280 }
   1281 
   1282 /*
   1283  * ar_next()
   1284  *	prompts the user for the next volume in this archive. For some devices
   1285  *	we may allow the media to be changed. Otherwise a new archive is
   1286  *	prompted for. By pax spec, if there is no controlling tty or an eof is
   1287  *	read on tty input, we must quit pax.
   1288  * Return:
   1289  *	0 when ready to continue, -1 when all done
   1290  */
   1291 
   1292 #if __STDC__
   1293 int
   1294 ar_next(void)
   1295 #else
   1296 int
   1297 ar_next()
   1298 #endif
   1299 {
   1300 	char buf[PAXPATHLEN+2];
   1301 	static int freeit = 0;
   1302 	sigset_t o_mask;
   1303 
   1304 	/*
   1305 	 * WE MUST CLOSE THE DEVICE. A lot of devices must see last close, (so
   1306 	 * things like writing EOF etc will be done) (Watch out ar_close() can
   1307 	 * also be called via a signal handler, so we must prevent a race.
   1308 	 */
   1309 	if (sigprocmask(SIG_BLOCK, &s_mask, &o_mask) < 0)
   1310 		syswarn(0, errno, "Unable to set signal mask");
   1311 	ar_close();
   1312 	if (sigprocmask(SIG_SETMASK, &o_mask, (sigset_t *)NULL) < 0)
   1313 		syswarn(0, errno, "Unable to restore signal mask");
   1314 
   1315 	if (done || !wr_trail || is_oldgnutar || force_one_volume)
   1316 		return(-1);
   1317 
   1318 	tty_prnt("\nATTENTION! %s archive volume change required.\n", argv0);
   1319 
   1320 	/*
   1321 	 * if i/o is on stdin or stdout, we cannot reopen it (we do not know
   1322 	 * the name), the user will be forced to type it in.
   1323 	 */
   1324 	if (strcmp(arcname, STDO) && strcmp(arcname, STDN) && (artyp != ISREG)
   1325 	    && (artyp != ISPIPE)) {
   1326 		if (artyp == ISTAPE) {
   1327 			tty_prnt("%s ready for archive tape volume: %d\n",
   1328 				arcname, arvol);
   1329 			tty_prnt("Load the NEXT TAPE on the tape drive");
   1330 		} else {
   1331 			tty_prnt("%s ready for archive volume: %d\n",
   1332 				arcname, arvol);
   1333 			tty_prnt("Load the NEXT STORAGE MEDIA (if required)");
   1334 		}
   1335 
   1336 		if ((act == ARCHIVE) || (act == APPND))
   1337 			tty_prnt(" and make sure it is WRITE ENABLED.\n");
   1338 		else
   1339 			tty_prnt("\n");
   1340 
   1341 		for(;;) {
   1342 			tty_prnt("Type \"y\" to continue, \".\" to quit %s,",
   1343 				argv0);
   1344 			tty_prnt(" or \"s\" to switch to new device.\nIf you");
   1345 			tty_prnt(" cannot change storage media, type \"s\"\n");
   1346 			tty_prnt("Is the device ready and online? > ");
   1347 
   1348 			if ((tty_read(buf,sizeof(buf))<0) || !strcmp(buf,".")){
   1349 				done = 1;
   1350 				lstrval = -1;
   1351 				tty_prnt("Quitting %s!\n", argv0);
   1352 				vfpart = 0;
   1353 				return(-1);
   1354 			}
   1355 
   1356 			if ((buf[0] == '\0') || (buf[1] != '\0')) {
   1357 				tty_prnt("%s unknown command, try again\n",buf);
   1358 				continue;
   1359 			}
   1360 
   1361 			switch (buf[0]) {
   1362 			case 'y':
   1363 			case 'Y':
   1364 				/*
   1365 				 * we are to continue with the same device
   1366 				 */
   1367 				if (ar_open(arcname) >= 0)
   1368 					return(0);
   1369 				tty_prnt("Cannot re-open %s, try again\n",
   1370 					arcname);
   1371 				continue;
   1372 			case 's':
   1373 			case 'S':
   1374 				/*
   1375 				 * user wants to open a different device
   1376 				 */
   1377 				tty_prnt("Switching to a different archive\n");
   1378 				break;
   1379 			default:
   1380 				tty_prnt("%s unknown command, try again\n",buf);
   1381 				continue;
   1382 			}
   1383 			break;
   1384 		}
   1385 	} else
   1386 		tty_prnt("Ready for archive volume: %d\n", arvol);
   1387 
   1388 	/*
   1389 	 * have to go to a different archive
   1390 	 */
   1391 	for (;;) {
   1392 		tty_prnt("Input archive name or \".\" to quit %s.\n", argv0);
   1393 		tty_prnt("Archive name > ");
   1394 
   1395 		if ((tty_read(buf, sizeof(buf)) < 0) || !strcmp(buf, ".")) {
   1396 			done = 1;
   1397 			lstrval = -1;
   1398 			tty_prnt("Quitting %s!\n", argv0);
   1399 			vfpart = 0;
   1400 			return(-1);
   1401 		}
   1402 		if (buf[0] == '\0') {
   1403 			tty_prnt("Empty file name, try again\n");
   1404 			continue;
   1405 		}
   1406 		if (!strcmp(buf, "..")) {
   1407 			tty_prnt("Illegal file name: .. try again\n");
   1408 			continue;
   1409 		}
   1410 		if (strlen(buf) > PAXPATHLEN) {
   1411 			tty_prnt("File name too long, try again\n");
   1412 			continue;
   1413 		}
   1414 
   1415 		/*
   1416 		 * try to open new archive
   1417 		 */
   1418 		if (ar_open(buf) >= 0) {
   1419 			if (freeit) {
   1420 				(void)free((char *)arcname);
   1421 				freeit = 0;
   1422 			}
   1423 			if ((arcname = strdup(buf)) == NULL) {
   1424 				done = 1;
   1425 				lstrval = -1;
   1426 				tty_warn(0, "Cannot save archive name.");
   1427 				return(-1);
   1428 			}
   1429 			freeit = 1;
   1430 			break;
   1431 		}
   1432 		tty_prnt("Cannot open %s, try again\n", buf);
   1433 		continue;
   1434 	}
   1435 	return(0);
   1436 }
   1437 
   1438 /*
   1439  * ar_start_gzip()
   1440  * starts the gzip compression/decompression process as a child, using magic
   1441  * to keep the fd the same in the calling function (parent).
   1442  */
   1443 void
   1444 #ifdef __STDC__
   1445 ar_start_gzip(int fd)
   1446 #else
   1447 ar_start_gzip(fd)
   1448 	int fd;
   1449 #endif
   1450 {
   1451 	pid_t pid;
   1452 	int fds[2];
   1453 	char *gzip_flags;
   1454 
   1455 	if (pipe(fds) < 0)
   1456 		err(1, "could not pipe");
   1457 	pid = fork();
   1458 	if (pid < 0)
   1459 		err(1, "could not fork");
   1460 
   1461 	/* parent */
   1462 	if (pid) {
   1463 		switch (act) {
   1464 		case ARCHIVE:
   1465 			dup2(fds[1], fd);
   1466 			break;
   1467 		case LIST:
   1468 		case EXTRACT:
   1469 			dup2(fds[0], fd);
   1470 			break;
   1471 		default:
   1472 			errx(1, "ar_start_gzip:  impossible");
   1473 		}
   1474 		close(fds[0]);
   1475 		close(fds[1]);
   1476 	} else {
   1477 		switch (act) {
   1478 		case ARCHIVE:
   1479 			dup2(fds[0], STDIN_FILENO);
   1480 			dup2(fd, STDOUT_FILENO);
   1481 			gzip_flags = "-c";
   1482 			break;
   1483 		case LIST:
   1484 		case EXTRACT:
   1485 			dup2(fds[1], STDOUT_FILENO);
   1486 			dup2(fd, STDIN_FILENO);
   1487 			gzip_flags = "-dc";
   1488 			break;
   1489 		default:
   1490 			errx(1, "ar_start_gzip:  impossible");
   1491 		}
   1492 		close(fds[0]);
   1493 		close(fds[1]);
   1494 		if (execlp(gzip_program, gzip_program, gzip_flags, NULL) < 0)
   1495 			err(1, "could not exec");
   1496 		/* NOTREACHED */
   1497 	}
   1498 }
   1499 
   1500 static const char *
   1501 timefmt(buf, size, sz, tm)
   1502 	char *buf;
   1503 	size_t size;
   1504 	off_t sz;
   1505 	time_t tm;
   1506 {
   1507 #ifdef NET2_STAT
   1508 	(void)snprintf(buf, size, "%lu secs (%lu bytes/sec)",
   1509 	    (unsigned long)tm, (unsigned long)(sz / tm));
   1510 #else
   1511 	(void)snprintf(buf, size, "%lu secs (%llu bytes/sec)",
   1512 	    (unsigned long)tm, (unsigned long long)(sz / tm));
   1513 #endif
   1514 	return buf;
   1515 }
   1516 
   1517 static const char *
   1518 sizefmt(buf, size, sz)
   1519 	char *buf;
   1520 	size_t size;
   1521 	off_t sz;
   1522 {
   1523 #ifdef NET2_STAT
   1524 	(void)snprintf(buf, size, "%lu bytes", (unsigned long)sz);
   1525 #else
   1526 	(void)snprintf(buf, size, "%llu bytes", (unsigned long long)sz);
   1527 #endif
   1528 	return buf;
   1529 }
   1530 
   1531 #if __STDC__
   1532 void
   1533 ar_summary(int n)
   1534 #else
   1535 void
   1536 ar_summary(n)
   1537 	int n;
   1538 #endif
   1539 {
   1540 	time_t secs;
   1541 	int len;
   1542 	char buf[MAXPATHLEN];
   1543 	char tbuf[MAXPATHLEN/4];
   1544 	char s1buf[MAXPATHLEN/8];
   1545 	char s2buf[MAXPATHLEN/8];
   1546 	FILE *outf;
   1547 
   1548 	if (act == LIST)
   1549 		outf = stdout;
   1550 	else
   1551 		outf = stderr;
   1552 
   1553 	/*
   1554 	 * If we are called from a signal (n != 0), use snprintf(3) so that we
   1555 	 * don't reenter stdio(3).
   1556 	 */
   1557 	(void)time(&secs);
   1558 	if ((secs -= starttime) == 0)
   1559 		secs = 1;
   1560 
   1561 	/*
   1562 	 * If we have not determined the format yet, we just say how many bytes
   1563 	 * we have skipped over looking for a header to id. there is no way we
   1564 	 * could have written anything yet.
   1565 	 */
   1566 	if (frmt == NULL) {
   1567 		len = snprintf(buf, sizeof(buf),
   1568 		    "unknown format, %s skipped in %s\n",
   1569 		    sizefmt(s1buf, sizeof(s1buf), rdcnt),
   1570 		    timefmt(tbuf, sizeof(tbuf), rdcnt, secs));
   1571 		if (n == 0)
   1572 			(void)fprintf(outf, "%s: %s", argv0, buf);
   1573 		else
   1574 			(void)write(STDERR_FILENO, buf, len);
   1575 		return;
   1576 	}
   1577 
   1578 
   1579 	if (n != 0) {
   1580 		len = snprintf(buf, sizeof(buf), "Working on `%s' (%s)\n",
   1581 		    archd.name, sizefmt(s1buf, sizeof(s1buf), archd.sb.st_size));
   1582 		(void)write(STDERR_FILENO, buf, len);
   1583 	}
   1584 
   1585 
   1586 	len = snprintf(buf, sizeof(buf),
   1587 	    "%s vol %d, %lu files, %s read, %s written in %s\n",
   1588 	    frmt->name, arvol-1, (unsigned long)flcnt,
   1589 	    sizefmt(s1buf, sizeof(s1buf), rdcnt),
   1590 	    sizefmt(s2buf, sizeof(s2buf), wrcnt),
   1591 	    timefmt(tbuf, sizeof(tbuf), rdcnt + wrcnt, secs));
   1592 	if (n == 0)
   1593 		(void)fprintf(outf, "%s: %s", argv0, buf);
   1594 	else
   1595 		(void)write(STDERR_FILENO, buf, strlen(buf));
   1596 }
   1597 
   1598 /*
   1599  * ar_dochdir(name)
   1600  *	change directory to name, and remember where we came from and
   1601  *	where we change to (for ar_open).
   1602  *
   1603  *	Maybe we could try to be smart and only do the actual chdir
   1604  *	when necessary to write a file read from the archive, but this
   1605  *	is not easy to get right given the pax code structure.
   1606  *
   1607  *	Be sure to not leak descriptors!
   1608  *
   1609  *	We are called N * M times when extracting, and N times when
   1610  *	writing archives, where
   1611  *	N:	number of -C options
   1612  *	M:	number of files in archive
   1613  *
   1614  * Returns 0 if all went well, else -1.
   1615  */
   1616 
   1617 #ifdef __STDC__
   1618 int
   1619 ar_dochdir(char *name)
   1620 #else
   1621 int
   1622 ar_dochdir(name)
   1623 	char *name;
   1624 #endif
   1625 {
   1626 	if (curdirfd == -1) {
   1627 		/* first time. remember where we came from */
   1628 		curdirfd = open(".", O_RDONLY);
   1629 		if (curdirfd < 0) {
   1630 			syswarn(0, errno, "failed to open directory .");
   1631 			return (-1);
   1632 		}
   1633 	} else /* XXX if (*name != '/') XXX */ {
   1634 		/*
   1635 		 * relative chdir. Make sure to get the same directory
   1636 		 * each time by fchdir-ing back first.
   1637 		 */
   1638 		fchdir(curdirfd);
   1639 	}
   1640 
   1641 	if (minusCfd != -1) {
   1642 		/* don't leak descriptors */
   1643 		close(minusCfd);
   1644 		minusCfd = -1;
   1645 	}
   1646 
   1647 	minusCfd = open(name, O_RDONLY);
   1648 	if (minusCfd < 0) {
   1649 		syswarn(0, errno, "failed to open directory %s", name);
   1650 		return (-1);
   1651 	}
   1652 
   1653 	fchdir(minusCfd);
   1654 	return (0);
   1655 }
   1656