Home | History | Annotate | Line # | Download | only in mt
mt.c revision 1.19
      1  1.19    lukem /*	$NetBSD: mt.c,v 1.19 1997/04/15 06:53:51 lukem Exp $	*/
      2   1.5      cgd 
      3   1.1      cgd /*
      4   1.5      cgd  * Copyright (c) 1980, 1993
      5   1.5      cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1      cgd  * modification, are permitted provided that the following conditions
      9   1.1      cgd  * are met:
     10   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1      cgd  *    must display the following acknowledgement:
     17   1.1      cgd  *	This product includes software developed by the University of
     18   1.1      cgd  *	California, Berkeley and its contributors.
     19   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1      cgd  *    may be used to endorse or promote products derived from this software
     21   1.1      cgd  *    without specific prior written permission.
     22   1.1      cgd  *
     23   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1      cgd  * SUCH DAMAGE.
     34   1.1      cgd  */
     35   1.1      cgd 
     36   1.1      cgd #ifndef lint
     37   1.5      cgd static char copyright[] =
     38   1.5      cgd "@(#) Copyright (c) 1980, 1993\n\
     39   1.5      cgd 	The Regents of the University of California.  All rights reserved.\n";
     40   1.1      cgd #endif /* not lint */
     41   1.1      cgd 
     42   1.1      cgd #ifndef lint
     43   1.5      cgd #if 0
     44   1.8      tls static char sccsid[] = "@(#)mt.c	8.2 (Berkeley) 6/6/93";
     45   1.5      cgd #else
     46  1.19    lukem static char rcsid[] = "$NetBSD: mt.c,v 1.19 1997/04/15 06:53:51 lukem Exp $";
     47   1.5      cgd #endif
     48   1.1      cgd #endif /* not lint */
     49   1.1      cgd 
     50   1.1      cgd /*
     51   1.1      cgd  * mt --
     52   1.1      cgd  *   magnetic tape manipulation program
     53   1.1      cgd  */
     54  1.17      jtc #include <rmt.h>
     55   1.1      cgd #include <sys/types.h>
     56   1.1      cgd #include <sys/ioctl.h>
     57   1.1      cgd #include <sys/mtio.h>
     58  1.12   scottr #include <sys/stat.h>
     59  1.12   scottr 
     60  1.12   scottr #include <ctype.h>
     61  1.12   scottr #include <err.h>
     62   1.1      cgd #include <fcntl.h>
     63  1.19    lukem #include <paths.h>
     64  1.12   scottr #include <stdio.h>
     65   1.5      cgd #include <stdlib.h>
     66   1.3      jtc #include <string.h>
     67  1.11   scottr #include <unistd.h>
     68   1.1      cgd 
     69  1.16      jtc /* pseudo ioctl constants */
     70  1.16      jtc #define MTASF	100
     71  1.16      jtc 
     72   1.1      cgd struct commands {
     73  1.16      jtc 	const char *c_name;
     74   1.1      cgd 	int c_code;
     75   1.1      cgd 	int c_ronly;
     76  1.16      jtc };
     77  1.16      jtc 
     78  1.16      jtc const struct commands com[] = {
     79  1.16      jtc 	{ "asf",	MTASF,  1 },
     80  1.14      mrg 	{ "blocksize",	MTSETBSIZ, 1 },
     81   1.5      cgd 	{ "bsf",	MTBSF,	1 },
     82   1.5      cgd 	{ "bsr",	MTBSR,	1 },
     83  1.14      mrg 	{ "density",	MTSETDNSTY, 1 },
     84   1.5      cgd 	{ "eof",	MTWEOF,	0 },
     85   1.5      cgd 	{ "eom",	MTEOM,	1 },
     86   1.5      cgd 	{ "erase",	MTERASE, 0 },
     87   1.5      cgd 	{ "fsf",	MTFSF,	1 },
     88   1.5      cgd 	{ "fsr",	MTFSR,	1 },
     89   1.5      cgd 	{ "offline",	MTOFFL,	1 },
     90   1.5      cgd 	{ "rewind",	MTREW,	1 },
     91   1.5      cgd 	{ "rewoffl",	MTOFFL,	1 },
     92   1.5      cgd 	{ "status",	MTNOP,	1 },
     93   1.4  mycroft 	{ "retension",	MTRETEN, 1 },
     94   1.5      cgd 	{ "weof",	MTWEOF,	0 },
     95   1.5      cgd 	{ NULL }
     96   1.1      cgd };
     97   1.1      cgd 
     98   1.5      cgd void printreg __P((char *, u_int, char *));
     99   1.5      cgd void status __P((struct mtget *));
    100   1.5      cgd void usage __P((void));
    101   1.1      cgd 
    102   1.3      jtc int
    103   1.1      cgd main(argc, argv)
    104   1.3      jtc 	int argc;
    105   1.5      cgd 	char *argv[];
    106   1.1      cgd {
    107  1.18      tls 	const struct commands *comp;
    108   1.5      cgd 	struct mtget mt_status;
    109   1.5      cgd 	struct mtop mt_com;
    110  1.12   scottr 	int ch, len, mtfd, flags;
    111   1.5      cgd 	char *p, *tape;
    112  1.16      jtc 	int count;
    113   1.1      cgd 
    114   1.5      cgd 	if ((tape = getenv("TAPE")) == NULL)
    115  1.19    lukem 		tape = _PATH_DEFTAPE;
    116   1.5      cgd 
    117   1.5      cgd 	while ((ch = getopt(argc, argv, "f:t:")) != -1)
    118   1.5      cgd 		switch (ch) {
    119   1.3      jtc 		case 'f':
    120   1.3      jtc 		case 't':
    121   1.3      jtc 			tape = optarg;
    122   1.3      jtc 			break;
    123   1.5      cgd 		case '?':
    124   1.3      jtc 		default:
    125   1.3      jtc 			usage();
    126   1.3      jtc 		}
    127   1.3      jtc 	argc -= optind;
    128   1.3      jtc 	argv += optind;
    129   1.3      jtc 
    130   1.5      cgd 	if (argc < 1 || argc > 2)
    131   1.3      jtc 		usage();
    132   1.3      jtc 
    133   1.5      cgd 	len = strlen(p = *argv++);
    134   1.5      cgd 	for (comp = com;; comp++) {
    135   1.5      cgd 		if (comp->c_name == NULL)
    136   1.5      cgd 			errx(1, "%s: unknown command", p);
    137   1.5      cgd 		if (strncmp(p, comp->c_name, len) == 0)
    138   1.1      cgd 			break;
    139   1.3      jtc 	}
    140  1.12   scottr 
    141  1.17      jtc 	flags = comp->c_ronly ? O_RDONLY : O_WRONLY;
    142  1.17      jtc 	if ((mtfd = open(tape, flags)) < 0)
    143   1.7       pk 		err(2, "%s", tape);
    144  1.16      jtc 
    145  1.16      jtc 	if (comp->c_code == MTASF) {
    146  1.16      jtc 		/* If mtget.mt_fileno was implemented, We could
    147  1.16      jtc 		   compute the minimal seek needed to position
    148  1.16      jtc 		   the tape.  Until then, rewind and seek from
    149  1.16      jtc 		   begining-of-tape */
    150  1.16      jtc 
    151  1.16      jtc 		/* zero is a valid count */
    152  1.16      jtc 		if (*argv) {
    153  1.16      jtc 			count = strtol(*argv, &p, 10);
    154  1.16      jtc 			if (count < 0 || *p)
    155  1.16      jtc 				errx(2, "%s: illegal count", *argv);
    156  1.16      jtc 		}
    157  1.16      jtc 		else
    158  1.16      jtc 			count = 1;
    159  1.16      jtc 
    160  1.16      jtc 		mt_com.mt_op = MTREW;
    161  1.16      jtc 		mt_com.mt_count = 1;
    162  1.17      jtc 		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
    163  1.16      jtc 			err(2, "%s", tape);
    164  1.16      jtc 
    165  1.16      jtc 		mt_com.mt_op = MTFSF;
    166  1.16      jtc 		mt_com.mt_count = count;
    167  1.17      jtc 		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
    168  1.16      jtc 			err(2, "%s", tape);
    169  1.16      jtc 
    170  1.16      jtc 	} else if (comp->c_code != MTNOP) {
    171  1.16      jtc 
    172  1.16      jtc 		/* zero is *not* a valid count */
    173   1.5      cgd 		if (*argv) {
    174  1.16      jtc 			count = strtol(*argv, &p, 10);
    175  1.16      jtc 			if (count <= 0 || *p)
    176   1.7       pk 				errx(2, "%s: illegal count", *argv);
    177   1.1      cgd 		}
    178   1.5      cgd 		else
    179  1.16      jtc 			count = 1;
    180  1.16      jtc 
    181  1.16      jtc 		mt_com.mt_op = comp->c_code;
    182  1.16      jtc 		mt_com.mt_count = count;
    183  1.16      jtc 
    184  1.17      jtc 		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
    185   1.7       pk 			err(2, "%s: %s", tape, comp->c_name);
    186  1.16      jtc 
    187   1.1      cgd 	} else {
    188  1.17      jtc 		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
    189  1.17      jtc 			err(2, "%s: %s", tape, comp->c_name);
    190  1.17      jtc 		status(&mt_status);
    191   1.1      cgd 	}
    192   1.9   scottr 
    193  1.17      jtc 	exit(0);
    194   1.5      cgd 	/* NOTREACHED */
    195   1.1      cgd }
    196   1.1      cgd 
    197   1.1      cgd #ifdef sun
    198   1.1      cgd #include <sundev/tmreg.h>
    199   1.1      cgd #include <sundev/arreg.h>
    200   1.1      cgd #endif
    201   1.1      cgd 
    202   1.1      cgd #ifdef tahoe
    203   1.1      cgd #include <tahoe/vba/cyreg.h>
    204   1.1      cgd #endif
    205   1.1      cgd 
    206   1.1      cgd struct tape_desc {
    207   1.1      cgd 	short	t_type;		/* type of magtape device */
    208   1.1      cgd 	char	*t_name;	/* printing name */
    209   1.1      cgd 	char	*t_dsbits;	/* "drive status" register */
    210   1.1      cgd 	char	*t_erbits;	/* "error" register */
    211   1.1      cgd } tapes[] = {
    212   1.1      cgd #ifdef sun
    213   1.1      cgd 	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
    214   1.1      cgd 	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
    215   1.1      cgd #endif
    216   1.1      cgd #ifdef tahoe
    217   1.1      cgd 	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
    218   1.1      cgd #endif
    219  1.15      mrg 	{ 0x7,		"SCSI",		"76543210",	"76543210" },
    220   1.1      cgd 	{ 0 }
    221   1.1      cgd };
    222   1.1      cgd 
    223   1.1      cgd /*
    224   1.1      cgd  * Interpret the status buffer returned
    225   1.1      cgd  */
    226   1.5      cgd void
    227   1.1      cgd status(bp)
    228  1.18      tls 	struct mtget *bp;
    229   1.1      cgd {
    230  1.18      tls 	struct tape_desc *mt;
    231   1.1      cgd 
    232   1.5      cgd 	for (mt = tapes;; mt++) {
    233   1.5      cgd 		if (mt->t_type == 0) {
    234   1.5      cgd 			(void)printf("%d: unknown tape drive type\n",
    235   1.5      cgd 			    bp->mt_type);
    236   1.5      cgd 			return;
    237   1.5      cgd 		}
    238   1.1      cgd 		if (mt->t_type == bp->mt_type)
    239   1.1      cgd 			break;
    240   1.1      cgd 	}
    241   1.5      cgd 	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
    242   1.1      cgd 	printreg("ds", bp->mt_dsreg, mt->t_dsbits);
    243   1.1      cgd 	printreg("\ner", bp->mt_erreg, mt->t_erbits);
    244   1.5      cgd 	(void)putchar('\n');
    245  1.14      mrg 	(void)printf("blocksize: %ld (%ld, %ld, %ld, %ld)\n",
    246  1.14      mrg 		bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1],
    247  1.14      mrg 		bp->mt_mblksiz[2], bp->mt_mblksiz[3]);
    248  1.14      mrg 	(void)printf("density: %ld (%ld, %ld, %ld, %ld)\n",
    249  1.14      mrg 		bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1],
    250  1.14      mrg 		bp->mt_mdensity[2], bp->mt_mdensity[3]);
    251   1.1      cgd }
    252   1.1      cgd 
    253   1.1      cgd /*
    254   1.5      cgd  * Print a register a la the %b format of the kernel's printf.
    255   1.1      cgd  */
    256   1.5      cgd void
    257   1.1      cgd printreg(s, v, bits)
    258   1.1      cgd 	char *s;
    259  1.18      tls 	u_int v;
    260  1.18      tls 	char *bits;
    261   1.1      cgd {
    262  1.18      tls 	int i, any = 0;
    263  1.18      tls 	char c;
    264   1.1      cgd 
    265   1.1      cgd 	if (bits && *bits == 8)
    266   1.1      cgd 		printf("%s=%o", s, v);
    267   1.1      cgd 	else
    268   1.1      cgd 		printf("%s=%x", s, v);
    269   1.1      cgd 	bits++;
    270   1.1      cgd 	if (v && bits) {
    271   1.1      cgd 		putchar('<');
    272  1.11   scottr 		while ((i = *bits++)) {
    273   1.1      cgd 			if (v & (1 << (i-1))) {
    274   1.1      cgd 				if (any)
    275   1.1      cgd 					putchar(',');
    276   1.1      cgd 				any = 1;
    277   1.1      cgd 				for (; (c = *bits) > 32; bits++)
    278   1.1      cgd 					putchar(c);
    279   1.1      cgd 			} else
    280   1.1      cgd 				for (; *bits > 32; bits++)
    281   1.1      cgd 					;
    282   1.1      cgd 		}
    283   1.1      cgd 		putchar('>');
    284   1.1      cgd 	}
    285   1.3      jtc }
    286   1.3      jtc 
    287   1.5      cgd void
    288   1.3      jtc usage()
    289   1.3      jtc {
    290   1.5      cgd 	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
    291  1.17      jtc 	exit(1);
    292   1.1      cgd }
    293