7d75fbd3d50d9a298b0937cf07c6149a23735370
[oweals/busybox.git] / miscutils / 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(struct FileInfo * i, 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                         return 1;
64                 }
65                 file = argv[2];
66                 argv += 2;
67                 argc -= 2;
68         }
69
70         while ( code->name != 0 ) {
71                 if ( strcmp(code->name, argv[1]) == 0 )
72                         break;
73                 code++;
74         }
75
76         if ( code->name == 0 ) {
77                 fprintf(stderr, "mt: unrecognized opcode %s.\n", argv[1]);
78                 return 1;
79         }
80
81         op.mt_op = code->value;
82         if ( argc >= 3 )
83                 op.mt_count = atoi(argv[2]); 
84         else
85                 op.mt_count = 1; /* One, not zero, right? */
86
87         if ( (fd = open(file, O_RDONLY, 0)) < 0 ) {
88                 name_and_error(file);
89                 return 1;
90         }
91
92         if ( ioctl(fd, MTIOCTOP, &op) != 0 ) {
93                 name_and_error(file);
94                 return 1;
95         }
96
97         return 0;
98 }