3
0

od_bloaty.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. /* od -- dump files in octal and other formats
  2. Copyright (C) 92, 1995-2004 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Written by Jim Meyering. */
  15. /* Busyboxed by Denys Vlasenko
  16. Based on od.c from coreutils-5.2.1
  17. Top bloat sources:
  18. 00000073 t parse_old_offset
  19. 0000007b t get_lcm
  20. 00000090 r long_options
  21. 00000092 t print_named_ascii
  22. 000000bf t print_ascii
  23. 00000168 t write_block
  24. 00000366 t decode_format_string
  25. 00000a71 T od_main
  26. Tested for compat with coreutils 6.3
  27. using this script. Minor differences fixed.
  28. #!/bin/sh
  29. echo STD
  30. time /path/to/coreutils/od \
  31. ...params... \
  32. >std
  33. echo Exit code $?
  34. echo BBOX
  35. time ./busybox od \
  36. ...params... \
  37. >bbox
  38. echo Exit code $?
  39. diff -u -a std bbox >bbox.diff || { echo Different!; sleep 1; }
  40. */
  41. #include "libbb.h"
  42. #include <getopt.h>
  43. #define assert(a) ((void)0)
  44. /* Check for 0x7f is a coreutils 6.3 addition */
  45. #define ISPRINT(c) (((c)>=' ') && (c) != 0x7f)
  46. typedef long double longdouble_t;
  47. typedef unsigned long long ulonglong_t;
  48. typedef long long llong;
  49. #if ENABLE_LFS
  50. # define xstrtooff_sfx xstrtoull_sfx
  51. #else
  52. # define xstrtooff_sfx xstrtoul_sfx
  53. #endif
  54. /* The default number of input bytes per output line. */
  55. #define DEFAULT_BYTES_PER_BLOCK 16
  56. /* The number of decimal digits of precision in a float. */
  57. #ifndef FLT_DIG
  58. # define FLT_DIG 7
  59. #endif
  60. /* The number of decimal digits of precision in a double. */
  61. #ifndef DBL_DIG
  62. # define DBL_DIG 15
  63. #endif
  64. /* The number of decimal digits of precision in a long double. */
  65. #ifndef LDBL_DIG
  66. # define LDBL_DIG DBL_DIG
  67. #endif
  68. enum size_spec {
  69. NO_SIZE,
  70. CHAR,
  71. SHORT,
  72. INT,
  73. LONG,
  74. LONG_LONG,
  75. FLOAT_SINGLE,
  76. FLOAT_DOUBLE,
  77. FLOAT_LONG_DOUBLE,
  78. N_SIZE_SPECS
  79. };
  80. enum output_format {
  81. SIGNED_DECIMAL,
  82. UNSIGNED_DECIMAL,
  83. OCTAL,
  84. HEXADECIMAL,
  85. FLOATING_POINT,
  86. NAMED_CHARACTER,
  87. CHARACTER
  88. };
  89. /* Each output format specification (from '-t spec' or from
  90. old-style options) is represented by one of these structures. */
  91. struct tspec {
  92. enum output_format fmt;
  93. enum size_spec size;
  94. void (*print_function) (size_t, const char *, const char *);
  95. char *fmt_string;
  96. int hexl_mode_trailer;
  97. int field_width;
  98. };
  99. /* Convert the number of 8-bit bytes of a binary representation to
  100. the number of characters (digits + sign if the type is signed)
  101. required to represent the same quantity in the specified base/type.
  102. For example, a 32-bit (4-byte) quantity may require a field width
  103. as wide as the following for these types:
  104. 11 unsigned octal
  105. 11 signed decimal
  106. 10 unsigned decimal
  107. 8 unsigned hexadecimal */
  108. static const uint8_t bytes_to_oct_digits[] ALIGN1 =
  109. {0, 3, 6, 8, 11, 14, 16, 19, 22, 25, 27, 30, 32, 35, 38, 41, 43};
  110. static const uint8_t bytes_to_signed_dec_digits[] ALIGN1 =
  111. {1, 4, 6, 8, 11, 13, 16, 18, 20, 23, 25, 28, 30, 33, 35, 37, 40};
  112. static const uint8_t bytes_to_unsigned_dec_digits[] ALIGN1 =
  113. {0, 3, 5, 8, 10, 13, 15, 17, 20, 22, 25, 27, 29, 32, 34, 37, 39};
  114. static const uint8_t bytes_to_hex_digits[] ALIGN1 =
  115. {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32};
  116. /* Convert enum size_spec to the size of the named type. */
  117. static const signed char width_bytes[] ALIGN1 = {
  118. -1,
  119. sizeof(char),
  120. sizeof(short),
  121. sizeof(int),
  122. sizeof(long),
  123. sizeof(ulonglong_t),
  124. sizeof(float),
  125. sizeof(double),
  126. sizeof(longdouble_t)
  127. };
  128. /* Ensure that for each member of 'enum size_spec' there is an
  129. initializer in the width_bytes array. */
  130. struct ERR_width_bytes_has_bad_size {
  131. char ERR_width_bytes_has_bad_size[ARRAY_SIZE(width_bytes) == N_SIZE_SPECS ? 1 : -1];
  132. };
  133. static smallint flag_dump_strings;
  134. /* Non-zero if an old-style 'pseudo-address' was specified. */
  135. static smallint flag_pseudo_start;
  136. static smallint limit_bytes_to_format;
  137. /* When zero and two or more consecutive blocks are equal, format
  138. only the first block and output an asterisk alone on the following
  139. line to indicate that identical blocks have been elided. */
  140. static smallint verbose;
  141. static smallint ioerror;
  142. static size_t string_min;
  143. /* An array of specs describing how to format each input block. */
  144. static size_t n_specs;
  145. static struct tspec *spec;
  146. /* Function that accepts an address and an optional following char,
  147. and prints the address and char to stdout. */
  148. static void (*format_address)(off_t, char);
  149. /* The difference between the old-style pseudo starting address and
  150. the number of bytes to skip. */
  151. static off_t pseudo_offset;
  152. /* When zero, MAX_BYTES_TO_FORMAT and END_OFFSET are ignored, and all
  153. input is formatted. */
  154. /* The number of input bytes formatted per output line. It must be
  155. a multiple of the least common multiple of the sizes associated with
  156. the specified output types. It should be as large as possible, but
  157. no larger than 16 -- unless specified with the -w option. */
  158. static unsigned bytes_per_block = 32; /* have to use unsigned, not size_t */
  159. /* A NULL-terminated list of the file-arguments from the command line. */
  160. static const char *const *file_list;
  161. /* The input stream associated with the current file. */
  162. static FILE *in_stream;
  163. #define MAX_INTEGRAL_TYPE_SIZE sizeof(ulonglong_t)
  164. static const unsigned char integral_type_size[MAX_INTEGRAL_TYPE_SIZE + 1] ALIGN1 = {
  165. [sizeof(char)] = CHAR,
  166. #if USHRT_MAX != UCHAR_MAX
  167. [sizeof(short)] = SHORT,
  168. #endif
  169. #if UINT_MAX != USHRT_MAX
  170. [sizeof(int)] = INT,
  171. #endif
  172. #if ULONG_MAX != UINT_MAX
  173. [sizeof(long)] = LONG,
  174. #endif
  175. #if ULLONG_MAX != ULONG_MAX
  176. [sizeof(ulonglong_t)] = LONG_LONG,
  177. #endif
  178. };
  179. #define MAX_FP_TYPE_SIZE sizeof(longdouble_t)
  180. static const unsigned char fp_type_size[MAX_FP_TYPE_SIZE + 1] ALIGN1 = {
  181. /* gcc seems to allow repeated indexes. Last one stays */
  182. [sizeof(longdouble_t)] = FLOAT_LONG_DOUBLE,
  183. [sizeof(double)] = FLOAT_DOUBLE,
  184. [sizeof(float)] = FLOAT_SINGLE
  185. };
  186. static unsigned
  187. gcd(unsigned u, unsigned v)
  188. {
  189. unsigned t;
  190. while (v != 0) {
  191. t = u % v;
  192. u = v;
  193. v = t;
  194. }
  195. return u;
  196. }
  197. /* Compute the least common multiple of U and V. */
  198. static unsigned
  199. lcm(unsigned u, unsigned v) {
  200. unsigned t = gcd(u, v);
  201. if (t == 0)
  202. return 0;
  203. return u * v / t;
  204. }
  205. static void
  206. print_s_char(size_t n_bytes, const char *block, const char *fmt_string)
  207. {
  208. while (n_bytes--) {
  209. int tmp = *(signed char *) block;
  210. printf(fmt_string, tmp);
  211. block += sizeof(unsigned char);
  212. }
  213. }
  214. static void
  215. print_char(size_t n_bytes, const char *block, const char *fmt_string)
  216. {
  217. while (n_bytes--) {
  218. unsigned tmp = *(unsigned char *) block;
  219. printf(fmt_string, tmp);
  220. block += sizeof(unsigned char);
  221. }
  222. }
  223. static void
  224. print_s_short(size_t n_bytes, const char *block, const char *fmt_string)
  225. {
  226. n_bytes /= sizeof(signed short);
  227. while (n_bytes--) {
  228. int tmp = *(signed short *) block;
  229. printf(fmt_string, tmp);
  230. block += sizeof(unsigned short);
  231. }
  232. }
  233. static void
  234. print_short(size_t n_bytes, const char *block, const char *fmt_string)
  235. {
  236. n_bytes /= sizeof(unsigned short);
  237. while (n_bytes--) {
  238. unsigned tmp = *(unsigned short *) block;
  239. printf(fmt_string, tmp);
  240. block += sizeof(unsigned short);
  241. }
  242. }
  243. static void
  244. print_int(size_t n_bytes, const char *block, const char *fmt_string)
  245. {
  246. n_bytes /= sizeof(unsigned);
  247. while (n_bytes--) {
  248. unsigned tmp = *(unsigned *) block;
  249. printf(fmt_string, tmp);
  250. block += sizeof(unsigned);
  251. }
  252. }
  253. #if UINT_MAX == ULONG_MAX
  254. # define print_long print_int
  255. #else
  256. static void
  257. print_long(size_t n_bytes, const char *block, const char *fmt_string)
  258. {
  259. n_bytes /= sizeof(unsigned long);
  260. while (n_bytes--) {
  261. unsigned long tmp = *(unsigned long *) block;
  262. printf(fmt_string, tmp);
  263. block += sizeof(unsigned long);
  264. }
  265. }
  266. #endif
  267. #if ULONG_MAX == ULLONG_MAX
  268. # define print_long_long print_long
  269. #else
  270. static void
  271. print_long_long(size_t n_bytes, const char *block, const char *fmt_string)
  272. {
  273. n_bytes /= sizeof(ulonglong_t);
  274. while (n_bytes--) {
  275. ulonglong_t tmp = *(ulonglong_t *) block;
  276. printf(fmt_string, tmp);
  277. block += sizeof(ulonglong_t);
  278. }
  279. }
  280. #endif
  281. static void
  282. print_float(size_t n_bytes, const char *block, const char *fmt_string)
  283. {
  284. n_bytes /= sizeof(float);
  285. while (n_bytes--) {
  286. float tmp = *(float *) block;
  287. printf(fmt_string, tmp);
  288. block += sizeof(float);
  289. }
  290. }
  291. static void
  292. print_double(size_t n_bytes, const char *block, const char *fmt_string)
  293. {
  294. n_bytes /= sizeof(double);
  295. while (n_bytes--) {
  296. double tmp = *(double *) block;
  297. printf(fmt_string, tmp);
  298. block += sizeof(double);
  299. }
  300. }
  301. static void
  302. print_long_double(size_t n_bytes, const char *block, const char *fmt_string)
  303. {
  304. n_bytes /= sizeof(longdouble_t);
  305. while (n_bytes--) {
  306. longdouble_t tmp = *(longdouble_t *) block;
  307. printf(fmt_string, tmp);
  308. block += sizeof(longdouble_t);
  309. }
  310. }
  311. /* print_[named]_ascii are optimized for speed.
  312. * Remember, someday you may want to pump gigabytes through this thing.
  313. * Saving a dozen of .text bytes here is counter-productive */
  314. static void
  315. print_named_ascii(size_t n_bytes, const char *block,
  316. const char *unused_fmt_string ATTRIBUTE_UNUSED)
  317. {
  318. /* Names for some non-printing characters. */
  319. static const char charname[33][3] ALIGN1 = {
  320. "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
  321. " bs", " ht", " nl", " vt", " ff", " cr", " so", " si",
  322. "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
  323. "can", " em", "sub", "esc", " fs", " gs", " rs", " us",
  324. " sp"
  325. };
  326. // buf[N] pos: 01234 56789
  327. char buf[12] = " x\0 0xx\0";
  328. // actually " x\0 xxx\0", but I want to share the string with below.
  329. // [12] because we take three 32bit stack slots anyway, and
  330. // gcc is too dumb to initialize with constant stores,
  331. // it copies initializer from rodata. Oh well.
  332. while (n_bytes--) {
  333. unsigned masked_c = *(unsigned char *) block++;
  334. masked_c &= 0x7f;
  335. if (masked_c == 0x7f) {
  336. fputs(" del", stdout);
  337. continue;
  338. }
  339. if (masked_c > ' ') {
  340. buf[3] = masked_c;
  341. fputs(buf, stdout);
  342. continue;
  343. }
  344. /* Why? Because printf(" %3.3s") is much slower... */
  345. buf[6] = charname[masked_c][0];
  346. buf[7] = charname[masked_c][1];
  347. buf[8] = charname[masked_c][2];
  348. fputs(buf+5, stdout);
  349. }
  350. }
  351. static void
  352. print_ascii(size_t n_bytes, const char *block,
  353. const char *unused_fmt_string ATTRIBUTE_UNUSED)
  354. {
  355. // buf[N] pos: 01234 56789
  356. char buf[12] = " x\0 0xx\0";
  357. while (n_bytes--) {
  358. const char *s;
  359. unsigned c = *(unsigned char *) block++;
  360. if (ISPRINT(c)) {
  361. buf[3] = c;
  362. fputs(buf, stdout);
  363. continue;
  364. }
  365. switch (c) {
  366. case '\0':
  367. s = " \\0";
  368. break;
  369. case '\007':
  370. s = " \\a";
  371. break;
  372. case '\b':
  373. s = " \\b";
  374. break;
  375. case '\f':
  376. s = " \\f";
  377. break;
  378. case '\n':
  379. s = " \\n";
  380. break;
  381. case '\r':
  382. s = " \\r";
  383. break;
  384. case '\t':
  385. s = " \\t";
  386. break;
  387. case '\v':
  388. s = " \\v";
  389. break;
  390. case '\x7f':
  391. s = " 177";
  392. break;
  393. default: /* c is never larger than 040 */
  394. buf[7] = (c >> 3) + '0';
  395. buf[8] = (c & 7) + '0';
  396. s = buf + 5;
  397. }
  398. fputs(s, stdout);
  399. }
  400. }
  401. /* Given a list of one or more input filenames FILE_LIST, set the global
  402. file pointer IN_STREAM and the global string INPUT_FILENAME to the
  403. first one that can be successfully opened. Modify FILE_LIST to
  404. reference the next filename in the list. A file name of "-" is
  405. interpreted as standard input. If any file open fails, give an error
  406. message and return nonzero. */
  407. static void
  408. open_next_file(void)
  409. {
  410. while (1) {
  411. if (!*file_list)
  412. return;
  413. in_stream = fopen_or_warn_stdin(*file_list++);
  414. if (in_stream) {
  415. break;
  416. }
  417. ioerror = 1;
  418. }
  419. if (limit_bytes_to_format && !flag_dump_strings)
  420. setbuf(in_stream, NULL);
  421. }
  422. /* Test whether there have been errors on in_stream, and close it if
  423. it is not standard input. Return nonzero if there has been an error
  424. on in_stream or stdout; return zero otherwise. This function will
  425. report more than one error only if both a read and a write error
  426. have occurred. IN_ERRNO, if nonzero, is the error number
  427. corresponding to the most recent action for IN_STREAM. */
  428. static void
  429. check_and_close(void)
  430. {
  431. if (in_stream) {
  432. if (ferror(in_stream)) {
  433. bb_error_msg("%s: read error", (in_stream == stdin)
  434. ? bb_msg_standard_input
  435. : file_list[-1]
  436. );
  437. ioerror = 1;
  438. }
  439. fclose_if_not_stdin(in_stream);
  440. in_stream = NULL;
  441. }
  442. if (ferror(stdout)) {
  443. bb_error_msg("write error");
  444. ioerror = 1;
  445. }
  446. }
  447. /* If S points to a single valid modern od format string, put
  448. a description of that format in *TSPEC, make *NEXT point at the
  449. character following the just-decoded format (if *NEXT is non-NULL),
  450. and return zero. For example, if S were "d4afL"
  451. *NEXT would be set to "afL" and *TSPEC would be
  452. {
  453. fmt = SIGNED_DECIMAL;
  454. size = INT or LONG; (whichever integral_type_size[4] resolves to)
  455. print_function = print_int; (assuming size == INT)
  456. fmt_string = "%011d%c";
  457. }
  458. S_ORIG is solely for reporting errors. It should be the full format
  459. string argument. */
  460. static void
  461. decode_one_format(const char *s_orig, const char *s, const char **next,
  462. struct tspec *tspec)
  463. {
  464. enum size_spec size_spec;
  465. unsigned size;
  466. enum output_format fmt;
  467. const char *p;
  468. char *end;
  469. char *fmt_string = NULL;
  470. void (*print_function) (size_t, const char *, const char *);
  471. unsigned c;
  472. unsigned field_width = 0;
  473. int pos;
  474. assert(tspec != NULL);
  475. switch (*s) {
  476. case 'd':
  477. case 'o':
  478. case 'u':
  479. case 'x': {
  480. static const char CSIL[] ALIGN1 = "CSIL";
  481. c = *s++;
  482. p = strchr(CSIL, *s);
  483. if (!p) {
  484. size = sizeof(int);
  485. if (isdigit(s[0])) {
  486. size = bb_strtou(s, &end, 0);
  487. if (errno == ERANGE
  488. || MAX_INTEGRAL_TYPE_SIZE < size
  489. || integral_type_size[size] == NO_SIZE
  490. ) {
  491. bb_error_msg_and_die("invalid type string '%s'; "
  492. "%u-byte %s type is not supported",
  493. s_orig, size, "integral");
  494. }
  495. s = end;
  496. }
  497. } else {
  498. static const uint8_t CSIL_sizeof[] = {
  499. sizeof(char),
  500. sizeof(short),
  501. sizeof(int),
  502. sizeof(long),
  503. };
  504. size = CSIL_sizeof[p - CSIL];
  505. }
  506. #define ISPEC_TO_FORMAT(Spec, Min_format, Long_format, Max_format) \
  507. ((Spec) == LONG_LONG ? (Max_format) \
  508. : ((Spec) == LONG ? (Long_format) : (Min_format)))
  509. #define FMT_BYTES_ALLOCATED 9
  510. size_spec = integral_type_size[size];
  511. {
  512. static const char doux[] ALIGN1 = "doux";
  513. static const char doux_fmt_letter[][4] = {
  514. "lld", "llo", "llu", "llx"
  515. };
  516. static const enum output_format doux_fmt[] = {
  517. SIGNED_DECIMAL,
  518. OCTAL,
  519. UNSIGNED_DECIMAL,
  520. HEXADECIMAL,
  521. };
  522. static const uint8_t *const doux_bytes_to_XXX[] = {
  523. bytes_to_signed_dec_digits,
  524. bytes_to_oct_digits,
  525. bytes_to_unsigned_dec_digits,
  526. bytes_to_hex_digits,
  527. };
  528. static const char doux_fmtstring[][sizeof(" %%0%u%s")] = {
  529. " %%%u%s",
  530. " %%0%u%s",
  531. " %%%u%s",
  532. " %%0%u%s",
  533. };
  534. pos = strchr(doux, c) - doux;
  535. fmt = doux_fmt[pos];
  536. field_width = doux_bytes_to_XXX[pos][size];
  537. p = doux_fmt_letter[pos] + 2;
  538. if (size_spec == LONG) p--;
  539. if (size_spec == LONG_LONG) p -= 2;
  540. fmt_string = xasprintf(doux_fmtstring[pos], field_width, p);
  541. }
  542. switch (size_spec) {
  543. case CHAR:
  544. print_function = (fmt == SIGNED_DECIMAL
  545. ? print_s_char
  546. : print_char);
  547. break;
  548. case SHORT:
  549. print_function = (fmt == SIGNED_DECIMAL
  550. ? print_s_short
  551. : print_short);
  552. break;
  553. case INT:
  554. print_function = print_int;
  555. break;
  556. case LONG:
  557. print_function = print_long;
  558. break;
  559. default: /* case LONG_LONG: */
  560. print_function = print_long_long;
  561. break;
  562. }
  563. break;
  564. }
  565. case 'f': {
  566. static const char FDL[] ALIGN1 = "FDL";
  567. fmt = FLOATING_POINT;
  568. ++s;
  569. p = strchr(FDL, *s);
  570. if (!p) {
  571. size = sizeof(double);
  572. if (isdigit(s[0])) {
  573. size = bb_strtou(s, &end, 0);
  574. if (errno == ERANGE || size > MAX_FP_TYPE_SIZE
  575. || fp_type_size[size] == NO_SIZE
  576. ) {
  577. bb_error_msg_and_die("invalid type string '%s'; "
  578. "%u-byte %s type is not supported",
  579. s_orig, size, "floating point");
  580. }
  581. s = end;
  582. }
  583. } else {
  584. static const uint8_t FDL_sizeof[] = {
  585. sizeof(float),
  586. sizeof(double),
  587. sizeof(longdouble_t),
  588. };
  589. size = FDL_sizeof[p - FDL];
  590. }
  591. size_spec = fp_type_size[size];
  592. switch (size_spec) {
  593. case FLOAT_SINGLE:
  594. print_function = print_float;
  595. field_width = FLT_DIG + 8;
  596. /* Don't use %#e; not all systems support it. */
  597. fmt_string = xasprintf(" %%%d.%de", field_width, FLT_DIG);
  598. break;
  599. case FLOAT_DOUBLE:
  600. print_function = print_double;
  601. field_width = DBL_DIG + 8;
  602. fmt_string = xasprintf(" %%%d.%de", field_width, DBL_DIG);
  603. break;
  604. default: /* case FLOAT_LONG_DOUBLE: */
  605. print_function = print_long_double;
  606. field_width = LDBL_DIG + 8;
  607. fmt_string = xasprintf(" %%%d.%dLe", field_width, LDBL_DIG);
  608. break;
  609. }
  610. break;
  611. }
  612. case 'a':
  613. ++s;
  614. fmt = NAMED_CHARACTER;
  615. size_spec = CHAR;
  616. print_function = print_named_ascii;
  617. field_width = 3;
  618. break;
  619. case 'c':
  620. ++s;
  621. fmt = CHARACTER;
  622. size_spec = CHAR;
  623. print_function = print_ascii;
  624. field_width = 3;
  625. break;
  626. default:
  627. bb_error_msg_and_die("invalid character '%c' "
  628. "in type string '%s'", *s, s_orig);
  629. }
  630. tspec->size = size_spec;
  631. tspec->fmt = fmt;
  632. tspec->print_function = print_function;
  633. tspec->fmt_string = fmt_string;
  634. tspec->field_width = field_width;
  635. tspec->hexl_mode_trailer = (*s == 'z');
  636. if (tspec->hexl_mode_trailer)
  637. s++;
  638. if (next != NULL)
  639. *next = s;
  640. }
  641. /* Decode the modern od format string S. Append the decoded
  642. representation to the global array SPEC, reallocating SPEC if
  643. necessary. */
  644. static void
  645. decode_format_string(const char *s)
  646. {
  647. const char *s_orig = s;
  648. while (*s != '\0') {
  649. struct tspec tspec;
  650. const char *next;
  651. decode_one_format(s_orig, s, &next, &tspec);
  652. assert(s != next);
  653. s = next;
  654. n_specs++;
  655. spec = xrealloc(spec, n_specs * sizeof(*spec));
  656. memcpy(&spec[n_specs-1], &tspec, sizeof *spec);
  657. }
  658. }
  659. /* Given a list of one or more input filenames FILE_LIST, set the global
  660. file pointer IN_STREAM to position N_SKIP in the concatenation of
  661. those files. If any file operation fails or if there are fewer than
  662. N_SKIP bytes in the combined input, give an error message and return
  663. nonzero. When possible, use seek rather than read operations to
  664. advance IN_STREAM. */
  665. static void
  666. skip(off_t n_skip)
  667. {
  668. if (n_skip == 0)
  669. return;
  670. while (in_stream) { /* !EOF */
  671. struct stat file_stats;
  672. /* First try seeking. For large offsets, this extra work is
  673. worthwhile. If the offset is below some threshold it may be
  674. more efficient to move the pointer by reading. There are two
  675. issues when trying to seek:
  676. - the file must be seekable.
  677. - before seeking to the specified position, make sure
  678. that the new position is in the current file.
  679. Try to do that by getting file's size using fstat.
  680. But that will work only for regular files. */
  681. /* The st_size field is valid only for regular files
  682. (and for symbolic links, which cannot occur here).
  683. If the number of bytes left to skip is at least
  684. as large as the size of the current file, we can
  685. decrement n_skip and go on to the next file. */
  686. if (fstat(fileno(in_stream), &file_stats) == 0
  687. && S_ISREG(file_stats.st_mode) && file_stats.st_size > 0
  688. ) {
  689. if (file_stats.st_size < n_skip) {
  690. n_skip -= file_stats.st_size;
  691. /* take "check & close / open_next" route */
  692. } else {
  693. if (fseeko(in_stream, n_skip, SEEK_CUR) != 0)
  694. ioerror = 1;
  695. return;
  696. }
  697. } else {
  698. /* If it's not a regular file with positive size,
  699. position the file pointer by reading. */
  700. char buf[1024];
  701. size_t n_bytes_to_read = 1024;
  702. size_t n_bytes_read;
  703. while (n_skip > 0) {
  704. if (n_skip < n_bytes_to_read)
  705. n_bytes_to_read = n_skip;
  706. n_bytes_read = fread(buf, 1, n_bytes_to_read, in_stream);
  707. n_skip -= n_bytes_read;
  708. if (n_bytes_read != n_bytes_to_read)
  709. break; /* EOF on this file or error */
  710. }
  711. }
  712. if (n_skip == 0)
  713. return;
  714. check_and_close();
  715. open_next_file();
  716. }
  717. if (n_skip)
  718. bb_error_msg_and_die("cannot skip past end of combined input");
  719. }
  720. typedef void FN_format_address(off_t address, char c);
  721. static void
  722. format_address_none(off_t address ATTRIBUTE_UNUSED, char c ATTRIBUTE_UNUSED)
  723. {
  724. }
  725. static char address_fmt[] ALIGN1 = "%0n"OFF_FMT"xc";
  726. /* Corresponds to 'x' above */
  727. #define address_base_char address_fmt[sizeof(address_fmt)-3]
  728. /* Corresponds to 'n' above */
  729. #define address_pad_len_char address_fmt[2]
  730. static void
  731. format_address_std(off_t address, char c)
  732. {
  733. /* Corresponds to 'c' */
  734. address_fmt[sizeof(address_fmt)-2] = c;
  735. printf(address_fmt, address);
  736. }
  737. #if ENABLE_GETOPT_LONG
  738. /* only used with --traditional */
  739. static void
  740. format_address_paren(off_t address, char c)
  741. {
  742. putchar('(');
  743. format_address_std(address, ')');
  744. if (c) putchar(c);
  745. }
  746. static void
  747. format_address_label(off_t address, char c)
  748. {
  749. format_address_std(address, ' ');
  750. format_address_paren(address + pseudo_offset, c);
  751. }
  752. #endif
  753. static void
  754. dump_hexl_mode_trailer(size_t n_bytes, const char *block)
  755. {
  756. fputs(" >", stdout);
  757. while (n_bytes--) {
  758. unsigned c = *(unsigned char *) block++;
  759. c = (ISPRINT(c) ? c : '.');
  760. putchar(c);
  761. }
  762. putchar('<');
  763. }
  764. /* Write N_BYTES bytes from CURR_BLOCK to standard output once for each
  765. of the N_SPEC format specs. CURRENT_OFFSET is the byte address of
  766. CURR_BLOCK in the concatenation of input files, and it is printed
  767. (optionally) only before the output line associated with the first
  768. format spec. When duplicate blocks are being abbreviated, the output
  769. for a sequence of identical input blocks is the output for the first
  770. block followed by an asterisk alone on a line. It is valid to compare
  771. the blocks PREV_BLOCK and CURR_BLOCK only when N_BYTES == BYTES_PER_BLOCK.
  772. That condition may be false only for the last input block -- and then
  773. only when it has not been padded to length BYTES_PER_BLOCK. */
  774. static void
  775. write_block(off_t current_offset, size_t n_bytes,
  776. const char *prev_block, const char *curr_block)
  777. {
  778. static char first = 1;
  779. static char prev_pair_equal = 0;
  780. size_t i;
  781. if (!verbose && !first
  782. && n_bytes == bytes_per_block
  783. && memcmp(prev_block, curr_block, bytes_per_block) == 0
  784. ) {
  785. if (prev_pair_equal) {
  786. /* The two preceding blocks were equal, and the current
  787. block is the same as the last one, so print nothing. */
  788. } else {
  789. puts("*");
  790. prev_pair_equal = 1;
  791. }
  792. } else {
  793. first = 0;
  794. prev_pair_equal = 0;
  795. for (i = 0; i < n_specs; i++) {
  796. if (i == 0)
  797. format_address(current_offset, '\0');
  798. else
  799. printf("%*s", address_pad_len_char - '0', "");
  800. (*spec[i].print_function) (n_bytes, curr_block, spec[i].fmt_string);
  801. if (spec[i].hexl_mode_trailer) {
  802. /* space-pad out to full line width, then dump the trailer */
  803. int datum_width = width_bytes[spec[i].size];
  804. int blank_fields = (bytes_per_block - n_bytes) / datum_width;
  805. int field_width = spec[i].field_width + 1;
  806. printf("%*s", blank_fields * field_width, "");
  807. dump_hexl_mode_trailer(n_bytes, curr_block);
  808. }
  809. putchar('\n');
  810. }
  811. }
  812. }
  813. static void
  814. read_block(size_t n, char *block, size_t *n_bytes_in_buffer)
  815. {
  816. assert(0 < n && n <= bytes_per_block);
  817. *n_bytes_in_buffer = 0;
  818. if (n == 0)
  819. return;
  820. while (in_stream != NULL) { /* EOF. */
  821. size_t n_needed;
  822. size_t n_read;
  823. n_needed = n - *n_bytes_in_buffer;
  824. n_read = fread(block + *n_bytes_in_buffer, 1, n_needed, in_stream);
  825. *n_bytes_in_buffer += n_read;
  826. if (n_read == n_needed)
  827. break;
  828. /* error check is done in check_and_close */
  829. check_and_close();
  830. open_next_file();
  831. }
  832. }
  833. /* Return the least common multiple of the sizes associated
  834. with the format specs. */
  835. static int
  836. get_lcm(void)
  837. {
  838. size_t i;
  839. int l_c_m = 1;
  840. for (i = 0; i < n_specs; i++)
  841. l_c_m = lcm(l_c_m, width_bytes[(int) spec[i].size]);
  842. return l_c_m;
  843. }
  844. #if ENABLE_GETOPT_LONG
  845. /* If S is a valid traditional offset specification with an optional
  846. leading '+' return nonzero and set *OFFSET to the offset it denotes. */
  847. static int
  848. parse_old_offset(const char *s, off_t *offset)
  849. {
  850. static const struct suffix_mult Bb[] = {
  851. { "B", 1024 },
  852. { "b", 512 },
  853. { }
  854. };
  855. char *p;
  856. int radix;
  857. /* Skip over any leading '+'. */
  858. if (s[0] == '+') ++s;
  859. /* Determine the radix we'll use to interpret S. If there is a '.',
  860. * it's decimal, otherwise, if the string begins with '0X'or '0x',
  861. * it's hexadecimal, else octal. */
  862. p = strchr(s, '.');
  863. radix = 8;
  864. if (p) {
  865. p[0] = '\0'; /* cheating */
  866. radix = 10;
  867. } else if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
  868. radix = 16;
  869. *offset = xstrtooff_sfx(s, radix, Bb);
  870. if (p) p[0] = '.';
  871. return (*offset >= 0);
  872. }
  873. #endif
  874. /* Read a chunk of size BYTES_PER_BLOCK from the input files, write the
  875. formatted block to standard output, and repeat until the specified
  876. maximum number of bytes has been read or until all input has been
  877. processed. If the last block read is smaller than BYTES_PER_BLOCK
  878. and its size is not a multiple of the size associated with a format
  879. spec, extend the input block with zero bytes until its length is a
  880. multiple of all format spec sizes. Write the final block. Finally,
  881. write on a line by itself the offset of the byte after the last byte
  882. read. */
  883. static void
  884. dump(off_t current_offset, off_t end_offset)
  885. {
  886. char *block[2];
  887. int idx;
  888. size_t n_bytes_read;
  889. block[0] = xmalloc(2*bytes_per_block);
  890. block[1] = block[0] + bytes_per_block;
  891. idx = 0;
  892. if (limit_bytes_to_format) {
  893. while (1) {
  894. size_t n_needed;
  895. if (current_offset >= end_offset) {
  896. n_bytes_read = 0;
  897. break;
  898. }
  899. n_needed = MIN(end_offset - current_offset,
  900. (off_t) bytes_per_block);
  901. read_block(n_needed, block[idx], &n_bytes_read);
  902. if (n_bytes_read < bytes_per_block)
  903. break;
  904. assert(n_bytes_read == bytes_per_block);
  905. write_block(current_offset, n_bytes_read,
  906. block[!idx], block[idx]);
  907. current_offset += n_bytes_read;
  908. idx = !idx;
  909. }
  910. } else {
  911. while (1) {
  912. read_block(bytes_per_block, block[idx], &n_bytes_read);
  913. if (n_bytes_read < bytes_per_block)
  914. break;
  915. assert(n_bytes_read == bytes_per_block);
  916. write_block(current_offset, n_bytes_read,
  917. block[!idx], block[idx]);
  918. current_offset += n_bytes_read;
  919. idx = !idx;
  920. }
  921. }
  922. if (n_bytes_read > 0) {
  923. int l_c_m;
  924. size_t bytes_to_write;
  925. l_c_m = get_lcm();
  926. /* Make bytes_to_write the smallest multiple of l_c_m that
  927. is at least as large as n_bytes_read. */
  928. bytes_to_write = l_c_m * ((n_bytes_read + l_c_m - 1) / l_c_m);
  929. memset(block[idx] + n_bytes_read, 0, bytes_to_write - n_bytes_read);
  930. write_block(current_offset, bytes_to_write,
  931. block[!idx], block[idx]);
  932. current_offset += n_bytes_read;
  933. }
  934. format_address(current_offset, '\n');
  935. if (limit_bytes_to_format && current_offset >= end_offset)
  936. check_and_close();
  937. free(block[0]);
  938. }
  939. /* Read a single byte into *C from the concatenation of the input files
  940. named in the global array FILE_LIST. On the first call to this
  941. function, the global variable IN_STREAM is expected to be an open
  942. stream associated with the input file INPUT_FILENAME. If IN_STREAM
  943. is at end-of-file, close it and update the global variables IN_STREAM
  944. and INPUT_FILENAME so they correspond to the next file in the list.
  945. Then try to read a byte from the newly opened file. Repeat if
  946. necessary until EOF is reached for the last file in FILE_LIST, then
  947. set *C to EOF and return. Subsequent calls do likewise. */
  948. static void
  949. read_char(int *c)
  950. {
  951. while (in_stream) { /* !EOF */
  952. *c = fgetc(in_stream);
  953. if (*c != EOF)
  954. return;
  955. check_and_close();
  956. open_next_file();
  957. }
  958. *c = EOF;
  959. }
  960. /* Read N bytes into BLOCK from the concatenation of the input files
  961. named in the global array FILE_LIST. On the first call to this
  962. function, the global variable IN_STREAM is expected to be an open
  963. stream associated with the input file INPUT_FILENAME. If all N
  964. bytes cannot be read from IN_STREAM, close IN_STREAM and update
  965. the global variables IN_STREAM and INPUT_FILENAME. Then try to
  966. read the remaining bytes from the newly opened file. Repeat if
  967. necessary until EOF is reached for the last file in FILE_LIST.
  968. On subsequent calls, don't modify BLOCK and return zero. Set
  969. *N_BYTES_IN_BUFFER to the number of bytes read. If an error occurs,
  970. it will be detected through ferror when the stream is about to be
  971. closed. If there is an error, give a message but continue reading
  972. as usual and return nonzero. Otherwise return zero. */
  973. /* STRINGS mode. Find each "string constant" in the input.
  974. A string constant is a run of at least 'string_min' ASCII
  975. graphic (or formatting) characters terminated by a null.
  976. Based on a function written by Richard Stallman for a
  977. traditional version of od. */
  978. static void
  979. dump_strings(off_t address, off_t end_offset)
  980. {
  981. size_t bufsize = MAX(100, string_min);
  982. char *buf = xmalloc(bufsize);
  983. while (1) {
  984. size_t i;
  985. int c;
  986. /* See if the next 'string_min' chars are all printing chars. */
  987. tryline:
  988. if (limit_bytes_to_format && (end_offset - string_min <= address))
  989. break;
  990. i = 0;
  991. while (!limit_bytes_to_format || address < end_offset) {
  992. if (i == bufsize) {
  993. bufsize += bufsize/8;
  994. buf = xrealloc(buf, bufsize);
  995. }
  996. read_char(&c);
  997. if (c < 0) { /* EOF */
  998. free(buf);
  999. return;
  1000. }
  1001. address++;
  1002. if (!c)
  1003. break;
  1004. if (!ISPRINT(c))
  1005. goto tryline; /* It isn't; give up on this string. */
  1006. buf[i++] = c; /* String continues; store it all. */
  1007. }
  1008. if (i < string_min) /* Too short! */
  1009. goto tryline;
  1010. /* If we get here, the string is all printable and NUL-terminated,
  1011. * so print it. It is all in 'buf' and 'i' is its length. */
  1012. buf[i] = 0;
  1013. format_address(address - i - 1, ' ');
  1014. for (i = 0; (c = buf[i]); i++) {
  1015. switch (c) {
  1016. case '\007': fputs("\\a", stdout); break;
  1017. case '\b': fputs("\\b", stdout); break;
  1018. case '\f': fputs("\\f", stdout); break;
  1019. case '\n': fputs("\\n", stdout); break;
  1020. case '\r': fputs("\\r", stdout); break;
  1021. case '\t': fputs("\\t", stdout); break;
  1022. case '\v': fputs("\\v", stdout); break;
  1023. default: putchar(c);
  1024. }
  1025. }
  1026. putchar('\n');
  1027. }
  1028. /* We reach this point only if we search through
  1029. (max_bytes_to_format - string_min) bytes before reaching EOF. */
  1030. free(buf);
  1031. check_and_close();
  1032. }
  1033. int od_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1034. int od_main(int argc, char **argv)
  1035. {
  1036. static const struct suffix_mult bkm[] = {
  1037. { "b", 512 },
  1038. { "k", 1024 },
  1039. { "m", 1024*1024 },
  1040. { }
  1041. };
  1042. enum {
  1043. OPT_A = 1 << 0,
  1044. OPT_N = 1 << 1,
  1045. OPT_a = 1 << 2,
  1046. OPT_b = 1 << 3,
  1047. OPT_c = 1 << 4,
  1048. OPT_d = 1 << 5,
  1049. OPT_f = 1 << 6,
  1050. OPT_h = 1 << 7,
  1051. OPT_i = 1 << 8,
  1052. OPT_j = 1 << 9,
  1053. OPT_l = 1 << 10,
  1054. OPT_o = 1 << 11,
  1055. OPT_t = 1 << 12,
  1056. OPT_v = 1 << 13,
  1057. OPT_x = 1 << 14,
  1058. OPT_s = 1 << 15,
  1059. OPT_S = 1 << 16,
  1060. OPT_w = 1 << 17,
  1061. OPT_traditional = (1 << 18) * ENABLE_GETOPT_LONG,
  1062. };
  1063. #if ENABLE_GETOPT_LONG
  1064. static const char od_longopts[] ALIGN1 =
  1065. "skip-bytes\0" Required_argument "j"
  1066. "address-radix\0" Required_argument "A"
  1067. "read-bytes\0" Required_argument "N"
  1068. "format\0" Required_argument "t"
  1069. "output-duplicates\0" No_argument "v"
  1070. "strings\0" Optional_argument "S"
  1071. "width\0" Optional_argument "w"
  1072. "traditional\0" No_argument "\xff"
  1073. ;
  1074. #endif
  1075. char *str_A, *str_N, *str_j, *str_S;
  1076. llist_t *lst_t = NULL;
  1077. unsigned opt;
  1078. int l_c_m;
  1079. /* The old-style 'pseudo starting address' to be printed in parentheses
  1080. after any true address. */
  1081. off_t pseudo_start = pseudo_start; // for gcc
  1082. /* The number of input bytes to skip before formatting and writing. */
  1083. off_t n_bytes_to_skip = 0;
  1084. /* The offset of the first byte after the last byte to be formatted. */
  1085. off_t end_offset = 0;
  1086. /* The maximum number of bytes that will be formatted. */
  1087. off_t max_bytes_to_format = 0;
  1088. spec = NULL;
  1089. format_address = format_address_std;
  1090. address_base_char = 'o';
  1091. address_pad_len_char = '7';
  1092. /* flag_dump_strings = 0; - already is */
  1093. /* Parse command line */
  1094. opt_complementary = "w+:t::"; /* -w N, -t is a list */
  1095. #if ENABLE_GETOPT_LONG
  1096. applet_long_options = od_longopts;
  1097. #endif
  1098. opt = getopt32(argv, "A:N:abcdfhij:lot:vxsS:"
  1099. "w::", // -w with optional param
  1100. // -S was -s and also had optional parameter
  1101. // but in coreutils 6.3 it was renamed and now has
  1102. // _mandatory_ parameter
  1103. &str_A, &str_N, &str_j, &lst_t, &str_S, &bytes_per_block);
  1104. argc -= optind;
  1105. argv += optind;
  1106. if (opt & OPT_A) {
  1107. static const char doxn[] ALIGN1 = "doxn";
  1108. static const char doxn_address_base_char[] ALIGN1 = {
  1109. 'u', 'o', 'x', /* '?' fourth one is not important */
  1110. };
  1111. static const uint8_t doxn_address_pad_len_char[] ALIGN1 = {
  1112. '7', '7', '6', /* '?' */
  1113. };
  1114. char *p;
  1115. int pos;
  1116. p = strchr(doxn, str_A[0]);
  1117. if (!p)
  1118. bb_error_msg_and_die("bad output address radix "
  1119. "'%c' (must be [doxn])", str_A[0]);
  1120. pos = p - doxn;
  1121. if (pos == 3) format_address = format_address_none;
  1122. address_base_char = doxn_address_base_char[pos];
  1123. address_pad_len_char = doxn_address_pad_len_char[pos];
  1124. }
  1125. if (opt & OPT_N) {
  1126. limit_bytes_to_format = 1;
  1127. max_bytes_to_format = xstrtooff_sfx(str_N, 0, bkm);
  1128. }
  1129. if (opt & OPT_a) decode_format_string("a");
  1130. if (opt & OPT_b) decode_format_string("oC");
  1131. if (opt & OPT_c) decode_format_string("c");
  1132. if (opt & OPT_d) decode_format_string("u2");
  1133. if (opt & OPT_f) decode_format_string("fF");
  1134. if (opt & OPT_h) decode_format_string("x2");
  1135. if (opt & OPT_i) decode_format_string("d2");
  1136. if (opt & OPT_j) n_bytes_to_skip = xstrtooff_sfx(str_j, 0, bkm);
  1137. if (opt & OPT_l) decode_format_string("d4");
  1138. if (opt & OPT_o) decode_format_string("o2");
  1139. //if (opt & OPT_t)...
  1140. while (lst_t) {
  1141. decode_format_string(lst_t->data);
  1142. lst_t = lst_t->link;
  1143. }
  1144. if (opt & OPT_v) verbose = 1;
  1145. if (opt & OPT_x) decode_format_string("x2");
  1146. if (opt & OPT_s) decode_format_string("d2");
  1147. if (opt & OPT_S) {
  1148. string_min = 3;
  1149. string_min = xstrtou_sfx(str_S, 0, bkm);
  1150. flag_dump_strings = 1;
  1151. }
  1152. //if (opt & OPT_w)...
  1153. //if (opt & OPT_traditional)...
  1154. if (flag_dump_strings && n_specs > 0)
  1155. bb_error_msg_and_die("no type may be specified when dumping strings");
  1156. /* If the --traditional option is used, there may be from
  1157. * 0 to 3 remaining command line arguments; handle each case
  1158. * separately.
  1159. * od [file] [[+]offset[.][b] [[+]label[.][b]]]
  1160. * The offset and pseudo_start have the same syntax.
  1161. *
  1162. * FIXME: POSIX 1003.1-2001 with XSI requires support for the
  1163. * traditional syntax even if --traditional is not given. */
  1164. #if ENABLE_GETOPT_LONG
  1165. if (opt & OPT_traditional) {
  1166. off_t o1, o2;
  1167. if (argc == 1) {
  1168. if (parse_old_offset(argv[0], &o1)) {
  1169. n_bytes_to_skip = o1;
  1170. --argc;
  1171. ++argv;
  1172. }
  1173. } else if (argc == 2) {
  1174. if (parse_old_offset(argv[0], &o1)
  1175. && parse_old_offset(argv[1], &o2)
  1176. ) {
  1177. n_bytes_to_skip = o1;
  1178. flag_pseudo_start = 1;
  1179. pseudo_start = o2;
  1180. argv += 2;
  1181. argc -= 2;
  1182. } else if (parse_old_offset(argv[1], &o2)) {
  1183. n_bytes_to_skip = o2;
  1184. --argc;
  1185. argv[1] = argv[0];
  1186. ++argv;
  1187. } else {
  1188. bb_error_msg_and_die("invalid second operand "
  1189. "in compatibility mode '%s'", argv[1]);
  1190. }
  1191. } else if (argc == 3) {
  1192. if (parse_old_offset(argv[1], &o1)
  1193. && parse_old_offset(argv[2], &o2)
  1194. ) {
  1195. n_bytes_to_skip = o1;
  1196. flag_pseudo_start = 1;
  1197. pseudo_start = o2;
  1198. argv[2] = argv[0];
  1199. argv += 2;
  1200. argc -= 2;
  1201. } else {
  1202. bb_error_msg_and_die("in compatibility mode "
  1203. "the last two arguments must be offsets");
  1204. }
  1205. } else if (argc > 3) {
  1206. bb_error_msg_and_die("compatibility mode supports "
  1207. "at most three arguments");
  1208. }
  1209. if (flag_pseudo_start) {
  1210. if (format_address == format_address_none) {
  1211. address_base_char = 'o';
  1212. address_pad_len_char = '7';
  1213. format_address = format_address_paren;
  1214. } else
  1215. format_address = format_address_label;
  1216. }
  1217. }
  1218. #endif
  1219. if (limit_bytes_to_format) {
  1220. end_offset = n_bytes_to_skip + max_bytes_to_format;
  1221. if (end_offset < n_bytes_to_skip)
  1222. bb_error_msg_and_die("skip-bytes + read-bytes is too large");
  1223. }
  1224. if (n_specs == 0) {
  1225. decode_format_string("o2");
  1226. n_specs = 1;
  1227. }
  1228. /* If no files were listed on the command line,
  1229. set the global pointer FILE_LIST so that it
  1230. references the null-terminated list of one name: "-". */
  1231. file_list = bb_argv_dash;
  1232. if (argc > 0) {
  1233. /* Set the global pointer FILE_LIST so that it
  1234. references the first file-argument on the command-line. */
  1235. file_list = (char const *const *) argv;
  1236. }
  1237. /* open the first input file */
  1238. open_next_file();
  1239. /* skip over any unwanted header bytes */
  1240. skip(n_bytes_to_skip);
  1241. if (!in_stream)
  1242. return EXIT_FAILURE;
  1243. pseudo_offset = (flag_pseudo_start ? pseudo_start - n_bytes_to_skip : 0);
  1244. /* Compute output block length. */
  1245. l_c_m = get_lcm();
  1246. if (opt & OPT_w) { /* -w: width */
  1247. if (!bytes_per_block || bytes_per_block % l_c_m != 0) {
  1248. bb_error_msg("warning: invalid width %u; using %d instead",
  1249. (unsigned)bytes_per_block, l_c_m);
  1250. bytes_per_block = l_c_m;
  1251. }
  1252. } else {
  1253. bytes_per_block = l_c_m;
  1254. if (l_c_m < DEFAULT_BYTES_PER_BLOCK)
  1255. bytes_per_block *= DEFAULT_BYTES_PER_BLOCK / l_c_m;
  1256. }
  1257. #ifdef DEBUG
  1258. for (i = 0; i < n_specs; i++) {
  1259. printf("%d: fmt=\"%s\" width=%d\n",
  1260. i, spec[i].fmt_string, width_bytes[spec[i].size]);
  1261. }
  1262. #endif
  1263. if (flag_dump_strings)
  1264. dump_strings(n_bytes_to_skip, end_offset);
  1265. else
  1266. dump(n_bytes_to_skip, end_offset);
  1267. if (fclose(stdin) == EOF)
  1268. bb_perror_msg_and_die(bb_msg_standard_input);
  1269. return ioerror;
  1270. }