aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2011-05-21 18:38:40 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2011-05-21 18:38:40 +0200
commit3ef344be91b153f863b91afdf6b0a0e441ac4ac7 (patch)
tree0fb179171e600627050ea949c2c560d31123a08d /coreutils
parent5c10fa5c241b116b8c623189a42673754b21dc8c (diff)
downloadbusybox-w32-3ef344be91b153f863b91afdf6b0a0e441ac4ac7.tar.gz
busybox-w32-3ef344be91b153f863b91afdf6b0a0e441ac4ac7.tar.bz2
busybox-w32-3ef344be91b153f863b91afdf6b0a0e441ac4ac7.zip
od: code shrink
function old new delta exit_code - 1 +1 open_next_file 73 72 -1 limit_bytes_to_format 1 - -1 ioerror 1 - -1 flag_pseudo_start 1 - -1 flag_dump_strings 1 - -1 od_main 2275 2164 -111 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/od_bloaty.c160
1 files changed, 78 insertions, 82 deletions
diff --git a/coreutils/od_bloaty.c b/coreutils/od_bloaty.c
index fcd9f60d6..96a4186e2 100644
--- a/coreutils/od_bloaty.c
+++ b/coreutils/od_bloaty.c
@@ -18,6 +18,11 @@
18/* Written by Jim Meyering. */ 18/* Written by Jim Meyering. */
19/* Busyboxed by Denys Vlasenko, based on od.c from coreutils-5.2.1 */ 19/* Busyboxed by Denys Vlasenko, based on od.c from coreutils-5.2.1 */
20 20
21
22/* #include "libbb.h" - done in od.c */
23#define assert(a) ((void)0)
24
25
21//usage:#if ENABLE_DESKTOP 26//usage:#if ENABLE_DESKTOP
22//usage:#define od_trivial_usage 27//usage:#define od_trivial_usage
23//usage: "[-abcdfhilovxs] [-t TYPE] [-A RADIX] [-N SIZE] [-j SKIP] [-S MINSTR] [-w WIDTH] [FILE...]" 28//usage: "[-abcdfhilovxs] [-t TYPE] [-A RADIX] [-N SIZE] [-j SKIP] [-S MINSTR] [-w WIDTH] [FILE...]"
@@ -30,9 +35,39 @@
30//usage: "Print FILEs (or stdin) unambiguously, as octal bytes by default" 35//usage: "Print FILEs (or stdin) unambiguously, as octal bytes by default"
31//usage:#endif 36//usage:#endif
32 37
33/* #include "libbb.h" - done in od.c */ 38enum {
39 OPT_A = 1 << 0,
40 OPT_N = 1 << 1,
41 OPT_a = 1 << 2,
42 OPT_b = 1 << 3,
43 OPT_c = 1 << 4,
44 OPT_d = 1 << 5,
45 OPT_f = 1 << 6,
46 OPT_h = 1 << 7,
47 OPT_i = 1 << 8,
48 OPT_j = 1 << 9,
49 OPT_l = 1 << 10,
50 OPT_o = 1 << 11,
51 OPT_t = 1 << 12,
52 /* When zero and two or more consecutive blocks are equal, format
53 only the first block and output an asterisk alone on the following
54 line to indicate that identical blocks have been elided: */
55 OPT_v = 1 << 13,
56 OPT_x = 1 << 14,
57 OPT_s = 1 << 15,
58 OPT_S = 1 << 16,
59 OPT_w = 1 << 17,
60 OPT_traditional = (1 << 18) * ENABLE_LONG_OPTS,
61};
62
63#define OD_GETOPT32() getopt32(argv, \
64 "A:N:abcdfhij:lot:vxsS:w::", \
65 /* -w with optional param */ \
66 /* -S was -s and also had optional parameter */ \
67 /* but in coreutils 6.3 it was renamed and now has */ \
68 /* _mandatory_ parameter */ \
69 &str_A, &str_N, &str_j, &lst_t, &str_S, &bytes_per_block)
34 70
35#define assert(a) ((void)0)
36 71
37/* Check for 0x7f is a coreutils 6.3 addition */ 72/* Check for 0x7f is a coreutils 6.3 addition */
38#define ISPRINT(c) (((c)>=' ') && (c) != 0x7f) 73#define ISPRINT(c) (((c)>=' ') && (c) != 0x7f)
@@ -139,15 +174,7 @@ struct ERR_width_bytes_has_bad_size {
139 char ERR_width_bytes_has_bad_size[ARRAY_SIZE(width_bytes) == N_SIZE_SPECS ? 1 : -1]; 174 char ERR_width_bytes_has_bad_size[ARRAY_SIZE(width_bytes) == N_SIZE_SPECS ? 1 : -1];
140}; 175};
141 176
142static smallint flag_dump_strings; 177static smallint exit_code;
143/* Non-zero if an old-style 'pseudo-address' was specified. */
144static smallint flag_pseudo_start;
145static smallint limit_bytes_to_format;
146/* When zero and two or more consecutive blocks are equal, format
147 only the first block and output an asterisk alone on the following
148 line to indicate that identical blocks have been elided. */
149static smallint verbose;
150static smallint ioerror;
151 178
152static size_t string_min; 179static size_t string_min;
153 180
@@ -160,7 +187,11 @@ static struct tspec *spec;
160static void (*format_address)(off_t, char); 187static void (*format_address)(off_t, char);
161/* The difference between the old-style pseudo starting address and 188/* The difference between the old-style pseudo starting address and
162 the number of bytes to skip. */ 189 the number of bytes to skip. */
190#if ENABLE_LONG_OPTS
163static off_t pseudo_offset; 191static off_t pseudo_offset;
192#else
193enum { pseudo_offset = 0 };
194#endif
164/* When zero, MAX_BYTES_TO_FORMAT and END_OFFSET are ignored, and all 195/* When zero, MAX_BYTES_TO_FORMAT and END_OFFSET are ignored, and all
165 input is formatted. */ 196 input is formatted. */
166 197
@@ -453,10 +484,10 @@ open_next_file(void)
453 if (in_stream) { 484 if (in_stream) {
454 break; 485 break;
455 } 486 }
456 ioerror = 1; 487 exit_code = 1;
457 } 488 }
458 489
459 if (limit_bytes_to_format && !flag_dump_strings) 490 if ((option_mask32 & (OPT_N|OPT_S)) == OPT_N)
460 setbuf(in_stream, NULL); 491 setbuf(in_stream, NULL);
461} 492}
462 493
@@ -476,7 +507,7 @@ check_and_close(void)
476 ? bb_msg_standard_input 507 ? bb_msg_standard_input
477 : file_list[-1] 508 : file_list[-1]
478 ); 509 );
479 ioerror = 1; 510 exit_code = 1;
480 } 511 }
481 fclose_if_not_stdin(in_stream); 512 fclose_if_not_stdin(in_stream);
482 in_stream = NULL; 513 in_stream = NULL;
@@ -484,7 +515,7 @@ check_and_close(void)
484 515
485 if (ferror(stdout)) { 516 if (ferror(stdout)) {
486 bb_error_msg(bb_msg_write_error); 517 bb_error_msg(bb_msg_write_error);
487 ioerror = 1; 518 exit_code = 1;
488 } 519 }
489} 520}
490 521
@@ -762,7 +793,7 @@ skip(off_t n_skip)
762 /* take "check & close / open_next" route */ 793 /* take "check & close / open_next" route */
763 } else { 794 } else {
764 if (fseeko(in_stream, n_skip, SEEK_CUR) != 0) 795 if (fseeko(in_stream, n_skip, SEEK_CUR) != 0)
765 ioerror = 1; 796 exit_code = 1;
766 return; 797 return;
767 } 798 }
768 } else { 799 } else {
@@ -863,7 +894,8 @@ write_block(off_t current_offset, size_t n_bytes,
863 static char prev_pair_equal = 0; 894 static char prev_pair_equal = 0;
864 size_t i; 895 size_t i;
865 896
866 if (!verbose && !first 897 if (!(option_mask32 & OPT_v)
898 && !first
867 && n_bytes == bytes_per_block 899 && n_bytes == bytes_per_block
868 && memcmp(prev_block, curr_block, bytes_per_block) == 0 900 && memcmp(prev_block, curr_block, bytes_per_block) == 0
869 ) { 901 ) {
@@ -956,7 +988,7 @@ dump(off_t current_offset, off_t end_offset)
956 block[1] = block[0] + bytes_per_block; 988 block[1] = block[0] + bytes_per_block;
957 989
958 idx = 0; 990 idx = 0;
959 if (limit_bytes_to_format) { 991 if (option_mask32 & OPT_N) {
960 while (1) { 992 while (1) {
961 size_t n_needed; 993 size_t n_needed;
962 if (current_offset >= end_offset) { 994 if (current_offset >= end_offset) {
@@ -1005,7 +1037,7 @@ dump(off_t current_offset, off_t end_offset)
1005 1037
1006 format_address(current_offset, '\n'); 1038 format_address(current_offset, '\n');
1007 1039
1008 if (limit_bytes_to_format && current_offset >= end_offset) 1040 if ((option_mask32 & OPT_N) && current_offset >= end_offset)
1009 check_and_close(); 1041 check_and_close();
1010 1042
1011 free(block[0]); 1043 free(block[0]);
@@ -1066,10 +1098,10 @@ dump_strings(off_t address, off_t end_offset)
1066 1098
1067 /* See if the next 'string_min' chars are all printing chars. */ 1099 /* See if the next 'string_min' chars are all printing chars. */
1068 tryline: 1100 tryline:
1069 if (limit_bytes_to_format && (end_offset - string_min <= address)) 1101 if ((option_mask32 & OPT_N) && (end_offset - string_min <= address))
1070 break; 1102 break;
1071 i = 0; 1103 i = 0;
1072 while (!limit_bytes_to_format || address < end_offset) { 1104 while (!(option_mask32 & OPT_N) || address < end_offset) {
1073 if (i == bufsize) { 1105 if (i == bufsize) {
1074 bufsize += bufsize/8; 1106 bufsize += bufsize/8;
1075 buf = xrealloc(buf, bufsize); 1107 buf = xrealloc(buf, bufsize);
@@ -1163,27 +1195,6 @@ int od_main(int argc UNUSED_PARAM, char **argv)
1163 { "m", 1024*1024 }, 1195 { "m", 1024*1024 },
1164 { "", 0 } 1196 { "", 0 }
1165 }; 1197 };
1166 enum {
1167 OPT_A = 1 << 0,
1168 OPT_N = 1 << 1,
1169 OPT_a = 1 << 2,
1170 OPT_b = 1 << 3,
1171 OPT_c = 1 << 4,
1172 OPT_d = 1 << 5,
1173 OPT_f = 1 << 6,
1174 OPT_h = 1 << 7,
1175 OPT_i = 1 << 8,
1176 OPT_j = 1 << 9,
1177 OPT_l = 1 << 10,
1178 OPT_o = 1 << 11,
1179 OPT_t = 1 << 12,
1180 OPT_v = 1 << 13,
1181 OPT_x = 1 << 14,
1182 OPT_s = 1 << 15,
1183 OPT_S = 1 << 16,
1184 OPT_w = 1 << 17,
1185 OPT_traditional = (1 << 18) * ENABLE_LONG_OPTS,
1186 };
1187#if ENABLE_LONG_OPTS 1198#if ENABLE_LONG_OPTS
1188 static const char od_longopts[] ALIGN1 = 1199 static const char od_longopts[] ALIGN1 =
1189 "skip-bytes\0" Required_argument "j" 1200 "skip-bytes\0" Required_argument "j"
@@ -1200,9 +1211,6 @@ int od_main(int argc UNUSED_PARAM, char **argv)
1200 llist_t *lst_t = NULL; 1211 llist_t *lst_t = NULL;
1201 unsigned opt; 1212 unsigned opt;
1202 int l_c_m; 1213 int l_c_m;
1203 /* The old-style 'pseudo starting address' to be printed in parentheses
1204 after any true address. */
1205 off_t pseudo_start = pseudo_start; // for gcc
1206 /* The number of input bytes to skip before formatting and writing. */ 1214 /* The number of input bytes to skip before formatting and writing. */
1207 off_t n_bytes_to_skip = 0; 1215 off_t n_bytes_to_skip = 0;
1208 /* The offset of the first byte after the last byte to be formatted. */ 1216 /* The offset of the first byte after the last byte to be formatted. */
@@ -1214,19 +1222,13 @@ int od_main(int argc UNUSED_PARAM, char **argv)
1214 format_address = format_address_std; 1222 format_address = format_address_std;
1215 address_base_char = 'o'; 1223 address_base_char = 'o';
1216 address_pad_len_char = '7'; 1224 address_pad_len_char = '7';
1217 /* flag_dump_strings = 0; - already is */
1218 1225
1219 /* Parse command line */ 1226 /* Parse command line */
1220 opt_complementary = "w+:t::"; /* -w N, -t is a list */ 1227 opt_complementary = "w+:t::"; /* -w N, -t is a list */
1221#if ENABLE_LONG_OPTS 1228#if ENABLE_LONG_OPTS
1222 applet_long_options = od_longopts; 1229 applet_long_options = od_longopts;
1223#endif 1230#endif
1224 opt = getopt32(argv, "A:N:abcdfhij:lot:vxsS:" 1231 opt = OD_GETOPT32();
1225 "w::", // -w with optional param
1226 // -S was -s and also had optional parameter
1227 // but in coreutils 6.3 it was renamed and now has
1228 // _mandatory_ parameter
1229 &str_A, &str_N, &str_j, &lst_t, &str_S, &bytes_per_block);
1230 argv += optind; 1232 argv += optind;
1231 if (opt & OPT_A) { 1233 if (opt & OPT_A) {
1232 static const char doxn[] ALIGN1 = "doxn"; 1234 static const char doxn[] ALIGN1 = "doxn";
@@ -1248,7 +1250,6 @@ int od_main(int argc UNUSED_PARAM, char **argv)
1248 address_pad_len_char = doxn_address_pad_len_char[pos]; 1250 address_pad_len_char = doxn_address_pad_len_char[pos];
1249 } 1251 }
1250 if (opt & OPT_N) { 1252 if (opt & OPT_N) {
1251 limit_bytes_to_format = 1;
1252 max_bytes_to_format = xstrtooff_sfx(str_N, 0, bkm); 1253 max_bytes_to_format = xstrtooff_sfx(str_N, 0, bkm);
1253 } 1254 }
1254 if (opt & OPT_a) decode_format_string("a"); 1255 if (opt & OPT_a) decode_format_string("a");
@@ -1261,22 +1262,18 @@ int od_main(int argc UNUSED_PARAM, char **argv)
1261 if (opt & OPT_j) n_bytes_to_skip = xstrtooff_sfx(str_j, 0, bkm); 1262 if (opt & OPT_j) n_bytes_to_skip = xstrtooff_sfx(str_j, 0, bkm);
1262 if (opt & OPT_l) decode_format_string("d4"); 1263 if (opt & OPT_l) decode_format_string("d4");
1263 if (opt & OPT_o) decode_format_string("o2"); 1264 if (opt & OPT_o) decode_format_string("o2");
1264 //if (opt & OPT_t)...
1265 while (lst_t) { 1265 while (lst_t) {
1266 decode_format_string(llist_pop(&lst_t)); 1266 decode_format_string(llist_pop(&lst_t));
1267 } 1267 }
1268 if (opt & OPT_v) verbose = 1;
1269 if (opt & OPT_x) decode_format_string("x2"); 1268 if (opt & OPT_x) decode_format_string("x2");
1270 if (opt & OPT_s) decode_format_string("d2"); 1269 if (opt & OPT_s) decode_format_string("d2");
1271 if (opt & OPT_S) { 1270 if (opt & OPT_S) {
1272 string_min = xstrtou_sfx(str_S, 0, bkm); 1271 string_min = xstrtou_sfx(str_S, 0, bkm);
1273 flag_dump_strings = 1;
1274 } 1272 }
1275 //if (opt & OPT_w)...
1276 //if (opt & OPT_traditional)...
1277 1273
1278 if (flag_dump_strings && n_specs > 0) 1274 // Bloat:
1279 bb_error_msg_and_die("no type may be specified when dumping strings"); 1275 //if ((option_mask32 & OPT_S) && n_specs > 0)
1276 // bb_error_msg_and_die("no type may be specified when dumping strings");
1280 1277
1281 /* If the --traditional option is used, there may be from 1278 /* If the --traditional option is used, there may be from
1282 * 0 to 3 remaining command line arguments; handle each case 1279 * 0 to 3 remaining command line arguments; handle each case
@@ -1289,9 +1286,10 @@ int od_main(int argc UNUSED_PARAM, char **argv)
1289 1286
1290#if ENABLE_LONG_OPTS 1287#if ENABLE_LONG_OPTS
1291 if (opt & OPT_traditional) { 1288 if (opt & OPT_traditional) {
1292 off_t o1, o2;
1293
1294 if (argv[0]) { 1289 if (argv[0]) {
1290 off_t pseudo_start = -1;
1291 off_t o1, o2;
1292
1295 if (!argv[1]) { /* one arg */ 1293 if (!argv[1]) { /* one arg */
1296 if (parse_old_offset(argv[0], &o1)) { 1294 if (parse_old_offset(argv[0], &o1)) {
1297 /* od --traditional OFFSET */ 1295 /* od --traditional OFFSET */
@@ -1305,7 +1303,6 @@ int od_main(int argc UNUSED_PARAM, char **argv)
1305 ) { 1303 ) {
1306 /* od --traditional OFFSET LABEL */ 1304 /* od --traditional OFFSET LABEL */
1307 n_bytes_to_skip = o1; 1305 n_bytes_to_skip = o1;
1308 flag_pseudo_start = 1;
1309 pseudo_start = o2; 1306 pseudo_start = o2;
1310 argv += 2; 1307 argv += 2;
1311 } else if (parse_old_offset(argv[1], &o2)) { 1308 } else if (parse_old_offset(argv[1], &o2)) {
@@ -1321,7 +1318,6 @@ int od_main(int argc UNUSED_PARAM, char **argv)
1321 ) { 1318 ) {
1322 /* od --traditional FILE OFFSET LABEL */ 1319 /* od --traditional FILE OFFSET LABEL */
1323 n_bytes_to_skip = o1; 1320 n_bytes_to_skip = o1;
1324 flag_pseudo_start = 1;
1325 pseudo_start = o2; 1321 pseudo_start = o2;
1326 argv[1] = NULL; 1322 argv[1] = NULL;
1327 } else { 1323 } else {
@@ -1330,21 +1326,23 @@ int od_main(int argc UNUSED_PARAM, char **argv)
1330 } else { /* >3 args */ 1326 } else { /* >3 args */
1331 bb_error_msg_and_die("too many arguments"); 1327 bb_error_msg_and_die("too many arguments");
1332 } 1328 }
1333 }
1334 /* else: od --traditional (without args) */
1335 1329
1336 if (flag_pseudo_start) { 1330 if (pseudo_start >= 0) {
1337 if (format_address == format_address_none) { 1331 if (format_address == format_address_none) {
1338 address_base_char = 'o'; 1332 address_base_char = 'o';
1339 address_pad_len_char = '7'; 1333 address_pad_len_char = '7';
1340 format_address = format_address_paren; 1334 format_address = format_address_paren;
1341 } else 1335 } else {
1342 format_address = format_address_label; 1336 format_address = format_address_label;
1337 }
1338 pseudo_offset = pseudo_start - n_bytes_to_skip;
1339 }
1343 } 1340 }
1341 /* else: od --traditional (without args) */
1344 } 1342 }
1345#endif 1343#endif
1346 1344
1347 if (limit_bytes_to_format) { 1345 if (option_mask32 & OPT_N) {
1348 end_offset = n_bytes_to_skip + max_bytes_to_format; 1346 end_offset = n_bytes_to_skip + max_bytes_to_format;
1349 if (end_offset < n_bytes_to_skip) 1347 if (end_offset < n_bytes_to_skip)
1350 bb_error_msg_and_die("SKIP + SIZE is too large"); 1348 bb_error_msg_and_die("SKIP + SIZE is too large");
@@ -1352,7 +1350,7 @@ int od_main(int argc UNUSED_PARAM, char **argv)
1352 1350
1353 if (n_specs == 0) { 1351 if (n_specs == 0) {
1354 decode_format_string("o2"); 1352 decode_format_string("o2");
1355 n_specs = 1; 1353 /*n_specs = 1; - done by decode_format_string */
1356 } 1354 }
1357 1355
1358 /* If no files were listed on the command line, 1356 /* If no files were listed on the command line,
@@ -1365,16 +1363,14 @@ int od_main(int argc UNUSED_PARAM, char **argv)
1365 file_list = (char const *const *) argv; 1363 file_list = (char const *const *) argv;
1366 } 1364 }
1367 1365
1368 /* open the first input file */ 1366 /* Open the first input file */
1369 open_next_file(); 1367 open_next_file();
1370 /* skip over any unwanted header bytes */ 1368 /* Skip over any unwanted header bytes */
1371 skip(n_bytes_to_skip); 1369 skip(n_bytes_to_skip);
1372 if (!in_stream) 1370 if (!in_stream)
1373 return EXIT_FAILURE; 1371 return EXIT_FAILURE;
1374 1372
1375 pseudo_offset = (flag_pseudo_start ? pseudo_start - n_bytes_to_skip : 0); 1373 /* Compute output block length */
1376
1377 /* Compute output block length. */
1378 l_c_m = get_lcm(); 1374 l_c_m = get_lcm();
1379 1375
1380 if (opt & OPT_w) { /* -w: width */ 1376 if (opt & OPT_w) { /* -w: width */
@@ -1396,7 +1392,7 @@ int od_main(int argc UNUSED_PARAM, char **argv)
1396 } 1392 }
1397#endif 1393#endif
1398 1394
1399 if (flag_dump_strings) 1395 if (option_mask32 & OPT_S)
1400 dump_strings(n_bytes_to_skip, end_offset); 1396 dump_strings(n_bytes_to_skip, end_offset);
1401 else 1397 else
1402 dump(n_bytes_to_skip, end_offset); 1398 dump(n_bytes_to_skip, end_offset);
@@ -1404,5 +1400,5 @@ int od_main(int argc UNUSED_PARAM, char **argv)
1404 if (fclose(stdin) == EOF) 1400 if (fclose(stdin) == EOF)
1405 bb_perror_msg_and_die(bb_msg_standard_input); 1401 bb_perror_msg_and_die(bb_msg_standard_input);
1406 1402
1407 return ioerror; 1403 return exit_code;
1408} 1404}