Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 1 | /* |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 2 | * od implementation for busybox |
| 3 | * Based on code from util-linux v 2.11l |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 4 | * |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 5 | * Copyright (c) 1990 |
| 6 | * The Regents of the University of California. All rights reserved. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 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 | * |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 22 | * Original copyright notice is retained at the end of this file. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 23 | */ |
| 24 | |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 25 | #include <ctype.h> |
| 26 | #include <getopt.h> |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 28 | #include "dump.h" |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 29 | #include "busybox.h" |
| 30 | |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 31 | extern FS *fshead; /* head of format strings */ |
| 32 | extern int blocksize; /* data block size */ |
| 33 | extern int length; /* max bytes to read */ |
| 34 | |
| 35 | #define ishexdigit(c) \ |
| 36 | ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) |
| 37 | |
| 38 | static void |
| 39 | odoffset(int argc, char ***argvp) |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 40 | { |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 41 | extern off_t skip; |
| 42 | register char *num, *p; |
| 43 | int base; |
| 44 | char *end; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 45 | |
| 46 | /* |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 47 | * The offset syntax of od(1) was genuinely bizarre. First, if |
| 48 | * it started with a plus it had to be an offset. Otherwise, if |
| 49 | * there were at least two arguments, a number or lower-case 'x' |
| 50 | * followed by a number makes it an offset. By default it was |
| 51 | * octal; if it started with 'x' or '0x' it was hex. If it ended |
| 52 | * in a '.', it was decimal. If a 'b' or 'B' was appended, it |
| 53 | * multiplied the number by 512 or 1024 byte units. There was |
| 54 | * no way to assign a block count to a hex offset. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 55 | * |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 56 | * We assumes it's a file if the offset is bad. |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 57 | */ |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 58 | p = **argvp; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 59 | |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 60 | if (!p) { |
| 61 | /* hey someone is probably piping to us ... */ |
| 62 | return; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 63 | } |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 64 | |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 65 | if (*p != '+' && (argc < 2 || |
| 66 | (!isdigit(p[0]) && (p[0] != 'x' || !ishexdigit(p[1]))))) |
| 67 | return; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 68 | |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 69 | base = 0; |
| 70 | /* |
| 71 | * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and |
| 72 | * set base. |
| 73 | */ |
| 74 | if (p[0] == '+') |
| 75 | ++p; |
| 76 | if (p[0] == 'x' && ishexdigit(p[1])) { |
| 77 | ++p; |
| 78 | base = 16; |
| 79 | } else if (p[0] == '0' && p[1] == 'x') { |
| 80 | p += 2; |
| 81 | base = 16; |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 84 | /* skip over the number */ |
| 85 | if (base == 16) |
| 86 | for (num = p; ishexdigit(*p); ++p); |
| 87 | else |
| 88 | for (num = p; isdigit(*p); ++p); |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 89 | |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 90 | /* check for no number */ |
| 91 | if (num == p) |
| 92 | return; |
| 93 | |
| 94 | /* if terminates with a '.', base is decimal */ |
| 95 | if (*p == '.') { |
| 96 | if (base) |
| 97 | return; |
| 98 | base = 10; |
| 99 | } |
| 100 | |
| 101 | skip = strtol(num, &end, base ? base : 8); |
| 102 | |
| 103 | /* if end isn't the same as p, we got a non-octal digit */ |
| 104 | if (end != p) |
| 105 | skip = 0; |
| 106 | else { |
| 107 | if (*p) { |
| 108 | if (*p == 'b') |
| 109 | skip *= 512; |
| 110 | else if (*p == 'B') |
| 111 | skip *= 1024; |
| 112 | ++p; |
| 113 | } |
| 114 | if (*p) |
| 115 | skip = 0; |
| 116 | else { |
| 117 | ++*argvp; |
| 118 | /* |
| 119 | * If the offset uses a non-octal base, the base of |
| 120 | * the offset is changed as well. This isn't pretty, |
| 121 | * but it's easy. |
| 122 | */ |
| 123 | #define TYPE_OFFSET 7 |
| 124 | if (base == 16) { |
| 125 | fshead->nextfu->fmt[TYPE_OFFSET] = 'x'; |
| 126 | fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x'; |
| 127 | } else if (base == 10) { |
| 128 | fshead->nextfu->fmt[TYPE_OFFSET] = 'd'; |
| 129 | fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd'; |
| 130 | } |
| 131 | } |
| 132 | } |
Glenn L McGrath | 6028111 | 2001-11-02 11:39:46 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Glenn L McGrath | e16860d | 2002-11-10 22:16:09 +0000 | [diff] [blame] | 135 | static void odprecede(void) |
| 136 | { |
| 137 | static int first = 1; |
| 138 | |
| 139 | if (first) { |
| 140 | first = 0; |
| 141 | add("\"%07.7_Ao\n\""); |
| 142 | add("\"%07.7_ao \""); |
| 143 | } else |
| 144 | add("\" \""); |
| 145 | } |
| 146 | |
| 147 | int od_main(int argc, char **argv) |
| 148 | { |
| 149 | int ch; |
| 150 | extern enum _vflag vflag; |
| 151 | vflag = FIRST; |
| 152 | length = -1; |
| 153 | |
| 154 | while ((ch = getopt(argc, argv, "aBbcDdeFfHhIiLlOoPpswvXx")) != EOF) |
| 155 | switch (ch) { |
| 156 | case 'a': |
| 157 | odprecede(); |
| 158 | add("16/1 \"%3_u \" \"\\n\""); |
| 159 | break; |
| 160 | case 'B': |
| 161 | case 'o': |
| 162 | odprecede(); |
| 163 | add("8/2 \" %06o \" \"\\n\""); |
| 164 | break; |
| 165 | case 'b': |
| 166 | odprecede(); |
| 167 | add("16/1 \"%03o \" \"\\n\""); |
| 168 | break; |
| 169 | case 'c': |
| 170 | odprecede(); |
| 171 | add("16/1 \"%3_c \" \"\\n\""); |
| 172 | break; |
| 173 | case 'd': |
| 174 | odprecede(); |
| 175 | add("8/2 \" %05u \" \"\\n\""); |
| 176 | break; |
| 177 | case 'D': |
| 178 | odprecede(); |
| 179 | add("4/4 \" %010u \" \"\\n\""); |
| 180 | break; |
| 181 | case 'e': /* undocumented in od */ |
| 182 | case 'F': |
| 183 | odprecede(); |
| 184 | add("2/8 \" %21.14e \" \"\\n\""); |
| 185 | break; |
| 186 | |
| 187 | case 'f': |
| 188 | odprecede(); |
| 189 | add("4/4 \" %14.7e \" \"\\n\""); |
| 190 | break; |
| 191 | case 'H': |
| 192 | case 'X': |
| 193 | odprecede(); |
| 194 | add("4/4 \" %08x \" \"\\n\""); |
| 195 | break; |
| 196 | case 'h': |
| 197 | case 'x': |
| 198 | odprecede(); |
| 199 | add("8/2 \" %04x \" \"\\n\""); |
| 200 | break; |
| 201 | case 'I': |
| 202 | case 'L': |
| 203 | case 'l': |
| 204 | odprecede(); |
| 205 | add("4/4 \" %11d \" \"\\n\""); |
| 206 | break; |
| 207 | case 'i': |
| 208 | odprecede(); |
| 209 | add("8/2 \" %6d \" \"\\n\""); |
| 210 | break; |
| 211 | case 'O': |
| 212 | odprecede(); |
| 213 | add("4/4 \" %011o \" \"\\n\""); |
| 214 | break; |
| 215 | case 'v': |
| 216 | vflag = ALL; |
| 217 | break; |
| 218 | case 'P': |
| 219 | case 'p': |
| 220 | case 's': |
| 221 | case 'w': |
| 222 | case '?': |
| 223 | default: |
| 224 | error_msg("od: od(1) has been deprecated for hexdump(1).\n"); |
| 225 | if (ch != '?') { |
| 226 | error_msg("od: hexdump(1) compatibility doesn't support the -%c option%s\n", |
| 227 | ch, ch == 's' ? "; see strings(1)." : "."); |
| 228 | } |
| 229 | show_usage(); |
| 230 | } |
| 231 | |
| 232 | if (!fshead) { |
| 233 | add("\"%07.7_Ao\n\""); |
| 234 | add("\"%07.7_ao \" 8/2 \"%06o \" \"\\n\""); |
| 235 | } |
| 236 | |
| 237 | argc -= optind; |
| 238 | argv += optind; |
| 239 | |
| 240 | odoffset(argc, &argv); |
| 241 | |
| 242 | return(dump(argv)); |
| 243 | } |
| 244 | |
| 245 | /*- |
| 246 | * Copyright (c) 1990 The Regents of the University of California. |
| 247 | * All rights reserved. |
| 248 | * |
| 249 | * Redistribution and use in source and binary forms, with or without |
| 250 | * modification, are permitted provided that the following conditions |
| 251 | * are met: |
| 252 | * 1. Redistributions of source code must retain the above copyright |
| 253 | * notice, this list of conditions and the following disclaimer. |
| 254 | * 2. Redistributions in binary form must reproduce the above copyright |
| 255 | * notice, this list of conditions and the following disclaimer in the |
| 256 | * documentation and/or other materials provided with the distribution. |
| 257 | * 3. Neither the name of the University nor the names of its contributors |
| 258 | * may be used to endorse or promote products derived from this software |
| 259 | * without specific prior written permission. |
| 260 | * |
| 261 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 262 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 263 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 264 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 265 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 266 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 267 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 268 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 269 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 270 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 271 | * SUCH DAMAGE. |
| 272 | */ |