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