hexdump.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * hexdump implementation for busybox
  3. * Based on code from util-linux v 2.11l
  4. *
  5. * Copyright (c) 1989
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * Original copyright notice is retained at the end of this file.
  23. */
  24. #include <getopt.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "busybox.h"
  28. #include "dump.h"
  29. static void bb_dump_addfile(char *name)
  30. {
  31. register char *p;
  32. FILE *fp;
  33. char *buf;
  34. fp = bb_xfopen(name, "r");
  35. while ((buf = bb_get_chomped_line_from_file(fp)) != NULL) {
  36. p = (char *) bb_skip_whitespace(buf);
  37. if (*p && (*p != '#')) {
  38. bb_dump_add(p);
  39. }
  40. free(buf);
  41. }
  42. fclose(fp);
  43. }
  44. static const char * const add_strings[] = {
  45. "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
  46. "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
  47. "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
  48. "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
  49. "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
  50. };
  51. static const char add_first[] = "\"%07.7_Ax\n\"";
  52. static const char hexdump_opts[] = "bcdoxe:f:n:s:v";
  53. static const struct suffix_mult suffixes[] = {
  54. {"b", 512 },
  55. {"k", 1024 },
  56. {"m", 1024*1024 },
  57. {NULL, 0 }
  58. };
  59. int hexdump_main(int argc, char **argv)
  60. {
  61. // register FS *tfs;
  62. const char *p;
  63. int ch;
  64. bb_dump_vflag = FIRST;
  65. bb_dump_length = -1;
  66. while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
  67. if ((p = strchr(hexdump_opts, ch)) != NULL) {
  68. if ((p - hexdump_opts) < 5) {
  69. bb_dump_add(add_first);
  70. bb_dump_add(add_strings[(int)(p - hexdump_opts)]);
  71. } else {
  72. /* Sae a little bit of space below by omitting the 'else's. */
  73. if (ch == 'e') {
  74. bb_dump_add(optarg);
  75. } /* else */
  76. if (ch == 'f') {
  77. bb_dump_addfile(optarg);
  78. } /* else */
  79. if (ch == 'n') {
  80. bb_dump_length = bb_xgetularg10_bnd(optarg, 0, INT_MAX);
  81. } /* else */
  82. if (ch == 's') {
  83. bb_dump_skip = bb_xgetularg_bnd_sfx(optarg, 10, 0, LONG_MAX, suffixes);
  84. } /* else */
  85. if (ch == 'v') {
  86. bb_dump_vflag = ALL;
  87. }
  88. }
  89. } else {
  90. bb_show_usage();
  91. }
  92. }
  93. if (!bb_dump_fshead) {
  94. bb_dump_add(add_first);
  95. bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
  96. }
  97. argv += optind;
  98. return(bb_dump_dump(argv));
  99. }
  100. /*
  101. * Copyright (c) 1989 The Regents of the University of California.
  102. * All rights reserved.
  103. *
  104. * Redistribution and use in source and binary forms, with or without
  105. * modification, are permitted provided that the following conditions
  106. * are met:
  107. * 1. Redistributions of source code must retain the above copyright
  108. * notice, this list of conditions and the following disclaimer.
  109. * 2. Redistributions in binary form must reproduce the above copyright
  110. * notice, this list of conditions and the following disclaimer in the
  111. * documentation and/or other materials provided with the distribution.
  112. * 3. Neither the name of the University nor the names of its contributors
  113. * may be used to endorse or promote products derived from this software
  114. * without specific prior written permission.
  115. *
  116. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  117. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  118. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  119. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  120. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  121. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  122. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  123. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  124. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  125. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  126. * SUCH DAMAGE.
  127. */