blob: e7995455291d733c077409642fee73300e78fa13 [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},
Matt Kraai1eb4acf2001-05-22 14:32:35 +000038 {"rewind", MTREW},
Erik Andersene49d5ec2000-02-08 19:58:47 +000039 {"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;
Matt Kraai73991ff2001-05-21 17:01:32 +000058 struct mtpos position;
Matt Kraai13a70ac2001-05-21 15:00:39 +000059 int fd, mode;
Erik Andersen5e1189e2000-04-15 16:34:54 +000060
Eric Andersen1eec89d2000-09-05 18:21:53 +000061 if (argc < 2) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 bb_show_usage();
Erik Andersen5e1189e2000-04-15 16:34:54 +000063 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000064
65 if (strcmp(argv[1], "-f") == 0) {
66 if (argc < 4) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 bb_show_usage();
Eric Andersencc8ed391999-10-05 16:24:54 +000068 }
69 file = argv[2];
70 argv += 2;
71 argc -= 2;
72 }
73
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 while (code->name != 0) {
75 if (strcmp(code->name, argv[1]) == 0)
Eric Andersencc8ed391999-10-05 16:24:54 +000076 break;
77 code++;
78 }
79
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 if (code->name == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 bb_error_msg("unrecognized opcode %s.", argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +000082 return EXIT_FAILURE;
Eric Andersencc8ed391999-10-05 16:24:54 +000083 }
84
85 op.mt_op = code->value;
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 if (argc >= 3)
87 op.mt_count = atoi(argv[2]);
Eric Andersencc8ed391999-10-05 16:24:54 +000088 else
Erik Andersene49d5ec2000-02-08 19:58:47 +000089 op.mt_count = 1; /* One, not zero, right? */
Eric Andersencc8ed391999-10-05 16:24:54 +000090
Matt Kraai13a70ac2001-05-21 15:00:39 +000091 switch (code->value) {
92 case MTWEOF:
93 case MTERASE:
94 case MTWSM:
95 case MTSETDRVBUFFER:
96 mode = O_WRONLY;
97 break;
98
99 default:
100 mode = O_RDONLY;
101 break;
102 }
103
104 if ((fd = open(file, mode, 0)) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 bb_perror_msg_and_die("%s", file);
Eric Andersencc8ed391999-10-05 16:24:54 +0000106
Matt Kraai73991ff2001-05-21 17:01:32 +0000107 switch (code->value) {
108 case MTTELL:
109 if (ioctl(fd, MTIOCPOS, &position) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 bb_perror_msg_and_die("%s", file);
Matt Kraai73991ff2001-05-21 17:01:32 +0000111 printf ("At block %d.\n", (int) position.mt_blkno);
112 break;
113
114 default:
115 if (ioctl(fd, MTIOCTOP, &op) != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116 bb_perror_msg_and_die("%s", file);
Matt Kraai73991ff2001-05-21 17:01:32 +0000117 break;
118 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000119
Matt Kraai3e856ce2000-12-01 02:55:13 +0000120 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000121}