Removed trailing \n from error_msg{,_and_die} messages.
[oweals/busybox.git] / mt.c
1 /* vi: set sw=4 ts=4: */
2 #include "busybox.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/mtio.h>
6 #include <sys/fcntl.h>
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 mt_main(int argc, char **argv)
53 {
54         const char *file = "/dev/tape";
55         const struct mt_opcodes *code = opcodes;
56         struct mtop op;
57         int fd;
58         
59         if (argc < 2) {
60                 usage(mt_usage);
61         }
62
63         if (strcmp(argv[1], "-f") == 0) {
64                 if (argc < 4) {
65                         usage(mt_usage);
66                 }
67                 file = argv[2];
68                 argv += 2;
69                 argc -= 2;
70         }
71
72         while (code->name != 0) {
73                 if (strcmp(code->name, argv[1]) == 0)
74                         break;
75                 code++;
76         }
77
78         if (code->name == 0) {
79                 error_msg("unrecognized opcode %s.", argv[1]);
80                 return EXIT_FAILURE;
81         }
82
83         op.mt_op = code->value;
84         if (argc >= 3)
85                 op.mt_count = atoi(argv[2]);
86         else
87                 op.mt_count = 1;                /* One, not zero, right? */
88
89         if ((fd = open(file, O_RDONLY, 0)) < 0)
90                 perror_msg_and_die("%s", file);
91
92         if (ioctl(fd, MTIOCTOP, &op) != 0)
93                 perror_msg_and_die("%s", file);
94
95         return EXIT_SUCCESS;
96 }