Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/util-linux/hexdump.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/util-linux/hexdump.c
r1765 r2725 5 5 * 6 6 * Copyright (c) 1989 7 * 7 * The Regents of the University of California. All rights reserved. 8 8 * 9 * Licensed under GPLv2 or later, see file L icense in this tarball for details.9 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 10 10 */ 11 11 12 #include <getopt.h>13 12 #include "libbb.h" 14 13 #include "dump.h" … … 16 15 /* This is a NOEXEC applet. Be very careful! */ 17 16 18 19 static void bb_dump_addfile(char *name) 17 static void bb_dump_addfile(dumper_t *dumper, char *name) 20 18 { 21 19 char *p; … … 23 21 char *buf; 24 22 25 fp = xfopen(name, "r"); 26 27 while ((buf = xmalloc_getline(fp)) != NULL) { 23 fp = xfopen_for_read(name); 24 while ((buf = xmalloc_fgetline(fp)) != NULL) { 28 25 p = skip_whitespace(buf); 29 30 26 if (*p && (*p != '#')) { 31 bb_dump_add( p);27 bb_dump_add(dumper, p); 32 28 } 33 29 free(buf); … … 37 33 38 34 static const char *const add_strings[] = { 39 "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", 40 "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", 41 "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", 42 "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", 43 "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", 35 "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */ 36 "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */ 37 "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */ 38 "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */ 39 "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */ 44 40 }; 45 41 46 42 static const char add_first[] ALIGN1 = "\"%07.7_Ax\n\""; 47 43 48 static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" ;44 static const char hexdump_opts[] ALIGN1 = "bcdoxCe:f:n:s:v" IF_FEATURE_HEXDUMP_REVERSE("R"); 49 45 50 46 static const struct suffix_mult suffixes[] = { … … 52 48 { "k", 1024 }, 53 49 { "m", 1024*1024 }, 54 { }50 { "", 0 } 55 51 }; 56 52 57 int hexdump_main(int argc, char **argv) ;53 int hexdump_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 58 54 int hexdump_main(int argc, char **argv) 59 55 { 56 dumper_t *dumper = alloc_dumper(); 60 57 const char *p; 61 58 int ch; 59 #if ENABLE_FEATURE_HEXDUMP_REVERSE 60 FILE *fp; 61 smallint rdump = 0; 62 #endif 62 63 63 bb_dump_vflag = FIRST; 64 bb_dump_length = -1; 64 if (ENABLE_HD && !applet_name[2]) { /* we are "hd" */ 65 ch = 'C'; 66 goto hd_applet; 67 } 65 68 69 /* We cannot use getopt32: in hexdump options are cumulative. 70 * E.g. "hexdump -C -C file" should dump each line twice */ 66 71 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) { 67 72 p = strchr(hexdump_opts, ch); … … 69 74 bb_show_usage(); 70 75 if ((p - hexdump_opts) < 5) { 71 bb_dump_add(add_first); 72 bb_dump_add(add_strings[(int)(p - hexdump_opts)]); 73 } else if (ch == 'C') { 74 bb_dump_add("\"%08.8_Ax\n\""); 75 bb_dump_add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" "); 76 bb_dump_add("\" |\" 16/1 \"%_p\" \"|\\n\""); 77 } else { 78 /* Save a little bit of space below by omitting the 'else's. */ 79 if (ch == 'e') { 80 bb_dump_add(optarg); 81 } /* else */ 82 if (ch == 'f') { 83 bb_dump_addfile(optarg); 84 } /* else */ 85 if (ch == 'n') { 86 bb_dump_length = xatoi_u(optarg); 87 } /* else */ 88 if (ch == 's') { 89 bb_dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes); 90 } /* else */ 91 if (ch == 'v') { 92 bb_dump_vflag = ALL; 93 } 76 bb_dump_add(dumper, add_first); 77 bb_dump_add(dumper, add_strings[(int)(p - hexdump_opts)]); 94 78 } 79 /* Save a little bit of space below by omitting the 'else's. */ 80 if (ch == 'C') { 81 hd_applet: 82 bb_dump_add(dumper, "\"%08.8_Ax\n\""); 83 bb_dump_add(dumper, "\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" "); 84 bb_dump_add(dumper, "\" |\" 16/1 \"%_p\" \"|\\n\""); 85 } 86 if (ch == 'e') { 87 bb_dump_add(dumper, optarg); 88 } /* else */ 89 if (ch == 'f') { 90 bb_dump_addfile(dumper, optarg); 91 } /* else */ 92 if (ch == 'n') { 93 dumper->dump_length = xatoi_positive(optarg); 94 } /* else */ 95 if (ch == 's') { /* compat: -s accepts hex numbers too */ 96 dumper->dump_skip = xstrtoul_range_sfx(optarg, /*base:*/ 0, /*lo:*/ 0, /*hi:*/ LONG_MAX, suffixes); 97 } /* else */ 98 if (ch == 'v') { 99 dumper->dump_vflag = ALL; 100 } 101 #if ENABLE_FEATURE_HEXDUMP_REVERSE 102 if (ch == 'R') { 103 rdump = 1; 104 } 105 #endif 95 106 } 96 107 97 if (! bb_dump_fshead) {98 bb_dump_add( add_first);99 bb_dump_add( "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");108 if (!dumper->fshead) { 109 bb_dump_add(dumper, add_first); 110 bb_dump_add(dumper, "\"%07.7_ax \" 8/2 \"%04x \" \"\\n\""); 100 111 } 101 112 102 113 argv += optind; 103 114 104 return bb_dump_dump(argv); 115 #if !ENABLE_FEATURE_HEXDUMP_REVERSE 116 return bb_dump_dump(dumper, argv); 117 #else 118 if (!rdump) { 119 return bb_dump_dump(dumper, argv); 120 } 121 122 /* -R: reverse of 'hexdump -Cv' */ 123 fp = stdin; 124 if (!*argv) { 125 argv--; 126 goto jump_in; 127 } 128 129 do { 130 char *buf; 131 fp = xfopen_for_read(*argv); 132 jump_in: 133 while ((buf = xmalloc_fgetline(fp)) != NULL) { 134 p = buf; 135 while (1) { 136 /* skip address or previous byte */ 137 while (isxdigit(*p)) p++; 138 while (*p == ' ') p++; 139 /* '|' char will break the line */ 140 if (!isxdigit(*p) || sscanf(p, "%x ", &ch) != 1) 141 break; 142 putchar(ch); 143 } 144 free(buf); 145 } 146 fclose(fp); 147 } while (*++argv); 148 149 fflush_stdout_and_exit(EXIT_SUCCESS); 150 #endif 105 151 }
Note:
See TracChangeset
for help on using the changeset viewer.