Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 3 | #include <stdlib.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 4 | #include <string.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 5 | #include <sys/mtio.h> |
| 6 | #include <sys/fcntl.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 7 | #include "busybox.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 { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 10 | char *name; |
| 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 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 53 | extern int mt_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 54 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | const char *file = "/dev/tape"; |
| 56 | const struct mt_opcodes *code = opcodes; |
| 57 | struct mtop op; |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 58 | struct mtpos position; |
Matt Kraai | 13a70ac | 2001-05-21 15:00:39 +0000 | [diff] [blame] | 59 | int fd, mode; |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 60 | |
Eric Andersen | 1eec89d | 2000-09-05 18:21:53 +0000 | [diff] [blame] | 61 | if (argc < 2) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | bb_show_usage(); |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 63 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | |
| 65 | if (strcmp(argv[1], "-f") == 0) { |
| 66 | if (argc < 4) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 67 | bb_show_usage(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 68 | } |
| 69 | file = argv[2]; |
| 70 | argv += 2; |
| 71 | argc -= 2; |
| 72 | } |
| 73 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | while (code->name != 0) { |
| 75 | if (strcmp(code->name, argv[1]) == 0) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 76 | break; |
| 77 | code++; |
| 78 | } |
| 79 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 80 | if (code->name == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 81 | bb_error_msg("unrecognized opcode %s.", argv[1]); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 82 | return EXIT_FAILURE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | op.mt_op = code->value; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 86 | if (argc >= 3) |
| 87 | op.mt_count = atoi(argv[2]); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 88 | else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 89 | op.mt_count = 1; /* One, not zero, right? */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 90 | |
Matt Kraai | 13a70ac | 2001-05-21 15:00:39 +0000 | [diff] [blame] | 91 | switch (code->value) { |
| 92 | case MTWEOF: |
| 93 | case MTERASE: |
| 94 | case MTWSM: |
| 95 | case MTSETDRVBUFFER: |
| 96 | mode = O_WRONLY; |
| 97 | break; |
| 98 | |
| 99 | default: |
| 100 | mode = O_RDONLY; |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | if ((fd = open(file, mode, 0)) < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 105 | bb_perror_msg_and_die("%s", file); |
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: |
| 109 | if (ioctl(fd, MTIOCPOS, &position) < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 110 | bb_perror_msg_and_die("%s", file); |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 111 | printf ("At block %d.\n", (int) position.mt_blkno); |
| 112 | break; |
| 113 | |
| 114 | default: |
| 115 | if (ioctl(fd, MTIOCTOP, &op) != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 116 | bb_perror_msg_and_die("%s", file); |
Matt Kraai | 73991ff | 2001-05-21 17:01:32 +0000 | [diff] [blame] | 117 | break; |
| 118 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 119 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 120 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 121 | } |