hexdump.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * hexdump implementation for busybox
  4. * Based on code from util-linux v 2.11l
  5. *
  6. * Copyright (c) 1989
  7. * The Regents of the University of California. All rights reserved.
  8. *
  9. * Licensed under GPLv2 or later, see file License in this tarball for details.
  10. */
  11. #include "libbb.h"
  12. #include "dump.h"
  13. /* This is a NOEXEC applet. Be very careful! */
  14. static void bb_dump_addfile(dumper_t *dumper, char *name)
  15. {
  16. char *p;
  17. FILE *fp;
  18. char *buf;
  19. fp = xfopen_for_read(name);
  20. while ((buf = xmalloc_fgetline(fp)) != NULL) {
  21. p = skip_whitespace(buf);
  22. if (*p && (*p != '#')) {
  23. bb_dump_add(dumper, p);
  24. }
  25. free(buf);
  26. }
  27. fclose(fp);
  28. }
  29. static const char *const add_strings[] = {
  30. "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
  31. "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
  32. "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
  33. "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
  34. "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
  35. };
  36. static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\"";
  37. static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R");
  38. static const struct suffix_mult suffixes[] = {
  39. { "b", 512 },
  40. { "k", 1024 },
  41. { "m", 1024*1024 },
  42. { }
  43. };
  44. int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  45. int hexdump_main(int argc, char **argv)
  46. {
  47. dumper_t *dumper = alloc_dumper();
  48. const char *p;
  49. int ch;
  50. #if ENABLE_FEATURE_HEXDUMP_REVERSE
  51. FILE *fp;
  52. smallint rdump = 0;
  53. #endif
  54. if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
  55. ch = 'C';
  56. goto hd_applet;
  57. }
  58. /* We cannot use getopt32: in hexdump options are cumulative.
  59. * E.g. "hexdump -C -C file" should dump each line twice */
  60. while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
  61. p = strchr(hexdump_opts, ch);
  62. if (!p)
  63. bb_show_usage();
  64. if ((p - hexdump_opts) < 5) {
  65. bb_dump_add(dumper, add_first);
  66. bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]);
  67. }
  68. /* Save a little bit of space below by omitting the 'else's. */
  69. if (ch == 'C') {
  70. hd_applet:
  71. bb_dump_add(dumper, "\"%08.8_Ax\n\"");
  72. bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
  73. bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\"");
  74. }
  75. if (ch == 'e') {
  76. bb_dump_add(dumper, optarg);
  77. } /* else */
  78. if (ch == 'f') {
  79. bb_dump_addfile(dumper, optarg);
  80. } /* else */
  81. if (ch == 'n') {
  82. dumper->dump_length = xatoi_u(optarg);
  83. } /* else */
  84. if (ch == 's') {
  85. dumper->dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes);
  86. } /* else */
  87. if (ch == 'v') {
  88. dumper->dump_vflag = ALL;
  89. }
  90. #if ENABLE_FEATURE_HEXDUMP_REVERSE
  91. if (ch == 'R') {
  92. rdump = 1;
  93. }
  94. #endif
  95. }
  96. if (!dumper->fshead) {
  97. bb_dump_add(dumper, add_first);
  98. bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
  99. }
  100. argv += optind;
  101. #if !ENABLE_FEATURE_HEXDUMP_REVERSE
  102. return bb_dump_dump(dumper, argv);
  103. #else
  104. if (!rdump) {
  105. return bb_dump_dump(dumper, argv);
  106. }
  107. /* -R: reverse of 'hexdump -Cv' */
  108. fp = stdin;
  109. if (!*argv) {
  110. argv--;
  111. goto jump_in;
  112. }
  113. do {
  114. char *buf;
  115. fp = xfopen_for_read(*argv);
  116. jump_in:
  117. while ((buf = xmalloc_fgetline(fp)) != NULL) {
  118. p = buf;
  119. while (1) {
  120. /* skip address or previous byte */
  121. while (isxdigit(*p)) p++;
  122. while (*p == ' ') p++;
  123. /* '|' char will break the line */
  124. if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
  125. break;
  126. putchar(ch);
  127. }
  128. free(buf);
  129. }
  130. fclose(fp);
  131. } while (*++argv);
  132. fflush_stdout_and_exit(EXIT_SUCCESS);
  133. #endif
  134. }