Magellan Linux

Diff of /trunk/mkinitrd-magellan/busybox/miscutils/mt.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 815 by niro, Sat Sep 1 22:45:15 2007 UTC revision 816 by niro, Fri Apr 24 18:33:46 2009 UTC
# Line 3  Line 3 
3   * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.   * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4   */   */
5    
6  #include "busybox.h"  #include "libbb.h"
7  #include <sys/mtio.h>  #include <sys/mtio.h>
8    
 struct mt_opcodes {  
  char *name;  
  short value;  
 };  
   
9  /* missing: eod/seod, stoptions, stwrthreshold, densities */  /* missing: eod/seod, stoptions, stwrthreshold, densities */
10  static const struct mt_opcodes opcodes[] = {  static const short opcode_value[] = {
11   {"bsf", MTBSF},   MTBSF,
12   {"bsfm", MTBSFM},   MTBSFM,
13   {"bsr", MTBSR},   MTBSR,
14   {"bss", MTBSS},   MTBSS,
15   {"datacompression", MTCOMPRESSION},   MTCOMPRESSION,
16   {"eom", MTEOM},   MTEOM,
17   {"erase", MTERASE},   MTERASE,
18   {"fsf", MTFSF},   MTFSF,
19   {"fsfm", MTFSFM},   MTFSFM,
20   {"fsr", MTFSR},   MTFSR,
21   {"fss", MTFSS},   MTFSS,
22   {"load", MTLOAD},   MTLOAD,
23   {"lock", MTLOCK},   MTLOCK,
24   {"mkpart", MTMKPART},   MTMKPART,
25   {"nop", MTNOP},   MTNOP,
26   {"offline", MTOFFL},   MTOFFL,
27   {"rewoffline", MTOFFL},   MTOFFL,
28   {"ras1", MTRAS1},   MTRAS1,
29   {"ras2", MTRAS2},   MTRAS2,
30   {"ras3", MTRAS3},   MTRAS3,
31   {"reset", MTRESET},   MTRESET,
32   {"retension", MTRETEN},   MTRETEN,
33   {"rewind", MTREW},   MTREW,
34   {"seek", MTSEEK},   MTSEEK,
35   {"setblk", MTSETBLK},   MTSETBLK,
36   {"setdensity", MTSETDENSITY},   MTSETDENSITY,
37   {"drvbuffer", MTSETDRVBUFFER},   MTSETDRVBUFFER,
38   {"setpart", MTSETPART},   MTSETPART,
39   {"tell", MTTELL},   MTTELL,
40   {"wset", MTWSM},   MTWSM,
41   {"unload", MTUNLOAD},   MTUNLOAD,
42   {"unlock", MTUNLOCK},   MTUNLOCK,
43   {"eof", MTWEOF},   MTWEOF,
44   {"weof", MTWEOF},   MTWEOF
  {0, 0}  
45  };  };
46    
47  int mt_main(int argc, char **argv)  static const char opcode_name[] ALIGN1 =
48     "bsf"             "\0"
49     "bsfm"            "\0"
50     "bsr"             "\0"
51     "bss"             "\0"
52     "datacompression" "\0"
53     "eom"             "\0"
54     "erase"           "\0"
55     "fsf"             "\0"
56     "fsfm"            "\0"
57     "fsr"             "\0"
58     "fss"             "\0"
59     "load"            "\0"
60     "lock"            "\0"
61     "mkpart"          "\0"
62     "nop"             "\0"
63     "offline"         "\0"
64     "rewoffline"      "\0"
65     "ras1"            "\0"
66     "ras2"            "\0"
67     "ras3"            "\0"
68     "reset"           "\0"
69     "retension"       "\0"
70     "rewind"          "\0"
71     "seek"            "\0"
72     "setblk"          "\0"
73     "setdensity"      "\0"
74     "drvbuffer"       "\0"
75     "setpart"         "\0"
76     "tell"            "\0"
77     "wset"            "\0"
78     "unload"          "\0"
79     "unlock"          "\0"
80     "eof"             "\0"
81     "weof"            "\0";
82    
83    int mt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
84    int mt_main(int argc UNUSED_PARAM, char **argv)
85  {  {
86   const char *file = "/dev/tape";   const char *file = "/dev/tape";
  const struct mt_opcodes *code = opcodes;  
87   struct mtop op;   struct mtop op;
88   struct mtpos position;   struct mtpos position;
89   int fd, mode;   int fd, mode, idx;
90    
91   if (argc < 2) {   if (!argv[1]) {
92   bb_show_usage();   bb_show_usage();
93   }   }
94    
95   if (strcmp(argv[1], "-f") == 0) {   if (strcmp(argv[1], "-f") == 0) {
96   if (argc < 4) {   if (!argv[2] || !argv[3])
97   bb_show_usage();   bb_show_usage();
  }  
98   file = argv[2];   file = argv[2];
99   argv += 2;   argv += 2;
  argc -= 2;  
100   }   }
101    
102   while (code->name != 0) {   idx = index_in_strings(opcode_name, argv[1]);
  if (strcmp(code->name, argv[1]) == 0)  
  break;  
  code++;  
  }  
103    
104   if (code->name == 0) {   if (idx < 0)
105   bb_error_msg("unrecognized opcode %s", argv[1]);   bb_error_msg_and_die("unrecognized opcode %s", argv[1]);
  return EXIT_FAILURE;  
  }  
106    
107   op.mt_op = code->value;   op.mt_op = opcode_value[idx];
108   if (argc >= 3)   if (argv[2])
109   op.mt_count = xatoi_u(argv[2]);   op.mt_count = xatoi_u(argv[2]);
110   else   else
111   op.mt_count = 1; /* One, not zero, right? */   op.mt_count = 1; /* One, not zero, right? */
112    
113   switch (code->value) {   switch (opcode_value[idx]) {
114   case MTWEOF:   case MTWEOF:
115   case MTERASE:   case MTERASE:
116   case MTWSM:   case MTWSM:
# Line 103  int mt_main(int argc, char **argv) Line 125  int mt_main(int argc, char **argv)
125    
126   fd = xopen(file, mode);   fd = xopen(file, mode);
127    
128   switch (code->value) {   switch (opcode_value[idx]) {
129   case MTTELL:   case MTTELL:
130   if (ioctl(fd, MTIOCPOS, &position) < 0)   ioctl_or_perror_and_die(fd, MTIOCPOS, &position, "%s", file);
131   bb_perror_msg_and_die("%s", file);   printf("At block %d\n", (int) position.mt_blkno);
  printf("At block %d.\n", (int) position.mt_blkno);  
132   break;   break;
133    
134   default:   default:
135   if (ioctl(fd, MTIOCTOP, &op) != 0)   ioctl_or_perror_and_die(fd, MTIOCTOP, &op, "%s", file);
  bb_perror_msg_and_die("%s", file);  
136   break;   break;
137   }   }
138    

Legend:
Removed from v.815  
changed lines
  Added in v.816