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 | /* |
| 3 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 4 | */ |
| 5 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 6 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | #include <sys/mtio.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 8 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 9 | /* missing: eod/seod, stoptions, stwrthreshold, densities */ |
| 10 | static 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 47 | static 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 83 | int mt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 84 | int mt_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 85 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 86 | const char *file = "/dev/tape"; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 87 | struct mtop op; |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 88 | struct mtpos position; |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 89 | int fd, mode, idx; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 90 | |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 91 | if (!argv[1]) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 92 | bb_show_usage(); |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 93 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 94 | |
| 95 | if (strcmp(argv[1], "-f") == 0) { |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 96 | if (!argv[2] || !argv[3]) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 97 | bb_show_usage(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 98 | file = argv[2]; |
| 99 | argv += 2; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 102 | idx = index_in_strings(opcode_name, argv[1]); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 103 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 104 | if (idx < 0) |
| 105 | bb_error_msg_and_die("unrecognized opcode %s", argv[1]); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 106 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 107 | op.mt_op = opcode_value[idx]; |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 108 | if (argv[2]) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 109 | op.mt_count = xatoi_u(argv[2]); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 110 | else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 111 | op.mt_count = 1; /* One, not zero, right? */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 112 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 113 | switch (opcode_value[idx]) { |
Matt Kraai | 13a70ac | 2001-05-21 15:00:39 +0000 | [diff] [blame] | 114 | case MTWEOF: |
| 115 | case MTERASE: |
| 116 | case MTWSM: |
| 117 | case MTSETDRVBUFFER: |
| 118 | mode = O_WRONLY; |
| 119 | break; |
| 120 | |
| 121 | default: |
| 122 | mode = O_RDONLY; |
| 123 | break; |
| 124 | } |
| 125 | |
Bernhard Reutner-Fischer | 64d7e93 | 2006-09-11 16:01:40 +0000 | [diff] [blame] | 126 | fd = xopen(file, mode); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 127 | |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 128 | switch (opcode_value[idx]) { |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 129 | case MTTELL: |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 130 | ioctl_or_perror_and_die(fd, MTIOCPOS, &position, "%s", file); |
Denis Vlasenko | 8add068 | 2007-10-11 19:38:59 +0000 | [diff] [blame] | 131 | printf("At block %d\n", (int) position.mt_blkno); |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 132 | break; |
| 133 | |
| 134 | default: |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 135 | ioctl_or_perror_and_die(fd, MTIOCTOP, &op, "%s", file); |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 136 | break; |
| 137 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 138 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 139 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 140 | } |