blob: b9bc2a11bc7c457cf3f2b15ebe6ee61043be7a70 [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
Eric Andersencc8ed391999-10-05 16:24:54 +00009struct mt_opcodes {
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000010 const char *name;
Erik Andersene49d5ec2000-02-08 19:58:47 +000011 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
Denis Vlasenko06af2162007-02-03 17:28:39 +000053int mt_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000054int mt_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000055{
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 const char *file = "/dev/tape";
57 const struct mt_opcodes *code = opcodes;
58 struct mtop op;
Matt Kraai73991ff2001-05-21 17:01:32 +000059 struct mtpos position;
Matt Kraai13a70ac2001-05-21 15:00:39 +000060 int fd, mode;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000061
Eric Andersen1eec89d2000-09-05 18:21:53 +000062 if (argc < 2) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 bb_show_usage();
Erik Andersen5e1189e2000-04-15 16:34:54 +000064 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000065
66 if (strcmp(argv[1], "-f") == 0) {
67 if (argc < 4) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000068 bb_show_usage();
Eric Andersencc8ed391999-10-05 16:24:54 +000069 }
70 file = argv[2];
71 argv += 2;
72 argc -= 2;
73 }
74
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 while (code->name != 0) {
76 if (strcmp(code->name, argv[1]) == 0)
Eric Andersencc8ed391999-10-05 16:24:54 +000077 break;
78 code++;
79 }
80
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 if (code->name == 0) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000082 bb_error_msg("unrecognized opcode %s", argv[1]);
Matt Kraai3e856ce2000-12-01 02:55:13 +000083 return EXIT_FAILURE;
Eric Andersencc8ed391999-10-05 16:24:54 +000084 }
85
86 op.mt_op = code->value;
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 if (argc >= 3)
Denis Vlasenko13858992006-10-08 12:49:22 +000088 op.mt_count = xatoi_u(argv[2]);
Eric Andersencc8ed391999-10-05 16:24:54 +000089 else
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 op.mt_count = 1; /* One, not zero, right? */
Eric Andersencc8ed391999-10-05 16:24:54 +000091
Matt Kraai13a70ac2001-05-21 15:00:39 +000092 switch (code->value) {
93 case MTWEOF:
94 case MTERASE:
95 case MTWSM:
96 case MTSETDRVBUFFER:
97 mode = O_WRONLY;
98 break;
99
100 default:
101 mode = O_RDONLY;
102 break;
103 }
104
Bernhard Reutner-Fischer64d7e932006-09-11 16:01:40 +0000105 fd = xopen(file, mode);
Eric Andersencc8ed391999-10-05 16:24:54 +0000106
Matt Kraai73991ff2001-05-21 17:01:32 +0000107 switch (code->value) {
108 case MTTELL:
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000109 ioctl_or_perror_and_die(fd, MTIOCPOS, &position, "%s", file);
Denis Vlasenko92258542006-11-01 10:25:35 +0000110 printf("At block %d.\n", (int) position.mt_blkno);
Matt Kraai73991ff2001-05-21 17:01:32 +0000111 break;
112
113 default:
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000114 ioctl_or_perror_and_die(fd, MTIOCTOP, &op, "%s", file);
Matt Kraai73991ff2001-05-21 17:01:32 +0000115 break;
116 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000117
Matt Kraai3e856ce2000-12-01 02:55:13 +0000118 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000119}