blob: 70d03cca40177f3f02d9ca29d26faab33423f8f0 [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) {
Mark Whitleyf57c9442000-12-07 19:56:48 +000078 error_msg("unrecognized opcode %s.\n", argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +000079 return EXIT_FAILURE;
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
Matt Kraaia9819b22000-12-22 01:48:07 +000088 if ((fd = open(file, O_RDONLY, 0)) < 0)
89 perror_msg_and_die("%s", file);
Eric Andersencc8ed391999-10-05 16:24:54 +000090
Matt Kraaia9819b22000-12-22 01:48:07 +000091 if (ioctl(fd, MTIOCTOP, &op) != 0)
92 perror_msg_and_die("%s", file);
Eric Andersencc8ed391999-10-05 16:24:54 +000093
Matt Kraai3e856ce2000-12-01 02:55:13 +000094 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000095}