blob: 9791b64b204b796cce108027165a520b1a2cfd09 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002#include "internal.h"
3#include <stdio.h>
4#include <sys/mtio.h>
5#include <sys/fcntl.h>
6
Erik Andersene49d5ec2000-02-08 19:58:47 +00007static const char mt_usage[] = "mt [-f device] opcode value\n";
Eric Andersencc8ed391999-10-05 16:24:54 +00008
9struct mt_opcodes {
Erik Andersene49d5ec2000-02-08 19:58:47 +000010 char *name;
11 short value;
Eric Andersencc8ed391999-10-05 16:24:54 +000012};
13
14/* missing: eod/seod, stoptions, stwrthreshold, densities */
Erik Andersene49d5ec2000-02-08 19:58:47 +000015static 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 Andersencc8ed391999-10-05 16:24:54 +000051};
52
Erik Andersene49d5ec2000-02-08 19:58:47 +000053extern int mt_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000054{
Erik Andersene49d5ec2000-02-08 19:58:47 +000055 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 Andersencc8ed391999-10-05 16:24:54 +000063 }
64 file = argv[2];
65 argv += 2;
66 argc -= 2;
67 }
68
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 while (code->name != 0) {
70 if (strcmp(code->name, argv[1]) == 0)
Eric Andersencc8ed391999-10-05 16:24:54 +000071 break;
72 code++;
73 }
74
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 if (code->name == 0) {
Eric Andersencc8ed391999-10-05 16:24:54 +000076 fprintf(stderr, "mt: unrecognized opcode %s.\n", argv[1]);
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000078 }
79
80 op.mt_op = code->value;
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 if (argc >= 3)
82 op.mt_count = atoi(argv[2]);
Eric Andersencc8ed391999-10-05 16:24:54 +000083 else
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 op.mt_count = 1; /* One, not zero, right? */
Eric Andersencc8ed391999-10-05 16:24:54 +000085
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 if ((fd = open(file, O_RDONLY, 0)) < 0) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000087 perror(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000089 }
90
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 if (ioctl(fd, MTIOCTOP, &op) != 0) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000092 perror(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 return (FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000094 }
95
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 return (TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000097}