blob: 6acae4ea1f846bcffd11f92cec56fc2c2a54e149 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3570a342000-09-25 21:45:58 +00002#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +00003#include <stdio.h>
4#include <sys/mtio.h>
5#include <sys/fcntl.h>
6
Eric Andersencc8ed391999-10-05 16:24:54 +00007struct mt_opcodes {
Erik Andersene49d5ec2000-02-08 19:58:47 +00008 char *name;
9 short value;
Eric Andersencc8ed391999-10-05 16:24:54 +000010};
11
12/* missing: eod/seod, stoptions, stwrthreshold, densities */
Erik Andersene49d5ec2000-02-08 19:58:47 +000013static const struct mt_opcodes opcodes[] = {
14 {"bsf", MTBSF},
15 {"bsfm", MTBSFM},
16 {"bsr", MTBSR},
17 {"bss", MTBSS},
18 {"datacompression", MTCOMPRESSION},
19 {"eom", MTEOM},
20 {"erase", MTERASE},
21 {"fsf", MTFSF},
22 {"fsfm", MTFSFM},
23 {"fsr", MTFSR},
24 {"fss", MTFSS},
25 {"load", MTLOAD},
26 {"lock", MTLOCK},
27 {"mkpart", MTMKPART},
28 {"nop", MTNOP},
29 {"offline", MTOFFL},
30 {"rewoffline", MTOFFL},
31 {"ras1", MTRAS1},
32 {"ras2", MTRAS2},
33 {"ras3", MTRAS3},
34 {"reset", MTRESET},
35 {"retension", MTRETEN},
36 {"rew", MTREW},
37 {"seek", MTSEEK},
38 {"setblk", MTSETBLK},
39 {"setdensity", MTSETDENSITY},
40 {"drvbuffer", MTSETDRVBUFFER},
41 {"setpart", MTSETPART},
42 {"tell", MTTELL},
43 {"wset", MTWSM},
44 {"unload", MTUNLOAD},
45 {"unlock", MTUNLOCK},
46 {"eof", MTWEOF},
47 {"weof", MTWEOF},
48 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +000049};
50
Erik Andersene49d5ec2000-02-08 19:58:47 +000051extern int mt_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000052{
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 const char *file = "/dev/tape";
54 const struct mt_opcodes *code = opcodes;
55 struct mtop op;
56 int fd;
Erik Andersen5e1189e2000-04-15 16:34:54 +000057
Eric Andersen1eec89d2000-09-05 18:21:53 +000058 if (argc < 2) {
Erik Andersen5e1189e2000-04-15 16:34:54 +000059 usage(mt_usage);
60 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000061
62 if (strcmp(argv[1], "-f") == 0) {
63 if (argc < 4) {
64 usage(mt_usage);
Eric Andersencc8ed391999-10-05 16:24:54 +000065 }
66 file = argv[2];
67 argv += 2;
68 argc -= 2;
69 }
70
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 while (code->name != 0) {
72 if (strcmp(code->name, argv[1]) == 0)
Eric Andersencc8ed391999-10-05 16:24:54 +000073 break;
74 code++;
75 }
76
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 if (code->name == 0) {
Matt Kraaid537a952000-07-14 01:51:25 +000078 errorMsg("unrecognized opcode %s.\n", argv[1]);
Erik Andersen5e1189e2000-04-15 16:34:54 +000079 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000080 }
81
82 op.mt_op = code->value;
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 if (argc >= 3)
84 op.mt_count = atoi(argv[2]);
Eric Andersencc8ed391999-10-05 16:24:54 +000085 else
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 op.mt_count = 1; /* One, not zero, right? */
Eric Andersencc8ed391999-10-05 16:24:54 +000087
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 if ((fd = open(file, O_RDONLY, 0)) < 0) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000089 perror(file);
Erik Andersen5e1189e2000-04-15 16:34:54 +000090 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000091 }
92
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 if (ioctl(fd, MTIOCTOP, &op) != 0) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000094 perror(file);
Erik Andersen5e1189e2000-04-15 16:34:54 +000095 exit (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000096 }
97
Eric Andersenb6106152000-06-19 17:25:40 +000098 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000099}