blob: eb93cb234f2a3fbe62c8e34ad44b9f134d4d22f7 [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>
Eric Andersened3ef502001-01-27 08:24:39 +00004#include <stdlib.h>
Eric Andersencc8ed391999-10-05 16:24:54 +00005#include <sys/mtio.h>
6#include <sys/fcntl.h>
7
Eric Andersencc8ed391999-10-05 16:24:54 +00008struct mt_opcodes {
Erik Andersene49d5ec2000-02-08 19:58:47 +00009 char *name;
10 short value;
Eric Andersencc8ed391999-10-05 16:24:54 +000011};
12
13/* missing: eod/seod, stoptions, stwrthreshold, densities */
Erik Andersene49d5ec2000-02-08 19:58:47 +000014static const struct mt_opcodes opcodes[] = {
15 {"bsf", MTBSF},
16 {"bsfm", MTBSFM},
17 {"bsr", MTBSR},
18 {"bss", MTBSS},
19 {"datacompression", MTCOMPRESSION},
20 {"eom", MTEOM},
21 {"erase", MTERASE},
22 {"fsf", MTFSF},
23 {"fsfm", MTFSFM},
24 {"fsr", MTFSR},
25 {"fss", MTFSS},
26 {"load", MTLOAD},
27 {"lock", MTLOCK},
28 {"mkpart", MTMKPART},
29 {"nop", MTNOP},
30 {"offline", MTOFFL},
31 {"rewoffline", MTOFFL},
32 {"ras1", MTRAS1},
33 {"ras2", MTRAS2},
34 {"ras3", MTRAS3},
35 {"reset", MTRESET},
36 {"retension", MTRETEN},
37 {"rew", MTREW},
38 {"seek", MTSEEK},
39 {"setblk", MTSETBLK},
40 {"setdensity", MTSETDENSITY},
41 {"drvbuffer", MTSETDRVBUFFER},
42 {"setpart", MTSETPART},
43 {"tell", MTTELL},
44 {"wset", MTWSM},
45 {"unload", MTUNLOAD},
46 {"unlock", MTUNLOCK},
47 {"eof", MTWEOF},
48 {"weof", MTWEOF},
49 {0, 0}
Eric Andersencc8ed391999-10-05 16:24:54 +000050};
51
Erik Andersene49d5ec2000-02-08 19:58:47 +000052extern int mt_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000053{
Erik Andersene49d5ec2000-02-08 19:58:47 +000054 const char *file = "/dev/tape";
55 const struct mt_opcodes *code = opcodes;
56 struct mtop op;
57 int fd;
Erik Andersen5e1189e2000-04-15 16:34:54 +000058
Eric Andersen1eec89d2000-09-05 18:21:53 +000059 if (argc < 2) {
Eric Andersen67991cf2001-02-14 21:23:06 +000060 show_usage();
Erik Andersen5e1189e2000-04-15 16:34:54 +000061 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000062
63 if (strcmp(argv[1], "-f") == 0) {
64 if (argc < 4) {
Eric Andersen67991cf2001-02-14 21:23:06 +000065 show_usage();
Eric Andersencc8ed391999-10-05 16:24:54 +000066 }
67 file = argv[2];
68 argv += 2;
69 argc -= 2;
70 }
71
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 while (code->name != 0) {
73 if (strcmp(code->name, argv[1]) == 0)
Eric Andersencc8ed391999-10-05 16:24:54 +000074 break;
75 code++;
76 }
77
Erik Andersene49d5ec2000-02-08 19:58:47 +000078 if (code->name == 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +000079 error_msg("unrecognized opcode %s.", argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +000080 return EXIT_FAILURE;
Eric Andersencc8ed391999-10-05 16:24:54 +000081 }
82
83 op.mt_op = code->value;
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 if (argc >= 3)
85 op.mt_count = atoi(argv[2]);
Eric Andersencc8ed391999-10-05 16:24:54 +000086 else
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 op.mt_count = 1; /* One, not zero, right? */
Eric Andersencc8ed391999-10-05 16:24:54 +000088
Matt Kraaia9819b22000-12-22 01:48:07 +000089 if ((fd = open(file, O_RDONLY, 0)) < 0)
90 perror_msg_and_die("%s", file);
Eric Andersencc8ed391999-10-05 16:24:54 +000091
Matt Kraaia9819b22000-12-22 01:48:07 +000092 if (ioctl(fd, MTIOCTOP, &op) != 0)
93 perror_msg_and_die("%s", file);
Eric Andersencc8ed391999-10-05 16:24:54 +000094
Matt Kraai3e856ce2000-12-01 02:55:13 +000095 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000096}