hexdump.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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(char *name)
  15. {
  16. char *p;
  17. FILE *fp;
  18. char *buf;
  19. fp = xfopen(name, "r");
  20. while ((buf = xmalloc_fgetline(fp)) != NULL) {
  21. p = skip_whitespace(buf);
  22. if (*p && (*p != '#')) {
  23. bb_dump_add(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" USE_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. const char *p;
  48. int ch;
  49. #if ENABLE_FEATURE_HEXDUMP_REVERSE
  50. FILE *fp;
  51. smallint rdump = 0;
  52. #endif
  53. bb_dump_vflag = FIRST;
  54. bb_dump_length = -1;
  55. if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */
  56. ch = 'C';
  57. goto hd_applet;
  58. }
  59. /* We cannot use getopt32: in hexdump options are cumulative.
  60. * E.g. "hexdump -C -C file" should dump each line twice */
  61. while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
  62. p = strchr(hexdump_opts, ch);
  63. if (!p)
  64. bb_show_usage();
  65. if ((p - hexdump_opts) < 5) {
  66. bb_dump_add(add_first);
  67. bb_dump_add(add_strings[(int)(p - hexdump_opts)]);
  68. }
  69. /* Save a little bit of space below by omitting the 'else's. */
  70. if (ch == 'C') {
  71. hd_applet:
  72. bb_dump_add("\"%08.8_Ax\n\"");
  73. bb_dump_add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" ");
  74. bb_dump_add("\" |\" 16/1 \"%_p\" \"|\\n\"");
  75. }
  76. if (ch == 'e') {
  77. bb_dump_add(optarg);
  78. } /* else */
  79. if (ch == 'f') {
  80. bb_dump_addfile(optarg);
  81. } /* else */
  82. if (ch == 'n') {
  83. bb_dump_length = xatoi_u(optarg);
  84. } /* else */
  85. if (ch == 's') {
  86. bb_dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes);
  87. } /* else */
  88. if (ch == 'v') {
  89. bb_dump_vflag = ALL;
  90. }
  91. #if ENABLE_FEATURE_HEXDUMP_REVERSE
  92. if (ch == 'R') {
  93. rdump = 1;
  94. }
  95. #endif
  96. }
  97. if (!bb_dump_fshead) {
  98. bb_dump_add(add_first);
  99. bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
  100. }
  101. argv += optind;
  102. #if !ENABLE_FEATURE_HEXDUMP_REVERSE
  103. return bb_dump_dump(argv);
  104. #else
  105. if (!rdump) {
  106. return bb_dump_dump(argv);
  107. }
  108. /* -R: reverse of 'hexdump -Cv' */
  109. fp = stdin;
  110. if (!*argv) {
  111. argv--;
  112. goto jump_in;
  113. }
  114. do {
  115. char *buf;
  116. fp = xfopen(*argv, "r");
  117. jump_in:
  118. while ((buf = xmalloc_fgetline(fp)) != NULL) {
  119. p = buf;
  120. while (1) {
  121. /* skip address or previous byte */
  122. while (isxdigit(*p)) p++;
  123. while (*p == ' ') p++;
  124. /* '|' char will break the line */
  125. if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1)
  126. break;
  127. putchar(ch);
  128. }
  129. free(buf);
  130. }
  131. fclose(fp);
  132. } while (*++argv);
  133. fflush_stdout_and_exit(EXIT_SUCCESS);
  134. #endif
  135. }