17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5d1a180bmaheshvs * Common Development and Distribution License (the "License").
6d1a180bmaheshvs * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
226451fdbvsakar * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
278509e9cToomas Soome/*	  All Rights Reserved	*/
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate/*
307c478bdstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bdstevel@tonic-gate * The Regents of the University of California
327c478bdstevel@tonic-gate * All Rights Reserved
337c478bdstevel@tonic-gate *
347c478bdstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
357c478bdstevel@tonic-gate * software developed by the University of California, Berkeley, and its
367c478bdstevel@tonic-gate * contributors.
377c478bdstevel@tonic-gate */
387c478bdstevel@tonic-gate
397c478bdstevel@tonic-gate#include <sys/param.h>
407c478bdstevel@tonic-gate#include <signal.h>
417c478bdstevel@tonic-gate#include <sys/types.h>
427c478bdstevel@tonic-gate#include <sys/sysmacros.h>
437c478bdstevel@tonic-gate#include <sys/vnode.h>
447c478bdstevel@tonic-gate#include <sys/fs/ufs_fsdir.h>
457c478bdstevel@tonic-gate#include <sys/fs/ufs_inode.h>
467c478bdstevel@tonic-gate#include <sys/fs/ufs_fs.h>
477c478bdstevel@tonic-gate#include <sys/stat.h>
487c478bdstevel@tonic-gate#include <sys/statvfs.h>
497c478bdstevel@tonic-gate#include <fcntl.h>
507c478bdstevel@tonic-gate#include <stdio.h>
517c478bdstevel@tonic-gate#include <unistd.h>
527c478bdstevel@tonic-gate#include <stdlib.h>
537c478bdstevel@tonic-gate#include <errno.h>
547c478bdstevel@tonic-gate#include <signal.h>
557c478bdstevel@tonic-gate#include <stdarg.h>
567c478bdstevel@tonic-gate#include <sys/errno.h>
577c478bdstevel@tonic-gate#include <sys/utsname.h>
587c478bdstevel@tonic-gate#include <sys/ipc.h>
597c478bdstevel@tonic-gate#include <sys/sem.h>
607c478bdstevel@tonic-gate#include <sys/shm.h>
617c478bdstevel@tonic-gate#include <archives.h>
627c478bdstevel@tonic-gate#include "volcopy.h"
637c478bdstevel@tonic-gate
647c478bdstevel@tonic-gate#include <locale.h>
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gate/*
677c478bdstevel@tonic-gate * main I/O information structure, contains information for the
687c478bdstevel@tonic-gate * source and destination files.
697c478bdstevel@tonic-gate */
707c478bdstevel@tonic-gate
717c478bdstevel@tonic-gatestruct file_info {
727c478bdstevel@tonic-gate	char	*f_dev_p,	/* name of device */
737c478bdstevel@tonic-gate		*f_vol_p;	/* volume name */
747c478bdstevel@tonic-gate	int	f_bsize,	/* size to buffer I/O to */
757c478bdstevel@tonic-gate		f_des,		/* file descriptor */
767c478bdstevel@tonic-gate		f_dev;		/* device type (generic I/O library) */
777c478bdstevel@tonic-gate} In, Out;
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate
807c478bdstevel@tonic-gateint	Sem_id[BUFCNT],	/* semaphore ids for controlling shared memory */
817c478bdstevel@tonic-gate	Shm_id[BUFCNT],	/* shared memory identifier */
827c478bdstevel@tonic-gate	*Cnts[BUFCNT];	/* an array of byte counts for shared memory */
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gatechar	Empty[BLKSIZ],	/* empty memory used to clear sections of memory */
857c478bdstevel@tonic-gate	*Buf[BUFCNT];	/* buffer pointers (possibly to shared memory) */
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gatestruct sembuf	Sem_buf,	/* semaphore operation buffer */
887c478bdstevel@tonic-gate		Rstsem_buf;	/* semaphore reset operation buffer */
897c478bdstevel@tonic-gate
907c478bdstevel@tonic-gatetypedef union {
917c478bdstevel@tonic-gate	char		dummy[SBSIZE];
927c478bdstevel@tonic-gate	struct fs	sblk;
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gate} sb_un;
957c478bdstevel@tonic-gate
967c478bdstevel@tonic-gatesb_un	isup, osup, tsup;
977c478bdstevel@tonic-gate
987c478bdstevel@tonic-gate#define	Isup isup.sblk
997c478bdstevel@tonic-gate#define	Osup osup.sblk
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gatestruct fs *Sptr = (struct fs *)&tsup.sblk;	/* super-block pointer */
1027c478bdstevel@tonic-gate
1037c478bdstevel@tonic-gatechar    *Ifname, *Ifpack, *Ofname, *Ofpack;
1047c478bdstevel@tonic-gatestruct volcopy_label	V_labl;
1057c478bdstevel@tonic-gate
1067c478bdstevel@tonic-gatechar	From_vol[VVOLLEN + 1],
1077c478bdstevel@tonic-gate	To_vol[VVOLLEN + 1],
1087c478bdstevel@tonic-gate	*Fsys_p;
1097c478bdstevel@tonic-gate
1107c478bdstevel@tonic-gatestatic	char	*nolabel = "";  /* used when there is no room for label */
1117c478bdstevel@tonic-gate
1127c478bdstevel@tonic-gateint	Blk_cnt = 1,	/* Do I/O in (Blk_cnt * BLKSIZ) byte blocks */
1137c478bdstevel@tonic-gate	Blocks = 0,	/* Number of blocks transferred */
1147c478bdstevel@tonic-gate	Bpi = 0,
1157c478bdstevel@tonic-gate	Bufcnt,
1167c478bdstevel@tonic-gate	Bufflg = 0,
1177c478bdstevel@tonic-gate	Bufsz = BLKSIZ,
1187c478bdstevel@tonic-gate	Disk_cnt = 1,	/* Disk I/O (Disk_cnt * Blk_cnt * BLKSIZ) byte blocks */
1197c478bdstevel@tonic-gate	Drive_typ = 0,	/* Flag for special tape drive types */
1207c478bdstevel@tonic-gate	Eomflg = 0,
1217c478bdstevel@tonic-gate	Ipc = 0,
1227c478bdstevel@tonic-gate	Itape,
1237c478bdstevel@tonic-gate	M3b15 = 0,	/* Machine types, set to 1 for the machine */
1247c478bdstevel@tonic-gate	M3b2 = 0,	/* the command is executing on */
1257c478bdstevel@tonic-gate	Otape,
1267c478bdstevel@tonic-gate	Pid = -1,
1277c478bdstevel@tonic-gate	R_blks = 0,	/* Number of blocks per tape reel */
1287c478bdstevel@tonic-gate	R_cur = 1,	/* Current tape reel being processed */
1297c478bdstevel@tonic-gate	R_len = 0,	/* Length in feet of tape reels */
1307c478bdstevel@tonic-gate	R_num = 0,	/* Number of tape reels to be processed */
1317c478bdstevel@tonic-gate	Shell_esc = 1,	/* Allow shell after delete -nosh (3b15) can disable */
1327c478bdstevel@tonic-gate	Yesflg = 0;
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gatevoid (*singal())();
1357c478bdstevel@tonic-gatelong	Fs,
1367c478bdstevel@tonic-gate	Fstype;
1377c478bdstevel@tonic-gate
1387c478bdstevel@tonic-gatetime_t	Tvec;
1397c478bdstevel@tonic-gate
1407c478bdstevel@tonic-gateFILE	*Devtty;
1417c478bdstevel@tonic-gate
1428509e9cToomas Soomestatic void	getinfs(),
1437c478bdstevel@tonic-gate		getoutfs();
1447c478bdstevel@tonic-gate
1458509e9cToomas Soomestatic char	*getfslabel(struct fs *);
1468509e9cToomas Soomestatic char	*getvolabel(struct fs *);
147d1a180bmaheshvsstatic void	prompt(int, const char *, ...);
148d1a180bmaheshvsstatic void	perr(int, const char *, ...);
1498509e9cToomas Soomestatic void	mklabel(void);
150d1a180bmaheshvsstatic void	get_mach_type(void);
1518509e9cToomas Soomestatic void	mem_setup(void);
152d1a180bmaheshvsstatic void	rprt(void);
1538509e9cToomas Soomestatic void	chgreel(struct file_info *, int);
154d1a180bmaheshvsstatic void	parent_copy(void);
1558509e9cToomas Soomestatic void	copy(void);
1568509e9cToomas Soomestatic void	flush_bufs(int);
1578509e9cToomas Soomestatic void	cleanup(void);
158d1a180bmaheshvs
159d1a180bmaheshvs#ifdef LOG
1608509e9cToomas Soomestatic int	fslog(void);
161d1a180bmaheshvs#endif	/* LOG */
1627c478bdstevel@tonic-gate
1637c478bdstevel@tonic-gate/*
1647c478bdstevel@tonic-gate * g_init(), g_read(), g_write() originally came from libgenIO,
1657c478bdstevel@tonic-gate * a totally obsolete device interface library, now deleted.
1667c478bdstevel@tonic-gate * volcopy should be deleted too, since it doesn't work.
1677c478bdstevel@tonic-gate */
1687c478bdstevel@tonic-gate
1697c478bdstevel@tonic-gate#define	G_TM_TAPE	1	/* Tapemaster controller    */
1707c478bdstevel@tonic-gate#define	G_XY_DISK	3	/* xy disks		*/
1717c478bdstevel@tonic-gate#define	G_SD_DISK	7	/* scsi sd disk		*/
1727c478bdstevel@tonic-gate#define	G_XT_TAPE	8	/* xt tapes		*/
1737c478bdstevel@tonic-gate#define	G_SF_FLOPPY	9	/* sf floppy		*/
1747c478bdstevel@tonic-gate#define	G_XD_DISK	10	/* xd disks		*/
1757c478bdstevel@tonic-gate#define	G_ST_TAPE	11	/* scsi tape		*/
1767c478bdstevel@tonic-gate#define	G_NS		12	/* noswap pseudo-dev	*/
1777c478bdstevel@tonic-gate#define	G_RAM		13	/* ram pseudo-dev	*/
1787c478bdstevel@tonic-gate#define	G_FT		14	/* tftp			*/
1797c478bdstevel@tonic-gate#define	G_HD		15	/* 386 network disk	*/
1807c478bdstevel@tonic-gate#define	G_FD		16	/* 386 AT disk		*/
1817c478bdstevel@tonic-gate#define	G_FILE		28	/* file, not a device	*/
1827c478bdstevel@tonic-gate#define	G_NO_DEV	29	/* device does not require special treatment */
1837c478bdstevel@tonic-gate#define	G_DEV_MAX	30	/* last valid device type */
1847c478bdstevel@tonic-gate
1857c478bdstevel@tonic-gate/*
1867c478bdstevel@tonic-gate * g_init: Determine the device being accessed, set the buffer size,
1877c478bdstevel@tonic-gate * and perform any device specific initialization. Since at this point
1887c478bdstevel@tonic-gate * Sun has no system call to read the configuration, the major numbers
1897c478bdstevel@tonic-gate * are assumed to be static and types are figured out as such. However,
1907c478bdstevel@tonic-gate * as a rough estimate, the buffer size for all types is set to 512
1917c478bdstevel@tonic-gate * as a default.
1927c478bdstevel@tonic-gate */
1937c478bdstevel@tonic-gatestatic int
1947c478bdstevel@tonic-gateg_init(int *devtype, int *fdes)
1957c478bdstevel@tonic-gate{
1967c478bdstevel@tonic-gate	major_t maj;
1977c478bdstevel@tonic-gate	int bufsize;
1987c478bdstevel@tonic-gate	struct stat64 st_buf;
1997c478bdstevel@tonic-gate	struct statvfs64 stfs_buf;
2007c478bdstevel@tonic-gate
2017c478bdstevel@tonic-gate	*devtype = G_NO_DEV;
2027c478bdstevel@tonic-gate	if (fstat64(*fdes, &st_buf) == -1)
2037c478bdstevel@tonic-gate		return (-1);
2044bc0a2ecasper	if (!S_ISCHR(st_buf.st_mode) && !S_ISBLK(st_buf.st_mode)) {
2054bc0a2ecasper		if (S_ISFIFO(st_buf.st_mode))
2067c478bdstevel@tonic-gate			bufsize = 512;
2077c478bdstevel@tonic-gate		else {
2087c478bdstevel@tonic-gate			/* find block size for this file system */
2097c478bdstevel@tonic-gate			*devtype = G_FILE;
2107c478bdstevel@tonic-gate			if (fstatvfs64(*fdes, &stfs_buf) < 0) {
2117c478bdstevel@tonic-gate				bufsize = -1;
2127c478bdstevel@tonic-gate				errno = ENODEV;
2137c478bdstevel@tonic-gate			} else {
2147c478bdstevel@tonic-gate				bufsize = stfs_buf.f_bsize;
2157c478bdstevel@tonic-gate			}
2167c478bdstevel@tonic-gate		}
2177c478bdstevel@tonic-gate		return (bufsize);
2187c478bdstevel@tonic-gate	}
2197c478bdstevel@tonic-gate
2207c478bdstevel@tonic-gate	return (512);
2217c478bdstevel@tonic-gate}
2227c478bdstevel@tonic-gate
2237c478bdstevel@tonic-gate/*
2247c478bdstevel@tonic-gate * g_read: Read nbytes of data from fdes (of type devtype) and place
2257c478bdstevel@tonic-gate * data in location pointed to by buf.  In case of end of medium,
2267c478bdstevel@tonic-gate * translate (where necessary) device specific EOM indications into
2277c478bdstevel@tonic-gate * the generic EOM indication of rv = -1, errno = ENOSPC.
2287c478bdstevel@tonic-gate */
2297c478bdstevel@tonic-gate/* ARGSUSED */
2307c478bdstevel@tonic-gatestatic ssize_t
2317c478bdstevel@tonic-gateg_read(int devtype, int fdes, void *buf, size_t nbytes)
2327c478bdstevel@tonic-gate{
2337c478bdstevel@tonic-gate	ssize_t rv;
2347c478bdstevel@tonic-gate
2357c478bdstevel@tonic-gate	rv = read(fdes, buf, nbytes);
2367c478bdstevel@tonic-gate
2377c478bdstevel@tonic-gate	/* st devices return 0 when no space left */
2387c478bdstevel@tonic-gate	if ((rv == 0 && errno == 0) || (rv == -1 && errno == EIO)) {
2397c478bdstevel@tonic-gate		errno = ENOSPC;
2407c478bdstevel@tonic-gate		rv = -1;
2417c478bdstevel@tonic-gate	}
2427c478bdstevel@tonic-gate
2437c478bdstevel@tonic-gate	return (rv);
2447c478bdstevel@tonic-gate}
2457c478bdstevel@tonic-gate
2467c478bdstevel@tonic-gate/*
2477c478bdstevel@tonic-gate * g_write: Write nbytes of data to fdes (of type devtype) from
2487c478bdstevel@tonic-gate * the location pointed to by buf.  In case of end of medium,
2497c478bdstevel@tonic-gate * translate (where necessary) device specific EOM indications into
2507c478bdstevel@tonic-gate * the generic EOM indication of rv = -1, errno = ENOSPC.
2517c478bdstevel@tonic-gate */
2527c478bdstevel@tonic-gate/* ARGSUSED */
2537c478bdstevel@tonic-gatestatic ssize_t
2547c478bdstevel@tonic-gateg_write(int devtype, int fdes, void *buf, size_t nbytes)
2557c478bdstevel@tonic-gate{
2567c478bdstevel@tonic-gate	ssize_t rv;
2577c478bdstevel@tonic-gate
2587c478bdstevel@tonic-gate	rv = write(fdes, buf, nbytes);
2597c478bdstevel@tonic-gate
2607c478bdstevel@tonic-gate	/* st devices return 0 when no more space left */
2617c478bdstevel@tonic-gate	if ((rv == 0 && errno == 0) || (rv == -1 && errno == EIO)) {
2627c478bdstevel@tonic-gate		errno = ENOSPC;
2637c478bdstevel@tonic-gate		rv = -1;
2647c478bdstevel@tonic-gate	}
2657c478bdstevel@tonic-gate
2667c478bdstevel@tonic-gate	return (rv);
2677c478bdstevel@tonic-gate}
2687c478bdstevel@tonic-gate
2697c478bdstevel@tonic-gate/*
2707c478bdstevel@tonic-gate * filesystem copy with propagation of volume ID and filesystem name:
2717c478bdstevel@tonic-gate *
2727c478bdstevel@tonic-gate * volcopy [-options]  filesystem /dev/from From_vol /dev/to To_vol
2737c478bdstevel@tonic-gate *
2747c478bdstevel@tonic-gate * options are:
2758509e9cToomas Soome *	-feet - length of tape
2768509e9cToomas Soome *	-bpi  - recording density
2778509e9cToomas Soome *	-reel - reel number (if not starting from beginning)
2788509e9cToomas Soome *	-buf  - use double buffered i/o (if dens >= 1600 bpi)
2797c478bdstevel@tonic-gate *	-block - Set the transfer block size to NUM physical blocks (512
2807c478bdstevel@tonic-gate *	bytes on 3B2 and 3B15).  Note that an arbitrary block size might
2817c478bdstevel@tonic-gate *	or might not work on a given system.  Also, the block size
2827c478bdstevel@tonic-gate *	read from the header of an input tape silently overrides this.
2837c478bdstevel@tonic-gate *	-nosh - Don't offer the user a shell after hitting break or delete.
2847c478bdstevel@tonic-gate *	-r - Read NUM transfer blocks from the disk at once and write it
2857c478bdstevel@tonic-gate *	to the output device one block at a time.  Intended only to
2867c478bdstevel@tonic-gate *	boost the 3B15 EDFC disk to tape performance.  Disabled on 3B2.
2878509e9cToomas Soome *	-a    - ask "y or n" instead of "DEL if wrong"
2888509e9cToomas Soome *	-s    - inverse of -a, from/to devices are printed followed by `?'.
2898509e9cToomas Soome *		User has 10 seconds to DEL if mistaken!
2908509e9cToomas Soome *	-y    - assume "yes" response to all questions
2917c478bdstevel@tonic-gate *
2927c478bdstevel@tonic-gate * Examples:
2937c478bdstevel@tonic-gate *
2947c478bdstevel@tonic-gate * volcopy root /dev/rdsk/0s2 pk5 /dev/rdsk/1s2 pk12
2957c478bdstevel@tonic-gate *
2967c478bdstevel@tonic-gate * volcopy u3 /dev/rdsk/1s5 pk1 /dev/rmt/0m tp123
2977c478bdstevel@tonic-gate *
2987c478bdstevel@tonic-gate * volcopy u5 /dev/rmt/0m -  /dev/rdsk/1s5 -
2997c478bdstevel@tonic-gate */
3007c478bdstevel@tonic-gate
301d1a180bmaheshvsint
302d1a180bmaheshvsmain(int argc, char *argv[])
3037c478bdstevel@tonic-gate{
304d1a180bmaheshvs	char c;
305d1a180bmaheshvs	int lfdes, altflg = 0, result, verify;
306d1a180bmaheshvs	long dist;
3077c478bdstevel@tonic-gate	char *align();
3087c478bdstevel@tonic-gate	void sigalrm(), sigint();
3097c478bdstevel@tonic-gate	struct stat stbuf;
3107c478bdstevel@tonic-gate	int cnt;
3117c478bdstevel@tonic-gate
3127c478bdstevel@tonic-gate	(void) setlocale(LC_ALL, "");
3137c478bdstevel@tonic-gate#if !defined(TEXT_DOMAIN)
3147c478bdstevel@tonic-gate#define	TEXT_DOMAIN "SYS_TEST"
3157c478bdstevel@tonic-gate#endif
3167c478bdstevel@tonic-gate	(void) textdomain(TEXT_DOMAIN);
3177c478bdstevel@tonic-gate
3187c478bdstevel@tonic-gate	(void) get_mach_type();
3197c478bdstevel@tonic-gate	(void) signal(SIGINT, sigint);
3207c478bdstevel@tonic-gate	(void) signal(SIGALRM, sigalrm);
3217c478bdstevel@tonic-gate	In.f_bsize = Out.f_bsize = BLKSIZ;
3227c478bdstevel@tonic-gate	while (argc > 1 && argv[1][0] == '-') {
3237c478bdstevel@tonic-gate		if (EQ(argv[1], "-a", 2)) {
3247c478bdstevel@tonic-gate			altflg |= MINUSA;
3257c478bdstevel@tonic-gate		} else if (EQ(argv[1], "-e", 2)) {
3267c478bdstevel@tonic-gate			Eomflg = 1;
3277c478bdstevel@tonic-gate		} else if (EQ(argv[1], "-s", 2)) {
3287c478bdstevel@tonic-gate			altflg |= MINUSS;
3297c478bdstevel@tonic-gate		} else if (EQ(argv[1], "-y", 2)) {
3307c478bdstevel@tonic-gate			Yesflg++;
3317c478bdstevel@tonic-gate		} else if (EQ(argv[1], "-buf", 4)) {
3327c478bdstevel@tonic-gate			Bufflg++;
3337c478bdstevel@tonic-gate		} else if (EQ(argv[1], "-bpi", 4)) {
3347c478bdstevel@tonic-gate			if ((c = argv[1][4]) >= '0' && c <= '9')
3357c478bdstevel@tonic-gate				Bpi = getbpi(&argv[1][4]);
3367c478bdstevel@tonic-gate			else {
3377c478bdstevel@tonic-gate				++argv;
3387c478bdstevel@tonic-gate				--argc;
3397c478bdstevel@tonic-gate				Bpi = getbpi(&argv[1][0]);
3407c478bdstevel@tonic-gate			}
3417c478bdstevel@tonic-gate		} else if (EQ(argv[1], "-feet", 5)) {
3427c478bdstevel@tonic-gate			if ((c = argv[1][5]) >= '0' && c <= '9')
3437c478bdstevel@tonic-gate				R_len = atoi(&argv[1][5]);
3447c478bdstevel@tonic-gate			else {
3457c478bdstevel@tonic-gate				++argv;
3467c478bdstevel@tonic-gate				--argc;
3477c478bdstevel@tonic-gate				R_len = atoi(&argv[1][0]);
3487c478bdstevel@tonic-gate			}
3497c478bdstevel@tonic-gate		} else if (EQ(argv[1], "-reel", 5)) {
3507c478bdstevel@tonic-gate			if ((c = argv[1][5]) >= '0' && c <= '9')
3517c478bdstevel@tonic-gate				R_cur = atoi(&argv[1][5]);
3527c478bdstevel@tonic-gate			else {
3537c478bdstevel@tonic-gate				++argv;
3547c478bdstevel@tonic-gate				--argc;
3557c478bdstevel@tonic-gate				R_cur = atoi(&argv[1][0]);
3567c478bdstevel@tonic-gate			}
3577c478bdstevel@tonic-gate		} else if (EQ(argv[1], "-r", 2)) { /* 3b15 only */
3587c478bdstevel@tonic-gate			if ((c = argv[1][2]) >= '0' && c <= '9')
3597c478bdstevel@tonic-gate				Disk_cnt = atoi(&argv[1][2]);
3607c478bdstevel@tonic-gate			else {
3617c478bdstevel@tonic-gate				++argv;
3627c478bdstevel@tonic-gate				--argc;
3637c478bdstevel@tonic-gate				Disk_cnt = atoi(&argv[1][0]);
3647c478bdstevel@tonic-gate			}
3657c478bdstevel@tonic-gate			if (Disk_cnt == 0)
3667c478bdstevel@tonic-gate		perr(1, "volcopy: Need a non-zero value for the -r option\n");
3677c478bdstevel@tonic-gate		} else if (EQ(argv[1], "-block", 6)) { /* 3b15 only */
3687c478bdstevel@tonic-gate			if ((c = argv[1][6]) >= '0' && c <= '9')
3697c478bdstevel@tonic-gate				Blk_cnt = atoi(&argv[1][6]);
3707c478bdstevel@tonic-gate			else {
3717c478bdstevel@tonic-gate				++argv;
3727c478bdstevel@tonic-gate				--argc;
3737c478bdstevel@tonic-gate				Blk_cnt = atoi(&argv[1][0]);
3747c478bdstevel@tonic-gate			}
3757c478bdstevel@tonic-gate			if (Blk_cnt == 0)
3767c478bdstevel@tonic-gate	perr(1, "volcopy: Need a non-zero value for the -block option\n");
3777c478bdstevel@tonic-gate		} else if (EQ(argv[1], "-nosh", 5)) { /* 3b15 only */
3787c478bdstevel@tonic-gate			Shell_esc = 0;
3797c478bdstevel@tonic-gate		} else
3807c478bdstevel@tonic-gate			perr(1, "<%s> invalid option\n", argv[1]);
3817c478bdstevel@tonic-gate		++argv;
3827c478bdstevel@tonic-gate		--argc;
3837c478bdstevel@tonic-gate	} /* argv[1][0] == '-' */
3847c478bdstevel@tonic-gate
3857c478bdstevel@tonic-gate	Devtty = fopen("/dev/tty", "r");
3867c478bdstevel@tonic-gate	if ((Devtty == NULL) && !isatty(0))
3877c478bdstevel@tonic-gate		Devtty = stdin;
3887c478bdstevel@tonic-gate	time(&Tvec);
3897c478bdstevel@tonic-gate
3907c478bdstevel@tonic-gate	if (Eomflg && R_len)
3917c478bdstevel@tonic-gate		perr(9, "volcopy: -e and -feet are mutually exclusive\n");
3927c478bdstevel@tonic-gate	if ((altflg & MINUSA) && (altflg & MINUSS))
3937c478bdstevel@tonic-gate		perr(9, "volcopy: -a and -s are mutually exclusive\n");
3947c478bdstevel@tonic-gate	if (argc != 6) /* if mandatory fields not present */
3957c478bdstevel@tonic-gate		perr(9, "ufs usage: volcopy [-F ufs] [generic options] \
3967c478bdstevel@tonic-gatefsname /devfrom volfrom /devto volto\n");
3977c478bdstevel@tonic-gate	if (!(altflg & MINUSA)) /* -a was not specified, use default (-s) */
3987c478bdstevel@tonic-gate		altflg |= MINUSS;
3997c478bdstevel@tonic-gate
4007c478bdstevel@tonic-gate	In.f_dev_p = argv[DEV_IN];
4017c478bdstevel@tonic-gate	Out.f_dev_p = argv[DEV_OUT];
4027c478bdstevel@tonic-gate	strncpy(To_vol, argv[VOL_OUT], VVOLLEN);
4037c478bdstevel@tonic-gate	To_vol[VVOLLEN] = '\0';
4047c478bdstevel@tonic-gate	Out.f_vol_p = &To_vol[0];
4057c478bdstevel@tonic-gate	strncpy(From_vol, argv[VOL_IN], VVOLLEN);
4067c478bdstevel@tonic-gate	From_vol[VVOLLEN] = '\0';
4077c478bdstevel@tonic-gate	In.f_vol_p = &From_vol[0];
4087c478bdstevel@tonic-gate	Fsys_p = argv[FIL_SYS];
4097c478bdstevel@tonic-gate
4107c478bdstevel@tonic-gate	if ((In.f_des = open(In.f_dev_p, O_RDONLY)) < 1)
4117c478bdstevel@tonic-gate		perr(10, "%s: cannot open\n", In.f_dev_p);
4127c478bdstevel@tonic-gate	if ((Out.f_des = open(Out.f_dev_p, O_RDONLY)) < 1)
4137c478bdstevel@tonic-gate		perr(10, "%s: cannot open\n", Out.f_dev_p);
4147c478bdstevel@tonic-gate
4157c478bdstevel@tonic-gate	if (fstat(In.f_des, &stbuf) < 0 || (stbuf.st_mode & S_IFMT) != S_IFCHR)
4167c478bdstevel@tonic-gate		perr(10, "From device not character-special\n");
4177c478bdstevel@tonic-gate	if (fstat(Out.f_des, &stbuf) < 0 || (stbuf.st_mode & S_IFMT) != S_IFCHR)
4187c478bdstevel@tonic-gate		perr(10, "To device not character-special\n");
4197c478bdstevel@tonic-gate
4207c478bdstevel@tonic-gate	if ((Itape = tapeck(&In, INPUT)) == 1)
4217c478bdstevel@tonic-gate		R_blks = V_labl.v_reelblks;
4227c478bdstevel@tonic-gate	Otape = tapeck(&Out, OUTPUT);
4237c478bdstevel@tonic-gate	if (Otape && Itape)
4247c478bdstevel@tonic-gate		perr(10, "Use dd(1) command to copy tapes\n");
4257c478bdstevel@tonic-gate	(void) mem_setup();
4267c478bdstevel@tonic-gate	if (Bufflg && !Ipc)
4277c478bdstevel@tonic-gate		perr(1, "The -buf option requires ipc\n");
4287c478bdstevel@tonic-gate	if (!Itape && !Otape)
4297c478bdstevel@tonic-gate		R_cur = 1;
4307c478bdstevel@tonic-gate	if (R_cur == 1 || !Itape) {
4317c478bdstevel@tonic-gate		/* read in superblock */
4327c478bdstevel@tonic-gate		verify = 0;
4337c478bdstevel@tonic-gate		(void) getinfs(In.f_dev, In.f_des, Sptr);
4347c478bdstevel@tonic-gate
4357c478bdstevel@tonic-gate		if ((Sptr->fs_magic != FS_MAGIC) &&
4367c478bdstevel@tonic-gate		    (Sptr->fs_magic != MTB_UFS_MAGIC))
4377c478bdstevel@tonic-gate			perr(10, "File System type unknown--get help\n");
4387c478bdstevel@tonic-gate
4396451fdbvsakar		if (Sptr->fs_magic == FS_MAGIC &&
4406451fdbvsakar		    (Sptr->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
4416451fdbvsakar		    Sptr->fs_version != UFS_VERSION_MIN))
4426451fdbvsakar			perr(10, "Unrecognized version of UFS--get help\n");
4436451fdbvsakar
4447c478bdstevel@tonic-gate		if (Sptr->fs_magic == MTB_UFS_MAGIC &&
4457c478bdstevel@tonic-gate		    (Sptr->fs_version > MTB_UFS_VERSION_1 ||
4467c478bdstevel@tonic-gate		    Sptr->fs_version < MTB_UFS_VERSION_MIN))
4477c478bdstevel@tonic-gate			perr(10, "Unrecognized version of UFS--get help\n");
4487c478bdstevel@tonic-gate
4497c478bdstevel@tonic-gate		(void) memcpy(&Isup, Sptr, Sptr->fs_sbsize);
4507c478bdstevel@tonic-gate		Ifname = getfslabel(&Isup);
4517c478bdstevel@tonic-gate		Ifpack = getvolabel(&Isup);
4527c478bdstevel@tonic-gate		Fs = Sptr->fs_size * Sptr->fs_nspf;
4537c478bdstevel@tonic-gate	}  /* R_cur == 1 || !Itape */
4547c478bdstevel@tonic-gate
4557c478bdstevel@tonic-gate	/* read in superblock */
4567c478bdstevel@tonic-gate	verify = !Otape || (altflg & MINUSS);
4577c478bdstevel@tonic-gate	(void) getoutfs(Out.f_dev, Out.f_des, Sptr, verify);
4587c478bdstevel@tonic-gate
4597c478bdstevel@tonic-gate	if ((Sptr->fs_magic == FS_MAGIC) || (Sptr->fs_magic == MTB_UFS_MAGIC)) {
4607c478bdstevel@tonic-gate		(void) memcpy(&Osup, Sptr, Sptr->fs_sbsize);
4617c478bdstevel@tonic-gate		Ofname = getfslabel(&Osup);
4627c478bdstevel@tonic-gate		Ofpack = getvolabel(&Osup);
4637c478bdstevel@tonic-gate	} else  {
4647c478bdstevel@tonic-gate		int i;
4657c478bdstevel@tonic-gate
4667c478bdstevel@tonic-gate		/* out vol does not contain a ufs file system */
4677c478bdstevel@tonic-gate		/* stuff let over from Isup */
4687c478bdstevel@tonic-gate		(void) memcpy(&Osup, &Isup, Isup.fs_sbsize);
4697c478bdstevel@tonic-gate		Ofname = getfslabel(&Osup);
4707c478bdstevel@tonic-gate		Ofpack = getvolabel(&Osup);
4717c478bdstevel@tonic-gate		/* wipe out the fs name and pack name for warning purposes */
4727c478bdstevel@tonic-gate		for (i = 0; i < 6; i++) Ofname[i] = ' ';
4737c478bdstevel@tonic-gate		for (i = 0; i < 6; i++) Ofpack[i] = ' ';
4747c478bdstevel@tonic-gate	}
4757c478bdstevel@tonic-gate
4767c478bdstevel@tonic-gate	if (Itape) {
4777c478bdstevel@tonic-gate		if (R_cur != 1) {
4787c478bdstevel@tonic-gate			(void) printf(gettext(
4797c478bdstevel@tonic-gate				"\nvolcopy: IF REEL 1 HAS NOT BEEN RESTORED,"));
4807c478bdstevel@tonic-gate			(void) printf(gettext(
4817c478bdstevel@tonic-gate				" STOP NOW AND START OVER ***\07\n"));
4827c478bdstevel@tonic-gate			if (!ask(" Continue? ")) {
4837c478bdstevel@tonic-gate				cleanup();
4847c478bdstevel@tonic-gate				exit(31+9);
4857c478bdstevel@tonic-gate			}
4867c478bdstevel@tonic-gate			strncpy(Ifname, Fsys_p, 6);
4877c478bdstevel@tonic-gate			strncpy(Ifpack, In.f_vol_p, 6);
4887c478bdstevel@tonic-gate		}
4897c478bdstevel@tonic-gate		if (V_labl.v_reel != R_cur || V_labl.v_reels != R_num)
4907c478bdstevel@tonic-gate	prompt(1, "Tape disagrees: Reel %d of %d : looking for %d of %d\n",
4917c478bdstevel@tonic-gate				V_labl.v_reel, V_labl.v_reels, R_cur, R_num);
4927c478bdstevel@tonic-gate	} else if (Otape) {
4937c478bdstevel@tonic-gate		strncpy(V_labl.v_volume, Out.f_vol_p, 6);
4947c478bdstevel@tonic-gate		strncpy(Ofpack, Out.f_vol_p, 6);
4957c478bdstevel@tonic-gate		strncpy(Ofname, Fsys_p, 6);
4967c478bdstevel@tonic-gate		if (!Eomflg) {
4977c478bdstevel@tonic-gate			R_num = Fs / R_blks + ((Fs % R_blks) ? 1 : 0);
4987c478bdstevel@tonic-gate			(void) printf(gettext(
4997c478bdstevel@tonic-gate				"You will need %d reels.\n"), R_num);
5007c478bdstevel@tonic-gate			(void) printf(gettext(
5017c478bdstevel@tonic-gate		"(\tThe same size and density is expected for all reels)\n"));
5027c478bdstevel@tonic-gate		}
5037c478bdstevel@tonic-gate	}
5047c478bdstevel@tonic-gate	if (NOT_EQ(Fsys_p, Ifname, 6)) {
5057c478bdstevel@tonic-gate		verify = !Otape || (altflg & MINUSS);
5067c478bdstevel@tonic-gate		prompt(verify,
5077c478bdstevel@tonic-gate			"arg. (%.6s) doesn't agree with from fs. (%.6s)\n",
5087c478bdstevel@tonic-gate			Fsys_p, Ifname);
5097c478bdstevel@tonic-gate	}
5107c478bdstevel@tonic-gate	if (NOT_EQ(In.f_vol_p, "-", 6) && NOT_EQ(In.f_vol_p, Ifpack, 6)) {
5117c478bdstevel@tonic-gate		verify = !Otape || (altflg & MINUSS);
5127c478bdstevel@tonic-gate	prompt(verify, "arg. (%.6s) doesn't agree with from vol.(%.6s)\n",
5137c478bdstevel@tonic-gate			In.f_vol_p, Ifpack);
5147c478bdstevel@tonic-gate	}
5157c478bdstevel@tonic-gate
5167c478bdstevel@tonic-gate	if (*In.f_vol_p == '-')
5177c478bdstevel@tonic-gate		In.f_vol_p = Ifpack;
5187c478bdstevel@tonic-gate	if (*Out.f_vol_p == '-')
5197c478bdstevel@tonic-gate		Out.f_vol_p = Ofpack;
5207c478bdstevel@tonic-gate
5217c478bdstevel@tonic-gate	if (R_cur == 1 && (Osup.fs_time + _2_DAYS) > Isup.fs_time) {
5227c478bdstevel@tonic-gate		time_t t;
5237c478bdstevel@tonic-gate
5247c478bdstevel@tonic-gate		verify = altflg & MINUSS;
5257c478bdstevel@tonic-gate		t = (time_t)Osup.fs_time;
5267c478bdstevel@tonic-gate		prompt(verify, "%s less than 48 hours older than %s\n"
5277c478bdstevel@tonic-gate		    "To filesystem dated:  %s",
5287c478bdstevel@tonic-gate		    Out.f_dev_p, In.f_dev_p, ctime(&t));
5297c478bdstevel@tonic-gate	}
5307c478bdstevel@tonic-gate	if (NOT_EQ(Out.f_vol_p, Ofpack, 6)) {
5317c478bdstevel@tonic-gate		prompt(1, "arg.(%.6s) doesn't agree with to vol.(%.6s)\n",
5327c478bdstevel@tonic-gate			Out.f_vol_p, Ofpack);
5337c478bdstevel@tonic-gate		strncpy(Ofpack, Out.f_vol_p, 6);
5347c478bdstevel@tonic-gate	}
5357c478bdstevel@tonic-gate	if (Isup.fs_size > Osup.fs_size && !Otape)
5367c478bdstevel@tonic-gate		prompt(1, "from fs larger than to fs\n");
5377c478bdstevel@tonic-gate	if (!Otape && NOT_EQ(Ifname, Ofname, 6)) {
5387c478bdstevel@tonic-gate		verify = altflg & MINUSS;
5397c478bdstevel@tonic-gate	prompt(verify, "warning! from fs(%.6s) differs from to fs(%.6s)\n",
5407c478bdstevel@tonic-gate			Ifname, Ofname);
5417c478bdstevel@tonic-gate	}
5427c478bdstevel@tonic-gate
5437c478bdstevel@tonic-gate	(void) printf(gettext("From: %s, to: %s? "), In.f_dev_p, Out.f_dev_p);
5447c478bdstevel@tonic-gate	if (!(altflg & MINUSA)) {
5457c478bdstevel@tonic-gate		(void) printf(gettext("(DEL if wrong)\n"));
5467c478bdstevel@tonic-gate		sleep(10);
5477c478bdstevel@tonic-gate	} else if (!ask("(y or n) "))
5487c478bdstevel@tonic-gate		perr(10, "\nvolcopy: STOP\n");
5497c478bdstevel@tonic-gate	close(In.f_des);
5507c478bdstevel@tonic-gate	close(Out.f_des);
5517c478bdstevel@tonic-gate	sync();
5527c478bdstevel@tonic-gate	In.f_des = open(In.f_dev_p, O_RDONLY);
5537c478bdstevel@tonic-gate	Out.f_des = open(Out.f_dev_p, O_WRONLY);
5547c478bdstevel@tonic-gate	errno = 0;
5557c478bdstevel@tonic-gate	if (g_init(&In.f_dev, &In.f_des) < 0 ||
5567c478bdstevel@tonic-gate	    g_init(&Out.f_dev, &Out.f_des) < 0)
5577c478bdstevel@tonic-gate		perr(1, "volcopy: Error %d during initialization\n", errno);
5587c478bdstevel@tonic-gate	if (Itape) {
5597c478bdstevel@tonic-gate		errno = 0;
5607c478bdstevel@tonic-gate		if (g_read(In.f_dev, In.f_des, &V_labl, sizeof (V_labl)) <
5617c478bdstevel@tonic-gate		    sizeof (V_labl))
5627c478bdstevel@tonic-gate			perr(10, "Error while reading label\n");
5637c478bdstevel@tonic-gate	} else if (Otape) {
5647c478bdstevel@tonic-gate		V_labl.v_reels = R_num;
5657c478bdstevel@tonic-gate		V_labl.v_reel = R_cur;
5667c478bdstevel@tonic-gate		V_labl.v_time = Tvec;
5677c478bdstevel@tonic-gate		V_labl.v_reelblks = R_blks;
5687c478bdstevel@tonic-gate		V_labl.v_blksize = BLKSIZ * Blk_cnt;
5697c478bdstevel@tonic-gate		V_labl.v_nblocks = Blk_cnt;
5707c478bdstevel@tonic-gate		V_labl.v_offset = 0L;
5717c478bdstevel@tonic-gate		V_labl.v_type = T_TYPE;
5727c478bdstevel@tonic-gate		errno = 0;
5737c478bdstevel@tonic-gate		if (g_write(Out.f_dev, Out.f_des, &V_labl, sizeof (V_labl)) <
5747c478bdstevel@tonic-gate		    sizeof (V_labl))
5757c478bdstevel@tonic-gate			perr(10, "Error while writing label\n");
5767c478bdstevel@tonic-gate	}
5777c478bdstevel@tonic-gate	if (R_cur > 1) {
5787c478bdstevel@tonic-gate		if (!Eomflg) {
5797c478bdstevel@tonic-gate			Fs = (R_cur - 1) * actual_blocks();
5807c478bdstevel@tonic-gate			lfdes = Otape ? In.f_des : Out.f_des;
5817c478bdstevel@tonic-gate			dist = (long)(Fs * BLKSIZ);
5827c478bdstevel@tonic-gate		} else { /* Eomflg */
5837c478bdstevel@tonic-gate			if (Otape)
5847c478bdstevel@tonic-gate		perr(1, "Cannot use -reel with -e when copying to tape\n");
5857c478bdstevel@tonic-gate			lfdes = Out.f_des;
5867c478bdstevel@tonic-gate			dist = (long)(V_labl.v_offset * BLKSIZ);
5877c478bdstevel@tonic-gate			Fs = V_labl.v_offset;
5887c478bdstevel@tonic-gate		}
5897c478bdstevel@tonic-gate		if (lseek(lfdes, dist, 0) < 0)
5907c478bdstevel@tonic-gate			perr(1, "Cannot lseek()\n");
5917c478bdstevel@tonic-gate		Sptr = Otape ? &Isup : &Osup;
5927c478bdstevel@tonic-gate		if ((Sptr -> fs_magic != FS_MAGIC) &&
5937c478bdstevel@tonic-gate		    (Sptr -> fs_magic != MTB_UFS_MAGIC))
5947c478bdstevel@tonic-gate			perr(10, "File System type unknown--get help!\n");
5957c478bdstevel@tonic-gate
5966451fdbvsakar		if (Sptr->fs_magic == FS_MAGIC &&
5976451fdbvsakar		    (Sptr->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
5986451fdbvsakar		    Sptr->fs_version != UFS_VERSION_MIN))
5996451fdbvsakar			perr(10, "Unrecognized version of UFS--get help\n");
6006451fdbvsakar
6017c478bdstevel@tonic-gate		if (Sptr->fs_magic == MTB_UFS_MAGIC &&
6027c478bdstevel@tonic-gate		    (Sptr->fs_version > MTB_UFS_VERSION_1 ||
6037c478bdstevel@tonic-gate		    Sptr->fs_version < MTB_UFS_VERSION_MIN))
6047c478bdstevel@tonic-gate			perr(10, "Unrecognized version of UFS--get help\n");
6057c478bdstevel@tonic-gate
6067c478bdstevel@tonic-gate		Fs = (Sptr->fs_size * Sptr->fs_nspf) - Fs;
6077c478bdstevel@tonic-gate	}
6087c478bdstevel@tonic-gate	if (Itape || Otape)
6097c478bdstevel@tonic-gate		rprt();
6107c478bdstevel@tonic-gate
6117c478bdstevel@tonic-gate	if (Ipc) {
6127c478bdstevel@tonic-gate		parent_copy();
6137c478bdstevel@tonic-gate		(void) cleanup();
6147c478bdstevel@tonic-gate	} else
6157c478bdstevel@tonic-gate		copy();
6167c478bdstevel@tonic-gate	(void) printf(gettext("  END: %ld blocks.\n"), Blocks);
6177c478bdstevel@tonic-gate
6187c478bdstevel@tonic-gate#ifdef LOG
6197c478bdstevel@tonic-gate	fslog();
6207c478bdstevel@tonic-gate#endif
6217c478bdstevel@tonic-gate	if (Blocks)
622d1a180bmaheshvs		return (0);
623d1a180bmaheshvs	return (31+1);		/* failed.. 0 blocks */
6247c478bdstevel@tonic-gate}
6257c478bdstevel@tonic-gate
6267c478bdstevel@tonic-gate/*
6277c478bdstevel@tonic-gate * sigalrm: catch alarm signals.
6287c478bdstevel@tonic-gate */
6297c478bdstevel@tonic-gate
6307c478bdstevel@tonic-gatevoid
6317c478bdstevel@tonic-gatesigalrm()
6327c478bdstevel@tonic-gate{
6337c478bdstevel@tonic-gate	void (*signal())();
6347c478bdstevel@tonic-gate
6357c478bdstevel@tonic-gate	(void) signal(SIGALRM, sigalrm);
6367c478bdstevel@tonic-gate}
6377c478bdstevel@tonic-gate
6387c478bdstevel@tonic-gate/*
6397c478bdstevel@tonic-gate * sigsys: catch illegal system calls to determine if IPC is available.
6407c478bdstevel@tonic-gate */
6417c478bdstevel@tonic-gate
6427c478bdstevel@tonic-gatevoid
6437c478bdstevel@tonic-gatesigsys()
6447c478bdstevel@tonic-gate{
6457c478bdstevel@tonic-gate
6467c478bdstevel@tonic-gate	Ipc = 0;
6477c478bdstevel@tonic-gate}
6487c478bdstevel@tonic-gate
6497c478bdstevel@tonic-gate/*
6507c478bdstevel@tonic-gate * sigint: catch interrupts and prompt user for shell or to quit.
6517c478bdstevel@tonic-gate */
6527c478bdstevel@tonic-gate
6537c478bdstevel@tonic-gatevoid
6547c478bdstevel@tonic-gatesigint()
6557c478bdstevel@tonic-gate{
6567c478bdstevel@tonic-gate	void (*signal())();
6577c478bdstevel@tonic-gate	extern char **environ;
658d1a180bmaheshvs	int tmpflg, i = 0, ps1 = -1, ps2 = -1;
6597c478bdstevel@tonic-gate
6607c478bdstevel@tonic-gate	tmpflg = Yesflg; /* override yesflag for duration of interrupt */
6617c478bdstevel@tonic-gate	Yesflg = 0;
6627c478bdstevel@tonic-gate	if (Shell_esc && ask("Want Shell?   ")) {
6637c478bdstevel@tonic-gate		if (!fork()) {
6647c478bdstevel@tonic-gate			/* both PS1 and PS2 must be exported */
6657c478bdstevel@tonic-gate			while (environ[i]) {
6667c478bdstevel@tonic-gate				if (EQ(environ[i], "PS1", 3))
6677c478bdstevel@tonic-gate					ps1 = i;
6687c478bdstevel@tonic-gate				if (EQ(environ[i], "PS2", 3))
6697c478bdstevel@tonic-gate					ps2 = i;
6707c478bdstevel@tonic-gate				i++;
6717c478bdstevel@tonic-gate			}
6727c478bdstevel@tonic-gate			if (ps1 >= 0 && ps2 >= 0)
6737c478bdstevel@tonic-gate				environ[ps1] = environ[ps2];
6747c478bdstevel@tonic-gate			(void) signal(SIGINT, SIG_DFL);
6757c478bdstevel@tonic-gate			execl("/usr/bin/sh", "/usr/bin/sh", 0);
6767c478bdstevel@tonic-gate		} else { /* parent */
6777c478bdstevel@tonic-gate			(void) signal(SIGINT, SIG_IGN);
6787c478bdstevel@tonic-gate			wait((int *)0);
6797c478bdstevel@tonic-gate		}
6807c478bdstevel@tonic-gate	} else if (ask("Want to quit?    ")) {
6817c478bdstevel@tonic-gate		if (Pid > 0)
6827c478bdstevel@tonic-gate			kill(Pid, 9);
6837c478bdstevel@tonic-gate		(void) cleanup(); /* ipc */
6847c478bdstevel@tonic-gate		exit(31+2);
6857c478bdstevel@tonic-gate	}
6867c478bdstevel@tonic-gate	(void) signal(SIGINT, sigint);
6877c478bdstevel@tonic-gate	Yesflg = tmpflg; /* reset Yesflg */
6887c478bdstevel@tonic-gate}
6897c478bdstevel@tonic-gate
6907c478bdstevel@tonic-gate/*
6917c478bdstevel@tonic-gate * actual_blocks: Calculate the actual number of blocks written to
6927c478bdstevel@tonic-gate * the tape (will differ from V_labl.v_reelblks if v_reelblks is not
6937c478bdstevel@tonic-gate * an even multiple of the blocking factor Blk_cnt).
6947c478bdstevel@tonic-gate */
6957c478bdstevel@tonic-gate
6967c478bdstevel@tonic-gateint
6977c478bdstevel@tonic-gateactual_blocks()
6987c478bdstevel@tonic-gate{
6997c478bdstevel@tonic-gate
7007c478bdstevel@tonic-gate	if (R_blks % Blk_cnt)
7017c478bdstevel@tonic-gate		return (((R_blks / Blk_cnt) + 1) * Blk_cnt);
7027c478bdstevel@tonic-gate	else
7037c478bdstevel@tonic-gate		return (R_blks);
7047c478bdstevel@tonic-gate}
7057c478bdstevel@tonic-gate
7067c478bdstevel@tonic-gate/*
7077c478bdstevel@tonic-gate * get_mach_type: Determine what machine this is executing on.
7087c478bdstevel@tonic-gate */
7097c478bdstevel@tonic-gate
710d1a180bmaheshvsstatic void
711d1a180bmaheshvsget_mach_type(void)
7127c478bdstevel@tonic-gate{
7137c478bdstevel@tonic-gate	struct utsname utsinfo;
7147c478bdstevel@tonic-gate
7157c478bdstevel@tonic-gate	errno = 0;
7167c478bdstevel@tonic-gate	if (uname(&utsinfo) < 0)
7177c478bdstevel@tonic-gate		perr(1, "Unable to determine machine type\n");
7187c478bdstevel@tonic-gate	if (strcmp(utsinfo.machine, "3B2") == 0)
7197c478bdstevel@tonic-gate		M3b2 = 1;
7207c478bdstevel@tonic-gate	else if (strcmp(utsinfo.machine, "3B15") == 0)
7217c478bdstevel@tonic-gate		M3b15 = 1;
7227c478bdstevel@tonic-gate}
7237c478bdstevel@tonic-gate
7247c478bdstevel@tonic-gate/*
7257c478bdstevel@tonic-gate * mem_setup: Determine memory needs and check for IPC.  If IPC is available,
7267c478bdstevel@tonic-gate * used shared memory and semaphores to increase performance.  If no IPC,
7277c478bdstevel@tonic-gate * get normal memory and only use one process.
7287c478bdstevel@tonic-gate */
7297c478bdstevel@tonic-gate
730d1a180bmaheshvsstatic void
731d1a180bmaheshvsmem_setup(void)
7327c478bdstevel@tonic-gate{
7337c478bdstevel@tonic-gate	void (*signal())();
734d1a180bmaheshvs	int cnt, num, size;
7357c478bdstevel@tonic-gate	char *align();
7367c478bdstevel@tonic-gate
7377c478bdstevel@tonic-gate	union semun {
7387c478bdstevel@tonic-gate		int val;
7397c478bdstevel@tonic-gate		struct semid_ds *buf;
7407c478bdstevel@tonic-gate		ushort_t *array;
7417c478bdstevel@tonic-gate	} sem_arg;
7427c478bdstevel@tonic-gate
7437c478bdstevel@tonic-gate	if (Blk_cnt == 1) {
7447c478bdstevel@tonic-gate		switch (Drive_typ) {
7457c478bdstevel@tonic-gate		case A_DRIVE:
7467c478bdstevel@tonic-gate			Blk_cnt = 32;
7477c478bdstevel@tonic-gate			break;
7487c478bdstevel@tonic-gate		case C_DRIVE:
7497c478bdstevel@tonic-gate			Blk_cnt = 10;
7507c478bdstevel@tonic-gate			break;
7517c478bdstevel@tonic-gate		case K_DRIVE:
7527c478bdstevel@tonic-gate			Blk_cnt = 4;
7537c478bdstevel@tonic-gate			break;
7547c478bdstevel@tonic-gate		case T_DRIVE:
7557c478bdstevel@tonic-gate			if (Bpi == 6250)
7567c478bdstevel@tonic-gate				Blk_cnt = 50;
7577c478bdstevel@tonic-gate			else
7587c478bdstevel@tonic-gate				Blk_cnt = 10;
7597c478bdstevel@tonic-gate			break;
7607c478bdstevel@tonic-gate		default:
7617c478bdstevel@tonic-gate			if (M3b15) {
7627c478bdstevel@tonic-gate				if (Itape || Otape)
7637c478bdstevel@tonic-gate					Blk_cnt = 16;
7647c478bdstevel@tonic-gate			} else {
7657c478bdstevel@tonic-gate				if (Otape || Itape) {
7667c478bdstevel@tonic-gate					if (Bpi == 6250)
7677c478bdstevel@tonic-gate						Blk_cnt = 50;
7687c478bdstevel@tonic-gate					else
7697c478bdstevel@tonic-gate						Blk_cnt = 10;
7707c478bdstevel@tonic-gate				}
7717c478bdstevel@tonic-gate			}
7727c478bdstevel@tonic-gate			break;
7737c478bdstevel@tonic-gate		} /* Drive_typ */
7747c478bdstevel@tonic-gate	} /* Blk_cnt == 1 */
7757c478bdstevel@tonic-gate	if (Blk_cnt > 1) /* user overrode g_init */
7767c478bdstevel@tonic-gate		In.f_bsize = Out.f_bsize = Blk_cnt * BLKSIZ;
7777c478bdstevel@tonic-gate	In.f_bsize = (!Itape) ? Disk_cnt * In.f_bsize : In.f_bsize;
7787c478bdstevel@tonic-gate	Out.f_bsize = (!Otape) ? Disk_cnt * Out.f_bsize : Out.f_bsize;
7797c478bdstevel@tonic-gate	Bufsz = find_lcm(In.f_bsize, Out.f_bsize);
7807c478bdstevel@tonic-gate	num = _128K / (Bufsz + sizeof (int));
7817c478bdstevel@tonic-gate	Bufsz *= num;
7827c478bdstevel@tonic-gate	size = Bufsz + sizeof (int);
7837c478bdstevel@tonic-gate	/* test to see if ipc is available, the shmat should fail with EINVAL */
7847c478bdstevel@tonic-gate	(void) signal(SIGSYS, sigsys);
7857c478bdstevel@tonic-gate	errno = 0;
7867c478bdstevel@tonic-gate	if (Ipc) {
7877c478bdstevel@tonic-gate		if ((int)shmat(0, (char *)NULL, 0) < 0 && errno != EINVAL)
7887c478bdstevel@tonic-gate			Ipc = 0; /* something went wrong */
7897c478bdstevel@tonic-gate	}
7907c478bdstevel@tonic-gate	if (Ipc) { /* ipc is available */
7917c478bdstevel@tonic-gate		Bufcnt = 2;
7927c478bdstevel@tonic-gate		sem_arg.val = 0;
7937c478bdstevel@tonic-gate		for (cnt = 0; cnt < BUFCNT; cnt++) {
7947c478bdstevel@tonic-gate			errno = 0;
7957c478bdstevel@tonic-gate			if ((Sem_id[cnt] = semget(IPC_PRIVATE, 1, 0)) < 0)
7967c478bdstevel@tonic-gate				perr(1, "Error allocating semaphores: %d",
7977c478bdstevel@tonic-gate				    errno);
7987c478bdstevel@tonic-gate			if (semctl(Sem_id[cnt], 0, SETVAL, sem_arg) < 0)
7997c478bdstevel@tonic-gate				perr(1, "Error setting semaphores: %d", errno);
8007c478bdstevel@tonic-gate			if ((Shm_id[cnt] = shmget(IPC_PRIVATE, size, 0)) < 0)
8017c478bdstevel@tonic-gate				perr(1, "Error allocating shared memory: %d",
8027c478bdstevel@tonic-gate				    errno);
8037c478bdstevel@tonic-gate			if ((Buf[cnt] = shmat(Shm_id[cnt], 0, 0)) == (void *)-1)
8047c478bdstevel@tonic-gate				perr(1, "Error attaching shared memory: %d",
8057c478bdstevel@tonic-gate				    errno);
8067c478bdstevel@tonic-gate			if (shmctl(Shm_id[cnt], SHM_LOCK, 0) < 0)
8077c478bdstevel@tonic-gate				perr(0, "Error locking in shared memory: %d",
8087c478bdstevel@tonic-gate				    errno);
8097c478bdstevel@tonic-gate			Cnts[cnt] = (int *)(Buf[cnt] + Bufsz);
8107c478bdstevel@tonic-gate		}
8117c478bdstevel@tonic-gate	} else { /* ipc is not available */
8127c478bdstevel@tonic-gate		Bufcnt = 1;
8137c478bdstevel@tonic-gate		if ((Buf[0] = align(size)) == (char *)NULL)
8147c478bdstevel@tonic-gate			perr(1, "Out of memory\n");
8157c478bdstevel@tonic-gate		Cnts[0] = (int *)(Buf[0] + Bufsz);
8167c478bdstevel@tonic-gate		*Cnts[0] = 0;
8177c478bdstevel@tonic-gate	}
8187c478bdstevel@tonic-gate}
8197c478bdstevel@tonic-gate
8207c478bdstevel@tonic-gate/*
8217c478bdstevel@tonic-gate * prompt: Prompt the user for verification.
8227c478bdstevel@tonic-gate */
8237c478bdstevel@tonic-gate
8247c478bdstevel@tonic-gatestatic void
8257c478bdstevel@tonic-gateprompt(int verify, const char *fmt, ...)
8267c478bdstevel@tonic-gate{
8277c478bdstevel@tonic-gate	va_list ap;
8287c478bdstevel@tonic-gate
8297c478bdstevel@tonic-gate	va_start(ap, fmt);
8307c478bdstevel@tonic-gate	if (fmt != NULL)
8317c478bdstevel@tonic-gate		(void) vfprintf(stdout, gettext(fmt), ap);
8327c478bdstevel@tonic-gate	va_end(ap);
8337c478bdstevel@tonic-gate	if (verify) {
8347c478bdstevel@tonic-gate		(void) fprintf(stdout, gettext("Type 'y' to override:    "));
8357c478bdstevel@tonic-gate		if (!ask("")) {
8367c478bdstevel@tonic-gate			cleanup();
8377c478bdstevel@tonic-gate			exit(31+9);
8387c478bdstevel@tonic-gate		}
8397c478bdstevel@tonic-gate	}
8407c478bdstevel@tonic-gate}
8417c478bdstevel@tonic-gate
8427c478bdstevel@tonic-gate/*
8437c478bdstevel@tonic-gate * ask: Ask the user a question and get the answer.
8447c478bdstevel@tonic-gate */
8457c478bdstevel@tonic-gate
846d1a180bmaheshvsint
847d1a180bmaheshvsask(char *s)
8487c478bdstevel@tonic-gate{
8497c478bdstevel@tonic-gate	char ans[12];
8507c478bdstevel@tonic-gate
8517c478bdstevel@tonic-gate	(void) printf(gettext(s));
8527c478bdstevel@tonic-gate	if (Yesflg) {
8537c478bdstevel@tonic-gate		(void) printf(gettext("YES\n"));
8547c478bdstevel@tonic-gate		return (1);
8557c478bdstevel@tonic-gate	}
8567c478bdstevel@tonic-gate	ans[0] = '\0';
8577c478bdstevel@tonic-gate	fgets(ans, 10, Devtty);
8587c478bdstevel@tonic-gate	for (;;) {
8597c478bdstevel@tonic-gate		switch (ans[0]) {
8607c478bdstevel@tonic-gate			case 'a':
8617c478bdstevel@tonic-gate			case 'A':
8627c478bdstevel@tonic-gate				if (Pid > 0) /* parent with a child */
8637c478bdstevel@tonic-gate					kill(Pid, 9);
8647c478bdstevel@tonic-gate				cleanup();
8657c478bdstevel@tonic-gate				exit(31+1);
8667c478bdstevel@tonic-gate			case 'y':
8677c478bdstevel@tonic-gate			case 'Y':
8687c478bdstevel@tonic-gate				return (1);
8697c478bdstevel@tonic-gate			case 'n':
8707c478bdstevel@tonic-gate			case 'N':
8717c478bdstevel@tonic-gate				return (0);
8727c478bdstevel@tonic-gate			default:
8737c478bdstevel@tonic-gate				(void) printf(gettext("\n(y or n)?"));
8747c478bdstevel@tonic-gate				fgets(ans, 10, Devtty);
8757c478bdstevel@tonic-gate		}
8767c478bdstevel@tonic-gate	}
8777c478bdstevel@tonic-gate}
8787c478bdstevel@tonic-gate
8797c478bdstevel@tonic-gate/*
8807c478bdstevel@tonic-gate * align: Align a malloc'd memory section on a page boundry.
8817c478bdstevel@tonic-gate */
8827c478bdstevel@tonic-gate
8837c478bdstevel@tonic-gatechar *
884d1a180bmaheshvsalign(int size)
8857c478bdstevel@tonic-gate{
886d1a180bmaheshvs	int pad;
8877c478bdstevel@tonic-gate
8887c478bdstevel@tonic-gate	if ((pad = ((int)malloc(0) & (PAGESIZE-1))) > 0) {
8897c478bdstevel@tonic-gate		pad = PAGESIZE - pad;
8907c478bdstevel@tonic-gate		if (malloc(pad) == (char *)NULL)
8917c478bdstevel@tonic-gate			return ((char *)NULL);
8927c478bdstevel@tonic-gate	}
8937c478bdstevel@tonic-gate	return (malloc((unsigned)size));
8947c478bdstevel@tonic-gate}
8957c478bdstevel@tonic-gate
8967c478bdstevel@tonic-gate/*
8977c478bdstevel@tonic-gate * child_copy:  Using IPC, this child process reads from shared memory
8987c478bdstevel@tonic-gate * and writes to the destination file system.
8997c478bdstevel@tonic-gate */
9007c478bdstevel@tonic-gate
9017c478bdstevel@tonic-gateint
9027c478bdstevel@tonic-gatechild_copy()
9037c478bdstevel@tonic-gate{
904d1a180bmaheshvs	int rv, cur_buf, left, have, tpcnt;
905d1a180bmaheshvs	char *c_p;
9067c478bdstevel@tonic-gate
9077c478bdstevel@tonic-gate	(void) signal(SIGINT, SIG_IGN);
9087c478bdstevel@tonic-gate	Sem_buf.sem_op = -1;
9097c478bdstevel@tonic-gate	(void) close(In.f_des);
9107c478bdstevel@tonic-gate	if (Otape && !Eomflg)
9117c478bdstevel@tonic-gate		tpcnt = actual_blocks() * BLKSIZ;
9127c478bdstevel@tonic-gate	cur_buf = 0;
9137c478bdstevel@tonic-gate	for (;;) {
9147c478bdstevel@tonic-gate		if (semop(Sem_id[cur_buf], &Sem_buf, 1) < 0)
9157c478bdstevel@tonic-gate			perr(1, "semaphore operation error %d\n", errno);
9167c478bdstevel@tonic-gate		left = *Cnts[cur_buf];
9177c478bdstevel@tonic-gate		if (!left)
9187c478bdstevel@tonic-gate			break;
9197c478bdstevel@tonic-gate		c_p = Buf[cur_buf];
9207c478bdstevel@tonic-gate		rv = 0;
9217c478bdstevel@tonic-gate		while (left) {
9227c478bdstevel@tonic-gate			have = (left < Out.f_bsize) ? left : Out.f_bsize;
9237c478bdstevel@tonic-gate			if (!Eomflg && Otape) {
9247c478bdstevel@tonic-gate				if (!tpcnt) {
9257c478bdstevel@tonic-gate					(void) chgreel(&Out, OUTPUT);
9267c478bdstevel@tonic-gate					tpcnt = actual_blocks() * BLKSIZ;
9277c478bdstevel@tonic-gate				}
9287c478bdstevel@tonic-gate				have = (tpcnt < have) ? tpcnt : have;
9297c478bdstevel@tonic-gate			}
9307c478bdstevel@tonic-gate			errno = 0;
9317c478bdstevel@tonic-gate			if ((rv = g_write(Out.f_dev, Out.f_des, c_p, have)) <
9327c478bdstevel@tonic-gate			    0) {
9337c478bdstevel@tonic-gate				if (Eomflg && errno == ENOSPC) {
9347c478bdstevel@tonic-gate					(void) chgreel(&Out, OUTPUT);
9357c478bdstevel@tonic-gate					continue;
9367c478bdstevel@tonic-gate				} else
9377c478bdstevel@tonic-gate					perr(1, "I/O error %d on write\n",
9387c478bdstevel@tonic-gate					    errno);
9397c478bdstevel@tonic-gate			}
9407c478bdstevel@tonic-gate			left -= rv;
9417c478bdstevel@tonic-gate			c_p += rv;
9427c478bdstevel@tonic-gate			V_labl.v_offset += rv;
9437c478bdstevel@tonic-gate			if (!Eomflg && Otape)
9447c478bdstevel@tonic-gate				tpcnt -= rv;
9457c478bdstevel@tonic-gate		}
9467c478bdstevel@tonic-gate		if (semop(Sem_id[cur_buf], &Sem_buf, 1) < 0)
9477c478bdstevel@tonic-gate			perr(2, "semaphore operation error %d\n", errno);
9487c478bdstevel@tonic-gate		cur_buf = (cur_buf + 1) % BUFCNT;
9497c478bdstevel@tonic-gate	}
9507c478bdstevel@tonic-gate	exit(0);
9517c478bdstevel@tonic-gate}
9527c478bdstevel@tonic-gate
9537c478bdstevel@tonic-gate/*
9547c478bdstevel@tonic-gate * parent_copy:  Using shared memory, the parent process reads fromt the
9557c478bdstevel@tonic-gate * source file system and writes to shared memory.
9567c478bdstevel@tonic-gate */
9577c478bdstevel@tonic-gate
958d1a180bmaheshvsstatic void
959d1a180bmaheshvsparent_copy(void)
9607c478bdstevel@tonic-gate{
961d1a180bmaheshvs	int rv, left, have, tpcnt, cur_buf;
962d1a180bmaheshvs	char *c_p;
9637c478bdstevel@tonic-gate	int eom = 0, xfer_cnt = Fs * BLKSIZ;
9647c478bdstevel@tonic-gate
9657c478bdstevel@tonic-gate	Sem_buf.sem_num = 0;
9667c478bdstevel@tonic-gate	Sem_buf.sem_flg = 0;
9677c478bdstevel@tonic-gate	if ((Pid = fork()) == 0)
9687c478bdstevel@tonic-gate		child_copy(); /* child does not return */
9697c478bdstevel@tonic-gate	(void) close(Out.f_des);
9707c478bdstevel@tonic-gate	Rstsem_buf.sem_num = 0;
9717c478bdstevel@tonic-gate	Rstsem_buf.sem_flg = 0;
9727c478bdstevel@tonic-gate	Rstsem_buf.sem_op = 2;
9737c478bdstevel@tonic-gate	Sem_buf.sem_op = 0;
9747c478bdstevel@tonic-gate	cur_buf = 0;
9757c478bdstevel@tonic-gate	if (Itape && !Eomflg)
9767c478bdstevel@tonic-gate		tpcnt = actual_blocks() * BLKSIZ;
9777c478bdstevel@tonic-gate	while (xfer_cnt) {
9787c478bdstevel@tonic-gate		if (semop(Sem_id[cur_buf], &Sem_buf, 1) < 0)
9797c478bdstevel@tonic-gate			perr(1, "Semaphore operation error %d\n", errno);
9807c478bdstevel@tonic-gate		c_p = Buf[cur_buf];
9817c478bdstevel@tonic-gate		left = Bufsz;
9827c478bdstevel@tonic-gate		rv = 0;
9837c478bdstevel@tonic-gate		while (left >= In.f_bsize && xfer_cnt) {
9847c478bdstevel@tonic-gate			have = (xfer_cnt < In.f_bsize) ? xfer_cnt : In.f_bsize;
9857c478bdstevel@tonic-gate			if (!Eomflg && Itape) {
9867c478bdstevel@tonic-gate				if (!tpcnt) {
9877c478bdstevel@tonic-gate					*Cnts[cur_buf] = Bufsz - left;
9887c478bdstevel@tonic-gate					(void) flush_bufs(cur_buf);
9897c478bdstevel@tonic-gate					(void) chgreel(&In, INPUT);
9907c478bdstevel@tonic-gate					tpcnt = actual_blocks() * BLKSIZ;
9917c478bdstevel@tonic-gate					cur_buf = (cur_buf == 0) ? 1 : 0;
9927c478bdstevel@tonic-gate					eom = 1;
9937c478bdstevel@tonic-gate					break;
9947c478bdstevel@tonic-gate				}
9957c478bdstevel@tonic-gate				have = (tpcnt < have) ? tpcnt : have;
9967c478bdstevel@tonic-gate			}
9977c478bdstevel@tonic-gate			errno = 0;
9987c478bdstevel@tonic-gate			if ((rv = g_read(In.f_dev, In.f_des, c_p, have)) < 0) {
9997c478bdstevel@tonic-gate				if (Eomflg && errno == ENOSPC) {
10007c478bdstevel@tonic-gate					*Cnts[cur_buf] = Bufsz - left;
10017c478bdstevel@tonic-gate					(void) flush_bufs(cur_buf);
10027c478bdstevel@tonic-gate					(void) chgreel(&In, INPUT);
10037c478bdstevel@tonic-gate					cur_buf = (cur_buf == 0) ? 1 : 0;
10047c478bdstevel@tonic-gate					eom = 1;
10057c478bdstevel@tonic-gate					break;
10067c478bdstevel@tonic-gate				} else
10077c478bdstevel@tonic-gate					perr(1, "I/O error %d on read\n",
10087c478bdstevel@tonic-gate					    errno);
10097c478bdstevel@tonic-gate			}
10107c478bdstevel@tonic-gate			left -= rv;
10117c478bdstevel@tonic-gate			c_p += rv;
10127c478bdstevel@tonic-gate			xfer_cnt -= rv;
10137c478bdstevel@tonic-gate			if (!Eomflg && Itape)
10147c478bdstevel@tonic-gate				tpcnt -= rv;
10157c478bdstevel@tonic-gate		}
10167c478bdstevel@tonic-gate		if (eom > 0) {
10177c478bdstevel@tonic-gate			eom = 0;
10187c478bdstevel@tonic-gate			if (Eomflg)
10197c478bdstevel@tonic-gate				xfer_cnt -= rv;
10207c478bdstevel@tonic-gate			else if (Itape)
10217c478bdstevel@tonic-gate				tpcnt -= rv;
10227c478bdstevel@tonic-gate			continue;
10237c478bdstevel@tonic-gate		}
10247c478bdstevel@tonic-gate		*Cnts[cur_buf] = Bufsz - left;
10257c478bdstevel@tonic-gate		Blocks += *Cnts[cur_buf];
10267c478bdstevel@tonic-gate		if (semop(Sem_id[cur_buf], &Rstsem_buf, 1) < 0)
10277c478bdstevel@tonic-gate			perr(2, "Semaphore operation error %d\n", errno);
10287c478bdstevel@tonic-gate		cur_buf = (cur_buf == 0) ? 1 : 0;
10297c478bdstevel@tonic-gate	}
10307c478bdstevel@tonic-gate	if (semop(Sem_id[cur_buf], &Sem_buf, 1) < 0)
10317c478bdstevel@tonic-gate		perr(3, "Semaphore operation error %d\n", errno);
10327c478bdstevel@tonic-gate	*Cnts[cur_buf] = 0;
10337c478bdstevel@tonic-gate	if (semop(Sem_id[cur_buf], &Rstsem_buf, 1) < 0)
10347c478bdstevel@tonic-gate		perr(4, "Semaphore operation error %d\n", errno);
10357c478bdstevel@tonic-gate	wait((int *)NULL);
10367c478bdstevel@tonic-gate	Blocks /= BLKSIZ;
10377c478bdstevel@tonic-gate}
10387c478bdstevel@tonic-gate
10397c478bdstevel@tonic-gate/*
10407c478bdstevel@tonic-gate * copy:  Copy without shared memory.  The process reads from the source
10417c478bdstevel@tonic-gate * filesystem and writes to the destination filesystem.
10427c478bdstevel@tonic-gate */
10437c478bdstevel@tonic-gate
1044d1a180bmaheshvsstatic void
1045d1a180bmaheshvscopy(void)
10467c478bdstevel@tonic-gate{
1047d1a180bmaheshvs	int rv, left, have, tpcnt = 1, xfer_cnt = Fs * BLKSIZ;
1048d1a180bmaheshvs	char *c_p;
10497c478bdstevel@tonic-gate
10507c478bdstevel@tonic-gate	if ((Itape || Otape) && !Eomflg)
10517c478bdstevel@tonic-gate		tpcnt = actual_blocks() * BLKSIZ;
10527c478bdstevel@tonic-gate	while (xfer_cnt) {
10537c478bdstevel@tonic-gate		c_p = (char *)(Buf[0] + *Cnts[0]);
10547c478bdstevel@tonic-gate		left = Bufsz - *Cnts[0];
10557c478bdstevel@tonic-gate		rv = 0;
10567c478bdstevel@tonic-gate		while (left >= In.f_bsize && xfer_cnt) {
10577c478bdstevel@tonic-gate			have = (xfer_cnt < In.f_bsize) ? xfer_cnt : In.f_bsize;
10587c478bdstevel@tonic-gate			if (!Eomflg && Itape) {
10597c478bdstevel@tonic-gate				if (!tpcnt) {
10607c478bdstevel@tonic-gate					*Cnts[0] = Bufsz - left;
10617c478bdstevel@tonic-gate					(void) chgreel(&In, INPUT);
10627c478bdstevel@tonic-gate					tpcnt = actual_blocks() * BLKSIZ;
10637c478bdstevel@tonic-gate					break;
10647c478bdstevel@tonic-gate				}
10657c478bdstevel@tonic-gate				have = (tpcnt < have) ? tpcnt : have;
10667c478bdstevel@tonic-gate			}
10677c478bdstevel@tonic-gate			errno = 0;
10687c478bdstevel@tonic-gate			if ((rv = g_read(In.f_dev, In.f_des, c_p, have)) < 0) {
10697c478bdstevel@tonic-gate				if (Eomflg && errno == ENOSPC) {
10707c478bdstevel@tonic-gate					(void) chgreel(&In, INPUT);
10717c478bdstevel@tonic-gate					break;
10727c478bdstevel@tonic-gate				} else
10737c478bdstevel@tonic-gate					perr(1, "I/O error %d on read\n",
10747c478bdstevel@tonic-gate					    errno);
10757c478bdstevel@tonic-gate			}
10767c478bdstevel@tonic-gate			left -= rv;
10777c478bdstevel@tonic-gate			c_p += rv;
10787c478bdstevel@tonic-gate			xfer_cnt -= rv;
10797c478bdstevel@tonic-gate			if (!Eomflg && Itape)
10807c478bdstevel@tonic-gate				tpcnt -= rv;
10817c478bdstevel@tonic-gate		} /* left >= In.f_bsize && xfer_cnt */
10827c478bdstevel@tonic-gate		*Cnts[0] = Bufsz - left;
10837c478bdstevel@tonic-gate		Blocks += *Cnts[0];
10847c478bdstevel@tonic-gate		c_p = Buf[0];
10857c478bdstevel@tonic-gate		left = *Cnts[0];
10867c478bdstevel@tonic-gate		rv = 0;
10877c478bdstevel@tonic-gate		while (left >= Out.f_bsize || (left > 0 && !xfer_cnt)) {
10887c478bdstevel@tonic-gate			have = (left < Out.f_bsize) ? left : Out.f_bsize;
10897c478bdstevel@tonic-gate			if (!Eomflg && Otape) {
10907c478bdstevel@tonic-gate				if (!tpcnt) {
10917c478bdstevel@tonic-gate					(void) chgreel(&Out, OUTPUT);
10927c478bdstevel@tonic-gate					tpcnt = actual_blocks() * BLKSIZ;
10937c478bdstevel@tonic-gate				}
10947c478bdstevel@tonic-gate				have = (tpcnt < have) ? tpcnt : have;
10957c478bdstevel@tonic-gate			}
10967c478bdstevel@tonic-gate			errno = 0;
10977c478bdstevel@tonic-gate			if ((rv = g_write(Out.f_dev, Out.f_des, c_p, have)) <
10987c478bdstevel@tonic-gate			    0) {
10997c478bdstevel@tonic-gate				if (Eomflg && errno == ENOSPC) {
11007c478bdstevel@tonic-gate					(void) chgreel(&Out, OUTPUT);
11017c478bdstevel@tonic-gate					continue;
11027c478bdstevel@tonic-gate				} else
11037c478bdstevel@tonic-gate					perr(1, "I/O error %d on write\n",
11047c478bdstevel@tonic-gate					    errno);
11057c478bdstevel@tonic-gate			}
11067c478bdstevel@tonic-gate			left -= rv;
11077c478bdstevel@tonic-gate			c_p += rv;
11087c478bdstevel@tonic-gate			V_labl.v_offset += rv;
11097c478bdstevel@tonic-gate			if (!Eomflg && Otape)
11107c478bdstevel@tonic-gate				tpcnt -= rv;
11117c478bdstevel@tonic-gate		} /* left >= Out.f_bsize */
11127c478bdstevel@tonic-gate		if (left) {
11137c478bdstevel@tonic-gate			(void) memcpy(Buf[0], c_p, left);
11147c478bdstevel@tonic-gate			Blocks -= left;
11157c478bdstevel@tonic-gate		}
11167c478bdstevel@tonic-gate		*Cnts[0] = left;
11177c478bdstevel@tonic-gate	} /* xfer_cnt */
11187c478bdstevel@tonic-gate	Blocks /= BLKSIZ;
11197c478bdstevel@tonic-gate}
11207c478bdstevel@tonic-gate
11217c478bdstevel@tonic-gate/*
11227c478bdstevel@tonic-gate * flush_bufs:  Permit child to read the remaining data from the
11237c478bdstevel@tonic-gate * buffer before prompting user for end-of-media.
11247c478bdstevel@tonic-gate */
11257c478bdstevel@tonic-gate
1126d1a180bmaheshvsstatic void
1127d1a180bmaheshvsflush_bufs(int buffer)
11287c478bdstevel@tonic-gate{
11297c478bdstevel@tonic-gate
11307c478bdstevel@tonic-gate	Blocks += *Cnts[buffer];
11317c478bdstevel@tonic-gate	if (semop(Sem_id[buffer], &Rstsem_buf, 1) < 0)
11327c478bdstevel@tonic-gate		perr(5, "Semaphore operation error %d\n", errno);
11337c478bdstevel@tonic-gate	if (semop(Sem_id[buffer], &Sem_buf, 1) < 0)
11347c478bdstevel@tonic-gate		perr(6, "Semaphore operation error %d\n", errno);
11357c478bdstevel@tonic-gate}
11367c478bdstevel@tonic-gate
11377c478bdstevel@tonic-gate/*
11387c478bdstevel@tonic-gate * cleanup:  Clean up shared memory and semaphore resources.
11397c478bdstevel@tonic-gate */
11407c478bdstevel@tonic-gate
1141d1a180bmaheshvsstatic void
1142d1a180bmaheshvscleanup(void)
11437c478bdstevel@tonic-gate{
1144d1a180bmaheshvs	int cnt;
11457c478bdstevel@tonic-gate
11467c478bdstevel@tonic-gate	if (Ipc) {
11477c478bdstevel@tonic-gate		for (cnt = 0; cnt < BUFCNT; cnt++) {
11487c478bdstevel@tonic-gate			(void) semctl(Sem_id[cnt], IPC_RMID, 0);
11497c478bdstevel@tonic-gate			(void) shmctl(Shm_id[cnt], IPC_RMID, 0);
11507c478bdstevel@tonic-gate		}
11517c478bdstevel@tonic-gate	}
11527c478bdstevel@tonic-gate}
11537c478bdstevel@tonic-gate
11547c478bdstevel@tonic-gate/*
11557c478bdstevel@tonic-gate * find_lcm: Find the lowest common multiple of two numbers.  This is used
11567c478bdstevel@tonic-gate * to determine the buffer size that should be malloc(3)'d such that the
11577c478bdstevel@tonic-gate * input and output data blocks can both fit evenly into the buffer.
11587c478bdstevel@tonic-gate */
11597c478bdstevel@tonic-gate
11607c478bdstevel@tonic-gateint
1161d1a180bmaheshvsfind_lcm(int sz1, int sz2)
11627c478bdstevel@tonic-gate{
1163d1a180bmaheshvs	int inc, lcm, small;
11647c478bdstevel@tonic-gate
11657c478bdstevel@tonic-gate	if (sz1 < sz2) {
11667c478bdstevel@tonic-gate		lcm = inc = sz2;
11677c478bdstevel@tonic-gate		small = sz1;
11687c478bdstevel@tonic-gate	} else { /* sz1 >= sz2 */
11697c478bdstevel@tonic-gate		lcm = inc = sz1;
11707c478bdstevel@tonic-gate		small = sz2;
11717c478bdstevel@tonic-gate	}
11727c478bdstevel@tonic-gate	while (lcm % small != 0)
11737c478bdstevel@tonic-gate		lcm += inc;
11747c478bdstevel@tonic-gate	return (lcm);
11757c478bdstevel@tonic-gate}
11767c478bdstevel@tonic-gate
11777c478bdstevel@tonic-gate/*
11787c478bdstevel@tonic-gate * Determine bpi information from drive names.
11797c478bdstevel@tonic-gate */
11807c478bdstevel@tonic-gate
1181d1a180bmaheshvsint
1182d1a180bmaheshvsgetbpi(char *inp)
11837c478bdstevel@tonic-gate{
11847c478bdstevel@tonic-gate
11857c478bdstevel@tonic-gate/*
11867c478bdstevel@tonic-gate * Kludge to recognize Accellerated Tape Controller usage from
11877c478bdstevel@tonic-gate * letter 'a' or 'A' following density given by user.
11887c478bdstevel@tonic-gate *
11897c478bdstevel@tonic-gate * Kludge to recognize 3B15 Compatibility Mode from
11907c478bdstevel@tonic-gate * letter 'c' or 'C' following density given by user.
11917c478bdstevel@tonic-gate */
11927c478bdstevel@tonic-gate	if (M3b15) {
11937c478bdstevel@tonic-gate		if (inp[4] == 'a' || inp[4] == 'A') {
11947c478bdstevel@tonic-gate			Drive_typ = A_DRIVE;
11957c478bdstevel@tonic-gate			inp[4] = '\0';
11967c478bdstevel@tonic-gate		}
11977c478bdstevel@tonic-gate		if (inp[4] == 'c' || inp[4] == 'C') {
11987c478bdstevel@tonic-gate			Drive_typ = C_DRIVE;
11997c478bdstevel@tonic-gate			inp[4] = '\0';
12007c478bdstevel@tonic-gate		}
12017c478bdstevel@tonic-gate	}
12027c478bdstevel@tonic-gate	return (atoi(inp));
12037c478bdstevel@tonic-gate}
12047c478bdstevel@tonic-gate
12057c478bdstevel@tonic-gate/*
12067c478bdstevel@tonic-gate * blks_per_ft:  Determine the number of blocks per foot of tape.
12077c478bdstevel@tonic-gate * Inter-block gap (dgap) is 0.3 in.
12087c478bdstevel@tonic-gate */
12097c478bdstevel@tonic-gate
12107c478bdstevel@tonic-gateint
1211d1a180bmaheshvsblks_per_ft(double disc)
12127c478bdstevel@tonic-gate{
1213d1a180bmaheshvs	double dcnt = Blk_cnt, dBpi = Bpi, dsiz = BLKSIZ, dgap = 0.3;
12147c478bdstevel@tonic-gate
12157c478bdstevel@tonic-gate	return ((int)(dcnt / (((dcnt * dsiz / dBpi) + dgap) / 12.0) * disc));
12167c478bdstevel@tonic-gate}
12177c478bdstevel@tonic-gate
12187c478bdstevel@tonic-gate/*
12197c478bdstevel@tonic-gate * tapeck:  Arbitrary block size.  Determine the number of physical blocks per
12207c478bdstevel@tonic-gate * foot of tape, including the inter-block gap, and the possibility of a short
12217c478bdstevel@tonic-gate * tape.  Assume the usable portion of a tape is 85% of its length for small
12227c478bdstevel@tonic-gate * block sizes and 88% for large block sizes.
12237c478bdstevel@tonic-gate */
12247c478bdstevel@tonic-gate
1225d1a180bmaheshvsint
1226d1a180bmaheshvstapeck(struct file_info *f_p, int dir)
12277c478bdstevel@tonic-gate{
1228d1a180bmaheshvs	int again = 1, verify, old_style, new_style;
12297c478bdstevel@tonic-gate	char resp[16];
12307c478bdstevel@tonic-gate
12317c478bdstevel@tonic-gate	errno = 0;
12327c478bdstevel@tonic-gate	if ((f_p->f_bsize = g_init(&f_p->f_dev, &f_p->f_des)) < 0)
12337c478bdstevel@tonic-gate		perr(1, "volcopy: Error %d during initialization\n", errno);
12347c478bdstevel@tonic-gate	if ((f_p->f_dev != G_TM_TAPE) && (f_p->f_dev != G_XT_TAPE) &&
12357c478bdstevel@tonic-gate	    (f_p->f_dev != G_ST_TAPE))
12367c478bdstevel@tonic-gate		return (0);
12377c478bdstevel@tonic-gate	V_labl.v_magic[0] = '\0';	/* scribble on old data */
12387c478bdstevel@tonic-gate	alarm(5);
12397c478bdstevel@tonic-gate	if (g_read(f_p->f_dev, f_p->f_des, &V_labl, sizeof (V_labl)) <= 0) {
12407c478bdstevel@tonic-gate		if (dir == INPUT)
12417c478bdstevel@tonic-gate			perror("input tape");
12427c478bdstevel@tonic-gate		else
12437c478bdstevel@tonic-gate			perror("output tape");
12447c478bdstevel@tonic-gate	}
12457c478bdstevel@tonic-gate	alarm(0);
12468509e9cToomas Soome	if (V_labl.v_reel == '\0' && dir == INPUT)
12477c478bdstevel@tonic-gate		perr(9, "Input tape is empty\n");
12487c478bdstevel@tonic-gate	else {
12497c478bdstevel@tonic-gate		old_style = strncmp(V_labl.v_magic, "Volcopy", 7) == 0;
12507c478bdstevel@tonic-gate		new_style = strncmp(V_labl.v_magic, "VOLCOPY", 7) == 0;
12517c478bdstevel@tonic-gate		if (!old_style && !new_style) {
12527c478bdstevel@tonic-gate			verify = (dir == INPUT) ? 0 : 1;
12537c478bdstevel@tonic-gate			prompt(verify, "Not a labeled tape\n");
12547c478bdstevel@tonic-gate			if (dir == INPUT)
12557c478bdstevel@tonic-gate				perr(10, "Input tape not made by volcopy\n");
12567c478bdstevel@tonic-gate			mklabel();
12577c478bdstevel@tonic-gate			strncpy(V_labl.v_volume, f_p->f_vol_p, 6);
12587c478bdstevel@tonic-gate			Osup.fs_time = 0;
12597c478bdstevel@tonic-gate		} else if (new_style) {
12607c478bdstevel@tonic-gate			Eomflg = (dir == INPUT) ? 1 : Eomflg;
12617c478bdstevel@tonic-gate			if (!Eomflg)
12627c478bdstevel@tonic-gate				strncpy(V_labl.v_magic, "Volcopy", 7);
12637c478bdstevel@tonic-gate		}
12647c478bdstevel@tonic-gate	}
12657c478bdstevel@tonic-gate	if (*f_p->f_vol_p == '-')
12667c478bdstevel@tonic-gate		strncpy(f_p->f_vol_p, V_labl.v_volume, 6);
12677c478bdstevel@tonic-gate	else if (NOT_EQ(V_labl.v_volume, f_p->f_vol_p, 6)) {
12687c478bdstevel@tonic-gate		prompt(1, "Header volume(%.6s) does not match (%s)\n",
12697c478bdstevel@tonic-gate			V_labl.v_volume, f_p->f_vol_p);
12707c478bdstevel@tonic-gate		strncpy(V_labl.v_volume, f_p->f_vol_p, 6);
12717c478bdstevel@tonic-gate	}
12727c478bdstevel@tonic-gate	if (dir == INPUT) {
12737c478bdstevel@tonic-gate		Bpi = V_labl.v_dens;
12747c478bdstevel@tonic-gate		if (!Eomflg) {
12757c478bdstevel@tonic-gate			R_len = V_labl.v_length;
12767c478bdstevel@tonic-gate			R_num = V_labl.v_reels;
12777c478bdstevel@tonic-gate		}
12787c478bdstevel@tonic-gate		if (M3b15) {
12797c478bdstevel@tonic-gate			if (V_labl.v_type == T_TYPE) {
12807c478bdstevel@tonic-gate				if (V_labl.v_nblocks == 0) {
12817c478bdstevel@tonic-gate					Blk_cnt = 10;
12827c478bdstevel@tonic-gate					Drive_typ = C_DRIVE;
12837c478bdstevel@tonic-gate				} else
12847c478bdstevel@tonic-gate					Blk_cnt = V_labl.v_nblocks;
12857c478bdstevel@tonic-gate				if (V_labl.v_nblocks == 32)
12867c478bdstevel@tonic-gate					Drive_typ = A_DRIVE;
12877c478bdstevel@tonic-gate				else
12887c478bdstevel@tonic-gate					Drive_typ = 0;
12897c478bdstevel@tonic-gate			} else {
12907c478bdstevel@tonic-gate				Drive_typ = 0;
12917c478bdstevel@tonic-gate				Blk_cnt = 10;
12927c478bdstevel@tonic-gate				Drive_typ = C_DRIVE;
12937c478bdstevel@tonic-gate			}
12947c478bdstevel@tonic-gate		}
12957c478bdstevel@tonic-gate	}
12967c478bdstevel@tonic-gate	while (!Eomflg && (R_len <= 0 || R_len > 3600)) {
12977c478bdstevel@tonic-gate		(void) printf(gettext(
12987c478bdstevel@tonic-gate			"Enter size of reel in feet for <%s>:   "),
12997c478bdstevel@tonic-gate				f_p->f_vol_p);
13007c478bdstevel@tonic-gate		fgets(resp, 10, Devtty);
13017c478bdstevel@tonic-gate		R_len = atoi(resp);
13027c478bdstevel@tonic-gate		if (R_len > 0 && R_len <= 3600)
13037c478bdstevel@tonic-gate			break;
13047c478bdstevel@tonic-gate		perr(0, "Size of reel must be > 0, <= 3600\n");
13057c478bdstevel@tonic-gate	}
13067c478bdstevel@tonic-gate	while (!Eomflg && again) {
13077c478bdstevel@tonic-gate		again = 0;
13087c478bdstevel@tonic-gate		if (!Bpi) {
13097c478bdstevel@tonic-gate			(void) printf(gettext(
13107c478bdstevel@tonic-gate				"Tape density? (i.e., 800 | 1600 | 6250)?   "));
13117c478bdstevel@tonic-gate			fgets(resp, 10, Devtty);
13127c478bdstevel@tonic-gate			Bpi = getbpi(resp);
13137c478bdstevel@tonic-gate		}
13147c478bdstevel@tonic-gate		switch (Bpi) {
13157c478bdstevel@tonic-gate		case 800:
13167c478bdstevel@tonic-gate			R_blks = Ft800x10 * R_len;
13177c478bdstevel@tonic-gate			break;
13187c478bdstevel@tonic-gate		case 1600:
13197c478bdstevel@tonic-gate			if (M3b15) {
13207c478bdstevel@tonic-gate				switch (Blk_cnt) {
13217c478bdstevel@tonic-gate				case 1: /* Writing a new tape */
13227c478bdstevel@tonic-gate					if (Drive_typ == A_DRIVE)
13237c478bdstevel@tonic-gate						R_blks = Ft1600x32 * R_len;
13247c478bdstevel@tonic-gate					else if (Drive_typ == C_DRIVE)
13257c478bdstevel@tonic-gate						R_blks = Ft1600x10 * R_len;
13267c478bdstevel@tonic-gate					else
13277c478bdstevel@tonic-gate						R_blks = Ft1600x16 * R_len;
13287c478bdstevel@tonic-gate					break;
13297c478bdstevel@tonic-gate				case 10:
13307c478bdstevel@tonic-gate					R_blks = Ft1600x10 * R_len;
13317c478bdstevel@tonic-gate					break;
13327c478bdstevel@tonic-gate				case 16:
13337c478bdstevel@tonic-gate					R_blks = Ft1600x16 * R_len;
13347c478bdstevel@tonic-gate					break;
13357c478bdstevel@tonic-gate				case 32:
13367c478bdstevel@tonic-gate					R_blks = Ft1600x32 * R_len;
13377c478bdstevel@tonic-gate					break;
13387c478bdstevel@tonic-gate				default:
13397c478bdstevel@tonic-gate					if (Blk_cnt < 32)
13407c478bdstevel@tonic-gate						R_blks = blks_per_ft(0.85);
13417c478bdstevel@tonic-gate					else
13427c478bdstevel@tonic-gate						R_blks = blks_per_ft(0.88);
13437c478bdstevel@tonic-gate					R_blks *= R_len;
13447c478bdstevel@tonic-gate				} /* Blk_cnt */
13457c478bdstevel@tonic-gate			} else
13467c478bdstevel@tonic-gate				R_blks = Ft1600x10 * R_len;
13477c478bdstevel@tonic-gate			break;
13487c478bdstevel@tonic-gate		case 6250:
13497c478bdstevel@tonic-gate			if (M3b15) {
13507c478bdstevel@tonic-gate				switch (Blk_cnt) {
13517c478bdstevel@tonic-gate				case 1: /* Writing a new tape */
13527c478bdstevel@tonic-gate					if (Drive_typ == A_DRIVE)
13537c478bdstevel@tonic-gate						R_blks = Ft6250x32 * R_len;
13547c478bdstevel@tonic-gate					else if (Drive_typ == C_DRIVE)
13557c478bdstevel@tonic-gate						R_blks = Ft6250x10 * R_len;
13567c478bdstevel@tonic-gate					else
13577c478bdstevel@tonic-gate						R_blks = Ft6250x16 * R_len;
13587c478bdstevel@tonic-gate					break;
13597c478bdstevel@tonic-gate				case 10:
13607c478bdstevel@tonic-gate					R_blks = Ft6250x10 * R_len;
13617c478bdstevel@tonic-gate					break;
13627c478bdstevel@tonic-gate				case 16:
13637c478bdstevel@tonic-gate					R_blks = Ft6250x16 * R_len;
13647c478bdstevel@tonic-gate					break;
13657c478bdstevel@tonic-gate				case 32:
13667c478bdstevel@tonic-gate					R_blks = Ft6250x32 * R_len;
13677c478bdstevel@tonic-gate					break;
13687c478bdstevel@tonic-gate				default:
13697c478bdstevel@tonic-gate					if (Blk_cnt < 32)
13707c478bdstevel@tonic-gate						R_blks = blks_per_ft(0.85);
13717c478bdstevel@tonic-gate					else
13727c478bdstevel@tonic-gate						R_blks = blks_per_ft(0.88);
13737c478bdstevel@tonic-gate					R_blks *= R_len;
13747c478bdstevel@tonic-gate				}
13757c478bdstevel@tonic-gate			} else
13767c478bdstevel@tonic-gate				R_blks = Ft6250x50 * R_len;
13777c478bdstevel@tonic-gate			break;
13787c478bdstevel@tonic-gate		default:
13797c478bdstevel@tonic-gate			perr(0, "Bpi must be 800, 1600, or 6250\n");
13807c478bdstevel@tonic-gate			Bpi = 0;
13817c478bdstevel@tonic-gate			again = 1;
13827c478bdstevel@tonic-gate		} /* Bpi */
13837c478bdstevel@tonic-gate	} /* again */
13847c478bdstevel@tonic-gate	(void) printf(gettext("\nReel %.6s"), V_labl.v_volume);
13857c478bdstevel@tonic-gate	if (!Eomflg) {
13867c478bdstevel@tonic-gate		V_labl.v_length = R_len;
13877c478bdstevel@tonic-gate		V_labl.v_dens = Bpi;
13887c478bdstevel@tonic-gate		(void) printf(gettext(", %d feet, %d BPI\n"), R_len, Bpi);
13897c478bdstevel@tonic-gate	} else
13907c478bdstevel@tonic-gate		(void) printf(gettext(", ? feet\n"));
13917c478bdstevel@tonic-gate	return (1);
13927c478bdstevel@tonic-gate}
13937c478bdstevel@tonic-gate
13947c478bdstevel@tonic-gate/*
13957c478bdstevel@tonic-gate * hdrck:  Look for and validate a volcopy style tape label.
13967c478bdstevel@tonic-gate */
13977c478bdstevel@tonic-gate
1398d1a180bmaheshvsint
1399d1a180bmaheshvshdrck(int dev, int fd, char *tvol)
14007c478bdstevel@tonic-gate{
1401d1a180bmaheshvs	int verify;
14027c478bdstevel@tonic-gate	struct volcopy_label tlabl;
14037c478bdstevel@tonic-gate
14047c478bdstevel@tonic-gate	alarm(15); /* don't scan whole tape for label */
14057c478bdstevel@tonic-gate	errno = 0;
14067c478bdstevel@tonic-gate	if (g_read(dev, fd, &tlabl, sizeof (tlabl)) != sizeof (tlabl)) {
14077c478bdstevel@tonic-gate		alarm(0);
14087c478bdstevel@tonic-gate		verify = Otape;
14097c478bdstevel@tonic-gate		prompt(verify, "Cannot read header\n");
14107c478bdstevel@tonic-gate		if (Itape)
14117c478bdstevel@tonic-gate			close(fd);
14127c478bdstevel@tonic-gate		else
14137c478bdstevel@tonic-gate			strncpy(V_labl.v_volume, tvol, 6);
14147c478bdstevel@tonic-gate		return (verify);
14157c478bdstevel@tonic-gate	}
14167c478bdstevel@tonic-gate	alarm(0);
14177c478bdstevel@tonic-gate	V_labl.v_reel = tlabl.v_reel;
14187c478bdstevel@tonic-gate	if (NOT_EQ(tlabl.v_volume, tvol, 6)) {
14197c478bdstevel@tonic-gate		perr(0, "Volume is <%.6s>, not <%s>.\n", tlabl.v_volume, tvol);
14207c478bdstevel@tonic-gate		if (ask("Want to override?   ")) {
14217c478bdstevel@tonic-gate			if (Otape)
14227c478bdstevel@tonic-gate				strncpy(V_labl.v_volume, tvol, 6);
14237c478bdstevel@tonic-gate			else
14247c478bdstevel@tonic-gate				strncpy(tvol, tlabl.v_volume, 6);
14257c478bdstevel@tonic-gate			return (1);
14267c478bdstevel@tonic-gate		}
14277c478bdstevel@tonic-gate		return (0);
14287c478bdstevel@tonic-gate	}
14297c478bdstevel@tonic-gate	return (1);
14307c478bdstevel@tonic-gate}
14317c478bdstevel@tonic-gate
14327c478bdstevel@tonic-gate/*
14337c478bdstevel@tonic-gate * mklabel:  Zero out and initialize a volcopy label.
14347c478bdstevel@tonic-gate */
14357c478bdstevel@tonic-gate
1436d1a180bmaheshvsstatic void
1437d1a180bmaheshvsmklabel(void)
14387c478bdstevel@tonic-gate{
14397c478bdstevel@tonic-gate
14407c478bdstevel@tonic-gate	(void) memcpy(&V_labl, Empty, sizeof (V_labl));
14417c478bdstevel@tonic-gate	if (!Eomflg)
14427c478bdstevel@tonic-gate		(void) strcpy(V_labl.v_magic, "Volcopy");
14437c478bdstevel@tonic-gate	else
14447c478bdstevel@tonic-gate		(void) strcpy(V_labl.v_magic, "VOLCOPY");
14457c478bdstevel@tonic-gate}
14467c478bdstevel@tonic-gate
14477c478bdstevel@tonic-gate/*
14487c478bdstevel@tonic-gate * rprt:  Report activity to user.
14497c478bdstevel@tonic-gate */
14507c478bdstevel@tonic-gate
1451d1a180bmaheshvsstatic void
1452d1a180bmaheshvsrprt(void)
14537c478bdstevel@tonic-gate{
14547c478bdstevel@tonic-gate
14557c478bdstevel@tonic-gate	if (Itape)
14567c478bdstevel@tonic-gate		(void) printf(gettext("\nReading "));
14577c478bdstevel@tonic-gate	else /* Otape */
14587c478bdstevel@tonic-gate		(void) printf(gettext("\nWriting "));
14597c478bdstevel@tonic-gate	if (!Eomflg)
14607c478bdstevel@tonic-gate		(void) printf(gettext(
14617c478bdstevel@tonic-gate			"REEL %d of %d VOL = %.6s\n"),
14627c478bdstevel@tonic-gate				R_cur, R_num, In.f_vol_p);
14637c478bdstevel@tonic-gate	else
14647c478bdstevel@tonic-gate		(void) printf(gettext(
14657c478bdstevel@tonic-gate			"REEL %d of ? VOL = %.6s\n"), R_cur, In.f_vol_p);
14667c478bdstevel@tonic-gate}
14677c478bdstevel@tonic-gate
14687c478bdstevel@tonic-gate#ifdef LOG
14697c478bdstevel@tonic-gate/*
14707c478bdstevel@tonic-gate * fslog: Log current activity.
14717c478bdstevel@tonic-gate */
14727c478bdstevel@tonic-gate
1473d1a180bmaheshvsstatic int
1474d1a180bmaheshvsfslog(void)
14757c478bdstevel@tonic-gate{
14767c478bdstevel@tonic-gate	FILE *fp = NULL;
14777c478bdstevel@tonic-gate
14787c478bdstevel@tonic-gate	fp = fopen("/var/adm/filesave.log", "a");
14797c478bdstevel@tonic-gate	if (fp == NULL) {
14807c478bdstevel@tonic-gate		perr(1, "volcopy: cannot open /var/adm/filesave.log\n");
14817c478bdstevel@tonic-gate	}
14827c478bdstevel@tonic-gate
14837c478bdstevel@tonic-gate	fprintf(fp, "%s%c%.6s%c%.6s -> %s%c%.6s%c%.6s on %.24s\n",
14847c478bdstevel@tonic-gate		In.f_dev_p, ';', Ifname, ';', Ifpack, Out.f_dev_p,
14857c478bdstevel@tonic-gate		';', Ofname, ';', Ofpack, ctime(&Tvec));
14867c478bdstevel@tonic-gate	fclose(fp);
1487d1a180bmaheshvs	return (0);
14887c478bdstevel@tonic-gate}
14897c478bdstevel@tonic-gate#endif	/* LOG */
14907c478bdstevel@tonic-gate
14917c478bdstevel@tonic-gate/*
14927c478bdstevel@tonic-gate * getname:  Get device name.
14937c478bdstevel@tonic-gate */
14947c478bdstevel@tonic-gate
1495d1a180bmaheshvsvoid
1496d1a180bmaheshvsgetname(char *nam_p)
14977c478bdstevel@tonic-gate{
1498d1a180bmaheshvs	int lastchar;
14997c478bdstevel@tonic-gate	char nam_buf[21];
15007c478bdstevel@tonic-gate
15017c478bdstevel@tonic-gate	nam_buf[0] = '\0';
15027c478bdstevel@tonic-gate	(void) printf(gettext("Changing drives? (type RETURN for no,\n"));
15037c478bdstevel@tonic-gate	(void) printf(gettext("\t`/dev/rmt/??\' or `/dev/rtp/??\' for yes: "));
15047c478bdstevel@tonic-gate	fgets(nam_buf, 20, Devtty);
15057c478bdstevel@tonic-gate	nam_buf[20] = '\0';
15067c478bdstevel@tonic-gate	lastchar = strlen(nam_buf) - 1;
15077c478bdstevel@tonic-gate	if (nam_buf[lastchar] == '\n')
15087c478bdstevel@tonic-gate		nam_buf[lastchar] = '\0'; /* remove it */
15097c478bdstevel@tonic-gate	if (nam_buf[0] != '\0')
15107c478bdstevel@tonic-gate		(void) strcpy(nam_p, nam_buf);
15117c478bdstevel@tonic-gate}
15127c478bdstevel@tonic-gate
15137c478bdstevel@tonic-gate/*
15147c478bdstevel@tonic-gate * chgreel:  Change reel on end-of-media.
15157c478bdstevel@tonic-gate */
15167c478bdstevel@tonic-gate
1517d1a180bmaheshvsstatic void
1518d1a180bmaheshvschgreel(struct file_info *f_p, int dir)
15197c478bdstevel@tonic-gate{
1520d1a180bmaheshvs	int again = 1, lastchar, temp;
15217c478bdstevel@tonic-gate	char vol_tmp[11];
15227c478bdstevel@tonic-gate
15237c478bdstevel@tonic-gate	R_cur++;
15247c478bdstevel@tonic-gate	while (again) {
15257c478bdstevel@tonic-gate		again = 0;
15267c478bdstevel@tonic-gate		errno = 0;
15277c478bdstevel@tonic-gate		(void) close(f_p->f_des);
15287c478bdstevel@tonic-gate		(void) getname(f_p->f_dev_p);
15297c478bdstevel@tonic-gate		(void) printf(gettext(
15307c478bdstevel@tonic-gate			"Mount tape %d\nType volume-ID when ready:   "), R_cur);
15317c478bdstevel@tonic-gate		vol_tmp[0] = '\0';
15327c478bdstevel@tonic-gate		fgets(vol_tmp, 10, Devtty);
15337c478bdstevel@tonic-gate		vol_tmp[10] = '\0';
15347c478bdstevel@tonic-gate		lastchar = strlen(vol_tmp) - 1;
15357c478bdstevel@tonic-gate		if (vol_tmp[lastchar] == '\n')
15367c478bdstevel@tonic-gate			vol_tmp[lastchar] = '\0'; /* remove it */
15377c478bdstevel@tonic-gate		if (vol_tmp[0] != '\0') { /* if null string, use old vol-id */
15387c478bdstevel@tonic-gate			strncpy(f_p->f_vol_p, vol_tmp, 6);
15397c478bdstevel@tonic-gate			strncpy(V_labl.v_volume, vol_tmp, 6);
15407c478bdstevel@tonic-gate		}
15417c478bdstevel@tonic-gate		errno = 0;
15427c478bdstevel@tonic-gate		f_p->f_des = open(f_p->f_dev_p, 0);
15437c478bdstevel@tonic-gate		if (f_p->f_des <= 0 || f_p->f_des > 10) {
15447c478bdstevel@tonic-gate			if (dir == INPUT)
15457c478bdstevel@tonic-gate				perror("input ERR");
15467c478bdstevel@tonic-gate			else
15477c478bdstevel@tonic-gate				perror("output ERR");
15487c478bdstevel@tonic-gate		}
15497c478bdstevel@tonic-gate		errno = 0;
15507c478bdstevel@tonic-gate		if (g_init(&(f_p->f_dev), &(f_p->f_des)) < 0)
15517c478bdstevel@tonic-gate			perr(0, "Initialization error %d\n", errno);
15527c478bdstevel@tonic-gate		if ((f_p->f_dev != G_TM_TAPE) && (f_p->f_dev != G_XT_TAPE) &&
15537c478bdstevel@tonic-gate		    (f_p->f_dev != G_ST_TAPE)) {
15547c478bdstevel@tonic-gate			(void) printf(gettext(
15557c478bdstevel@tonic-gate				"\n'%s' is not a valid device"), f_p->f_dev_p);
15567c478bdstevel@tonic-gate			(void) printf(gettext(
15577c478bdstevel@tonic-gate		"\n\tenter device name `/dev/rmt/??\' or `/dev/rtp/??\' :"));
15587c478bdstevel@tonic-gate			again = 1;
15597c478bdstevel@tonic-gate			continue;
15607c478bdstevel@tonic-gate		}
15617c478bdstevel@tonic-gate		if (!hdrck(f_p->f_dev, f_p->f_des, f_p->f_vol_p)) {
15627c478bdstevel@tonic-gate			again = 1;
15637c478bdstevel@tonic-gate			continue;
15647c478bdstevel@tonic-gate		}
15657c478bdstevel@tonic-gate		switch (dir) {
15667c478bdstevel@tonic-gate		case INPUT:
15677c478bdstevel@tonic-gate			if (V_labl.v_reel != R_cur) {
15687c478bdstevel@tonic-gate				perr(0, "Need reel %d, label says reel %d\n",
15697c478bdstevel@tonic-gate				    R_cur, V_labl.v_reel);
15707c478bdstevel@tonic-gate				again = 1;
15717c478bdstevel@tonic-gate				continue;
15727c478bdstevel@tonic-gate			}
15737c478bdstevel@tonic-gate			break;
15747c478bdstevel@tonic-gate		case OUTPUT:
15757c478bdstevel@tonic-gate			V_labl.v_reel = R_cur;
15767c478bdstevel@tonic-gate			temp = V_labl.v_offset;
15777c478bdstevel@tonic-gate			V_labl.v_offset /= BUFSIZ;
15787c478bdstevel@tonic-gate			close(f_p->f_des);
15797c478bdstevel@tonic-gate			sleep(2);
15807c478bdstevel@tonic-gate			errno = 0;
15817c478bdstevel@tonic-gate			f_p->f_des = open(f_p->f_dev_p, 1);
15827c478bdstevel@tonic-gate			if (f_p->f_des <= 0 || f_p->f_des > 10)
15837c478bdstevel@tonic-gate				perror("output ERR");
15847c478bdstevel@tonic-gate			errno = 0;
15857c478bdstevel@tonic-gate			if (g_init(&(f_p->f_dev), &(f_p->f_des)) < 0)
15867c478bdstevel@tonic-gate				perr(1, "Initialization error %d\n", errno);
15877c478bdstevel@tonic-gate			errno = 0;
15887c478bdstevel@tonic-gate			if (g_write(f_p->f_dev, f_p->f_des, &V_labl,
15897c478bdstevel@tonic-gate			    sizeof (V_labl)) < 0) {
15907c478bdstevel@tonic-gate				perr(0, "Cannot re-write header -Try again!\n");
15917c478bdstevel@tonic-gate				again = 1;
15927c478bdstevel@tonic-gate				V_labl.v_offset = temp;
15937c478bdstevel@tonic-gate				continue;
15947c478bdstevel@tonic-gate			}
15957c478bdstevel@tonic-gate			V_labl.v_offset = temp;
15967c478bdstevel@tonic-gate			break;
15977c478bdstevel@tonic-gate		default:
15987c478bdstevel@tonic-gate			perr(1, "Impossible case\n");
15997c478bdstevel@tonic-gate		} /* dir */
16007c478bdstevel@tonic-gate	} /* again */
16017c478bdstevel@tonic-gate	rprt();
16027c478bdstevel@tonic-gate}
16037c478bdstevel@tonic-gate
16047c478bdstevel@tonic-gate/*
16057c478bdstevel@tonic-gate * perr:  Print error messages.
16067c478bdstevel@tonic-gate */
16077c478bdstevel@tonic-gate
16087c478bdstevel@tonic-gatestatic void
16097c478bdstevel@tonic-gateperr(int severity, const char *fmt, ...)
16107c478bdstevel@tonic-gate{
16117c478bdstevel@tonic-gate	va_list ap;
16127c478bdstevel@tonic-gate
16137c478bdstevel@tonic-gate	va_start(ap, fmt);
16147c478bdstevel@tonic-gate	(void) fflush(stdout);
16157c478bdstevel@tonic-gate	(void) fflush(stderr);
16167c478bdstevel@tonic-gate	if (severity == 10) {
16177c478bdstevel@tonic-gate		(void) vfprintf(stdout, gettext(fmt), ap);
16187c478bdstevel@tonic-gate		(void) fprintf(stdout, gettext(
16197c478bdstevel@tonic-gate			"\t%d reel(s) completed\n"), --R_cur);
16207c478bdstevel@tonic-gate		(void) fflush(stdout);
16217c478bdstevel@tonic-gate		(void) fflush(stderr);
16227c478bdstevel@tonic-gate		cleanup();
16237c478bdstevel@tonic-gate		exit(31+9);
16247c478bdstevel@tonic-gate	}
16257c478bdstevel@tonic-gate	(void) vfprintf(stderr, gettext(fmt), ap);
16267c478bdstevel@tonic-gate	(void) fflush(stderr);
16277c478bdstevel@tonic-gate	va_end(ap);
16287c478bdstevel@tonic-gate	if (severity > 0) {
16297c478bdstevel@tonic-gate		(void) cleanup();
16307c478bdstevel@tonic-gate		exit(31+severity);
16317c478bdstevel@tonic-gate	}
16327c478bdstevel@tonic-gate}
16337c478bdstevel@tonic-gate
16347c478bdstevel@tonic-gate
16357c478bdstevel@tonic-gatestatic void
1636d1a180bmaheshvsgetinfs(int dev, int fd, char *buf)
16377c478bdstevel@tonic-gate{
16387c478bdstevel@tonic-gate	int cnt;
16397c478bdstevel@tonic-gate	int i;
16407c478bdstevel@tonic-gate
16417c478bdstevel@tonic-gate	if (lseek(fd, SBLOCK * DEV_BSIZE, 0) != SBLOCK * DEV_BSIZE) {
16427c478bdstevel@tonic-gate		perr(10, "Unable to lseek on input\n");
16437c478bdstevel@tonic-gate	}
16447c478bdstevel@tonic-gate	cnt = SBSIZE/DEV_BSIZE;
16457c478bdstevel@tonic-gate	for (i = 0; i < cnt; i++) {
16467c478bdstevel@tonic-gate		if (g_read(dev, fd, (char *)buf + i*DEV_BSIZE, DEV_BSIZE)
16477c478bdstevel@tonic-gate			!= DEV_BSIZE) {
16487c478bdstevel@tonic-gate			perr(10, "Unable to read on input\n");
16497c478bdstevel@tonic-gate		}
16507c478bdstevel@tonic-gate	}
16517c478bdstevel@tonic-gate}
16527c478bdstevel@tonic-gate
16537c478bdstevel@tonic-gatestatic void
1654d1a180bmaheshvsgetoutfs(int dev, int fd, char *buf, int verify)
16557c478bdstevel@tonic-gate{
16567c478bdstevel@tonic-gate	int cnt;
16577c478bdstevel@tonic-gate	int i;
16587c478bdstevel@tonic-gate
16597c478bdstevel@tonic-gate	errno = 0;
16607c478bdstevel@tonic-gate	if (lseek(fd, SBLOCK * DEV_BSIZE, 0) != SBLOCK * DEV_BSIZE) {
16617c478bdstevel@tonic-gate		prompt(verify, "Unable to lseek on output\n", errno);
16627c478bdstevel@tonic-gate	}
16637c478bdstevel@tonic-gate	cnt = SBSIZE/DEV_BSIZE;
16647c478bdstevel@tonic-gate	for (i = 0; i < cnt; i++) {
16657c478bdstevel@tonic-gate		if (g_read(dev, fd, (char *)buf + i*DEV_BSIZE, DEV_BSIZE)
16667c478bdstevel@tonic-gate			!= DEV_BSIZE) {
16677c478bdstevel@tonic-gate			prompt(verify, "Unable to read on output\n", errno);
16687c478bdstevel@tonic-gate		}
16697c478bdstevel@tonic-gate	}
16707c478bdstevel@tonic-gate}
16717c478bdstevel@tonic-gate
16727c478bdstevel@tonic-gatestatic char *
1673d1a180bmaheshvsgetfslabel(struct fs *sb)
16747c478bdstevel@tonic-gate{
16757c478bdstevel@tonic-gate	int i;
16767c478bdstevel@tonic-gate	int blk;
16777c478bdstevel@tonic-gate
16787c478bdstevel@tonic-gate	/*
16797c478bdstevel@tonic-gate	 * is there room for label?
16807c478bdstevel@tonic-gate	 */
16817c478bdstevel@tonic-gate
16827c478bdstevel@tonic-gate	if (sb->fs_cpc <= 0)
16837c478bdstevel@tonic-gate		return (nolabel);
16847c478bdstevel@tonic-gate
16857c478bdstevel@tonic-gate	/*
16867c478bdstevel@tonic-gate	 * calculate the available blocks for each rotational position
16877c478bdstevel@tonic-gate	 */
16887c478bdstevel@tonic-gate	blk = sb->fs_spc * sb->fs_cpc / sb->fs_nspf;
16897c478bdstevel@tonic-gate	for (i = 0; i < blk; i += sb->fs_frag)
16907c478bdstevel@tonic-gate		/* void */;
16917c478bdstevel@tonic-gate	i -= sb->fs_frag;
16927c478bdstevel@tonic-gate	blk = i / sb->fs_frag;
16937c478bdstevel@tonic-gate
16947c478bdstevel@tonic-gate	return ((char *)&(fs_rotbl(sb)[blk]));
16957c478bdstevel@tonic-gate}
16967c478bdstevel@tonic-gate
16977c478bdstevel@tonic-gatestatic char *
1698d1a180bmaheshvsgetvolabel(struct fs *sb)
16997c478bdstevel@tonic-gate{
17007c478bdstevel@tonic-gate	char *p;
17017c478bdstevel@tonic-gate	int i;
17027c478bdstevel@tonic-gate
17037c478bdstevel@tonic-gate	p = getfslabel(sb);
17047c478bdstevel@tonic-gate
17057c478bdstevel@tonic-gate	if (p == nolabel || p == NULL)
17067c478bdstevel@tonic-gate		return (nolabel);
17077c478bdstevel@tonic-gate
17087c478bdstevel@tonic-gate	for (i = 0; *p && i < 6; p++, i++)
17097c478bdstevel@tonic-gate		;
17107c478bdstevel@tonic-gate	p++;
17117c478bdstevel@tonic-gate	return (p);
17127c478bdstevel@tonic-gate}
1713