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> |
| 28 | #include "dump.h" |
| 29 | #include "busybox.h" |
| 30 | |
| 31 | extern off_t skip; /* bytes to skip */ |
| 32 | |
| 33 | extern FS *fshead; /* head of format strings */ |
| 34 | extern int blocksize; /* data block size */ |
| 35 | extern int length; /* max bytes to read */ |
| 36 | |
| 37 | void addfile(char *name) |
| 38 | { |
| 39 | register char *p; |
| 40 | FILE *fp; |
| 41 | int ch; |
| 42 | char buf[2048 + 1]; |
| 43 | |
| 44 | if (!(fp = fopen(name, "r"))) { |
| 45 | error_msg_and_die("hexdump: can't read %s.\n", name); |
| 46 | } |
| 47 | while (fgets(buf, sizeof(buf), fp)) { |
| 48 | if (!(p = index(buf, '\n'))) { |
| 49 | error_msg("hexdump: line too long.\n"); |
| 50 | while ((ch = getchar()) != '\n' && ch != EOF); |
| 51 | continue; |
| 52 | } |
| 53 | *p = '\0'; |
| 54 | for (p = buf; *p && isspace(*p); ++p); |
| 55 | if (!*p || *p == '#') { |
| 56 | continue; |
| 57 | } |
| 58 | add(p); |
| 59 | } |
| 60 | (void)fclose(fp); |
| 61 | } |
| 62 | |
| 63 | int hexdump_main(int argc, char **argv) |
| 64 | { |
| 65 | // register FS *tfs; |
| 66 | char *p; |
| 67 | int ch; |
| 68 | extern enum _vflag vflag; |
| 69 | vflag = FIRST; |
| 70 | length = -1; |
| 71 | |
| 72 | while ((ch = getopt(argc, argv, "bcde:f:n:os:vx")) != EOF) { |
| 73 | switch (ch) { |
| 74 | case 'b': |
| 75 | add("\"%07.7_Ax\n\""); |
| 76 | add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\""); |
| 77 | break; |
| 78 | case 'c': |
| 79 | add("\"%07.7_Ax\n\""); |
| 80 | add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\""); |
| 81 | break; |
| 82 | case 'd': |
| 83 | add("\"%07.7_Ax\n\""); |
| 84 | add("\"%07.7_ax \" 8/2 \" %05u \" \"\\n\""); |
| 85 | break; |
| 86 | case 'e': |
| 87 | add(optarg); |
| 88 | break; |
| 89 | case 'f': |
| 90 | addfile(optarg); |
| 91 | break; |
| 92 | case 'n': |
| 93 | if ((length = atoi(optarg)) < 0) { |
| 94 | error_msg_and_die("hexdump: bad length value.\n"); |
| 95 | } |
| 96 | break; |
| 97 | case 'o': |
| 98 | add("\"%07.7_Ax\n\""); |
| 99 | add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\""); |
| 100 | break; |
| 101 | case 's': |
| 102 | if ((skip = strtol(optarg, &p, 0)) < 0) { |
| 103 | error_msg_and_die("hexdump: bad skip value.\n"); |
| 104 | } |
| 105 | switch(*p) { |
| 106 | case 'b': |
| 107 | skip *= 512; |
| 108 | break; |
| 109 | case 'k': |
| 110 | skip *= 1024; |
| 111 | break; |
| 112 | case 'm': |
| 113 | skip *= 1048576; |
| 114 | break; |
| 115 | } |
| 116 | break; |
| 117 | case 'v': |
| 118 | vflag = ALL; |
| 119 | break; |
| 120 | case 'x': |
| 121 | add("\"%07.7_Ax\n\""); |
| 122 | add("\"%07.7_ax \" 8/2 \" %04x \" \"\\n\""); |
| 123 | break; |
| 124 | case '?': |
| 125 | show_usage(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (!fshead) { |
| 130 | add("\"%07.7_Ax\n\""); |
| 131 | add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\""); |
| 132 | } |
| 133 | |
| 134 | argv += optind; |
| 135 | |
| 136 | return(dump(argv)); |
| 137 | } |
| 138 | /* |
| 139 | * Copyright (c) 1989 The Regents of the University of California. |
| 140 | * All rights reserved. |
| 141 | * |
| 142 | * Redistribution and use in source and binary forms, with or without |
| 143 | * modification, are permitted provided that the following conditions |
| 144 | * are met: |
| 145 | * 1. Redistributions of source code must retain the above copyright |
| 146 | * notice, this list of conditions and the following disclaimer. |
| 147 | * 2. Redistributions in binary form must reproduce the above copyright |
| 148 | * notice, this list of conditions and the following disclaimer in the |
| 149 | * documentation and/or other materials provided with the distribution. |
Aaron Lehmann | 69d4178 | 2002-06-23 22:25:24 +0000 | [diff] [blame] | 150 | * 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] | 151 | * may be used to endorse or promote products derived from this software |
| 152 | * without specific prior written permission. |
| 153 | * |
| 154 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 155 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 156 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 157 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 158 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 159 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 160 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 161 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 162 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 163 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 164 | * SUCH DAMAGE. |
| 165 | */ |