blob: 28922f8d9227d73ad88d16fac0741ed90dd0eb5c [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 Andersen7ab9c7e2000-05-12 19:41:47 +00007static const char mt_usage[] = "mt [-f device] opcode value\n"
8#ifndef BB_FEATURE_TRIVIAL_HELP
9 "\nControl magnetic tape drive operation\n"
10#endif
11 ;
Eric Andersencc8ed391999-10-05 16:24:54 +000012
13struct mt_opcodes {
Erik Andersene49d5ec2000-02-08 19:58:47 +000014 char *name;
15 short value;
Eric Andersencc8ed391999-10-05 16:24:54 +000016};
17
18/* missing: eod/seod, stoptions, stwrthreshold, densities */
Erik Andersene49d5ec2000-02-08 19:58:47 +000019static const struct mt_opcodes opcodes[] = {
20 {"bsf", MTBSF},
21 {"bsfm", MTBSFM},
22 {"bsr", MTBSR},
23 {"bss", MTBSS},
24 {"datacompression", MTCOMPRESSION},
25 {"eom", MTEOM},
26 {"erase", MTERASE},
27 {"fsf", MTFSF},
28 {"fsfm", MTFSFM},
29 {"fsr", MTFSR},
30 {"fss", MTFSS},
31 {"load", MTLOAD},
32 {"lock", MTLOCK},
33 {"mkpart", MTMKPART},
34 {"nop", MTNOP},
35 {"offline", MTOFFL},
36 {"rewoffline", MTOFFL},
37 {"ras1", MTRAS1},
38 {"ras2", MTRAS2},
39 {"ras3", MTRAS3},
40 {"reset", MTRESET},
41 {"retension", MTRETEN},
42 {"rew", MTREW},
43 {"seek", MTSEEK},
44 {"setblk", MTSETBLK},
45 {"setdensity", MTSETDENSITY},
46 {"drvbuffer", MTSETDRVBUFFER},
47 {"setpart", MTSETPART},
48 {"tell", MTTELL},
49 {"wset", MTWSM},
50 {"unload", MTUNLOAD},
51 {"unlock", MTUNLOCK},
52 {"eof", MTWEOF},
53 {"weof", MTWEOF},
54 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +000055};
56
Erik Andersene49d5ec2000-02-08 19:58:47 +000057extern int mt_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000058{
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 const char *file = "/dev/tape";
60 const struct mt_opcodes *code = opcodes;
61 struct mtop op;
62 int fd;
Erik Andersen5e1189e2000-04-15 16:34:54 +000063
64 if ((argc != 2 && argc != 3) || **(argv + 1) == '-') {
65 usage(mt_usage);
66 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000067
68 if (strcmp(argv[1], "-f") == 0) {
69 if (argc < 4) {
70 usage(mt_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +000071 }
72 file = argv[2];
73 argv += 2;
74 argc -= 2;
75 }
76
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 while (code->name != 0) {
78 if (strcmp(code->name, argv[1]) == 0)
Eric Andersencc8ed391999-10-05 16:24:54 +000079 break;
80 code++;
81 }
82
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 if (code->name == 0) {
Eric Andersencc8ed391999-10-05 16:24:54 +000084 fprintf(stderr, "mt: unrecognized opcode %s.\n", argv[1]);
Erik Andersen5e1189e2000-04-15 16:34:54 +000085 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000086 }
87
88 op.mt_op = code->value;
Erik Andersene49d5ec2000-02-08 19:58:47 +000089 if (argc >= 3)
90 op.mt_count = atoi(argv[2]);
Eric Andersencc8ed391999-10-05 16:24:54 +000091 else
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 op.mt_count = 1; /* One, not zero, right? */
Eric Andersencc8ed391999-10-05 16:24:54 +000093
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 if ((fd = open(file, O_RDONLY, 0)) < 0) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000095 perror(file);
Erik Andersen5e1189e2000-04-15 16:34:54 +000096 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000097 }
98
Erik Andersene49d5ec2000-02-08 19:58:47 +000099 if (ioctl(fd, MTIOCTOP, &op) != 0) {
Eric Andersenb0e9a701999-10-18 22:28:26 +0000100 perror(file);
Erik Andersen5e1189e2000-04-15 16:34:54 +0000101 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000102 }
103
Erik Andersen5e1189e2000-04-15 16:34:54 +0000104 exit (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000105}