xref: /original-bsd/usr.bin/mt/mt.c (revision 1f3a482a)
1 static	char *sccsid = "@(#)mt.c	4.2 (Berkeley) 81/07/05";
2 
3 /*
4  * mt
5  */
6 
7 #include <stdio.h>
8 #include <ctype.h>
9 #include <sys/types.h>
10 #include <sys/mtio.h>
11 #include <sys/ioctl.h>
12 
13 struct commands {
14 	char *c_name;
15 	int c_code;
16 	int c_ronly;
17 } com[] = {
18 	"eof",	MTWEOF,	0,
19 	"fsf",	MTFSF,	1,
20 	"bsf",	MTBSF,	1,
21 	"fsr",	MTFSR,	1,
22 	"bsr",	MTBSR,	1,
23 	"rewind",	MTREW,	1,
24 	"offline",	MTOFFL,	1,
25 	0,0
26 };
27 
28 int mtfd;
29 struct mtop mt_com;
30 char *tape;
31 
32 main(argc, argv)
33 char **argv;
34 {
35 	char line[80], *getenv();
36 	register char *cp;
37 	register struct commands *comp;
38 
39 	if (argc < 2) {
40 		fprintf(stderr, "usage: mt [ -t tape ] command [ count ]\n");
41 		exit(1);
42 	}
43 	if ((strcmp(argv[1], "-t") == 0) && argc > 2) {
44 		argc -= 2;
45 		tape = argv[2];
46 		argv += 2;
47 	} else
48 		if ((tape = getenv("TAPE")) == NULL)
49 			tape = "/dev/rmt12";
50 	cp = argv[1];
51 	for (comp = com; comp->c_name != NULL; comp++)
52 		if (strncmp(cp, comp->c_name, strlen(cp)) == 0)
53 			break;
54 	if (comp->c_name == NULL) {
55 		fprintf(stderr, "mt: don't grok \"%s\"\n", cp);
56 		exit(1);
57 	}
58 	if ((mtfd = open(tape, comp->c_ronly ? 0 : 2)) < 0) {
59 		perror(tape);
60 		exit(1);
61 	}
62 	mt_com.mt_count = (argc > 2 ? atoi(argv[2]) : 1);
63 	mt_com.mt_op = comp->c_code;
64 	if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0) {
65 		fprintf(stderr, "%s %d ", comp->c_name, mt_com.mt_count);
66 		perror("failed");
67 	}
68 }
69