aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-10-11 19:38:59 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-10-11 19:38:59 +0000
commit8add0685733cce787e7256dd7a938b5d44809662 (patch)
treeb5feb3dc2acdd4d4bedadbe6895c98d5a67c1a54
parent2ea8c40e8f642ded3d68e99f0675ce2e7b8794ee (diff)
downloadbusybox-w32-8add0685733cce787e7256dd7a938b5d44809662.tar.gz
busybox-w32-8add0685733cce787e7256dd7a938b5d44809662.tar.bz2
busybox-w32-8add0685733cce787e7256dd7a938b5d44809662.zip
mt: eliminate vector of structures with pointers (bad for libbusybox).
It's a win for static build too: function old new delta opcode_name - 213 +213 opcode_value - 68 +68 mt_main 281 256 -25 opcodes 280 - -280 ------------------------------------------------------------------------------ (add/remove: 2/1 grow/shrink: 0/1 up/down: 281/-305) Total: -24 bytes text data bss dec hex filename 767403 974 9420 777797 bde45 busybox_old 767224 974 9420 777618 bdd92 busybox_unstripped
-rw-r--r--miscutils/mt.c135
1 files changed, 79 insertions, 56 deletions
diff --git a/miscutils/mt.c b/miscutils/mt.c
index be1f21a12..c16073c61 100644
--- a/miscutils/mt.c
+++ b/miscutils/mt.c
@@ -6,58 +6,87 @@
6#include "libbb.h" 6#include "libbb.h"
7#include <sys/mtio.h> 7#include <sys/mtio.h>
8 8
9struct mt_opcodes {
10 const char *name;
11 short value;
12};
13
14/* missing: eod/seod, stoptions, stwrthreshold, densities */ 9/* missing: eod/seod, stoptions, stwrthreshold, densities */
15static const struct mt_opcodes opcodes[] = { 10static const short opcode_value[] = {
16 {"bsf", MTBSF}, 11 MTBSF,
17 {"bsfm", MTBSFM}, 12 MTBSFM,
18 {"bsr", MTBSR}, 13 MTBSR,
19 {"bss", MTBSS}, 14 MTBSS,
20 {"datacompression", MTCOMPRESSION}, 15 MTCOMPRESSION,
21 {"eom", MTEOM}, 16 MTEOM,
22 {"erase", MTERASE}, 17 MTERASE,
23 {"fsf", MTFSF}, 18 MTFSF,
24 {"fsfm", MTFSFM}, 19 MTFSFM,
25 {"fsr", MTFSR}, 20 MTFSR,
26 {"fss", MTFSS}, 21 MTFSS,
27 {"load", MTLOAD}, 22 MTLOAD,
28 {"lock", MTLOCK}, 23 MTLOCK,
29 {"mkpart", MTMKPART}, 24 MTMKPART,
30 {"nop", MTNOP}, 25 MTNOP,
31 {"offline", MTOFFL}, 26 MTOFFL,
32 {"rewoffline", MTOFFL}, 27 MTOFFL,
33 {"ras1", MTRAS1}, 28 MTRAS1,
34 {"ras2", MTRAS2}, 29 MTRAS2,
35 {"ras3", MTRAS3}, 30 MTRAS3,
36 {"reset", MTRESET}, 31 MTRESET,
37 {"retension", MTRETEN}, 32 MTRETEN,
38 {"rewind", MTREW}, 33 MTREW,
39 {"seek", MTSEEK}, 34 MTSEEK,
40 {"setblk", MTSETBLK}, 35 MTSETBLK,
41 {"setdensity", MTSETDENSITY}, 36 MTSETDENSITY,
42 {"drvbuffer", MTSETDRVBUFFER}, 37 MTSETDRVBUFFER,
43 {"setpart", MTSETPART}, 38 MTSETPART,
44 {"tell", MTTELL}, 39 MTTELL,
45 {"wset", MTWSM}, 40 MTWSM,
46 {"unload", MTUNLOAD}, 41 MTUNLOAD,
47 {"unlock", MTUNLOCK}, 42 MTUNLOCK,
48 {"eof", MTWEOF}, 43 MTWEOF,
49 {"weof", MTWEOF}, 44 MTWEOF
50 {0, 0}
51}; 45};
52 46
47static 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
53int mt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 83int mt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
54int mt_main(int argc, char **argv) 84int mt_main(int argc, char **argv)
55{ 85{
56 const char *file = "/dev/tape"; 86 const char *file = "/dev/tape";
57 const struct mt_opcodes *code = opcodes;
58 struct mtop op; 87 struct mtop op;
59 struct mtpos position; 88 struct mtpos position;
60 int fd, mode; 89 int fd, mode, idx;
61 90
62 if (argc < 2) { 91 if (argc < 2) {
63 bb_show_usage(); 92 bb_show_usage();
@@ -72,24 +101,18 @@ int mt_main(int argc, char **argv)
72 argc -= 2; 101 argc -= 2;
73 } 102 }
74 103
75 while (code->name != 0) { 104 idx = index_in_strings(opcode_name, argv[1]);
76 if (strcmp(code->name, argv[1]) == 0)
77 break;
78 code++;
79 }
80 105
81 if (code->name == 0) { 106 if (idx < 0)
82 bb_error_msg("unrecognized opcode %s", argv[1]); 107 bb_error_msg_and_die("unrecognized opcode %s", argv[1]);
83 return EXIT_FAILURE;
84 }
85 108
86 op.mt_op = code->value; 109 op.mt_op = opcode_value[idx];
87 if (argc >= 3) 110 if (argc >= 3)
88 op.mt_count = xatoi_u(argv[2]); 111 op.mt_count = xatoi_u(argv[2]);
89 else 112 else
90 op.mt_count = 1; /* One, not zero, right? */ 113 op.mt_count = 1; /* One, not zero, right? */
91 114
92 switch (code->value) { 115 switch (opcode_value[idx]) {
93 case MTWEOF: 116 case MTWEOF:
94 case MTERASE: 117 case MTERASE:
95 case MTWSM: 118 case MTWSM:
@@ -104,10 +127,10 @@ int mt_main(int argc, char **argv)
104 127
105 fd = xopen(file, mode); 128 fd = xopen(file, mode);
106 129
107 switch (code->value) { 130 switch (opcode_value[idx]) {
108 case MTTELL: 131 case MTTELL:
109 ioctl_or_perror_and_die(fd, MTIOCPOS, &position, "%s", file); 132 ioctl_or_perror_and_die(fd, MTIOCPOS, &position, "%s", file);
110 printf("At block %d.\n", (int) position.mt_blkno); 133 printf("At block %d\n", (int) position.mt_blkno);
111 break; 134 break;
112 135
113 default: 136 default: