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 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 9 | struct mt_opcodes { |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 10 | const char *name; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 11 | short value; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 12 | }; |
| 13 | |
| 14 | /* missing: eod/seod, stoptions, stwrthreshold, densities */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 15 | static 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 Kraai | 1eb4acf | 2001-05-22 14:32:35 +0000 | [diff] [blame] | 38 | {"rewind", MTREW}, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 39 | {"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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 53 | int mt_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 54 | int mt_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 55 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | const char *file = "/dev/tape"; |
| 57 | const struct mt_opcodes *code = opcodes; |
| 58 | struct mtop op; |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 59 | struct mtpos position; |
Matt Kraai | 13a70ac | 2001-05-21 15:00:39 +0000 | [diff] [blame] | 60 | int fd, mode; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 61 | |
Eric Andersen | 1eec89d | 2000-09-05 18:21:53 +0000 | [diff] [blame] | 62 | if (argc < 2) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | bb_show_usage(); |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 64 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | |
| 66 | if (strcmp(argv[1], "-f") == 0) { |
| 67 | if (argc < 4) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 68 | bb_show_usage(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 69 | } |
| 70 | file = argv[2]; |
| 71 | argv += 2; |
| 72 | argc -= 2; |
| 73 | } |
| 74 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 75 | while (code->name != 0) { |
| 76 | if (strcmp(code->name, argv[1]) == 0) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 77 | break; |
| 78 | code++; |
| 79 | } |
| 80 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | if (code->name == 0) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 82 | bb_error_msg("unrecognized opcode %s", argv[1]); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 83 | return EXIT_FAILURE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | op.mt_op = code->value; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 87 | if (argc >= 3) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 88 | op.mt_count = xatoi_u(argv[2]); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 89 | else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 90 | op.mt_count = 1; /* One, not zero, right? */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 91 | |
Matt Kraai | 13a70ac | 2001-05-21 15:00:39 +0000 | [diff] [blame] | 92 | 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-Fischer | 64d7e93 | 2006-09-11 16:01:40 +0000 | [diff] [blame] | 105 | fd = xopen(file, mode); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 106 | |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 107 | switch (code->value) { |
| 108 | case MTTELL: |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 109 | ioctl_or_perror_and_die(fd, MTIOCPOS, &position, "%s", file); |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 110 | printf("At block %d.\n", (int) position.mt_blkno); |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 111 | break; |
| 112 | |
| 113 | default: |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 114 | ioctl_or_perror_and_die(fd, MTIOCTOP, &op, "%s", file); |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 115 | break; |
| 116 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 117 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 118 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 119 | } |