More stuff.
[oweals/busybox.git] / mt.c
1 #include "internal.h"
2 #include <stdio.h>
3 #include <sys/mtio.h>
4 #include <sys/fcntl.h>
5
6 const char      mt_usage[] = "mt [-f device] opcode value\n";
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
53 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 }