blob: cf20d1711bbf11d695cc31bfeadaf4cc66b81d1e [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002#include "internal.h"
3#include <stdio.h>
4#include <sys/mtio.h>
5#include <sys/fcntl.h>
6
Erik Andersen5e1189e2000-04-15 16:34:54 +00007static const char mt_usage[] = "mt [-f device] opcode value\n\n"
8 "Control magnetic tape drive operation\n";
Eric Andersencc8ed391999-10-05 16:24:54 +00009
10struct mt_opcodes {
Erik Andersene49d5ec2000-02-08 19:58:47 +000011 char *name;
12 short value;
Eric Andersencc8ed391999-10-05 16:24:54 +000013};
14
15/* missing: eod/seod, stoptions, stwrthreshold, densities */
Erik Andersene49d5ec2000-02-08 19:58:47 +000016static const struct mt_opcodes opcodes[] = {
17 {"bsf", MTBSF},
18 {"bsfm", MTBSFM},
19 {"bsr", MTBSR},
20 {"bss", MTBSS},
21 {"datacompression", MTCOMPRESSION},
22 {"eom", MTEOM},
23 {"erase", MTERASE},
24 {"fsf", MTFSF},
25 {"fsfm", MTFSFM},
26 {"fsr", MTFSR},
27 {"fss", MTFSS},
28 {"load", MTLOAD},
29 {"lock", MTLOCK},
30 {"mkpart", MTMKPART},
31 {"nop", MTNOP},
32 {"offline", MTOFFL},
33 {"rewoffline", MTOFFL},
34 {"ras1", MTRAS1},
35 {"ras2", MTRAS2},
36 {"ras3", MTRAS3},
37 {"reset", MTRESET},
38 {"retension", MTRETEN},
39 {"rew", MTREW},
40 {"seek", MTSEEK},
41 {"setblk", MTSETBLK},
42 {"setdensity", MTSETDENSITY},
43 {"drvbuffer", MTSETDRVBUFFER},
44 {"setpart", MTSETPART},
45 {"tell", MTTELL},
46 {"wset", MTWSM},
47 {"unload", MTUNLOAD},
48 {"unlock", MTUNLOCK},
49 {"eof", MTWEOF},
50 {"weof", MTWEOF},
51 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +000052};
53
Erik Andersene49d5ec2000-02-08 19:58:47 +000054extern int mt_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000055{
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 const char *file = "/dev/tape";
57 const struct mt_opcodes *code = opcodes;
58 struct mtop op;
59 int fd;
Erik Andersen5e1189e2000-04-15 16:34:54 +000060
61 if ((argc != 2 && argc != 3) || **(argv + 1) == '-') {
62 usage(mt_usage);
63 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000064
65 if (strcmp(argv[1], "-f") == 0) {
66 if (argc < 4) {
67 usage(mt_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +000068 }
69 file = argv[2];
70 argv += 2;
71 argc -= 2;
72 }
73
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 while (code->name != 0) {
75 if (strcmp(code->name, argv[1]) == 0)
Eric Andersencc8ed391999-10-05 16:24:54 +000076 break;
77 code++;
78 }
79
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 if (code->name == 0) {
Eric Andersencc8ed391999-10-05 16:24:54 +000081 fprintf(stderr, "mt: unrecognized opcode %s.\n", argv[1]);
Erik Andersen5e1189e2000-04-15 16:34:54 +000082 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000083 }
84
85 op.mt_op = code->value;
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 if (argc >= 3)
87 op.mt_count = atoi(argv[2]);
Eric Andersencc8ed391999-10-05 16:24:54 +000088 else
Erik Andersene49d5ec2000-02-08 19:58:47 +000089 op.mt_count = 1; /* One, not zero, right? */
Eric Andersencc8ed391999-10-05 16:24:54 +000090
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 if ((fd = open(file, O_RDONLY, 0)) < 0) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000092 perror(file);
Erik Andersen5e1189e2000-04-15 16:34:54 +000093 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000094 }
95
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 if (ioctl(fd, MTIOCTOP, &op) != 0) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000097 perror(file);
Erik Andersen5e1189e2000-04-15 16:34:54 +000098 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000099 }
100
Erik Andersen5e1189e2000-04-15 16:34:54 +0000101 exit (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000102}