od_bloaty.c 37 KB

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