These messages can be .rodata, so make them even more const.
[oweals/busybox.git] / mt.c
1 /* vi: set sw=4 ts=4: */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/mtio.h>
6 #include <sys/fcntl.h>
7 #include "busybox.h"
8
9 struct mt_opcodes {
10         char *name;
11         short value;
12 };
13
14 /* missing: eod/seod, stoptions, stwrthreshold, densities */
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}
51 };
52
53 extern int mt_main(int argc, char **argv)
54 {
55         const char *file = "/dev/tape";
56         const struct mt_opcodes *code = opcodes;
57         struct mtop op;
58         int fd;
59         
60         if (argc < 2) {
61                 show_usage();
62         }
63
64         if (strcmp(argv[1], "-f") == 0) {
65                 if (argc < 4) {
66                         show_usage();
67                 }
68                 file = argv[2];
69                 argv += 2;
70                 argc -= 2;
71         }
72
73         while (code->name != 0) {
74                 if (strcmp(code->name, argv[1]) == 0)
75                         break;
76                 code++;
77         }
78
79         if (code->name == 0) {
80                 error_msg("unrecognized opcode %s.", argv[1]);
81                 return EXIT_FAILURE;
82         }
83
84         op.mt_op = code->value;
85         if (argc >= 3)
86                 op.mt_count = atoi(argv[2]);
87         else
88                 op.mt_count = 1;                /* One, not zero, right? */
89
90         if ((fd = open(file, O_RDONLY, 0)) < 0)
91                 perror_msg_and_die("%s", file);
92
93         if (ioctl(fd, MTIOCTOP, &op) != 0)
94                 perror_msg_and_die("%s", file);
95
96         return EXIT_SUCCESS;
97 }