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 | */ |
| 5 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 6 | //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 Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 19 | #include <sys/mtio.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 21 | /* missing: eod/seod, stoptions, stwrthreshold, densities */ |
| 22 | static 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 59 | static 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 95 | int mt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 96 | int mt_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 97 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 98 | const char *file = "/dev/tape"; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 99 | struct mtop op; |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 100 | struct mtpos position; |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 101 | int fd, mode, idx; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 102 | |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 103 | if (!argv[1]) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 104 | bb_show_usage(); |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 105 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 106 | |
| 107 | if (strcmp(argv[1], "-f") == 0) { |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 108 | if (!argv[2] || !argv[3]) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 109 | bb_show_usage(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 110 | file = argv[2]; |
| 111 | argv += 2; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 114 | idx = index_in_strings(opcode_name, argv[1]); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 115 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 116 | if (idx < 0) |
| 117 | bb_error_msg_and_die("unrecognized opcode %s", argv[1]); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 118 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 119 | op.mt_op = opcode_value[idx]; |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 120 | if (argv[2]) |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 121 | op.mt_count = xatoi_positive(argv[2]); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 122 | else |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 123 | op.mt_count = 1; /* One, not zero, right? */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 124 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 125 | switch (opcode_value[idx]) { |
Matt Kraai | 13a70ac | 2001-05-21 15:00:39 +0000 | [diff] [blame] | 126 | 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-Fischer | 64d7e93 | 2006-09-11 16:01:40 +0000 | [diff] [blame] | 138 | fd = xopen(file, mode); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 139 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 140 | switch (opcode_value[idx]) { |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 141 | case MTTELL: |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 142 | ioctl_or_perror_and_die(fd, MTIOCPOS, &position, "%s", file); |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 143 | printf("At block %d\n", (int) position.mt_blkno); |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 144 | break; |
| 145 | |
| 146 | default: |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 147 | ioctl_or_perror_and_die(fd, MTIOCTOP, &op, "%s", file); |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 148 | break; |
| 149 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 150 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 151 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 152 | } |