blob: c16073c61cbff3c26d173a038fcf715e68f764b9 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00006#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +00007#include <sys/mtio.h>
Eric Andersencc8ed391999-10-05 16:24:54 +00008
Denis Vlasenko8add0682007-10-11 19:38:59 +00009/* missing: eod/seod, stoptions, stwrthreshold, densities */
10static const short opcode_value[] = {
11 MTBSF,
12 MTBSFM,
13 MTBSR,
14 MTBSS,
15 MTCOMPRESSION,
16 MTEOM,
17 MTERASE,
18 MTFSF,
19 MTFSFM,
20 MTFSR,
21 MTFSS,
22 MTLOAD,
23 MTLOCK,
24 MTMKPART,
25 MTNOP,
26 MTOFFL,
27 MTOFFL,
28 MTRAS1,
29 MTRAS2,
30 MTRAS3,
31 MTRESET,
32 MTRETEN,
33 MTREW,
34 MTSEEK,
35 MTSETBLK,
36 MTSETDENSITY,
37 MTSETDRVBUFFER,
38 MTSETPART,
39 MTTELL,
40 MTWSM,
41 MTUNLOAD,
42 MTUNLOCK,
43 MTWEOF,
44 MTWEOF
Eric Andersencc8ed391999-10-05 16:24:54 +000045};
46
Denis Vlasenko8add0682007-10-11 19:38:59 +000047static const char opcode_name[] ALIGN1 =
48 "bsf" "\0"
49 "bsfm" "\0"
50 "bsr" "\0"
51 "bss" "\0"
52 "datacompression" "\0"
53 "eom" "\0"
54 "erase" "\0"
55 "fsf" "\0"
56 "fsfm" "\0"
57 "fsr" "\0"
58 "fss" "\0"
59 "load" "\0"
60 "lock" "\0"
61 "mkpart" "\0"
62 "nop" "\0"
63 "offline" "\0"
64 "rewoffline" "\0"
65 "ras1" "\0"
66 "ras2" "\0"
67 "ras3" "\0"
68 "reset" "\0"
69 "retension" "\0"
70 "rewind" "\0"
71 "seek" "\0"
72 "setblk" "\0"
73 "setdensity" "\0"
74 "drvbuffer" "\0"
75 "setpart" "\0"
76 "tell" "\0"
77 "wset" "\0"
78 "unload" "\0"
79 "unlock" "\0"
80 "eof" "\0"
81 "weof" "\0";
Eric Andersencc8ed391999-10-05 16:24:54 +000082
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000083int mt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000084int mt_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000085{
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 const char *file = "/dev/tape";
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 struct mtop op;
Matt Kraai73991ff2001-05-21 17:01:32 +000088 struct mtpos position;
Denis Vlasenko8add0682007-10-11 19:38:59 +000089 int fd, mode, idx;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000090
Eric Andersen1eec89d2000-09-05 18:21:53 +000091 if (argc < 2) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 bb_show_usage();
Erik Andersen5e1189e2000-04-15 16:34:54 +000093 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000094
95 if (strcmp(argv[1], "-f") == 0) {
96 if (argc < 4) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 bb_show_usage();
Eric Andersencc8ed391999-10-05 16:24:54 +000098 }
99 file = argv[2];
100 argv += 2;
101 argc -= 2;
102 }
103
Denis Vlasenko8add0682007-10-11 19:38:59 +0000104 idx = index_in_strings(opcode_name, argv[1]);
Eric Andersencc8ed391999-10-05 16:24:54 +0000105
Denis Vlasenko8add0682007-10-11 19:38:59 +0000106 if (idx < 0)
107 bb_error_msg_and_die("unrecognized opcode %s", argv[1]);
Eric Andersencc8ed391999-10-05 16:24:54 +0000108
Denis Vlasenko8add0682007-10-11 19:38:59 +0000109 op.mt_op = opcode_value[idx];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110 if (argc >= 3)
Denis Vlasenko13858992006-10-08 12:49:22 +0000111 op.mt_count = xatoi_u(argv[2]);
Eric Andersencc8ed391999-10-05 16:24:54 +0000112 else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113 op.mt_count = 1; /* One, not zero, right? */
Eric Andersencc8ed391999-10-05 16:24:54 +0000114
Denis Vlasenko8add0682007-10-11 19:38:59 +0000115 switch (opcode_value[idx]) {
Matt Kraai13a70ac2001-05-21 15:00:39 +0000116 case MTWEOF:
117 case MTERASE:
118 case MTWSM:
119 case MTSETDRVBUFFER:
120 mode = O_WRONLY;
121 break;
122
123 default:
124 mode = O_RDONLY;
125 break;
126 }
127
Bernhard Reutner-Fischer64d7e932006-09-11 16:01:40 +0000128 fd = xopen(file, mode);
Eric Andersencc8ed391999-10-05 16:24:54 +0000129
Denis Vlasenko8add0682007-10-11 19:38:59 +0000130 switch (opcode_value[idx]) {
Matt Kraai73991ff2001-05-21 17:01:32 +0000131 case MTTELL:
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000132 ioctl_or_perror_and_die(fd, MTIOCPOS, &position, "%s", file);
Denis Vlasenko8add0682007-10-11 19:38:59 +0000133 printf("At block %d\n", (int) position.mt_blkno);
Matt Kraai73991ff2001-05-21 17:01:32 +0000134 break;
135
136 default:
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000137 ioctl_or_perror_and_die(fd, MTIOCTOP, &op, "%s", file);
Matt Kraai73991ff2001-05-21 17:01:32 +0000138 break;
139 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000140
Matt Kraai3e856ce2000-12-01 02:55:13 +0000141 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000142}