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