Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 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 | |
| 25 | #include <getopt.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 28 | #include "busybox.h" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 29 | #include "dump.h" |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 30 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 31 | static void bb_dump_addfile(char *name) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 32 | { |
| 33 | register char *p; |
| 34 | FILE *fp; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 35 | char *buf; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 36 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 37 | fp = bb_xfopen(name, "r"); |
| 38 | |
| 39 | while ((buf = bb_get_chomped_line_from_file(fp)) != NULL) { |
| 40 | p = (char *) bb_skip_whitespace(buf); |
| 41 | |
| 42 | if (*p && (*p != '#')) { |
| 43 | bb_dump_add(p); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 44 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | free(buf); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 46 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 47 | fclose(fp); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 50 | static const char * const add_strings[] = { |
| 51 | "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */ |
| 52 | "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */ |
| 53 | "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */ |
| 54 | "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */ |
| 55 | "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */ |
| 56 | }; |
| 57 | |
| 58 | static const char add_first[] = "\"%07.7_Ax\n\""; |
| 59 | |
Paul Fox | 969af89 | 2005-11-28 21:06:00 +0000 | [diff] [blame] | 60 | static const char hexdump_opts[] = "bcdoxCe:f:n:s:v"; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 61 | |
| 62 | static const struct suffix_mult suffixes[] = { |
| 63 | {"b", 512 }, |
| 64 | {"k", 1024 }, |
| 65 | {"m", 1024*1024 }, |
| 66 | {NULL, 0 } |
| 67 | }; |
| 68 | |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 69 | int hexdump_main(int argc, char **argv) |
| 70 | { |
| 71 | // register FS *tfs; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | const char *p; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 73 | int ch; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 74 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | bb_dump_vflag = FIRST; |
| 76 | bb_dump_length = -1; |
| 77 | |
| 78 | while ((ch = getopt(argc, argv, hexdump_opts)) > 0) { |
| 79 | if ((p = strchr(hexdump_opts, ch)) != NULL) { |
| 80 | if ((p - hexdump_opts) < 5) { |
| 81 | bb_dump_add(add_first); |
| 82 | bb_dump_add(add_strings[(int)(p - hexdump_opts)]); |
Paul Fox | 969af89 | 2005-11-28 21:06:00 +0000 | [diff] [blame] | 83 | } else if (ch == 'C') { |
| 84 | bb_dump_add("\"%08.8_Ax\n\""); |
| 85 | bb_dump_add("\"%08.8_ax \" 8/1 \"%02x \" \" \" 8/1 \"%02x \" "); |
| 86 | bb_dump_add("\" |\" 16/1 \"%_p\" \"|\\n\""); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 87 | } else { |
| 88 | /* Sae a little bit of space below by omitting the 'else's. */ |
| 89 | if (ch == 'e') { |
| 90 | bb_dump_add(optarg); |
| 91 | } /* else */ |
| 92 | if (ch == 'f') { |
| 93 | bb_dump_addfile(optarg); |
| 94 | } /* else */ |
| 95 | if (ch == 'n') { |
| 96 | bb_dump_length = bb_xgetularg10_bnd(optarg, 0, INT_MAX); |
| 97 | } /* else */ |
| 98 | if (ch == 's') { |
| 99 | bb_dump_skip = bb_xgetularg_bnd_sfx(optarg, 10, 0, LONG_MAX, suffixes); |
| 100 | } /* else */ |
| 101 | if (ch == 'v') { |
| 102 | bb_dump_vflag = ALL; |
| 103 | } |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 104 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 105 | } else { |
| 106 | bb_show_usage(); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 110 | if (!bb_dump_fshead) { |
| 111 | bb_dump_add(add_first); |
| 112 | bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\""); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | argv += optind; |
| 116 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 117 | return(bb_dump_dump(argv)); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 118 | } |
| 119 | /* |
| 120 | * Copyright (c) 1989 The Regents of the University of California. |
| 121 | * All rights reserved. |
| 122 | * |
| 123 | * Redistribution and use in source and binary forms, with or without |
| 124 | * modification, are permitted provided that the following conditions |
| 125 | * are met: |
| 126 | * 1. Redistributions of source code must retain the above copyright |
| 127 | * notice, this list of conditions and the following disclaimer. |
| 128 | * 2. Redistributions in binary form must reproduce the above copyright |
| 129 | * notice, this list of conditions and the following disclaimer in the |
| 130 | * documentation and/or other materials provided with the distribution. |
Aaron Lehmann | 69d4178 | 2002-06-23 22:25:24 +0000 | [diff] [blame] | 131 | * 3. Neither the name of the University nor the names of its contributors |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 132 | * may be used to endorse or promote products derived from this software |
| 133 | * without specific prior written permission. |
| 134 | * |
| 135 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 136 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 137 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 138 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 139 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 140 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 141 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 142 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 143 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 144 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 145 | * SUCH DAMAGE. |
| 146 | */ |