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 "internal.h" |
| 3 | #include <stdio.h> |
| 4 | #include <sys/mtio.h> |
| 5 | #include <sys/fcntl.h> |
| 6 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 7 | static const char mt_usage[] = "mt [-f device] opcode value\n"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 8 | |
| 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}, |
| 38 | {"rew", MTREW}, |
| 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; |
| 58 | int fd; |
| 59 | |
| 60 | if (strcmp(argv[1], "-f") == 0) { |
| 61 | if (argc < 4) { |
| 62 | usage(mt_usage); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 63 | } |
| 64 | file = argv[2]; |
| 65 | argv += 2; |
| 66 | argc -= 2; |
| 67 | } |
| 68 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | while (code->name != 0) { |
| 70 | if (strcmp(code->name, argv[1]) == 0) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 71 | break; |
| 72 | code++; |
| 73 | } |
| 74 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 75 | if (code->name == 0) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 76 | fprintf(stderr, "mt: unrecognized opcode %s.\n", argv[1]); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | return (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | op.mt_op = code->value; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | if (argc >= 3) |
| 82 | op.mt_count = atoi(argv[2]); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 83 | else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 84 | op.mt_count = 1; /* One, not zero, right? */ |
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 | if ((fd = open(file, O_RDONLY, 0)) < 0) { |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 87 | perror(file); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 88 | return (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 91 | if (ioctl(fd, MTIOCTOP, &op) != 0) { |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 92 | perror(file); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 93 | return (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 97 | } |