aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-11-28 23:37:46 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-11-28 23:37:46 +0000
commit601ae1378ba7bb59e9c1a19fcc4ddd7bf9fb2e1b (patch)
tree79ad3707bb3374a2e429135ece7e604a26cde8c1
parentdf38188367a9fe10cc4efc00d3236be05178ff4e (diff)
downloadbusybox-w32-601ae1378ba7bb59e9c1a19fcc4ddd7bf9fb2e1b.tar.gz
busybox-w32-601ae1378ba7bb59e9c1a19fcc4ddd7bf9fb2e1b.tar.bz2
busybox-w32-601ae1378ba7bb59e9c1a19fcc4ddd7bf9fb2e1b.zip
od: sometime ago I landed BIG od implementation
from coreutils. My fault. This commit contains cleanups and size reductions.
-rw-r--r--coreutils/od_bloaty.c1370
-rw-r--r--networking/httpd.c16
-rw-r--r--networking/wget.c2
3 files changed, 627 insertions, 761 deletions
diff --git a/coreutils/od_bloaty.c b/coreutils/od_bloaty.c
index a88ca25dc..dff7e3f50 100644
--- a/coreutils/od_bloaty.c
+++ b/coreutils/od_bloaty.c
@@ -18,29 +18,55 @@
18/* Written by Jim Meyering. */ 18/* Written by Jim Meyering. */
19 19
20/* Busyboxed by Denis Vlasenko 20/* Busyboxed by Denis Vlasenko
21** Based on od.c from coreutils-5.2.1 21
22** Top bloat sources: 22Based on od.c from coreutils-5.2.1
23** get_lcm 111 23Top bloat sources:
24** print_ascii 145 24
25** format_address_std 159 2500000073 t parse_old_offset
26** check_and_close 160 260000007b t get_lcm
27** open_next_file 170 2700000090 r long_options
28** write_block 368 2800000092 t print_named_ascii
29** decode_format_string 1169 29000000bf t print_ascii
30** od_main 2623 3000000168 t write_block
31** TODO: get rid of getopt() 3100000366 t decode_format_string
3200000a71 T od_main
33
34Tested for compat with coreutils 6.3
35using this script. Minor differences fixed.
36
37#!/bin/sh
38echo STD
39time /path/to/coreutils/od \
40...params... \
41>std
42echo Exit code $?
43echo BBOX
44time ./busybox od \
45...params... \
46>bbox
47echo Exit code $?
48diff -u -a std bbox >bbox.diff || { echo Different!; sleep 1; }
49
32*/ 50*/
33 51
52
34#include "busybox.h" 53#include "busybox.h"
35#include <getopt.h> 54#include <getopt.h>
36 55
37#define assert(a) ((void)0) 56#define assert(a) ((void)0)
38#define ISPRINT(c) ((c)>=' ') 57
58/* Check for 0x7f is a coreutils 6.3 addition */
59#define ISPRINT(c) (((c)>=' ') && (c) != 0x7f)
39 60
40typedef long double longdouble_t; 61typedef long double longdouble_t;
41typedef unsigned long long ulonglong_t; 62typedef unsigned long long ulonglong_t;
42#define xstrtoumax_sfx xstrtoul_sfx 63typedef long long llong;
43#define xstrtoumax_range_sfx xstrtoul_range_sfx 64
65#if ENABLE_LFS
66# define xstrtooff_sfx xstrtoull_sfx
67#else
68# define xstrtooff_sfx xstrtoul_sfx
69#endif
44 70
45/* The default number of input bytes per output line. */ 71/* The default number of input bytes per output line. */
46#define DEFAULT_BYTES_PER_BLOCK 16 72#define DEFAULT_BYTES_PER_BLOCK 16
@@ -67,7 +93,6 @@ enum size_spec {
67 INT, 93 INT,
68 LONG, 94 LONG,
69 LONG_LONG, 95 LONG_LONG,
70 /* FIXME: add INTMAX support, too */
71 FLOAT_SINGLE, 96 FLOAT_SINGLE,
72 FLOAT_DOUBLE, 97 FLOAT_DOUBLE,
73 FLOAT_LONG_DOUBLE, 98 FLOAT_LONG_DOUBLE,
@@ -105,16 +130,16 @@ struct tspec {
105 10 unsigned decimal 130 10 unsigned decimal
106 8 unsigned hexadecimal */ 131 8 unsigned hexadecimal */
107 132
108static const unsigned char bytes_to_oct_digits[] = 133static const uint8_t bytes_to_oct_digits[] =
109{0, 3, 6, 8, 11, 14, 16, 19, 22, 25, 27, 30, 32, 35, 38, 41, 43}; 134{0, 3, 6, 8, 11, 14, 16, 19, 22, 25, 27, 30, 32, 35, 38, 41, 43};
110 135
111static const unsigned char bytes_to_signed_dec_digits[] = 136static const uint8_t bytes_to_signed_dec_digits[] =
112{1, 4, 6, 8, 11, 13, 16, 18, 20, 23, 25, 28, 30, 33, 35, 37, 40}; 137{1, 4, 6, 8, 11, 13, 16, 18, 20, 23, 25, 28, 30, 33, 35, 37, 40};
113 138
114static const unsigned char bytes_to_unsigned_dec_digits[] = 139static const uint8_t bytes_to_unsigned_dec_digits[] =
115{0, 3, 5, 8, 10, 13, 15, 17, 20, 22, 25, 27, 29, 32, 34, 37, 39}; 140{0, 3, 5, 8, 10, 13, 15, 17, 20, 22, 25, 27, 29, 32, 34, 37, 39};
116 141
117static const unsigned char bytes_to_hex_digits[] = 142static const uint8_t bytes_to_hex_digits[] =
118{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32}; 143{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32};
119 144
120/* Convert enum size_spec to the size of the named type. */ 145/* Convert enum size_spec to the size of the named type. */
@@ -137,57 +162,32 @@ struct dummy {
137 [sizeof width_bytes / sizeof width_bytes[0] == N_SIZE_SPECS ? 1 : -1]; 162 [sizeof width_bytes / sizeof width_bytes[0] == N_SIZE_SPECS ? 1 : -1];
138}; 163};
139 164
140/* Names for some non-printing characters. */
141static const char charname[33][4] = {
142 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
143 "bs", "ht", "nl", "vt", "ff", "cr", "so", "si",
144 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
145 "can", "em", "sub", "esc", "fs", "gs", "rs", "us",
146 "sp"
147};
148
149/* Address base (8, 10 or 16). */
150static int address_base;
151
152/* The number of octal digits required to represent the largest
153 address value. */
154#define MAX_ADDRESS_LENGTH \
155 ((sizeof(uintmax_t) * CHAR_BIT + CHAR_BIT - 1) / 3)
156
157/* Width of a normal address. */
158static int address_pad_len;
159
160static size_t string_min; 165static size_t string_min;
161static int flag_dump_strings; 166static int flag_dump_strings;
162 167
163/* Non-zero if we should recognize the older non-option arguments
164 that specified at most one file and optional arguments specifying
165 offset and pseudo-start address. */
166static int traditional;
167
168/* Non-zero if an old-style 'pseudo-address' was specified. */ 168/* Non-zero if an old-style 'pseudo-address' was specified. */
169static int flag_pseudo_start; 169static int flag_pseudo_start;
170 170
171/* The difference between the old-style pseudo starting address and 171/* The difference between the old-style pseudo starting address and
172 the number of bytes to skip. */ 172 the number of bytes to skip. */
173static uintmax_t pseudo_offset; 173static off_t pseudo_offset;
174 174
175/* Function that accepts an address and an optional following char, 175/* Function that accepts an address and an optional following char,
176 and prints the address and char to stdout. */ 176 and prints the address and char to stdout. */
177static void (*format_address) (uintmax_t, char); 177static void (*format_address) (off_t, char);
178 178
179/* The number of input bytes to skip before formatting and writing. */ 179/* The number of input bytes to skip before formatting and writing. */
180static uintmax_t n_bytes_to_skip; // = 0; 180static off_t n_bytes_to_skip; // = 0;
181 181
182/* When zero, MAX_BYTES_TO_FORMAT and END_OFFSET are ignored, and all 182/* When zero, MAX_BYTES_TO_FORMAT and END_OFFSET are ignored, and all
183 input is formatted. */ 183 input is formatted. */
184static int limit_bytes_to_format; // = 0; 184static int limit_bytes_to_format; // = 0;
185 185
186/* The maximum number of bytes that will be formatted. */ 186/* The maximum number of bytes that will be formatted. */
187static uintmax_t max_bytes_to_format; 187static off_t max_bytes_to_format;
188 188
189/* The offset of the first byte after the last byte to be formatted. */ 189/* The offset of the first byte after the last byte to be formatted. */
190static uintmax_t end_offset; 190static off_t end_offset;
191 191
192/* When nonzero and two or more consecutive blocks are equal, format 192/* When nonzero and two or more consecutive blocks are equal, format
193 only the first block and output an asterisk alone on the following 193 only the first block and output an asterisk alone on the following
@@ -195,10 +195,8 @@ static uintmax_t end_offset;
195static int abbreviate_duplicate_blocks = 1; 195static int abbreviate_duplicate_blocks = 1;
196 196
197/* An array of specs describing how to format each input block. */ 197/* An array of specs describing how to format each input block. */
198static struct tspec *spec;
199
200/* The number of format specs. */
201static size_t n_specs; 198static size_t n_specs;
199static struct tspec *spec;
202 200
203/* The number of input bytes formatted per output line. It must be 201/* The number of input bytes formatted per output line. It must be
204 a multiple of the least common multiple of the sizes associated with 202 a multiple of the least common multiple of the sizes associated with
@@ -215,25 +213,36 @@ static char const *const *file_list;
215 213
216/* Initializer for file_list if no file-arguments 214/* Initializer for file_list if no file-arguments
217 were specified on the command line. */ 215 were specified on the command line. */
218static char const *const default_file_list[] = {"-", NULL}; 216static char const *const default_file_list[] = { "-", NULL };
219 217
220/* The input stream associated with the current file. */ 218/* The input stream associated with the current file. */
221static FILE *in_stream; 219static FILE *in_stream;
222 220
223/* If nonzero, at least one of the files we read was standard input. */ 221static int ioerror;
224static int have_read_stdin;
225 222
226#define MAX_INTEGRAL_TYPE_SIZE sizeof(ulonglong_t) 223#define MAX_INTEGRAL_TYPE_SIZE sizeof(ulonglong_t)
227static unsigned char integral_type_size[MAX_INTEGRAL_TYPE_SIZE + 1]; 224static unsigned char integral_type_size[MAX_INTEGRAL_TYPE_SIZE + 1] = {
225 [sizeof(char)] = CHAR,
226#if USHRT_MAX != UCHAR_MAX
227 [sizeof(short)] = SHORT,
228#endif
229#if UINT_MAX != USHRT_MAX
230 [sizeof(int)] = INT,
231#endif
232#if ULONG_MAX != UINT_MAX
233 [sizeof(long)] = LONG,
234#endif
235#if ULLONG_MAX != ULONG_MAX
236 [sizeof(ulonglong_t)] = LONG_LONG,
237#endif
238};
228 239
229#define MAX_FP_TYPE_SIZE sizeof(longdouble_t) 240#define MAX_FP_TYPE_SIZE sizeof(longdouble_t)
230static unsigned char fp_type_size[MAX_FP_TYPE_SIZE + 1]; 241static unsigned char fp_type_size[MAX_FP_TYPE_SIZE + 1] = {
231 242 /* gcc seems to allow repeated indexes. Last one stays */
232 243 [sizeof(longdouble_t)] = FLOAT_LONG_DOUBLE,
233/* For long options that have no equivalent short option, use a 244 [sizeof(double)] = FLOAT_DOUBLE,
234 non-character as a pseudo short option, starting with CHAR_MAX + 1. */ 245 [sizeof(float)] = FLOAT_SINGLE,
235enum {
236 TRADITIONAL_OPTION = CHAR_MAX + 1
237}; 246};
238 247
239 248
@@ -261,12 +270,8 @@ lcm(unsigned u, unsigned v) {
261static void 270static void
262print_s_char(size_t n_bytes, const char *block, const char *fmt_string) 271print_s_char(size_t n_bytes, const char *block, const char *fmt_string)
263{ 272{
264 size_t i; 273 while (n_bytes--) {
265 for (i = n_bytes; i > 0; i--) { 274 int tmp = *(signed char *) block;
266 int tmp = (unsigned) *(unsigned char *) block;
267 if (tmp > SCHAR_MAX)
268 tmp -= SCHAR_MAX - SCHAR_MIN + 1;
269 assert(tmp <= SCHAR_MAX);
270 printf(fmt_string, tmp); 275 printf(fmt_string, tmp);
271 block += sizeof(unsigned char); 276 block += sizeof(unsigned char);
272 } 277 }
@@ -275,8 +280,7 @@ print_s_char(size_t n_bytes, const char *block, const char *fmt_string)
275static void 280static void
276print_char(size_t n_bytes, const char *block, const char *fmt_string) 281print_char(size_t n_bytes, const char *block, const char *fmt_string)
277{ 282{
278 size_t i; 283 while (n_bytes--) {
279 for (i = n_bytes; i > 0; i--) {
280 unsigned tmp = *(unsigned char *) block; 284 unsigned tmp = *(unsigned char *) block;
281 printf(fmt_string, tmp); 285 printf(fmt_string, tmp);
282 block += sizeof(unsigned char); 286 block += sizeof(unsigned char);
@@ -286,12 +290,9 @@ print_char(size_t n_bytes, const char *block, const char *fmt_string)
286static void 290static void
287print_s_short(size_t n_bytes, const char *block, const char *fmt_string) 291print_s_short(size_t n_bytes, const char *block, const char *fmt_string)
288{ 292{
289 size_t i; 293 n_bytes /= sizeof(signed short);
290 for (i = n_bytes / sizeof(unsigned short); i > 0; i--) { 294 while (n_bytes--) {
291 int tmp = (unsigned) *(unsigned short *) block; 295 int tmp = *(signed short *) block;
292 if (tmp > SHRT_MAX)
293 tmp -= SHRT_MAX - SHRT_MIN + 1;
294 assert(tmp <= SHRT_MAX);
295 printf(fmt_string, tmp); 296 printf(fmt_string, tmp);
296 block += sizeof(unsigned short); 297 block += sizeof(unsigned short);
297 } 298 }
@@ -300,8 +301,8 @@ print_s_short(size_t n_bytes, const char *block, const char *fmt_string)
300static void 301static void
301print_short(size_t n_bytes, const char *block, const char *fmt_string) 302print_short(size_t n_bytes, const char *block, const char *fmt_string)
302{ 303{
303 size_t i; 304 n_bytes /= sizeof(unsigned short);
304 for (i = n_bytes / sizeof(unsigned short); i > 0; i--) { 305 while (n_bytes--) {
305 unsigned tmp = *(unsigned short *) block; 306 unsigned tmp = *(unsigned short *) block;
306 printf(fmt_string, tmp); 307 printf(fmt_string, tmp);
307 block += sizeof(unsigned short); 308 block += sizeof(unsigned short);
@@ -311,41 +312,49 @@ print_short(size_t n_bytes, const char *block, const char *fmt_string)
311static void 312static void
312print_int(size_t n_bytes, const char *block, const char *fmt_string) 313print_int(size_t n_bytes, const char *block, const char *fmt_string)
313{ 314{
314 size_t i; 315 n_bytes /= sizeof(unsigned);
315 for (i = n_bytes / sizeof(unsigned); i > 0; i--) { 316 while (n_bytes--) {
316 unsigned tmp = *(unsigned *) block; 317 unsigned tmp = *(unsigned *) block;
317 printf(fmt_string, tmp); 318 printf(fmt_string, tmp);
318 block += sizeof(unsigned); 319 block += sizeof(unsigned);
319 } 320 }
320} 321}
321 322
323#if UINT_MAX == ULONG_MAX
324# define print_long print_int
325#else
322static void 326static void
323print_long(size_t n_bytes, const char *block, const char *fmt_string) 327print_long(size_t n_bytes, const char *block, const char *fmt_string)
324{ 328{
325 size_t i; 329 n_bytes /= sizeof(unsigned long);
326 for (i = n_bytes / sizeof(unsigned long); i > 0; i--) { 330 while (n_bytes--) {
327 unsigned long tmp = *(unsigned long *) block; 331 unsigned long tmp = *(unsigned long *) block;
328 printf(fmt_string, tmp); 332 printf(fmt_string, tmp);
329 block += sizeof(unsigned long); 333 block += sizeof(unsigned long);
330 } 334 }
331} 335}
336#endif
332 337
338#if ULONG_MAX == ULLONG_MAX
339# define print_long_long print_long
340#else
333static void 341static void
334print_long_long(size_t n_bytes, const char *block, const char *fmt_string) 342print_long_long(size_t n_bytes, const char *block, const char *fmt_string)
335{ 343{
336 size_t i; 344 n_bytes /= sizeof(ulonglong_t);
337 for (i = n_bytes / sizeof(ulonglong_t); i > 0; i--) { 345 while (n_bytes--) {
338 ulonglong_t tmp = *(ulonglong_t *) block; 346 ulonglong_t tmp = *(ulonglong_t *) block;
339 printf(fmt_string, tmp); 347 printf(fmt_string, tmp);
340 block += sizeof(ulonglong_t); 348 block += sizeof(ulonglong_t);
341 } 349 }
342} 350}
351#endif
343 352
344static void 353static void
345print_float(size_t n_bytes, const char *block, const char *fmt_string) 354print_float(size_t n_bytes, const char *block, const char *fmt_string)
346{ 355{
347 size_t i; 356 n_bytes /= sizeof(float);
348 for (i = n_bytes / sizeof(float); i > 0; i--) { 357 while (n_bytes--) {
349 float tmp = *(float *) block; 358 float tmp = *(float *) block;
350 printf(fmt_string, tmp); 359 printf(fmt_string, tmp);
351 block += sizeof(float); 360 block += sizeof(float);
@@ -355,8 +364,8 @@ print_float(size_t n_bytes, const char *block, const char *fmt_string)
355static void 364static void
356print_double(size_t n_bytes, const char *block, const char *fmt_string) 365print_double(size_t n_bytes, const char *block, const char *fmt_string)
357{ 366{
358 size_t i; 367 n_bytes /= sizeof(double);
359 for (i = n_bytes / sizeof(double); i > 0; i--) { 368 while (n_bytes--) {
360 double tmp = *(double *) block; 369 double tmp = *(double *) block;
361 printf(fmt_string, tmp); 370 printf(fmt_string, tmp);
362 block += sizeof(double); 371 block += sizeof(double);
@@ -366,50 +375,55 @@ print_double(size_t n_bytes, const char *block, const char *fmt_string)
366static void 375static void
367print_long_double(size_t n_bytes, const char *block, const char *fmt_string) 376print_long_double(size_t n_bytes, const char *block, const char *fmt_string)
368{ 377{
369 size_t i; 378 n_bytes /= sizeof(longdouble_t);
370 for (i = n_bytes / sizeof(longdouble_t); i > 0; i--) { 379 while (n_bytes--) {
371 longdouble_t tmp = *(longdouble_t *) block; 380 longdouble_t tmp = *(longdouble_t *) block;
372 printf(fmt_string, tmp); 381 printf(fmt_string, tmp);
373 block += sizeof(longdouble_t); 382 block += sizeof(longdouble_t);
374 } 383 }
375} 384}
376 385
377static void 386/* print_[named]_ascii are optimized for speed.
378dump_hexl_mode_trailer(size_t n_bytes, const char *block) 387 * Remember, someday you may want to pump gigabytes thru this thing.
379{ 388 * Saving a dozen of .text bytes here is counter-productive */
380 size_t i;
381 fputs(" >", stdout);
382 for (i = n_bytes; i > 0; i--) {
383 unsigned c = *(unsigned char *) block;
384 unsigned c2 = (ISPRINT(c) ? c : '.');
385 putchar(c2);
386 block += sizeof(unsigned char);
387 }
388 putchar('<');
389}
390 389
391static void 390static void
392print_named_ascii(size_t n_bytes, const char *block, 391print_named_ascii(size_t n_bytes, const char *block,
393 const char *unused_fmt_string ATTRIBUTE_UNUSED) 392 const char *unused_fmt_string ATTRIBUTE_UNUSED)
394{ 393{
395 size_t i; 394 /* Names for some non-printing characters. */
396 for (i = n_bytes; i > 0; i--) { 395 static const char charname[33][3] = {
397 unsigned c = *(unsigned char *) block; 396 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
398 unsigned masked_c = (0x7f & c); 397 " bs", " ht", " nl", " vt", " ff", " cr", " so", " si",
399 const char *s; 398 "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
400 char buf[5]; 399 "can", " em", "sub", "esc", " fs", " gs", " rs", " us",
401 400 " sp"
402 if (masked_c == 127) 401 };
403 s = "del"; 402 // buf[N] pos: 01234 56789
404 else if (masked_c <= 040) 403 char buf[12] = " x\0 0xx\0";
405 s = charname[masked_c]; 404 // actually " x\0 xxx\0", but I want to share the string with below.
406 else { 405 // [12] because we take three 32bit stack slots anyway, and
407 sprintf(buf, " %c", masked_c); 406 // gcc is too dumb to initialize with constant stores,
408 s = buf; 407 // it copies initializer from rodata. Oh well.
408
409 while (n_bytes--) {
410 unsigned masked_c = *(unsigned char *) block++;
411
412 masked_c &= 0x7f;
413 if (masked_c == 0x7f) {
414 fputs(" del", stdout);
415 continue;
409 } 416 }
410 417 if (masked_c > ' ') {
411 printf(" %3s", s); 418 buf[3] = masked_c;
412 block += sizeof(unsigned char); 419 fputs(buf, stdout);
420 continue;
421 }
422 /* Why? Because printf(" %3.3s") is much slower... */
423 buf[6] = charname[masked_c][0];
424 buf[7] = charname[masked_c][1];
425 buf[8] = charname[masked_c][2];
426 fputs(buf+5, stdout);
413 } 427 }
414} 428}
415 429
@@ -417,53 +431,113 @@ static void
417print_ascii(size_t n_bytes, const char *block, 431print_ascii(size_t n_bytes, const char *block,
418 const char *unused_fmt_string ATTRIBUTE_UNUSED) 432 const char *unused_fmt_string ATTRIBUTE_UNUSED)
419{ 433{
420 size_t i; 434 // buf[N] pos: 01234 56789
421 for (i = n_bytes; i > 0; i--) { 435 char buf[12] = " x\0 0xx\0";
422 unsigned c = *(unsigned char *) block; 436
437 while (n_bytes--) {
423 const char *s; 438 const char *s;
424 char buf[5]; 439 unsigned c = *(unsigned char *) block++;
425 440
441 if (ISPRINT(c)) {
442 buf[3] = c;
443 fputs(buf, stdout);
444 continue;
445 }
426 switch (c) { 446 switch (c) {
427 case '\0': 447 case '\0':
428 s = " \\0"; 448 s = " \\0";
429 break; 449 break;
430 case '\007': 450 case '\007':
431 s = " \\a"; 451 s = " \\a";
432 break; 452 break;
433 case '\b': 453 case '\b':
434 s = " \\b"; 454 s = " \\b";
435 break; 455 break;
436 case '\f': 456 case '\f':
437 s = " \\f"; 457 s = " \\f";
438 break; 458 break;
439 case '\n': 459 case '\n':
440 s = " \\n"; 460 s = " \\n";
441 break; 461 break;
442 case '\r': 462 case '\r':
443 s = " \\r"; 463 s = " \\r";
444 break; 464 break;
445 case '\t': 465 case '\t':
446 s = " \\t"; 466 s = " \\t";
447 break; 467 break;
448 case '\v': 468 case '\v':
449 s = " \\v"; 469 s = " \\v";
450 break; 470 break;
451 default: 471 case '\x7f':
452 sprintf(buf, (ISPRINT(c) ? " %c" : "%03o"), c); 472 s = " 177";
453 s = (const char *) buf; 473 break;
474 default: /* c is never larger than 040 */
475 buf[7] = (c >> 3) + '0';
476 buf[8] = (c & 7) + '0';
477 s = buf + 5;
454 } 478 }
479 fputs(s, stdout);
480 }
481}
455 482
456 printf(" %3s", s); 483/* Given a list of one or more input filenames FILE_LIST, set the global
457 block += sizeof(unsigned char); 484 file pointer IN_STREAM and the global string INPUT_FILENAME to the
485 first one that can be successfully opened. Modify FILE_LIST to
486 reference the next filename in the list. A file name of "-" is
487 interpreted as standard input. If any file open fails, give an error
488 message and return nonzero. */
489
490static void
491open_next_file(void)
492{
493 while(1) {
494 input_filename = *file_list;
495 if (!input_filename)
496 return;
497 file_list++;
498 in_stream = fopen_or_warn_stdin(input_filename);
499 if (in_stream) {
500 if (in_stream == stdin)
501 input_filename = bb_msg_standard_input;
502 break;
503 }
504 ioerror = 1;
505 }
506
507 if (limit_bytes_to_format && !flag_dump_strings)
508 setbuf(in_stream, NULL);
509}
510
511/* Test whether there have been errors on in_stream, and close it if
512 it is not standard input. Return nonzero if there has been an error
513 on in_stream or stdout; return zero otherwise. This function will
514 report more than one error only if both a read and a write error
515 have occurred. IN_ERRNO, if nonzero, is the error number
516 corresponding to the most recent action for IN_STREAM. */
517
518static void
519check_and_close(void)
520{
521 if (in_stream) {
522 if (ferror(in_stream)) {
523 bb_error_msg("%s: read error", input_filename);
524 ioerror = 1;
525 }
526 fclose_if_not_stdin(in_stream);
527 in_stream = NULL;
528 }
529
530 if (ferror(stdout)) {
531 bb_error_msg("write error");
532 ioerror = 1;
458 } 533 }
459} 534}
460 535
461/* If S points to a single valid modern od format string, put 536/* If S points to a single valid modern od format string, put
462 a description of that format in *TSPEC, make *NEXT point at the 537 a description of that format in *TSPEC, make *NEXT point at the
463 character following the just-decoded format (if *NEXT is non-NULL), 538 character following the just-decoded format (if *NEXT is non-NULL),
464 and return zero. If S is not valid, don't modify *NEXT or *TSPEC, 539 and return zero. For example, if S were "d4afL"
465 give a diagnostic, and return nonzero. For example, if S were 540 *NEXT would be set to "afL" and *TSPEC would be
466 "d4afL" *NEXT would be set to "afL" and *TSPEC would be
467 { 541 {
468 fmt = SIGNED_DECIMAL; 542 fmt = SIGNED_DECIMAL;
469 size = INT or LONG; (whichever integral_type_size[4] resolves to) 543 size = INT or LONG; (whichever integral_type_size[4] resolves to)
@@ -471,22 +545,22 @@ print_ascii(size_t n_bytes, const char *block,
471 fmt_string = "%011d%c"; 545 fmt_string = "%011d%c";
472 } 546 }
473 S_ORIG is solely for reporting errors. It should be the full format 547 S_ORIG is solely for reporting errors. It should be the full format
474 string argument. 548 string argument. */
475 */
476 549
477static int 550static void
478decode_one_format(const char *s_orig, const char *s, const char **next, 551decode_one_format(const char *s_orig, const char *s, const char **next,
479 struct tspec *tspec) 552 struct tspec *tspec)
480{ 553{
481 enum size_spec size_spec; 554 enum size_spec size_spec;
482 unsigned long size; 555 unsigned size;
483 enum output_format fmt; 556 enum output_format fmt;
484 const char *pre_fmt_string; 557 const char *p;
485 char *end; 558 char *end;
486 char *fmt_string; 559 char *fmt_string = NULL;
487 void (*print_function) (size_t, const char *, const char *); 560 void (*print_function) (size_t, const char *, const char *);
488 unsigned c; 561 unsigned c;
489 unsigned field_width = 0; 562 unsigned field_width = 0;
563 int pos;
490 564
491 assert(tspec != NULL); 565 assert(tspec != NULL);
492 566
@@ -494,40 +568,33 @@ decode_one_format(const char *s_orig, const char *s, const char **next,
494 case 'd': 568 case 'd':
495 case 'o': 569 case 'o':
496 case 'u': 570 case 'u':
497 case 'x': 571 case 'x': {
498 c = *s; 572 static const char CSIL[] = "CSIL";
499 ++s; 573
500 switch (*s) { 574 c = *s++;
501 case 'C': 575 p = strchr(CSIL, *s);
502 ++s; 576 if (!p) {
503 size = sizeof(char);
504 break;
505 case 'S':
506 ++s;
507 size = sizeof(short);
508 break;
509 case 'I':
510 ++s;
511 size = sizeof(int);
512 break;
513 case 'L':
514 ++s;
515 size = sizeof(long);
516 break;
517 default:
518 size = sizeof(int); 577 size = sizeof(int);
519 if (isdigit(s[0])) { 578 if (isdigit(s[0])) {
520 size = strtoul(s, &end, 0); 579 size = bb_strtou(s, &end, 0);
521 if (end[0] || MAX_INTEGRAL_TYPE_SIZE < size 580 if (errno == ERANGE
581 || MAX_INTEGRAL_TYPE_SIZE < size
522 || integral_type_size[size] == NO_SIZE 582 || integral_type_size[size] == NO_SIZE
523 ) { 583 ) {
524 bb_error_msg("invalid type string '%s'; " 584 bb_error_msg_and_die("invalid type string '%s'; "
525 "%lu-byte integral type is not spported", s_orig, size); 585 "%u-byte %s type is not supported",
526 return 1; 586 s_orig, size, "integral");
527 } 587 }
528 s = end; 588 s = end;
529 } 589 }
530 break; 590 } else {
591 static const uint8_t CSIL_sizeof[] = {
592 sizeof(char),
593 sizeof(short),
594 sizeof(int),
595 sizeof(long),
596 };
597 size = CSIL_sizeof[p - CSIL];
531 } 598 }
532 599
533#define ISPEC_TO_FORMAT(Spec, Min_format, Long_format, Max_format) \ 600#define ISPEC_TO_FORMAT(Spec, Min_format, Long_format, Max_format) \
@@ -535,41 +602,41 @@ decode_one_format(const char *s_orig, const char *s, const char **next,
535 : ((Spec) == LONG ? (Long_format) : (Min_format))) 602 : ((Spec) == LONG ? (Long_format) : (Min_format)))
536 603
537#define FMT_BYTES_ALLOCATED 9 604#define FMT_BYTES_ALLOCATED 9
538 fmt_string = xmalloc(FMT_BYTES_ALLOCATED);
539
540 size_spec = integral_type_size[size]; 605 size_spec = integral_type_size[size];
541 606
542 switch (c) { 607 {
543 case 'd': 608 static const char doux[] = "doux";
544 fmt = SIGNED_DECIMAL; 609 static const char doux_fmt_letter[][4] = {
545 sprintf(fmt_string, " %%%u%s", 610 "lld", "llo", "llu", "llx"
546 (field_width = bytes_to_signed_dec_digits[size]), 611 };
547 ISPEC_TO_FORMAT(size_spec, "d", "ld", PRIdMAX)); 612 static const enum output_format doux_fmt[] = {
548 break; 613 SIGNED_DECIMAL,
549 case 'o': 614 OCTAL,
550 fmt = OCTAL; 615 UNSIGNED_DECIMAL,
551 sprintf(fmt_string, " %%0%u%s", 616 HEXADECIMAL,
552 (field_width = bytes_to_oct_digits[size]), 617 };
553 ISPEC_TO_FORMAT(size_spec, "o", "lo", PRIoMAX)); 618 static const uint8_t *const doux_bytes_to_XXX[] = {
554 break; 619 bytes_to_signed_dec_digits,
555 case 'u': 620 bytes_to_oct_digits,
556 fmt = UNSIGNED_DECIMAL; 621 bytes_to_unsigned_dec_digits,
557 sprintf(fmt_string, " %%%u%s", 622 bytes_to_hex_digits,
558 (field_width = bytes_to_unsigned_dec_digits[size]), 623 };
559 ISPEC_TO_FORMAT(size_spec, "u", "lu", PRIuMAX)); 624 static const char doux_fmtstring[][sizeof(" %%0%u%s")] = {
560 break; 625 " %%%u%s",
561 case 'x': 626 " %%0%u%s",
562 fmt = HEXADECIMAL; 627 " %%%u%s",
563 sprintf(fmt_string, " %%0%u%s", 628 " %%0%u%s",
564 (field_width = bytes_to_hex_digits[size]), 629 };
565 ISPEC_TO_FORMAT(size_spec, "x", "lx", PRIxMAX)); 630
566 break; 631 pos = strchr(doux, c) - doux;
567 default: 632 fmt = doux_fmt[pos];
568 abort(); 633 field_width = doux_bytes_to_XXX[pos][size];
634 p = doux_fmt_letter[pos] + 2;
635 if (size_spec == LONG) p--;
636 if (size_spec == LONG_LONG) p -= 2;
637 fmt_string = xasprintf(doux_fmtstring[pos], field_width, p);
569 } 638 }
570 639
571 assert(strlen(fmt_string) < FMT_BYTES_ALLOCATED);
572
573 switch (size_spec) { 640 switch (size_spec) {
574 case CHAR: 641 case CHAR:
575 print_function = (fmt == SIGNED_DECIMAL 642 print_function = (fmt == SIGNED_DECIMAL
@@ -587,80 +654,69 @@ decode_one_format(const char *s_orig, const char *s, const char **next,
587 case LONG: 654 case LONG:
588 print_function = print_long; 655 print_function = print_long;
589 break; 656 break;
590 case LONG_LONG: 657 default: /* case LONG_LONG: */
591 print_function = print_long_long; 658 print_function = print_long_long;
592 break; 659 break;
593 default:
594 abort();
595 } 660 }
596 break; 661 break;
662 }
663
664 case 'f': {
665 static const char FDL[] = "FDL";
597 666
598 case 'f':
599 fmt = FLOATING_POINT; 667 fmt = FLOATING_POINT;
600 ++s; 668 ++s;
601 switch (*s) { 669 p = strchr(FDL, *s);
602 case 'F': 670 if (!p) {
603 ++s;
604 size = sizeof(float);
605 break;
606 case 'D':
607 ++s;
608 size = sizeof(double);
609 break;
610 case 'L':
611 ++s;
612 size = sizeof(longdouble_t);
613 break;
614 default:
615 size = sizeof(double); 671 size = sizeof(double);
616 if (isdigit(s[0])) { 672 if (isdigit(s[0])) {
617 size = strtoul(s, &end, 0); 673 size = bb_strtou(s, &end, 0);
618 if (end[0] || size > MAX_FP_TYPE_SIZE 674 if (errno == ERANGE || size > MAX_FP_TYPE_SIZE
619 || fp_type_size[size] == NO_SIZE 675 || fp_type_size[size] == NO_SIZE
620 ) { 676 ) {
621 bb_error_msg("invalid type string '%s'; " 677 bb_error_msg_and_die("invalid type string '%s'; "
622 "%lu-byte floating point type is not supported", s_orig, size); 678 "%u-byte %s type is not supported",
623 return 1; 679 s_orig, size, "floating point");
624 } 680 }
625 s = end; 681 s = end;
626 } 682 }
627 break; 683 } else {
684 static const uint8_t FDL_sizeof[] = {
685 sizeof(float),
686 sizeof(double),
687 sizeof(longdouble_t),
688 };
689
690 size = FDL_sizeof[p - FDL];
628 } 691 }
692
629 size_spec = fp_type_size[size]; 693 size_spec = fp_type_size[size];
630 694
631 switch (size_spec) { 695 switch (size_spec) {
632 case FLOAT_SINGLE: 696 case FLOAT_SINGLE:
633 print_function = print_float; 697 print_function = print_float;
698 field_width = FLT_DIG + 8;
634 /* Don't use %#e; not all systems support it. */ 699 /* Don't use %#e; not all systems support it. */
635 pre_fmt_string = " %%%d.%de"; 700 fmt_string = xasprintf(" %%%d.%de", field_width, FLT_DIG);
636 fmt_string = xmalloc(strlen(pre_fmt_string));
637 sprintf(fmt_string, pre_fmt_string,
638 (field_width = FLT_DIG + 8), FLT_DIG);
639 break; 701 break;
640 case FLOAT_DOUBLE: 702 case FLOAT_DOUBLE:
641 print_function = print_double; 703 print_function = print_double;
642 pre_fmt_string = " %%%d.%de"; 704 field_width = DBL_DIG + 8;
643 fmt_string = xmalloc(strlen(pre_fmt_string)); 705 fmt_string = xasprintf(" %%%d.%de", field_width, DBL_DIG);
644 sprintf(fmt_string, pre_fmt_string,
645 (field_width = DBL_DIG + 8), DBL_DIG);
646 break; 706 break;
647 case FLOAT_LONG_DOUBLE: 707 default: /* case FLOAT_LONG_DOUBLE: */
648 print_function = print_long_double; 708 print_function = print_long_double;
649 pre_fmt_string = " %%%d.%dLe"; 709 field_width = LDBL_DIG + 8;
650 fmt_string = xmalloc(strlen(pre_fmt_string)); 710 fmt_string = xasprintf(" %%%d.%dLe", field_width, LDBL_DIG);
651 sprintf(fmt_string, pre_fmt_string,
652 (field_width = LDBL_DIG + 8), LDBL_DIG);
653 break; 711 break;
654 default:
655 abort();
656 } 712 }
657 break; 713 break;
714 }
658 715
659 case 'a': 716 case 'a':
660 ++s; 717 ++s;
661 fmt = NAMED_CHARACTER; 718 fmt = NAMED_CHARACTER;
662 size_spec = CHAR; 719 size_spec = CHAR;
663 fmt_string = NULL;
664 print_function = print_named_ascii; 720 print_function = print_named_ascii;
665 field_width = 3; 721 field_width = 3;
666 break; 722 break;
@@ -668,13 +724,12 @@ decode_one_format(const char *s_orig, const char *s, const char **next,
668 ++s; 724 ++s;
669 fmt = CHARACTER; 725 fmt = CHARACTER;
670 size_spec = CHAR; 726 size_spec = CHAR;
671 fmt_string = NULL;
672 print_function = print_ascii; 727 print_function = print_ascii;
673 field_width = 3; 728 field_width = 3;
674 break; 729 break;
675 default: 730 default:
676 bb_error_msg("invalid character '%c' in type string '%s'", *s, s_orig); 731 bb_error_msg_and_die("invalid character '%c' "
677 return 1; 732 "in type string '%s'", *s, s_orig);
678 } 733 }
679 734
680 tspec->size = size_spec; 735 tspec->size = size_spec;
@@ -689,96 +744,22 @@ decode_one_format(const char *s_orig, const char *s, const char **next,
689 744
690 if (next != NULL) 745 if (next != NULL)
691 *next = s; 746 *next = s;
692
693 return 0;
694}
695
696/* Given a list of one or more input filenames FILE_LIST, set the global
697 file pointer IN_STREAM and the global string INPUT_FILENAME to the
698 first one that can be successfully opened. Modify FILE_LIST to
699 reference the next filename in the list. A file name of "-" is
700 interpreted as standard input. If any file open fails, give an error
701 message and return nonzero. */
702
703static int
704open_next_file(void)
705{
706 int err = 0;
707
708 do {
709 input_filename = *file_list;
710 if (input_filename == NULL)
711 return err;
712 ++file_list;
713
714 if (input_filename[0] == '-' && !input_filename[1]) {
715 input_filename = "standard input";
716 in_stream = stdin;
717 have_read_stdin = 1;
718 } else {
719 in_stream = fopen(input_filename, "r");
720 if (in_stream == NULL) {
721 bb_perror_msg("%s", input_filename);
722 err = 1;
723 }
724 }
725 } while (in_stream == NULL);
726
727 if (limit_bytes_to_format && !flag_dump_strings)
728 setbuf(in_stream, NULL);
729
730 return err;
731}
732
733/* Test whether there have been errors on in_stream, and close it if
734 it is not standard input. Return nonzero if there has been an error
735 on in_stream or stdout; return zero otherwise. This function will
736 report more than one error only if both a read and a write error
737 have occurred. IN_ERRNO, if nonzero, is the error number
738 corresponding to the most recent action for IN_STREAM. */
739
740static int
741check_and_close(int in_errno)
742{
743 int err = 0;
744
745 if (in_stream != NULL) {
746 if (ferror(in_stream)) {
747 bb_error_msg("%s: read error", input_filename);
748 if (in_stream != stdin)
749 fclose(in_stream);
750 err = 1;
751 } else if (in_stream != stdin && fclose(in_stream) == EOF) {
752 bb_perror_msg("%s", input_filename);
753 err = 1;
754 }
755 in_stream = NULL;
756 }
757
758 if (ferror(stdout)) {
759 bb_error_msg("write error");
760 err = 1;
761 }
762
763 return err;
764} 747}
765 748
766/* Decode the modern od format string S. Append the decoded 749/* Decode the modern od format string S. Append the decoded
767 representation to the global array SPEC, reallocating SPEC if 750 representation to the global array SPEC, reallocating SPEC if
768 necessary. Return zero if S is valid, nonzero otherwise. */ 751 necessary. Return zero if S is valid, nonzero otherwise. */
769 752
770static int 753static void
771decode_format_string(const char *s) 754decode_format_string(const char *s)
772{ 755{
773 const char *s_orig = s; 756 const char *s_orig = s;
774 assert(s != NULL);
775 757
776 while (*s != '\0') { 758 while (*s != '\0') {
777 struct tspec tspec; 759 struct tspec tspec;
778 const char *next; 760 const char *next;
779 761
780 if (decode_one_format(s_orig, s, &next, &tspec)) 762 decode_one_format(s_orig, s, &next, &tspec);
781 return 1;
782 763
783 assert(s != next); 764 assert(s != next);
784 s = next; 765 s = next;
@@ -786,8 +767,6 @@ decode_format_string(const char *s)
786 spec = xrealloc(spec, n_specs * sizeof(*spec)); 767 spec = xrealloc(spec, n_specs * sizeof(*spec));
787 memcpy(&spec[n_specs-1], &tspec, sizeof *spec); 768 memcpy(&spec[n_specs-1], &tspec, sizeof *spec);
788 } 769 }
789
790 return 0;
791} 770}
792 771
793/* Given a list of one or more input filenames FILE_LIST, set the global 772/* Given a list of one or more input filenames FILE_LIST, set the global
@@ -797,16 +776,13 @@ decode_format_string(const char *s)
797 nonzero. When possible, use seek rather than read operations to 776 nonzero. When possible, use seek rather than read operations to
798 advance IN_STREAM. */ 777 advance IN_STREAM. */
799 778
800static int 779static void
801skip(uintmax_t n_skip) 780skip(off_t n_skip)
802{ 781{
803 int err = 0;
804 int in_errno = 0;
805
806 if (n_skip == 0) 782 if (n_skip == 0)
807 return 0; 783 return;
808 784
809 while (in_stream != NULL) { /* EOF */ 785 while (in_stream) { /* !EOF */
810 struct stat file_stats; 786 struct stat file_stats;
811 787
812 /* First try seeking. For large offsets, this extra work is 788 /* First try seeking. For large offsets, this extra work is
@@ -819,87 +795,71 @@ skip(uintmax_t n_skip)
819 Try to do that by getting file's size using fstat. 795 Try to do that by getting file's size using fstat.
820 But that will work only for regular files. */ 796 But that will work only for regular files. */
821 797
822 if (fstat(fileno(in_stream), &file_stats) == 0) {
823 /* The st_size field is valid only for regular files 798 /* The st_size field is valid only for regular files
824 (and for symbolic links, which cannot occur here). 799 (and for symbolic links, which cannot occur here).
825 If the number of bytes left to skip is at least 800 If the number of bytes left to skip is at least
826 as large as the size of the current file, we can 801 as large as the size of the current file, we can
827 decrement n_skip and go on to the next file. */ 802 decrement n_skip and go on to the next file. */
828 803 if (fstat(fileno(in_stream), &file_stats) == 0
829 if (S_ISREG(file_stats.st_mode) && 0 <= file_stats.st_size) { 804 && S_ISREG(file_stats.st_mode) && file_stats.st_size >= 0
830 if ((uintmax_t) file_stats.st_size <= n_skip) 805 ) {
831 n_skip -= file_stats.st_size; 806 if (file_stats.st_size < n_skip) {
832 else { 807 n_skip -= file_stats.st_size;
833 if (fseeko(in_stream, n_skip, SEEK_CUR) != 0) { 808 /* take check&close / open_next route */
834 in_errno = errno;
835 err = 1;
836 }
837 n_skip = 0;
838 }
839 } else { 809 } else {
840 810 if (fseeko(in_stream, n_skip, SEEK_CUR) != 0)
841 /* If it's not a regular file with nonnegative size, 811 ioerror = 1;
842 position the file pointer by reading. */ 812 return;
843 char buf[BUFSIZ]; 813 }
844 size_t n_bytes_read, n_bytes_to_read = BUFSIZ; 814 } else {
845 815 /* If it's not a regular file with nonnegative size,
846 while (0 < n_skip) { 816 position the file pointer by reading. */
847 if (n_skip < n_bytes_to_read) 817 char buf[BUFSIZ];
848 n_bytes_to_read = n_skip; 818 size_t n_bytes_read, n_bytes_to_read = BUFSIZ;
849 n_bytes_read = fread(buf, 1, n_bytes_to_read, in_stream); 819
850 n_skip -= n_bytes_read; 820 while (n_skip > 0) {
851 if (n_bytes_read != n_bytes_to_read) { 821 if (n_skip < n_bytes_to_read)
852 in_errno = errno; 822 n_bytes_to_read = n_skip;
853 err = 1; 823 n_bytes_read = fread(buf, 1, n_bytes_to_read, in_stream);
854 n_skip = 0; 824 n_skip -= n_bytes_read;
855 break; 825 if (n_bytes_read != n_bytes_to_read)
856 } 826 break; /* EOF on this file or error */
857 }
858 } 827 }
859 if (n_skip == 0)
860 break;
861 } else { /* cannot fstat() file */
862 bb_perror_msg("%s", input_filename);
863 err = 1;
864 } 828 }
829 if (n_skip == 0)
830 return;
865 831
866 err |= check_and_close(in_errno); 832 check_and_close();
867 err |= open_next_file(); 833 open_next_file();
868 } 834 }
869 835
870 if (n_skip != 0) 836 if (n_skip)
871 bb_error_msg_and_die("cannot skip past end of combined input"); 837 bb_error_msg_and_die("cannot skip past end of combined input");
872 return err;
873} 838}
874 839
840
841typedef void FN_format_address(off_t address, char c);
842
875static void 843static void
876format_address_none(uintmax_t address ATTRIBUTE_UNUSED, char c ATTRIBUTE_UNUSED) 844format_address_none(off_t address ATTRIBUTE_UNUSED, char c ATTRIBUTE_UNUSED)
877{ 845{
878} 846}
879 847
848static int address_pad_len;
849static char address_fmt[] = "%0*"OFF_FMT"xc";
850/* Corresponds to 'x' above */
851#define address_base_char address_fmt[sizeof(address_fmt)-3]
852
880static void 853static void
881format_address_std(uintmax_t address, char c) 854format_address_std(off_t address, char c)
882{ 855{
883 char buf[MAX_ADDRESS_LENGTH + 2]; 856 /* Corresponds to 'c' */
884 char *p = buf + sizeof(buf)-1; 857 address_fmt[sizeof(address_fmt)-2] = c;
885 char const *pbound; 858 printf(address_fmt, address_pad_len, address);
886
887 *p = '\0';
888 *--p = c;
889 pbound = p - address_pad_len;
890
891 do {
892 *--p = "0123456789abcdef"[address % address_base];
893 address /= address_base;
894 } while (address);
895
896 while (pbound < p)
897 *--p = '0';
898 fputs(p, stdout);
899} 859}
900 860
901static void 861static void
902format_address_paren(uintmax_t address, char c) 862format_address_paren(off_t address, char c)
903{ 863{
904 putchar('('); 864 putchar('(');
905 format_address_std(address, ')'); 865 format_address_std(address, ')');
@@ -907,12 +867,25 @@ format_address_paren(uintmax_t address, char c)
907} 867}
908 868
909static void 869static void
910format_address_label(uintmax_t address, char c) 870format_address_label(off_t address, char c)
911{ 871{
912 format_address_std(address, ' '); 872 format_address_std(address, ' ');
913 format_address_paren(address + pseudo_offset, c); 873 format_address_paren(address + pseudo_offset, c);
914} 874}
915 875
876
877static void
878dump_hexl_mode_trailer(size_t n_bytes, const char *block)
879{
880 fputs(" >", stdout);
881 while (n_bytes--) {
882 unsigned c = *(unsigned char *) block++;
883 c = (ISPRINT(c) ? c : '.');
884 putchar(c);
885 }
886 putchar('<');
887}
888
916/* Write N_BYTES bytes from CURR_BLOCK to standard output once for each 889/* Write N_BYTES bytes from CURR_BLOCK to standard output once for each
917 of the N_SPEC format specs. CURRENT_OFFSET is the byte address of 890 of the N_SPEC format specs. CURRENT_OFFSET is the byte address of
918 CURR_BLOCK in the concatenation of input files, and it is printed 891 CURR_BLOCK in the concatenation of input files, and it is printed
@@ -925,11 +898,12 @@ format_address_label(uintmax_t address, char c)
925 only when it has not been padded to length BYTES_PER_BLOCK. */ 898 only when it has not been padded to length BYTES_PER_BLOCK. */
926 899
927static void 900static void
928write_block(uintmax_t current_offset, size_t n_bytes, 901write_block(off_t current_offset, size_t n_bytes,
929 const char *prev_block, const char *curr_block) 902 const char *prev_block, const char *curr_block)
930{ 903{
931 static int first = 1; 904 static char first = 1;
932 static int prev_pair_equal = 0; 905 static char prev_pair_equal = 0;
906 size_t i;
933 907
934 if (abbreviate_duplicate_blocks 908 if (abbreviate_duplicate_blocks
935 && !first 909 && !first
@@ -944,8 +918,7 @@ write_block(uintmax_t current_offset, size_t n_bytes,
944 prev_pair_equal = 1; 918 prev_pair_equal = 1;
945 } 919 }
946 } else { 920 } else {
947 size_t i; 921 first = 0;
948
949 prev_pair_equal = 0; 922 prev_pair_equal = 0;
950 for (i = 0; i < n_specs; i++) { 923 for (i = 0; i < n_specs; i++) {
951 if (i == 0) 924 if (i == 0)
@@ -964,63 +937,17 @@ write_block(uintmax_t current_offset, size_t n_bytes,
964 putchar('\n'); 937 putchar('\n');
965 } 938 }
966 } 939 }
967 first = 0;
968}
969
970/* Read a single byte into *C from the concatenation of the input files
971 named in the global array FILE_LIST. On the first call to this
972 function, the global variable IN_STREAM is expected to be an open
973 stream associated with the input file INPUT_FILENAME. If IN_STREAM
974 is at end-of-file, close it and update the global variables IN_STREAM
975 and INPUT_FILENAME so they correspond to the next file in the list.
976 Then try to read a byte from the newly opened file. Repeat if
977 necessary until EOF is reached for the last file in FILE_LIST, then
978 set *C to EOF and return. Subsequent calls do likewise. The return
979 value is nonzero if any errors occured, zero otherwise. */
980
981static int
982read_char(int *c)
983{
984 int err = 0;
985
986 *c = EOF;
987
988 while (in_stream != NULL) { /* EOF. */
989 *c = fgetc(in_stream);
990 if (*c != EOF)
991 break;
992 err |= check_and_close(errno);
993 err |= open_next_file();
994 }
995
996 return err;
997} 940}
998 941
999/* Read N bytes into BLOCK from the concatenation of the input files 942static void
1000 named in the global array FILE_LIST. On the first call to this
1001 function, the global variable IN_STREAM is expected to be an open
1002 stream associated with the input file INPUT_FILENAME. If all N
1003 bytes cannot be read from IN_STREAM, close IN_STREAM and update
1004 the global variables IN_STREAM and INPUT_FILENAME. Then try to
1005 read the remaining bytes from the newly opened file. Repeat if
1006 necessary until EOF is reached for the last file in FILE_LIST.
1007 On subsequent calls, don't modify BLOCK and return zero. Set
1008 *N_BYTES_IN_BUFFER to the number of bytes read. If an error occurs,
1009 it will be detected through ferror when the stream is about to be
1010 closed. If there is an error, give a message but continue reading
1011 as usual and return nonzero. Otherwise return zero. */
1012
1013static int
1014read_block(size_t n, char *block, size_t *n_bytes_in_buffer) 943read_block(size_t n, char *block, size_t *n_bytes_in_buffer)
1015{ 944{
1016 int err = 0;
1017
1018 assert(0 < n && n <= bytes_per_block); 945 assert(0 < n && n <= bytes_per_block);
1019 946
1020 *n_bytes_in_buffer = 0; 947 *n_bytes_in_buffer = 0;
1021 948
1022 if (n == 0) 949 if (n == 0)
1023 return 0; 950 return;
1024 951
1025 while (in_stream != NULL) { /* EOF. */ 952 while (in_stream != NULL) { /* EOF. */
1026 size_t n_needed; 953 size_t n_needed;
@@ -1031,11 +958,10 @@ read_block(size_t n, char *block, size_t *n_bytes_in_buffer)
1031 *n_bytes_in_buffer += n_read; 958 *n_bytes_in_buffer += n_read;
1032 if (n_read == n_needed) 959 if (n_read == n_needed)
1033 break; 960 break;
1034 err |= check_and_close(errno); 961 /* error check is done in check_and_close */
1035 err |= open_next_file(); 962 check_and_close();
963 open_next_file();
1036 } 964 }
1037
1038 return err;
1039} 965}
1040 966
1041/* Return the least common multiple of the sizes associated 967/* Return the least common multiple of the sizes associated
@@ -1056,37 +982,34 @@ get_lcm(void)
1056 leading '+' return nonzero and set *OFFSET to the offset it denotes. */ 982 leading '+' return nonzero and set *OFFSET to the offset it denotes. */
1057 983
1058static int 984static int
1059parse_old_offset(const char *s, uintmax_t *offset) 985parse_old_offset(const char *s, off_t *offset)
1060{ 986{
1061 static const struct suffix_mult Bb[] = { 987 static const struct suffix_mult Bb[] = {
1062 { "B", 1024 }, 988 { "B", 1024 },
1063 { "b", 512 }, 989 { "b", 512 },
1064 { NULL, 0 } 990 { NULL, 0 }
1065 }; 991 };
1066 992 char *p;
1067 int radix; 993 int radix;
1068 994
1069 if (*s == '\0')
1070 return 0;
1071
1072 /* Skip over any leading '+'. */ 995 /* Skip over any leading '+'. */
1073 if (s[0] == '+') 996 if (s[0] == '+') ++s;
1074 ++s;
1075 997
1076 /* Determine the radix we'll use to interpret S. If there is a '.', 998 /* Determine the radix we'll use to interpret S. If there is a '.',
1077 it's decimal, otherwise, if the string begins with '0X'or '0x', 999 * it's decimal, otherwise, if the string begins with '0X'or '0x',
1078 it's hexadecimal, else octal. */ 1000 * it's hexadecimal, else octal. */
1079 if (strchr(s, '.') != NULL) 1001 p = strchr(s, '.');
1002 radix = 8;
1003 if (p) {
1004 p[0] = '\0'; /* cheating */
1080 radix = 10; 1005 radix = 10;
1081 else { 1006 } else if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
1082 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) 1007 radix = 16;
1083 radix = 16; 1008
1084 else 1009 *offset = xstrtooff_sfx(s, radix, Bb);
1085 radix = 8; 1010 if (p) p[0] = '.';
1086 }
1087 1011
1088 *offset = xstrtoumax_sfx(s, radix, Bb); 1012 return (*offset >= 0);
1089 return 1;
1090} 1013}
1091 1014
1092/* Read a chunk of size BYTES_PER_BLOCK from the input files, write the 1015/* Read a chunk of size BYTES_PER_BLOCK from the input files, write the
@@ -1101,13 +1024,12 @@ parse_old_offset(const char *s, uintmax_t *offset)
1101 check_and_close, and if any was nonzero, return nonzero. 1024 check_and_close, and if any was nonzero, return nonzero.
1102 Otherwise, return zero. */ 1025 Otherwise, return zero. */
1103 1026
1104static int 1027static void
1105dump(void) 1028dump(void)
1106{ 1029{
1107 char *block[2]; 1030 char *block[2];
1108 uintmax_t current_offset; 1031 off_t current_offset;
1109 int idx; 1032 int idx;
1110 int err;
1111 size_t n_bytes_read; 1033 size_t n_bytes_read;
1112 1034
1113 block[0] = xmalloc(2*bytes_per_block); 1035 block[0] = xmalloc(2*bytes_per_block);
@@ -1116,7 +1038,6 @@ dump(void)
1116 current_offset = n_bytes_to_skip; 1038 current_offset = n_bytes_to_skip;
1117 1039
1118 idx = 0; 1040 idx = 0;
1119 err = 0;
1120 if (limit_bytes_to_format) { 1041 if (limit_bytes_to_format) {
1121 while (1) { 1042 while (1) {
1122 size_t n_needed; 1043 size_t n_needed;
@@ -1125,8 +1046,8 @@ dump(void)
1125 break; 1046 break;
1126 } 1047 }
1127 n_needed = MIN(end_offset - current_offset, 1048 n_needed = MIN(end_offset - current_offset,
1128 (uintmax_t) bytes_per_block); 1049 (off_t) bytes_per_block);
1129 err |= read_block(n_needed, block[idx], &n_bytes_read); 1050 read_block(n_needed, block[idx], &n_bytes_read);
1130 if (n_bytes_read < bytes_per_block) 1051 if (n_bytes_read < bytes_per_block)
1131 break; 1052 break;
1132 assert(n_bytes_read == bytes_per_block); 1053 assert(n_bytes_read == bytes_per_block);
@@ -1137,7 +1058,7 @@ dump(void)
1137 } 1058 }
1138 } else { 1059 } else {
1139 while (1) { 1060 while (1) {
1140 err |= read_block(bytes_per_block, block[idx], &n_bytes_read); 1061 read_block(bytes_per_block, block[idx], &n_bytes_read);
1141 if (n_bytes_read < bytes_per_block) 1062 if (n_bytes_read < bytes_per_block)
1142 break; 1063 break;
1143 assert(n_bytes_read == bytes_per_block); 1064 assert(n_bytes_read == bytes_per_block);
@@ -1167,13 +1088,49 @@ dump(void)
1167 format_address(current_offset, '\n'); 1088 format_address(current_offset, '\n');
1168 1089
1169 if (limit_bytes_to_format && current_offset >= end_offset) 1090 if (limit_bytes_to_format && current_offset >= end_offset)
1170 err |= check_and_close(0); 1091 check_and_close();
1171 1092
1172 free(block[0]); 1093 free(block[0]);
1094}
1095
1096/* Read a single byte into *C from the concatenation of the input files
1097 named in the global array FILE_LIST. On the first call to this
1098 function, the global variable IN_STREAM is expected to be an open
1099 stream associated with the input file INPUT_FILENAME. If IN_STREAM
1100 is at end-of-file, close it and update the global variables IN_STREAM
1101 and INPUT_FILENAME so they correspond to the next file in the list.
1102 Then try to read a byte from the newly opened file. Repeat if
1103 necessary until EOF is reached for the last file in FILE_LIST, then
1104 set *C to EOF and return. Subsequent calls do likewise. The return
1105 value is nonzero if any errors occured, zero otherwise. */
1173 1106
1174 return err; 1107static void
1108read_char(int *c)
1109{
1110 while (in_stream) { /* !EOF */
1111 *c = fgetc(in_stream);
1112 if (*c != EOF)
1113 return;
1114 check_and_close();
1115 open_next_file();
1116 }
1117 *c = EOF;
1175} 1118}
1176 1119
1120/* Read N bytes into BLOCK from the concatenation of the input files
1121 named in the global array FILE_LIST. On the first call to this
1122 function, the global variable IN_STREAM is expected to be an open
1123 stream associated with the input file INPUT_FILENAME. If all N
1124 bytes cannot be read from IN_STREAM, close IN_STREAM and update
1125 the global variables IN_STREAM and INPUT_FILENAME. Then try to
1126 read the remaining bytes from the newly opened file. Repeat if
1127 necessary until EOF is reached for the last file in FILE_LIST.
1128 On subsequent calls, don't modify BLOCK and return zero. Set
1129 *N_BYTES_IN_BUFFER to the number of bytes read. If an error occurs,
1130 it will be detected through ferror when the stream is about to be
1131 closed. If there is an error, give a message but continue reading
1132 as usual and return nonzero. Otherwise return zero. */
1133
1177/* STRINGS mode. Find each "string constant" in the input. 1134/* STRINGS mode. Find each "string constant" in the input.
1178 A string constant is a run of at least 'string_min' ASCII 1135 A string constant is a run of at least 'string_min' ASCII
1179 graphic (or formatting) characters terminated by a null. 1136 graphic (or formatting) characters terminated by a null.
@@ -1181,98 +1138,58 @@ dump(void)
1181 traditional version of od. Return nonzero if an error 1138 traditional version of od. Return nonzero if an error
1182 occurs. Otherwise, return zero. */ 1139 occurs. Otherwise, return zero. */
1183 1140
1184static int 1141static void
1185dump_strings(void) 1142dump_strings(void)
1186{ 1143{
1187 size_t bufsize = MAX(100, string_min); 1144 size_t bufsize = MAX(100, string_min);
1188 char *buf = xmalloc(bufsize); 1145 char *buf = xmalloc(bufsize);
1189 uintmax_t address = n_bytes_to_skip; 1146 off_t address = n_bytes_to_skip;
1190 int err;
1191 1147
1192 err = 0;
1193 while (1) { 1148 while (1) {
1194 size_t i; 1149 size_t i;
1195 int c; 1150 int c;
1196 1151
1197 /* See if the next 'string_min' chars are all printing chars. */ 1152 /* See if the next 'string_min' chars are all printing chars. */
1198 tryline: 1153 tryline:
1199 1154 if (limit_bytes_to_format && (end_offset - string_min <= address))
1200 if (limit_bytes_to_format
1201 && (end_offset < string_min || end_offset - string_min <= address)
1202 ) {
1203 break; 1155 break;
1204 } 1156 i = 0;
1205
1206 for (i = 0; i < string_min; i++) {
1207 err |= read_char(&c);
1208 address++;
1209 if (c < 0) {
1210 free(buf);
1211 return err;
1212 }
1213 if (!ISPRINT(c))
1214 /* Found a non-printing. Try again starting with next char. */
1215 goto tryline;
1216 buf[i] = c;
1217 }
1218
1219 /* We found a run of 'string_min' printable characters.
1220 Now see if it is terminated with a null byte. */
1221 while (!limit_bytes_to_format || address < end_offset) { 1157 while (!limit_bytes_to_format || address < end_offset) {
1222 if (i == bufsize) { 1158 if (i == bufsize) {
1223 bufsize *= 2; 1159 bufsize += bufsize/8;
1224 buf = xrealloc(buf, bufsize * sizeof(*buf)); 1160 buf = xrealloc(buf, bufsize);
1225 } 1161 }
1226 err |= read_char(&c); 1162 read_char(&c);
1227 address++; 1163 if (c < 0) { /* EOF */
1228 if (c < 0) {
1229 free(buf); 1164 free(buf);
1230 return err; 1165 return;
1231 } 1166 }
1232 if (c == '\0') 1167 address++;
1233 break; /* It is; print this string. */ 1168 if (!c)
1169 break;
1234 if (!ISPRINT(c)) 1170 if (!ISPRINT(c))
1235 goto tryline; /* It isn't; give up on this string. */ 1171 goto tryline; /* It isn't; give up on this string. */
1236 buf[i++] = c; /* String continues; store it all. */ 1172 buf[i++] = c; /* String continues; store it all. */
1237 } 1173 }
1238 1174
1175 if (i < string_min) /* Too short! */
1176 goto tryline;
1177
1239 /* If we get here, the string is all printable and null-terminated, 1178 /* If we get here, the string is all printable and null-terminated,
1240 so print it. It is all in 'buf' and 'i' is its length. */ 1179 * so print it. It is all in 'buf' and 'i' is its length. */
1241 buf[i] = 0; 1180 buf[i] = 0;
1242 format_address(address - i - 1, ' '); 1181 format_address(address - i - 1, ' ');
1243 1182
1244 for (i = 0; (c = buf[i]); i++) { 1183 for (i = 0; (c = buf[i]); i++) {
1245 switch (c) { 1184 switch (c) {
1246 case '\007': 1185 case '\007': fputs("\\a", stdout); break;
1247 fputs("\\a", stdout); 1186 case '\b': fputs("\\b", stdout); break;
1248 break; 1187 case '\f': fputs("\\f", stdout); break;
1249 1188 case '\n': fputs("\\n", stdout); break;
1250 case '\b': 1189 case '\r': fputs("\\r", stdout); break;
1251 fputs("\\b", stdout); 1190 case '\t': fputs("\\t", stdout); break;
1252 break; 1191 case '\v': fputs("\\v", stdout); break;
1253 1192 default: putc(c, stdout);
1254 case '\f':
1255 fputs("\\f", stdout);
1256 break;
1257
1258 case '\n':
1259 fputs("\\n", stdout);
1260 break;
1261
1262 case '\r':
1263 fputs("\\r", stdout);
1264 break;
1265
1266 case '\t':
1267 fputs("\\t", stdout);
1268 break;
1269
1270 case '\v':
1271 fputs("\\v", stdout);
1272 break;
1273
1274 default:
1275 putc(c, stdout);
1276 } 1193 }
1277 } 1194 }
1278 putchar('\n'); 1195 putchar('\n');
@@ -1280,11 +1197,9 @@ dump_strings(void)
1280 1197
1281 /* We reach this point only if we search through 1198 /* We reach this point only if we search through
1282 (max_bytes_to_format - string_min) bytes before reaching EOF. */ 1199 (max_bytes_to_format - string_min) bytes before reaching EOF. */
1283
1284 free(buf); 1200 free(buf);
1285 1201
1286 err |= check_and_close(0); 1202 check_and_close();
1287 return err;
1288} 1203}
1289 1204
1290int 1205int
@@ -1296,227 +1211,181 @@ od_main(int argc, char **argv)
1296 { "m", 1024*1024 }, 1211 { "m", 1024*1024 },
1297 { NULL, 0 } 1212 { NULL, 0 }
1298 }; 1213 };
1214 unsigned opt;
1215 int l_c_m;
1216 /* The old-style 'pseudo starting address' to be printed in parentheses
1217 after any true address. */
1218 off_t pseudo_start = 0; // only for gcc
1219 enum {
1220 OPT_A = 1 << 0,
1221 OPT_N = 1 << 1,
1222 OPT_a = 1 << 2,
1223 OPT_b = 1 << 3,
1224 OPT_c = 1 << 4,
1225 OPT_d = 1 << 5,
1226 OPT_f = 1 << 6,
1227 OPT_h = 1 << 7,
1228 OPT_i = 1 << 8,
1229 OPT_j = 1 << 9,
1230 OPT_l = 1 << 10,
1231 OPT_o = 1 << 11,
1232 OPT_t = 1 << 12,
1233 OPT_v = 1 << 13,
1234 OPT_x = 1 << 14,
1235 OPT_s = 1 << 15,
1236 OPT_S = 1 << 16,
1237 OPT_w = 1 << 17,
1238 OPT_traditional = 1 << 18,
1239 };
1299 static const struct option long_options[] = { 1240 static const struct option long_options[] = {
1300 { "skip-bytes", required_argument, NULL, 'j' }, 1241 { "skip-bytes", required_argument, NULL, 'j' },
1301 { "address-radix", required_argument, NULL, 'A' }, 1242 { "address-radix", required_argument, NULL, 'A' },
1302 { "read-bytes", required_argument, NULL, 'N' }, 1243 { "read-bytes", required_argument, NULL, 'N' },
1303 { "format", required_argument, NULL, 't' }, 1244 { "format", required_argument, NULL, 't' },
1304 { "output-duplicates", no_argument, NULL, 'v' }, 1245 { "output-duplicates", no_argument, NULL, 'v' },
1305 { "strings", optional_argument, NULL, 's' }, 1246 { "strings", optional_argument, NULL, 'S' },
1306 { "traditional", no_argument, NULL, TRADITIONAL_OPTION }, 1247 { "width", optional_argument, NULL, 'w' },
1307 { "width", optional_argument, NULL, 'w' }, 1248 { "traditional", no_argument, NULL, 0xff },
1308 { NULL, 0, NULL, 0 } 1249 { NULL, 0, NULL, 0 }
1309 }; 1250 };
1310 1251 char *str_A, *str_N, *str_j, *str_S;
1311 int c; 1252 char *str_w = NULL;
1312 int n_files; 1253 llist_t *lst_t = NULL;
1313 int l_c_m;
1314 size_t desired_width = 0; // only for gcc
1315 int width_specified = 0;
1316 int n_failed_decodes = 0;
1317 int err;
1318
1319 /* The old-style 'pseudo starting address' to be printed in parentheses
1320 after any true address. */
1321 uintmax_t pseudo_start = 0; // only for gcc
1322
1323 err = 0;
1324
1325 integral_type_size[sizeof(char)] = CHAR;
1326 integral_type_size[sizeof(short)] = SHORT;
1327 integral_type_size[sizeof(int)] = INT;
1328 integral_type_size[sizeof(long)] = LONG;
1329 /* If 'long' and 'long long' have the same size, it's fine
1330 to overwrite the entry for 'long' with this one. */
1331 integral_type_size[sizeof(ulonglong_t)] = LONG_LONG;
1332
1333 fp_type_size[sizeof(float)] = FLOAT_SINGLE;
1334 /* The array entry for 'double' is filled in after that for longdouble_t
1335 so that if 'long double' is the same type or if long double isn't
1336 supported FLOAT_LONG_DOUBLE will never be used. */
1337 fp_type_size[sizeof(longdouble_t)] = FLOAT_LONG_DOUBLE;
1338 fp_type_size[sizeof(double)] = FLOAT_DOUBLE;
1339 1254
1340 spec = NULL; 1255 spec = NULL;
1341
1342 format_address = format_address_std; 1256 format_address = format_address_std;
1343 address_base = 8; 1257 address_base_char = 'o';
1344 address_pad_len = 7; 1258 address_pad_len = 7;
1345 flag_dump_strings = 0; 1259 flag_dump_strings = 0;
1346 1260
1347 while ((c = getopt_long(argc, argv, "A:N:abcdfhij:lot:vxs:w:", long_options, NULL)) != -1) { 1261 /* Parse command line */
1348 switch (c) { 1262 opt_complementary = "t::"; // list
1349 case 0: 1263 applet_long_options = long_options;
1350 break; 1264 opt = getopt32(argc, argv, "A:N:abcdfhij:lot:vxsS:"
1351 case 'A': 1265 "w::", // -w with optional param
1352 switch (optarg[0]) { 1266 // -S was -s and also had optional parameter
1353 case 'd': 1267 // but in coreutils 6.3 it was renamed and now has
1354 format_address = format_address_std; 1268 // _mandatory_ parameter
1355 address_base = 10; 1269 &str_A, &str_N, &str_j, &lst_t, &str_S, &str_w);
1356 address_pad_len = 7; 1270 argc -= optind;
1357 break; 1271 argv += optind;
1358 case 'o': 1272 if (opt & OPT_A) {
1359 format_address = format_address_std; 1273 static const char doxn[] = "doxn";
1360 address_base = 8; 1274 static FN_format_address *const doxn_format_address[] = {
1361 address_pad_len = 7; 1275 format_address_std,
1362 break; 1276 format_address_std,
1363 case 'x': 1277 format_address_std,
1364 format_address = format_address_std; 1278 format_address_none,
1365 address_base = 16; 1279 };
1366 address_pad_len = 6; 1280 static const char doxn_address_base_char[] = { 'u', 'o', 'x', 'x' };
1367 break; 1281 static const uint8_t doxn_address_pad_len[] = { 7, 7, 6, 0 };
1368 case 'n': 1282 char *p;
1369 format_address = format_address_none; 1283 int pos;
1370 address_pad_len = 0; 1284 p = strchr(doxn, str_A[0]);
1371 break; 1285 if (!p)
1372 default: 1286 bb_error_msg_and_die("bad output address radix "
1373 bb_error_msg_and_die("bad output address radix '%c' " 1287 "'%c' (must be [doxn])", str_A[0]);
1374 "(must be [doxn])", optarg[0]); 1288 pos = p - doxn;
1375 break; 1289 format_address = doxn_format_address[pos];
1376 } 1290 address_base_char = doxn_address_base_char[pos];
1377 break; 1291 address_pad_len = doxn_address_pad_len[pos];
1378
1379 case 'j':
1380 n_bytes_to_skip = xstrtoumax_sfx(optarg, 0, bkm);
1381 break;
1382 case 'N':
1383 limit_bytes_to_format = 1;
1384
1385 max_bytes_to_format = xstrtoumax_sfx(optarg, 0, bkm);
1386 break;
1387 case 's':
1388 if (optarg == NULL)
1389 string_min = 3;
1390 else {
1391 string_min = xstrtoumax_range_sfx(optarg, 0, 0, SIZE_MAX, bkm);
1392 }
1393 flag_dump_strings = 1;
1394 break;
1395 case 't':
1396 if (decode_format_string(optarg))
1397 ++n_failed_decodes;
1398 break;
1399 case 'v':
1400 abbreviate_duplicate_blocks = 0;
1401 break;
1402 case TRADITIONAL_OPTION:
1403 traditional = 1;
1404 break;
1405
1406 /* The next several cases map the traditional format
1407 specification options to the corresponding modern format
1408 specs. GNU od accepts any combination of old- and
1409 new-style options. Format specification options accumulate. */
1410
1411#define CASE_OLD_ARG(old_char,new_string) \
1412 case old_char: \
1413 if (decode_format_string(new_string)) \
1414 ++n_failed_decodes; \
1415 break
1416
1417 CASE_OLD_ARG('a', "a");
1418 CASE_OLD_ARG('b', "oC");
1419 CASE_OLD_ARG('c', "c");
1420 CASE_OLD_ARG('d', "u2");
1421 CASE_OLD_ARG('f', "fF");
1422 CASE_OLD_ARG('h', "x2");
1423 CASE_OLD_ARG('i', "d2");
1424 CASE_OLD_ARG('l', "d4");
1425 CASE_OLD_ARG('o', "o2");
1426 CASE_OLD_ARG('x', "x2");
1427
1428 /* FIXME: POSIX 1003.1-2001 with XSI requires this:
1429 CASE_OLD_ARG('s', "d2");
1430 for the traditional syntax, but this conflicts with case
1431 's' above. */
1432
1433#undef CASE_OLD_ARG
1434
1435 case 'w':
1436 width_specified = 1;
1437 desired_width = 32;
1438 if (optarg)
1439 desired_width = xatoul_range(optarg, 0, SIZE_MAX);
1440 break;
1441 default:
1442 bb_show_usage();
1443 break;
1444 }
1445 } 1292 }
1446 1293 if (opt & OPT_N) {
1447 if (n_failed_decodes > 0) 1294 limit_bytes_to_format = 1;
1448 exit(EXIT_FAILURE); 1295 max_bytes_to_format = xstrtooff_sfx(str_N, 0, bkm);
1296 }
1297 if (opt & OPT_a) decode_format_string("a");
1298 if (opt & OPT_b) decode_format_string("oC");
1299 if (opt & OPT_c) decode_format_string("c");
1300 if (opt & OPT_d) decode_format_string("u2");
1301 if (opt & OPT_f) decode_format_string("fF");
1302 if (opt & OPT_h) decode_format_string("x2");
1303 if (opt & OPT_i) decode_format_string("d2");
1304 if (opt & OPT_j) n_bytes_to_skip = xstrtooff_sfx(str_j, 0, bkm);
1305 if (opt & OPT_l) decode_format_string("d4");
1306 if (opt & OPT_o) decode_format_string("o2");
1307 //if (opt & OPT_t)...
1308 lst_t = rev_llist(lst_t);
1309 while (lst_t) {
1310 decode_format_string(lst_t->data);
1311 lst_t = lst_t->link;
1312 }
1313 if (opt & OPT_v) abbreviate_duplicate_blocks = 0;
1314 if (opt & OPT_x) decode_format_string("x2");
1315 if (opt & OPT_s) decode_format_string("d2");
1316 if (opt & OPT_S) {
1317 string_min = 3;
1318 string_min = xstrtou_sfx(str_S, 0, bkm);
1319 flag_dump_strings = 1;
1320 }
1321 //if (opt & OPT_w)...
1322 //if (opt & OPT_traditional)...
1449 1323
1450 if (flag_dump_strings && n_specs > 0) 1324 if (flag_dump_strings && n_specs > 0)
1451 bb_error_msg_and_die("no type may be specified when dumping strings"); 1325 bb_error_msg_and_die("no type may be specified when dumping strings");
1452 1326
1453 n_files = argc - optind;
1454
1455 /* If the --traditional option is used, there may be from 1327 /* If the --traditional option is used, there may be from
1456 0 to 3 remaining command line arguments; handle each case 1328 * 0 to 3 remaining command line arguments; handle each case
1457 separately. 1329 * separately.
1458 od [file] [[+]offset[.][b] [[+]label[.][b]]] 1330 * od [file] [[+]offset[.][b] [[+]label[.][b]]]
1459 The offset and pseudo_start have the same syntax. 1331 * The offset and pseudo_start have the same syntax.
1460 1332 *
1461 FIXME: POSIX 1003.1-2001 with XSI requires support for the 1333 * FIXME: POSIX 1003.1-2001 with XSI requires support for the
1462 traditional syntax even if --traditional is not given. */ 1334 * traditional syntax even if --traditional is not given. */
1463 1335
1464 if (traditional) { 1336 if (opt & OPT_traditional) {
1465 uintmax_t offset; 1337 off_t o1, o2;
1466 1338
1467 if (n_files == 1) { 1339 if (argc == 1) {
1468 if (parse_old_offset(argv[optind], &offset)) { 1340 if (parse_old_offset(argv[0], &o1)) {
1469 n_bytes_to_skip = offset; 1341 n_bytes_to_skip = o1;
1470 --n_files; 1342 --argc;
1471 ++argv; 1343 ++argv;
1472 } 1344 }
1473 } else if (n_files == 2) { 1345 } else if (argc == 2) {
1474 uintmax_t o1, o2; 1346 if (parse_old_offset(argv[0], &o1)
1475 1347 && parse_old_offset(argv[1], &o2)
1476 if (parse_old_offset(argv[optind], &o1)
1477 && parse_old_offset(argv[optind + 1], &o2)
1478 ) { 1348 ) {
1479 n_bytes_to_skip = o1; 1349 n_bytes_to_skip = o1;
1480 flag_pseudo_start = 1; 1350 flag_pseudo_start = 1;
1481 pseudo_start = o2; 1351 pseudo_start = o2;
1482 argv += 2; 1352 argv += 2;
1483 n_files -= 2; 1353 argc -= 2;
1484 } else if (parse_old_offset(argv[optind + 1], &o2)) { 1354 } else if (parse_old_offset(argv[1], &o2)) {
1485 n_bytes_to_skip = o2; 1355 n_bytes_to_skip = o2;
1486 --n_files; 1356 --argc;
1487 argv[optind + 1] = argv[optind]; 1357 argv[1] = argv[0];
1488 ++argv; 1358 ++argv;
1489 } else { 1359 } else {
1490 bb_error_msg_and_die("invalid second operand " 1360 bb_error_msg_and_die("invalid second operand "
1491 "in compatibility mode '%s'", argv[optind + 1]); 1361 "in compatibility mode '%s'", argv[1]);
1492 } 1362 }
1493 } else if (n_files == 3) { 1363 } else if (argc == 3) {
1494 uintmax_t o1, o2; 1364 if (parse_old_offset(argv[1], &o1)
1495 if (parse_old_offset(argv[optind + 1], &o1) 1365 && parse_old_offset(argv[2], &o2)
1496 && parse_old_offset(argv[optind + 2], &o2)
1497 ) { 1366 ) {
1498 n_bytes_to_skip = o1; 1367 n_bytes_to_skip = o1;
1499 flag_pseudo_start = 1; 1368 flag_pseudo_start = 1;
1500 pseudo_start = o2; 1369 pseudo_start = o2;
1501 argv[optind + 2] = argv[optind]; 1370 argv[2] = argv[0];
1502 argv += 2; 1371 argv += 2;
1503 n_files -= 2; 1372 argc -= 2;
1504 } else { 1373 } else {
1505 bb_error_msg_and_die("in compatibility mode " 1374 bb_error_msg_and_die("in compatibility mode "
1506 "the last two arguments must be offsets"); 1375 "the last two arguments must be offsets");
1507 } 1376 }
1508 } else if (n_files > 3) { 1377 } else if (argc > 3) {
1509 bb_error_msg_and_die("compatibility mode supports " 1378 bb_error_msg_and_die("compatibility mode supports "
1510 "at most three arguments"); 1379 "at most three arguments");
1511 } 1380 }
1512 1381
1513 if (flag_pseudo_start) { 1382 if (flag_pseudo_start) {
1383 format_address = format_address_label;
1514 if (format_address == format_address_none) { 1384 if (format_address == format_address_none) {
1515 address_base = 8; 1385 address_base_char = 'o';
1516 address_pad_len = 7; 1386 address_pad_len = 7;
1517 format_address = format_address_paren; 1387 format_address = format_address_paren;
1518 } else 1388 }
1519 format_address = format_address_label;
1520 } 1389 }
1521 } 1390 }
1522 1391
@@ -1527,53 +1396,45 @@ od_main(int argc, char **argv)
1527 } 1396 }
1528 1397
1529 if (n_specs == 0) { 1398 if (n_specs == 0) {
1530 if (decode_format_string("o2")) { 1399 decode_format_string("o2");
1531 /* This happens on Cray systems that don't have a 2-byte
1532 integral type. */
1533 exit(EXIT_FAILURE);
1534 }
1535 n_specs = 1; 1400 n_specs = 1;
1536 } 1401 }
1537 1402
1538 if (n_files > 0) { 1403 /* If no files were listed on the command line,
1404 set the global pointer FILE_LIST so that it
1405 references the null-terminated list of one name: "-". */
1406 file_list = default_file_list;
1407 if (argc > 0) {
1539 /* Set the global pointer FILE_LIST so that it 1408 /* Set the global pointer FILE_LIST so that it
1540 references the first file-argument on the command-line. */ 1409 references the first file-argument on the command-line. */
1541 file_list = (char const *const *) &argv[optind]; 1410 file_list = (char const *const *) argv;
1542 } else {
1543 /* No files were listed on the command line.
1544 Set the global pointer FILE_LIST so that it
1545 references the null-terminated list of one name: "-". */
1546 file_list = default_file_list;
1547 } 1411 }
1548 1412
1549 /* open the first input file */ 1413 /* open the first input file */
1550 err |= open_next_file(); 1414 open_next_file();
1551 if (in_stream == NULL)
1552 goto cleanup;
1553
1554 /* skip over any unwanted header bytes */ 1415 /* skip over any unwanted header bytes */
1555 err |= skip(n_bytes_to_skip); 1416 skip(n_bytes_to_skip);
1556 if (in_stream == NULL) 1417 if (!in_stream)
1557 goto cleanup; 1418 return 1;
1558 1419
1559 pseudo_offset = (flag_pseudo_start ? pseudo_start - n_bytes_to_skip : 0); 1420 pseudo_offset = (flag_pseudo_start ? pseudo_start - n_bytes_to_skip : 0);
1560 1421
1561 /* Compute output block length. */ 1422 /* Compute output block length. */
1562 l_c_m = get_lcm(); 1423 l_c_m = get_lcm();
1563 1424
1564 if (width_specified) { 1425 if (opt & OPT_w) { /* -w: width */
1565 if (desired_width != 0 && desired_width % l_c_m == 0) 1426 bytes_per_block = 32;
1566 bytes_per_block = desired_width; 1427 if (str_w)
1567 else { 1428 bytes_per_block = xatou(str_w);
1568 bb_error_msg("warning: invalid width %lu; using %d instead", 1429 if (!bytes_per_block || bytes_per_block % l_c_m != 0) {
1569 (unsigned long) desired_width, l_c_m); 1430 bb_error_msg("warning: invalid width %u; using %d instead",
1431 bytes_per_block, l_c_m);
1570 bytes_per_block = l_c_m; 1432 bytes_per_block = l_c_m;
1571 } 1433 }
1572 } else { 1434 } else {
1435 bytes_per_block = l_c_m;
1573 if (l_c_m < DEFAULT_BYTES_PER_BLOCK) 1436 if (l_c_m < DEFAULT_BYTES_PER_BLOCK)
1574 bytes_per_block = l_c_m * (DEFAULT_BYTES_PER_BLOCK / l_c_m); 1437 bytes_per_block *= DEFAULT_BYTES_PER_BLOCK / l_c_m;
1575 else
1576 bytes_per_block = l_c_m;
1577 } 1438 }
1578 1439
1579#ifdef DEBUG 1440#ifdef DEBUG
@@ -1583,12 +1444,13 @@ od_main(int argc, char **argv)
1583 } 1444 }
1584#endif 1445#endif
1585 1446
1586 err |= (flag_dump_strings ? dump_strings() : dump()); 1447 if (flag_dump_strings)
1587 1448 dump_strings();
1588cleanup: 1449 else
1450 dump();
1589 1451
1590 if (have_read_stdin && fclose(stdin) == EOF) 1452 if (fclose(stdin) == EOF)
1591 bb_perror_msg_and_die("standard input"); 1453 bb_perror_msg_and_die(bb_msg_standard_input);
1592 1454
1593 exit(err == 0 ? EXIT_SUCCESS : EXIT_FAILURE); 1455 return (ioerror != 0); /* err != 0 - return 1 (failure) */
1594} 1456}
diff --git a/networking/httpd.c b/networking/httpd.c
index 0de60ba06..f95e0c06e 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -671,7 +671,7 @@ static char *encodeString(const char *string)
671 * 671 *
672 * $Parameters: 672 * $Parameters:
673 * (char *) string . . . The first string to decode. 673 * (char *) string . . . The first string to decode.
674 * (int) flag . . . 1 if require decode '+' as ' ' for CGI 674 * (int) flag . . . 1 if need to decode '+' as ' ' for CGI
675 * 675 *
676 * $Return: (char *) . . . . A pointer to the decoded string (same as input). 676 * $Return: (char *) . . . . A pointer to the decoded string (same as input).
677 * 677 *
@@ -685,14 +685,18 @@ static char *decodeString(char *orig, int flag_plus_to_space)
685 char *ptr = string; 685 char *ptr = string;
686 686
687 while (*ptr) { 687 while (*ptr) {
688 if (*ptr == '+' && flag_plus_to_space) { *string++ = ' '; ptr++; } 688 if (*ptr == '+' && flag_plus_to_space) {
689 else if (*ptr != '%') *string++ = *ptr++; 689 *string++ = ' ';
690 else { 690 ptr++;
691 } else if (*ptr != '%') {
692 *string++ = *ptr++;
693 } else {
691 unsigned int value1, value2; 694 unsigned int value1, value2;
692 695
693 ptr++; 696 ptr++;
694 if (sscanf(ptr, "%1X", &value1) != 1 || 697 if (sscanf(ptr, "%1X", &value1) != 1
695 sscanf(ptr+1, "%1X", &value2) != 1) { 698 || sscanf(ptr+1, "%1X", &value2) != 1
699 ) {
696 if (!flag_plus_to_space) 700 if (!flag_plus_to_space)
697 return NULL; 701 return NULL;
698 *string++ = '%'; 702 *string++ = '%';
diff --git a/networking/wget.c b/networking/wget.c
index 49ebda73c..028e18c73 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -139,7 +139,7 @@ int wget_main(int argc, char **argv)
139 { "passive-ftp", no_argument, NULL, 0xff }, 139 { "passive-ftp", no_argument, NULL, 0xff },
140 { "header", required_argument, NULL, 0xfe }, 140 { "header", required_argument, NULL, 0xfe },
141 { 0, 0, 0, 0 } 141 { 0, 0, 0, 0 }
142}; 142 };
143 applet_long_options = wget_long_options; 143 applet_long_options = wget_long_options;
144#endif 144#endif
145 opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::"); 145 opt_complementary = "-1" USE_FEATURE_WGET_LONG_OPTIONS(":\xfe::");