blob: 20afd3a50b2c72eff7cbb0c836bdba06df0ca697 [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/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00004 */
5
Pere Orga5bc8c002011-04-11 03:29:49 +02006//usage:#define mt_trivial_usage
7//usage: "[-f device] opcode value"
8//usage:#define mt_full_usage "\n\n"
9//usage: "Control magnetic tape drive operation\n"
10//usage: "\n"
11//usage: "Available Opcodes:\n"
12//usage: "\n"
13//usage: "bsf bsfm bsr bss datacompression drvbuffer eof eom erase\n"
14//usage: "fsf fsfm fsr fss load lock mkpart nop offline ras1 ras2\n"
15//usage: "ras3 reset retension rewind rewoffline seek setblk setdensity\n"
16//usage: "setpart tell unload unlock weof wset"
17
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000019#include <sys/mtio.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000020
Denis Vlasenko8add0682007-10-11 19:38:59 +000021/* missing: eod/seod, stoptions, stwrthreshold, densities */
22static const short opcode_value[] = {
23 MTBSF,
24 MTBSFM,
25 MTBSR,
26 MTBSS,
27 MTCOMPRESSION,
28 MTEOM,
29 MTERASE,
30 MTFSF,
31 MTFSFM,
32 MTFSR,
33 MTFSS,
34 MTLOAD,
35 MTLOCK,
36 MTMKPART,
37 MTNOP,
38 MTOFFL,
39 MTOFFL,
40 MTRAS1,
41 MTRAS2,
42 MTRAS3,
43 MTRESET,
44 MTRETEN,
45 MTREW,
46 MTSEEK,
47 MTSETBLK,
48 MTSETDENSITY,
49 MTSETDRVBUFFER,
50 MTSETPART,
51 MTTELL,
52 MTWSM,
53 MTUNLOAD,
54 MTUNLOCK,
55 MTWEOF,
56 MTWEOF
Eric Andersencc8ed391999-10-05 16:24:54 +000057};
58
Denis Vlasenko8add0682007-10-11 19:38:59 +000059static const char opcode_name[] ALIGN1 =
60 "bsf" "\0"
61 "bsfm" "\0"
62 "bsr" "\0"
63 "bss" "\0"
64 "datacompression" "\0"
65 "eom" "\0"
66 "erase" "\0"
67 "fsf" "\0"
68 "fsfm" "\0"
69 "fsr" "\0"
70 "fss" "\0"
71 "load" "\0"
72 "lock" "\0"
73 "mkpart" "\0"
74 "nop" "\0"
75 "offline" "\0"
76 "rewoffline" "\0"
77 "ras1" "\0"
78 "ras2" "\0"
79 "ras3" "\0"
80 "reset" "\0"
81 "retension" "\0"
82 "rewind" "\0"
83 "seek" "\0"
84 "setblk" "\0"
85 "setdensity" "\0"
86 "drvbuffer" "\0"
87 "setpart" "\0"
88 "tell" "\0"
89 "wset" "\0"
90 "unload" "\0"
91 "unlock" "\0"
92 "eof" "\0"
93 "weof" "\0";
Eric Andersencc8ed391999-10-05 16:24:54 +000094
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000095int mt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000096int mt_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000097{
Erik Andersene49d5ec2000-02-08 19:58:47 +000098 const char *file = "/dev/tape";
Erik Andersene49d5ec2000-02-08 19:58:47 +000099 struct mtop op;
Matt Kraai73991ff2001-05-21 17:01:32 +0000100 struct mtpos position;
Denis Vlasenko8add0682007-10-11 19:38:59 +0000101 int fd, mode, idx;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000102
Denis Vlasenko1d426652008-03-17 09:09:09 +0000103 if (!argv[1]) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 bb_show_usage();
Erik Andersen5e1189e2000-04-15 16:34:54 +0000105 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106
107 if (strcmp(argv[1], "-f") == 0) {
Denis Vlasenko1d426652008-03-17 09:09:09 +0000108 if (!argv[2] || !argv[3])
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 bb_show_usage();
Eric Andersencc8ed391999-10-05 16:24:54 +0000110 file = argv[2];
111 argv += 2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000112 }
113
Denis Vlasenko8add0682007-10-11 19:38:59 +0000114 idx = index_in_strings(opcode_name, argv[1]);
Eric Andersencc8ed391999-10-05 16:24:54 +0000115
Denis Vlasenko8add0682007-10-11 19:38:59 +0000116 if (idx < 0)
117 bb_error_msg_and_die("unrecognized opcode %s", argv[1]);
Eric Andersencc8ed391999-10-05 16:24:54 +0000118
Denis Vlasenko8add0682007-10-11 19:38:59 +0000119 op.mt_op = opcode_value[idx];
Denis Vlasenko1d426652008-03-17 09:09:09 +0000120 if (argv[2])
Denys Vlasenko77832482010-08-12 14:14:45 +0200121 op.mt_count = xatoi_positive(argv[2]);
Eric Andersencc8ed391999-10-05 16:24:54 +0000122 else
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200123 op.mt_count = 1; /* One, not zero, right? */
Eric Andersencc8ed391999-10-05 16:24:54 +0000124
Denis Vlasenko8add0682007-10-11 19:38:59 +0000125 switch (opcode_value[idx]) {
Matt Kraai13a70ac2001-05-21 15:00:39 +0000126 case MTWEOF:
127 case MTERASE:
128 case MTWSM:
129 case MTSETDRVBUFFER:
130 mode = O_WRONLY;
131 break;
132
133 default:
134 mode = O_RDONLY;
135 break;
136 }
137
Bernhard Reutner-Fischer64d7e932006-09-11 16:01:40 +0000138 fd = xopen(file, mode);
Eric Andersencc8ed391999-10-05 16:24:54 +0000139
Denis Vlasenko8add0682007-10-11 19:38:59 +0000140 switch (opcode_value[idx]) {
Matt Kraai73991ff2001-05-21 17:01:32 +0000141 case MTTELL:
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000142 ioctl_or_perror_and_die(fd, MTIOCPOS, &position, "%s", file);
Denis Vlasenko8add0682007-10-11 19:38:59 +0000143 printf("At block %d\n", (int) position.mt_blkno);
Matt Kraai73991ff2001-05-21 17:01:32 +0000144 break;
145
146 default:
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000147 ioctl_or_perror_and_die(fd, MTIOCTOP, &op, "%s", file);
Matt Kraai73991ff2001-05-21 17:01:32 +0000148 break;
149 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000150
Matt Kraai3e856ce2000-12-01 02:55:13 +0000151 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000152}