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