3
0

od_bloaty.c 38 KB

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