Oops. Forgot the usleep.c file.
[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";
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 (strcmp(argv[1], "-f") == 0) {
61                 if (argc < 4) {
62                         usage(mt_usage);
63                 }
64                 file = argv[2];
65                 argv += 2;
66                 argc -= 2;
67         }
68
69         while (code->name != 0) {
70                 if (strcmp(code->name, argv[1]) == 0)
71                         break;
72                 code++;
73         }
74
75         if (code->name == 0) {
76                 fprintf(stderr, "mt: unrecognized opcode %s.\n", argv[1]);
77                 return (FALSE);
78         }
79
80         op.mt_op = code->value;
81         if (argc >= 3)
82                 op.mt_count = atoi(argv[2]);
83         else
84                 op.mt_count = 1;                /* One, not zero, right? */
85
86         if ((fd = open(file, O_RDONLY, 0)) < 0) {
87                 perror(file);
88                 return (FALSE);
89         }
90
91         if (ioctl(fd, MTIOCTOP, &op) != 0) {
92                 perror(file);
93                 return (FALSE);
94         }
95
96         return (TRUE);
97 }