blob: 49dc70ac61e9610a9d6f6ff65c9e3a1c5218cabf [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) {
Eric Andersen67991cf2001-02-14 21:23:06 +000062 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) {
Eric Andersen67991cf2001-02-14 21:23:06 +000067 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) {
Matt Kraaidd19c692001-01-31 19:00:21 +000081 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)
Matt Kraaia9819b22000-12-22 01:48:07 +0000105 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)
110 perror_msg_and_die("%s", file);
111 printf ("At block %d.\n", (int) position.mt_blkno);
112 break;
113
114 default:
115 if (ioctl(fd, MTIOCTOP, &op) != 0)
116 perror_msg_and_die("%s", file);
117 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}