Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 4 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 5 | //config:config MT |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 6 | //config: bool "mt (2.5 kb)" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 7 | //config: default y |
| 8 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 9 | //config: mt is used to control tape devices. You can use the mt utility |
| 10 | //config: to advance or rewind a tape past a specified number of archive |
| 11 | //config: files on the tape. |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 12 | |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 13 | //applet:IF_MT(APPLET(mt, BB_DIR_BIN, BB_SUID_DROP)) |
| 14 | |
| 15 | //kbuild:lib-$(CONFIG_MT) += mt.o |
| 16 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 17 | //usage:#define mt_trivial_usage |
| 18 | //usage: "[-f device] opcode value" |
| 19 | //usage:#define mt_full_usage "\n\n" |
| 20 | //usage: "Control magnetic tape drive operation\n" |
| 21 | //usage: "\n" |
| 22 | //usage: "Available Opcodes:\n" |
| 23 | //usage: "\n" |
| 24 | //usage: "bsf bsfm bsr bss datacompression drvbuffer eof eom erase\n" |
| 25 | //usage: "fsf fsfm fsr fss load lock mkpart nop offline ras1 ras2\n" |
| 26 | //usage: "ras3 reset retension rewind rewoffline seek setblk setdensity\n" |
| 27 | //usage: "setpart tell unload unlock weof wset" |
| 28 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 29 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 30 | #include <sys/mtio.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 32 | /* missing: eod/seod, stoptions, stwrthreshold, densities */ |
| 33 | static const short opcode_value[] = { |
| 34 | MTBSF, |
| 35 | MTBSFM, |
| 36 | MTBSR, |
| 37 | MTBSS, |
| 38 | MTCOMPRESSION, |
| 39 | MTEOM, |
| 40 | MTERASE, |
| 41 | MTFSF, |
| 42 | MTFSFM, |
| 43 | MTFSR, |
| 44 | MTFSS, |
| 45 | MTLOAD, |
| 46 | MTLOCK, |
| 47 | MTMKPART, |
| 48 | MTNOP, |
| 49 | MTOFFL, |
| 50 | MTOFFL, |
| 51 | MTRAS1, |
| 52 | MTRAS2, |
| 53 | MTRAS3, |
| 54 | MTRESET, |
| 55 | MTRETEN, |
| 56 | MTREW, |
| 57 | MTSEEK, |
| 58 | MTSETBLK, |
| 59 | MTSETDENSITY, |
| 60 | MTSETDRVBUFFER, |
| 61 | MTSETPART, |
| 62 | MTTELL, |
| 63 | MTWSM, |
| 64 | MTUNLOAD, |
| 65 | MTUNLOCK, |
| 66 | MTWEOF, |
| 67 | MTWEOF |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 70 | static const char opcode_name[] ALIGN1 = |
| 71 | "bsf" "\0" |
| 72 | "bsfm" "\0" |
| 73 | "bsr" "\0" |
| 74 | "bss" "\0" |
| 75 | "datacompression" "\0" |
| 76 | "eom" "\0" |
| 77 | "erase" "\0" |
| 78 | "fsf" "\0" |
| 79 | "fsfm" "\0" |
| 80 | "fsr" "\0" |
| 81 | "fss" "\0" |
| 82 | "load" "\0" |
| 83 | "lock" "\0" |
| 84 | "mkpart" "\0" |
| 85 | "nop" "\0" |
| 86 | "offline" "\0" |
| 87 | "rewoffline" "\0" |
| 88 | "ras1" "\0" |
| 89 | "ras2" "\0" |
| 90 | "ras3" "\0" |
| 91 | "reset" "\0" |
| 92 | "retension" "\0" |
| 93 | "rewind" "\0" |
| 94 | "seek" "\0" |
| 95 | "setblk" "\0" |
| 96 | "setdensity" "\0" |
| 97 | "drvbuffer" "\0" |
| 98 | "setpart" "\0" |
| 99 | "tell" "\0" |
| 100 | "wset" "\0" |
| 101 | "unload" "\0" |
| 102 | "unlock" "\0" |
| 103 | "eof" "\0" |
| 104 | "weof" "\0"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 105 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 106 | int mt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 107 | int mt_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 108 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 109 | const char *file = "/dev/tape"; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 110 | struct mtop op; |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 111 | struct mtpos position; |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 112 | int fd, mode, idx; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 113 | |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 114 | if (!argv[1]) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 115 | bb_show_usage(); |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 116 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 117 | |
| 118 | if (strcmp(argv[1], "-f") == 0) { |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 119 | if (!argv[2] || !argv[3]) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 120 | bb_show_usage(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 121 | file = argv[2]; |
| 122 | argv += 2; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 125 | idx = index_in_strings(opcode_name, argv[1]); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 126 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 127 | if (idx < 0) |
| 128 | bb_error_msg_and_die("unrecognized opcode %s", argv[1]); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 129 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 130 | op.mt_op = opcode_value[idx]; |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 131 | if (argv[2]) |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 132 | op.mt_count = xatoi_positive(argv[2]); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 133 | else |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 134 | op.mt_count = 1; /* One, not zero, right? */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 135 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 136 | switch (opcode_value[idx]) { |
Matt Kraai | 13a70ac | 2001-05-21 15:00:39 +0000 | [diff] [blame] | 137 | case MTWEOF: |
| 138 | case MTERASE: |
| 139 | case MTWSM: |
| 140 | case MTSETDRVBUFFER: |
| 141 | mode = O_WRONLY; |
| 142 | break; |
| 143 | |
| 144 | default: |
| 145 | mode = O_RDONLY; |
| 146 | break; |
| 147 | } |
| 148 | |
Bernhard Reutner-Fischer | 64d7e93 | 2006-09-11 16:01:40 +0000 | [diff] [blame] | 149 | fd = xopen(file, mode); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 150 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 151 | switch (opcode_value[idx]) { |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 152 | case MTTELL: |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 153 | ioctl_or_perror_and_die(fd, MTIOCPOS, &position, "%s", file); |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 154 | printf("At block %d\n", (int) position.mt_blkno); |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 155 | break; |
| 156 | |
| 157 | default: |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 158 | ioctl_or_perror_and_die(fd, MTIOCTOP, &op, "%s", file); |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 159 | break; |
| 160 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 161 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 162 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 163 | } |