"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 | |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 12 | #include "busybox.h" |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 13 | #include <getopt.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 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 16 | static void bb_dump_addfile(char *name) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 17 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 18 | char *p; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 19 | FILE *fp; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 20 | char *buf; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 21 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame^] | 22 | fp = xfopen(name, "r"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 23 | |
| 24 | while ((buf = bb_get_chomped_line_from_file(fp)) != NULL) { |
Rob Landley | ea224be | 2006-06-18 20:20:07 +0000 | [diff] [blame] | 25 | p = skip_whitespace(buf); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 26 | |
| 27 | if (*p && (*p != '#')) { |
| 28 | bb_dump_add(p); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 29 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 30 | free(buf); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 31 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 32 | fclose(fp); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 35 | static const char * const add_strings[] = { |
| 36 | "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */ |
| 37 | "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */ |
| 38 | "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */ |
| 39 | "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */ |
| 40 | "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */ |
| 41 | }; |
| 42 | |
| 43 | static const char add_first[] = "\"%07.7_Ax\n\""; |
| 44 | |
Paul Fox | 969af89 | 2005-11-28 21:06:00 +0000 | [diff] [blame] | 45 | static const char hexdump_opts[] = "bcdoxCe:f:n:s:v"; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 46 | |
| 47 | static const struct suffix_mult suffixes[] = { |
| 48 | {"b", 512 }, |
| 49 | {"k", 1024 }, |
| 50 | {"m", 1024*1024 }, |
| 51 | {NULL, 0 } |
| 52 | }; |
| 53 | |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 54 | int hexdump_main(int argc, char **argv) |
| 55 | { |
| 56 | // register FS *tfs; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 57 | const char *p; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 58 | int ch; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 59 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | bb_dump_vflag = FIRST; |
| 61 | bb_dump_length = -1; |
| 62 | |
| 63 | while ((ch = getopt(argc, argv, hexdump_opts)) > 0) { |
| 64 | if ((p = strchr(hexdump_opts, ch)) != NULL) { |
| 65 | if ((p - hexdump_opts) < 5) { |
| 66 | bb_dump_add(add_first); |
| 67 | bb_dump_add(add_strings[(int)(p - hexdump_opts)]); |
Paul Fox | 969af89 | 2005-11-28 21:06:00 +0000 | [diff] [blame] | 68 | } else if (ch == 'C') { |
| 69 | bb_dump_add("\"%08.8_Ax\n\""); |
| 70 | bb_dump_add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" "); |
| 71 | bb_dump_add("\" |\" 16/1 \"%_p\" \"|\\n\""); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | } else { |
| 73 | /* Sae a little bit of space below by omitting the 'else's. */ |
| 74 | if (ch == 'e') { |
| 75 | bb_dump_add(optarg); |
| 76 | } /* else */ |
| 77 | if (ch == 'f') { |
| 78 | bb_dump_addfile(optarg); |
| 79 | } /* else */ |
| 80 | if (ch == 'n') { |
| 81 | bb_dump_length = bb_xgetularg10_bnd(optarg, 0, INT_MAX); |
| 82 | } /* else */ |
| 83 | if (ch == 's') { |
| 84 | bb_dump_skip = bb_xgetularg_bnd_sfx(optarg, 10, 0, LONG_MAX, suffixes); |
| 85 | } /* else */ |
| 86 | if (ch == 'v') { |
| 87 | bb_dump_vflag = ALL; |
| 88 | } |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 89 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 90 | } else { |
| 91 | bb_show_usage(); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 95 | if (!bb_dump_fshead) { |
| 96 | bb_dump_add(add_first); |
| 97 | bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\""); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | argv += optind; |
| 101 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | return(bb_dump_dump(argv)); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 103 | } |