Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1 | #include "internal.h" |
| 2 | #include <stdio.h> |
| 3 | #include <sys/mtio.h> |
| 4 | #include <sys/fcntl.h> |
| 5 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 6 | static const char mt_usage[] = "mt [-f device] opcode value\n"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | |
| 8 | struct mt_opcodes { |
| 9 | char * name; |
| 10 | short value; |
| 11 | }; |
| 12 | |
| 13 | /* missing: eod/seod, stoptions, stwrthreshold, densities */ |
| 14 | static const struct mt_opcodes opcodes[] = { |
| 15 | { "bsf", MTBSF }, |
| 16 | { "bsfm", MTBSFM }, |
| 17 | { "bsr", MTBSR }, |
| 18 | { "bss", MTBSS }, |
| 19 | { "datacompression", MTCOMPRESSION }, |
| 20 | { "eom", MTEOM }, |
| 21 | { "erase", MTERASE }, |
| 22 | { "fsf", MTFSF }, |
| 23 | { "fsfm", MTFSFM }, |
| 24 | { "fsr", MTFSR }, |
| 25 | { "fss", MTFSS }, |
| 26 | { "load", MTLOAD }, |
| 27 | { "lock", MTLOCK }, |
| 28 | { "mkpart", MTMKPART }, |
| 29 | { "nop", MTNOP }, |
| 30 | { "offline",MTOFFL }, |
| 31 | { "rewoffline",MTOFFL }, |
| 32 | { "ras1", MTRAS1 }, |
| 33 | { "ras2", MTRAS2 }, |
| 34 | { "ras3", MTRAS3 }, |
| 35 | { "reset", MTRESET }, |
| 36 | { "retension", MTRETEN }, |
| 37 | { "rew", MTREW }, |
| 38 | { "seek", MTSEEK }, |
| 39 | { "setblk", MTSETBLK }, |
| 40 | { "setdensity", MTSETDENSITY }, |
| 41 | { "drvbuffer", MTSETDRVBUFFER }, |
| 42 | { "setpart", MTSETPART }, |
| 43 | { "tell", MTTELL }, |
| 44 | { "wset", MTWSM }, |
| 45 | { "unload", MTUNLOAD }, |
| 46 | { "unlock", MTUNLOCK }, |
| 47 | { "eof", MTWEOF }, |
| 48 | { "weof", MTWEOF }, |
| 49 | { 0, 0 } |
| 50 | }; |
| 51 | |
| 52 | extern int |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 53 | mt_main(int argc, char** argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 54 | { |
| 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 ) { |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 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 | |
| 69 | while ( code->name != 0 ) { |
| 70 | if ( strcmp(code->name, argv[1]) == 0 ) |
| 71 | break; |
| 72 | code++; |
| 73 | } |
| 74 | |
| 75 | if ( code->name == 0 ) { |
| 76 | fprintf(stderr, "mt: unrecognized opcode %s.\n", argv[1]); |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +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; |
| 81 | if ( argc >= 3 ) |
| 82 | op.mt_count = atoi(argv[2]); |
| 83 | else |
| 84 | op.mt_count = 1; /* One, not zero, right? */ |
| 85 | |
| 86 | if ( (fd = open(file, O_RDONLY, 0)) < 0 ) { |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 87 | perror(file); |
| 88 | return( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if ( ioctl(fd, MTIOCTOP, &op) != 0 ) { |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 92 | perror(file); |
| 93 | return( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 96 | return( TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 97 | } |