blob: f562a91a257a39681b151c7ce8abb92dea34077b [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00002/*
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */
5
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +00006#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +00007#include <sys/mtio.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
Rob Landleydfba7412006-03-06 20:47:33 +000053int 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;
Eric Andersenc7bda1c2004-03-15 08:29:22 +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
Rob Landleyd921b2e2006-08-03 15:41:12 +0000104 fd = xopen3(file, mode, 0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000105
Matt Kraai73991ff2001-05-21 17:01:32 +0000106 switch (code->value) {
107 case MTTELL:
108 if (ioctl(fd, MTIOCPOS, &position) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 bb_perror_msg_and_die("%s", file);
Matt Kraai73991ff2001-05-21 17:01:32 +0000110 printf ("At block %d.\n", (int) position.mt_blkno);
111 break;
112
113 default:
114 if (ioctl(fd, MTIOCTOP, &op) != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 bb_perror_msg_and_die("%s", file);
Matt Kraai73991ff2001-05-21 17:01:32 +0000116 break;
117 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000118
Matt Kraai3e856ce2000-12-01 02:55:13 +0000119 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000120}