Magellan Linux

Annotation of /tags/mkinitrd-6_1_12/busybox/miscutils/mt.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (hide annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (17 years ago) by niro
Original Path: trunk/mkinitrd-magellan/busybox/miscutils/mt.c
File MIME type: text/plain
File size: 2202 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

1 niro 532 /* vi: set sw=4 ts=4: */
2     /*
3     * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4     */
5    
6     #include "busybox.h"
7     #include <sys/mtio.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     {"rewind", 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     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     struct mtpos position;
59     int fd, mode;
60    
61     if (argc < 2) {
62     bb_show_usage();
63     }
64    
65     if (strcmp(argv[1], "-f") == 0) {
66     if (argc < 4) {
67     bb_show_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     bb_error_msg("unrecognized opcode %s", argv[1]);
82     return EXIT_FAILURE;
83     }
84    
85     op.mt_op = code->value;
86     if (argc >= 3)
87     op.mt_count = xatoi_u(argv[2]);
88     else
89     op.mt_count = 1; /* One, not zero, right? */
90    
91     switch (code->value) {
92     case MTWEOF:
93     case MTERASE:
94     case MTWSM:
95     case MTSETDRVBUFFER:
96     mode = O_WRONLY;
97     break;
98    
99     default:
100     mode = O_RDONLY;
101     break;
102     }
103    
104     fd = xopen(file, mode);
105    
106     switch (code->value) {
107     case MTTELL:
108     if (ioctl(fd, MTIOCPOS, &position) < 0)
109     bb_perror_msg_and_die("%s", file);
110     printf("At block %d.\n", (int) position.mt_blkno);
111     break;
112    
113     default:
114     if (ioctl(fd, MTIOCTOP, &op) != 0)
115     bb_perror_msg_and_die("%s", file);
116     break;
117     }
118    
119     return EXIT_SUCCESS;
120     }