More documentation updates, and minor fixes to make things sync
[oweals/busybox.git] / mt.c
1 /* vi: set sw=4 ts=4: */
2 #include "internal.h"
3 #include <stdio.h>
4 #include <sys/mtio.h>
5 #include <sys/fcntl.h>
6
7 static const char mt_usage[] = "mt [-f device] opcode value\n\n"
8                         "Control magnetic tape drive operation\n";
9
10 struct mt_opcodes {
11         char *name;
12         short value;
13 };
14
15 /* missing: eod/seod, stoptions, stwrthreshold, densities */
16 static const struct mt_opcodes opcodes[] = {
17         {"bsf", MTBSF},
18         {"bsfm", MTBSFM},
19         {"bsr", MTBSR},
20         {"bss", MTBSS},
21         {"datacompression", MTCOMPRESSION},
22         {"eom", MTEOM},
23         {"erase", MTERASE},
24         {"fsf", MTFSF},
25         {"fsfm", MTFSFM},
26         {"fsr", MTFSR},
27         {"fss", MTFSS},
28         {"load", MTLOAD},
29         {"lock", MTLOCK},
30         {"mkpart", MTMKPART},
31         {"nop", MTNOP},
32         {"offline", MTOFFL},
33         {"rewoffline", MTOFFL},
34         {"ras1", MTRAS1},
35         {"ras2", MTRAS2},
36         {"ras3", MTRAS3},
37         {"reset", MTRESET},
38         {"retension", MTRETEN},
39         {"rew", MTREW},
40         {"seek", MTSEEK},
41         {"setblk", MTSETBLK},
42         {"setdensity", MTSETDENSITY},
43         {"drvbuffer", MTSETDRVBUFFER},
44         {"setpart", MTSETPART},
45         {"tell", MTTELL},
46         {"wset", MTWSM},
47         {"unload", MTUNLOAD},
48         {"unlock", MTUNLOCK},
49         {"eof", MTWEOF},
50         {"weof", MTWEOF},
51         {0, 0}
52 };
53
54 extern int mt_main(int argc, char **argv)
55 {
56         const char *file = "/dev/tape";
57         const struct mt_opcodes *code = opcodes;
58         struct mtop op;
59         int fd;
60         
61         if ((argc != 2 && argc != 3) || **(argv + 1) == '-') {
62                 usage(mt_usage);
63         }
64
65         if (strcmp(argv[1], "-f") == 0) {
66                 if (argc < 4) {
67                         usage(mt_usage);
68                 }
69                 file = argv[2];
70                 argv += 2;
71                 argc -= 2;
72         }
73
74         while (code->name != 0) {
75                 if (strcmp(code->name, argv[1]) == 0)
76                         break;
77                 code++;
78         }
79
80         if (code->name == 0) {
81                 fprintf(stderr, "mt: unrecognized opcode %s.\n", argv[1]);
82                 exit (FALSE);
83         }
84
85         op.mt_op = code->value;
86         if (argc >= 3)
87                 op.mt_count = atoi(argv[2]);
88         else
89                 op.mt_count = 1;                /* One, not zero, right? */
90
91         if ((fd = open(file, O_RDONLY, 0)) < 0) {
92                 perror(file);
93                 exit (FALSE);
94         }
95
96         if (ioctl(fd, MTIOCTOP, &op) != 0) {
97                 perror(file);
98                 exit (FALSE);
99         }
100
101         exit (TRUE);
102 }