"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 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 | * |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file License in this tarball for details. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 12 | #include <getopt.h> |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 13 | #include "busybox.h" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 14 | #include "dump.h" |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 16 | /* This is a NOEXEC applet. Be very careful! */ |
| 17 | |
| 18 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 19 | static void bb_dump_addfile(char *name) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 20 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 21 | char *p; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 22 | FILE *fp; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 23 | char *buf; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 24 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 25 | fp = xfopen(name, "r"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 26 | |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 27 | while ((buf = xmalloc_getline(fp)) != NULL) { |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 28 | p = skip_whitespace(buf); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 29 | |
| 30 | if (*p && (*p != '#')) { |
| 31 | bb_dump_add(p); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 32 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 33 | free(buf); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 34 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 35 | fclose(fp); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | static const char * const add_strings[] = { |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 39 | "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */ |
| 40 | "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */ |
| 41 | "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */ |
| 42 | "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */ |
| 43 | "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static const char add_first[] = "\"%07.7_Ax\n\""; |
| 47 | |
Paul Fox | 969af89 | 2005-11-28 21:06:00 +0000 | [diff] [blame] | 48 | static const char hexdump_opts[] = "bcdoxCe:f:n:s:v"; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | |
| 50 | static const struct suffix_mult suffixes[] = { |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 51 | { "b", 512 }, |
| 52 | { "k", 1024 }, |
| 53 | { "m", 1024*1024 }, |
| 54 | { NULL, 0 } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 57 | int hexdump_main(int argc, char **argv); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 58 | int hexdump_main(int argc, char **argv) |
| 59 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | const char *p; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 61 | int ch; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 62 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | bb_dump_vflag = FIRST; |
| 64 | bb_dump_length = -1; |
| 65 | |
| 66 | while ((ch = getopt(argc, argv, hexdump_opts)) > 0) { |
Denis Vlasenko | 4c196a8 | 2006-09-23 15:53:01 +0000 | [diff] [blame] | 67 | p = strchr(hexdump_opts, ch); |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 68 | if (!p) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 69 | bb_show_usage(); |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 70 | 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') { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 86 | bb_dump_length = xatoi_u(optarg); |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 87 | } /* else */ |
| 88 | if (ch == 's') { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 89 | bb_dump_skip = xatoul_range_sfx(optarg, 0, LONG_MAX, suffixes); |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 90 | } /* else */ |
| 91 | if (ch == 'v') { |
| 92 | bb_dump_vflag = ALL; |
| 93 | } |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 97 | if (!bb_dump_fshead) { |
| 98 | bb_dump_add(add_first); |
| 99 | bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\""); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | argv += optind; |
| 103 | |
Denis Vlasenko | 2dbeaa9 | 2006-09-23 13:31:46 +0000 | [diff] [blame] | 104 | return bb_dump_dump(argv); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 105 | } |