diff options
| author | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-11-02 11:39:46 +0000 |
|---|---|---|
| committer | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-11-02 11:39:46 +0000 |
| commit | 8340fd72faedc46029b2a74db8d9a1ef13e2ef8d (patch) | |
| tree | c84573dea7b7461c7d2174391911075b1470e86e | |
| parent | 67336acd8c5953785ed07c02cb5276e5b27edddf (diff) | |
| download | busybox-w32-8340fd72faedc46029b2a74db8d9a1ef13e2ef8d.tar.gz busybox-w32-8340fd72faedc46029b2a74db8d9a1ef13e2ef8d.tar.bz2 busybox-w32-8340fd72faedc46029b2a74db8d9a1ef13e2ef8d.zip | |
Introduce od and hexdump applets
git-svn-id: svn://busybox.net/trunk/busybox@3625 69ca8d6d-28ef-0310-b511-8ec308f3f277
| -rw-r--r-- | coreutils/od.c | 270 | ||||
| -rw-r--r-- | include/applets.h | 6 | ||||
| -rw-r--r-- | include/usage.h | 23 | ||||
| -rw-r--r-- | libbb/Makefile | 2 | ||||
| -rw-r--r-- | libbb/dump.c | 863 | ||||
| -rw-r--r-- | util-linux/Makefile | 1 | ||||
| -rw-r--r-- | util-linux/config.in | 1 | ||||
| -rw-r--r-- | util-linux/hexdump.c | 169 |
8 files changed, 1334 insertions, 1 deletions
diff --git a/coreutils/od.c b/coreutils/od.c new file mode 100644 index 000000000..681af5c45 --- /dev/null +++ b/coreutils/od.c | |||
| @@ -0,0 +1,270 @@ | |||
| 1 | /* | ||
| 2 | * od implementation for busybox | ||
| 3 | * Based on code from util-linux v 2.11l | ||
| 4 | * | ||
| 5 | * Copyright (c) 1990 | ||
| 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 <ctype.h> | ||
| 26 | #include <getopt.h> | ||
| 27 | #include <stdlib.h> | ||
| 28 | #include "dump.h" | ||
| 29 | #include "busybox.h" | ||
| 30 | |||
| 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) | ||
| 40 | { | ||
| 41 | extern off_t skip; | ||
| 42 | register char *num, *p; | ||
| 43 | int base; | ||
| 44 | char *end; | ||
| 45 | |||
| 46 | /* | ||
| 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. | ||
| 55 | * | ||
| 56 | * We assumes it's a file if the offset is bad. | ||
| 57 | */ | ||
| 58 | p = **argvp; | ||
| 59 | if (*p != '+' && (argc < 2 || | ||
| 60 | (!isdigit(p[0]) && (p[0] != 'x' || !ishexdigit(p[1]))))) | ||
| 61 | return; | ||
| 62 | |||
| 63 | base = 0; | ||
| 64 | /* | ||
| 65 | * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and | ||
| 66 | * set base. | ||
| 67 | */ | ||
| 68 | if (p[0] == '+') | ||
| 69 | ++p; | ||
| 70 | if (p[0] == 'x' && ishexdigit(p[1])) { | ||
| 71 | ++p; | ||
| 72 | base = 16; | ||
| 73 | } else if (p[0] == '0' && p[1] == 'x') { | ||
| 74 | p += 2; | ||
| 75 | base = 16; | ||
| 76 | } | ||
| 77 | |||
| 78 | /* skip over the number */ | ||
| 79 | if (base == 16) | ||
| 80 | for (num = p; ishexdigit(*p); ++p); | ||
| 81 | else | ||
| 82 | for (num = p; isdigit(*p); ++p); | ||
| 83 | |||
| 84 | /* check for no number */ | ||
| 85 | if (num == p) | ||
| 86 | return; | ||
| 87 | |||
| 88 | /* if terminates with a '.', base is decimal */ | ||
| 89 | if (*p == '.') { | ||
| 90 | if (base) | ||
| 91 | return; | ||
| 92 | base = 10; | ||
| 93 | } | ||
| 94 | |||
| 95 | skip = strtol(num, &end, base ? base : 8); | ||
| 96 | |||
| 97 | /* if end isn't the same as p, we got a non-octal digit */ | ||
| 98 | if (end != p) | ||
| 99 | skip = 0; | ||
| 100 | else { | ||
| 101 | if (*p) { | ||
| 102 | if (*p == 'b') | ||
| 103 | skip *= 512; | ||
| 104 | else if (*p == 'B') | ||
| 105 | skip *= 1024; | ||
| 106 | ++p; | ||
| 107 | } | ||
| 108 | if (*p) | ||
| 109 | skip = 0; | ||
| 110 | else { | ||
| 111 | ++*argvp; | ||
| 112 | /* | ||
| 113 | * If the offset uses a non-octal base, the base of | ||
| 114 | * the offset is changed as well. This isn't pretty, | ||
| 115 | * but it's easy. | ||
| 116 | */ | ||
| 117 | #define TYPE_OFFSET 7 | ||
| 118 | if (base == 16) { | ||
| 119 | fshead->nextfu->fmt[TYPE_OFFSET] = 'x'; | ||
| 120 | fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x'; | ||
| 121 | } else if (base == 10) { | ||
| 122 | fshead->nextfu->fmt[TYPE_OFFSET] = 'd'; | ||
| 123 | fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd'; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | static void odprecede(void) | ||
| 130 | { | ||
| 131 | static int first = 1; | ||
| 132 | |||
| 133 | if (first) { | ||
| 134 | first = 0; | ||
| 135 | add("\"%07.7_Ao\n\""); | ||
| 136 | add("\"%07.7_ao \""); | ||
| 137 | } else | ||
| 138 | add("\" \""); | ||
| 139 | } | ||
| 140 | |||
| 141 | int od_main(int argc, char **argv) | ||
| 142 | { | ||
| 143 | int ch; | ||
| 144 | extern enum _vflag vflag; | ||
| 145 | vflag = FIRST; | ||
| 146 | length = -1; | ||
| 147 | |||
| 148 | while ((ch = getopt(argc, argv, "aBbcDdeFfHhIiLlOoPpswvXx")) != EOF) | ||
| 149 | switch (ch) { | ||
| 150 | case 'a': | ||
| 151 | odprecede(); | ||
| 152 | add("16/1 \"%3_u \" \"\\n\""); | ||
| 153 | break; | ||
| 154 | case 'B': | ||
| 155 | case 'o': | ||
| 156 | odprecede(); | ||
| 157 | add("8/2 \" %06o \" \"\\n\""); | ||
| 158 | break; | ||
| 159 | case 'b': | ||
| 160 | odprecede(); | ||
| 161 | add("16/1 \"%03o \" \"\\n\""); | ||
| 162 | break; | ||
| 163 | case 'c': | ||
| 164 | odprecede(); | ||
| 165 | add("16/1 \"%3_c \" \"\\n\""); | ||
| 166 | break; | ||
| 167 | case 'd': | ||
| 168 | odprecede(); | ||
| 169 | add("8/2 \" %05u \" \"\\n\""); | ||
| 170 | break; | ||
| 171 | case 'D': | ||
| 172 | odprecede(); | ||
| 173 | add("4/4 \" %010u \" \"\\n\""); | ||
| 174 | break; | ||
| 175 | case 'e': /* undocumented in od */ | ||
| 176 | case 'F': | ||
| 177 | odprecede(); | ||
| 178 | add("2/8 \" %21.14e \" \"\\n\""); | ||
| 179 | break; | ||
| 180 | |||
| 181 | case 'f': | ||
| 182 | odprecede(); | ||
| 183 | add("4/4 \" %14.7e \" \"\\n\""); | ||
| 184 | break; | ||
| 185 | case 'H': | ||
| 186 | case 'X': | ||
| 187 | odprecede(); | ||
| 188 | add("4/4 \" %08x \" \"\\n\""); | ||
| 189 | break; | ||
| 190 | case 'h': | ||
| 191 | case 'x': | ||
| 192 | odprecede(); | ||
| 193 | add("8/2 \" %04x \" \"\\n\""); | ||
| 194 | break; | ||
| 195 | case 'I': | ||
| 196 | case 'L': | ||
| 197 | case 'l': | ||
| 198 | odprecede(); | ||
| 199 | add("4/4 \" %11d \" \"\\n\""); | ||
| 200 | break; | ||
| 201 | case 'i': | ||
| 202 | odprecede(); | ||
| 203 | add("8/2 \" %6d \" \"\\n\""); | ||
| 204 | break; | ||
| 205 | case 'O': | ||
| 206 | odprecede(); | ||
| 207 | add("4/4 \" %011o \" \"\\n\""); | ||
| 208 | break; | ||
| 209 | case 'v': | ||
| 210 | vflag = ALL; | ||
| 211 | break; | ||
| 212 | case 'P': | ||
| 213 | case 'p': | ||
| 214 | case 's': | ||
| 215 | case 'w': | ||
| 216 | case '?': | ||
| 217 | default: | ||
| 218 | error_msg("od: od(1) has been deprecated for hexdump(1).\n"); | ||
| 219 | if (ch != '?') { | ||
| 220 | error_msg("od: hexdump(1) compatibility doesn't support the -%c option%s\n", | ||
| 221 | ch, ch == 's' ? "; see strings(1)." : "."); | ||
| 222 | } | ||
| 223 | show_usage(); | ||
| 224 | } | ||
| 225 | |||
| 226 | if (!fshead) { | ||
| 227 | add("\"%07.7_Ao\n\""); | ||
| 228 | add("\"%07.7_ao \" 8/2 \"%06o \" \"\\n\""); | ||
| 229 | } | ||
| 230 | |||
| 231 | argc -= optind; | ||
| 232 | argv += optind; | ||
| 233 | |||
| 234 | odoffset(argc, &argv); | ||
| 235 | |||
| 236 | return(dump(argv)); | ||
| 237 | } | ||
| 238 | |||
| 239 | /*- | ||
| 240 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 241 | * All rights reserved. | ||
| 242 | * | ||
| 243 | * Redistribution and use in source and binary forms, with or without | ||
| 244 | * modification, are permitted provided that the following conditions | ||
| 245 | * are met: | ||
| 246 | * 1. Redistributions of source code must retain the above copyright | ||
| 247 | * notice, this list of conditions and the following disclaimer. | ||
| 248 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 249 | * notice, this list of conditions and the following disclaimer in the | ||
| 250 | * documentation and/or other materials provided with the distribution. | ||
| 251 | * 3. All advertising materials mentioning features or use of this software | ||
| 252 | * must display the following acknowledgement: | ||
| 253 | * This product includes software developed by the University of | ||
| 254 | * California, Berkeley and its contributors. | ||
| 255 | * 4. Neither the name of the University nor the names of its contributors | ||
| 256 | * may be used to endorse or promote products derived from this software | ||
| 257 | * without specific prior written permission. | ||
| 258 | * | ||
| 259 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 260 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 261 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 262 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 263 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 264 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 265 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 266 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 267 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 268 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 269 | * SUCH DAMAGE. | ||
| 270 | */ | ||
diff --git a/include/applets.h b/include/applets.h index 44d274b85..ea196cb66 100644 --- a/include/applets.h +++ b/include/applets.h | |||
| @@ -200,6 +200,9 @@ | |||
| 200 | #ifdef CONFIG_HEAD | 200 | #ifdef CONFIG_HEAD |
| 201 | APPLET(head, head_main, _BB_DIR_USR_BIN) | 201 | APPLET(head, head_main, _BB_DIR_USR_BIN) |
| 202 | #endif | 202 | #endif |
| 203 | #ifdef CONFIG_HEXDUMP | ||
| 204 | APPLET(hexdump, hexdump_main, _BB_DIR_USR_BIN) | ||
| 205 | #endif | ||
| 203 | #ifdef CONFIG_HOSTID | 206 | #ifdef CONFIG_HOSTID |
| 204 | APPLET(hostid, hostid_main, _BB_DIR_USR_BIN) | 207 | APPLET(hostid, hostid_main, _BB_DIR_USR_BIN) |
| 205 | #endif | 208 | #endif |
| @@ -317,6 +320,9 @@ | |||
| 317 | #ifdef CONFIG_NSLOOKUP | 320 | #ifdef CONFIG_NSLOOKUP |
| 318 | APPLET(nslookup, nslookup_main, _BB_DIR_USR_BIN) | 321 | APPLET(nslookup, nslookup_main, _BB_DIR_USR_BIN) |
| 319 | #endif | 322 | #endif |
| 323 | #ifdef CONFIG_OD | ||
| 324 | APPLET(od, od_main, _BB_DIR_USR_BIN) | ||
| 325 | #endif | ||
| 320 | #ifdef CONFIG_PIDOF | 326 | #ifdef CONFIG_PIDOF |
| 321 | APPLET(pidof, pidof_main, _BB_DIR_BIN) | 327 | APPLET(pidof, pidof_main, _BB_DIR_BIN) |
| 322 | #endif | 328 | #endif |
diff --git a/include/usage.h b/include/usage.h index 46c11c2a3..12d5e1ed5 100644 --- a/include/usage.h +++ b/include/usage.h | |||
| @@ -646,6 +646,23 @@ | |||
| 646 | "root:x:0:0:root:/root:/bin/bash\n" \ | 646 | "root:x:0:0:root:/root:/bin/bash\n" \ |
| 647 | "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n" | 647 | "daemon:x:1:1:daemon:/usr/sbin:/bin/sh\n" |
| 648 | 648 | ||
| 649 | #define hexdump_trivial_usage \ | ||
| 650 | "[-[bcdefnosvx]] [OPTION] FILE" | ||
| 651 | #define hexdump_full_usage \ | ||
| 652 | "The hexdump utility is a filter which displays the specified files,\n" \ | ||
| 653 | "or the standard input, if no files are specified, in a user specified\n"\ | ||
| 654 | "format\n" \ | ||
| 655 | "\t-b\t\tOne-byte octal display\n" \ | ||
| 656 | "\t-c\t\tOne-byte character display\n" \ | ||
| 657 | "\t-d\t\tTwo-byte decimal display\n" \ | ||
| 658 | "\t-e FORMAT STRING\n" \ | ||
| 659 | "\t-f FORMAT FILE\n" \ | ||
| 660 | "\t-n LENGTH\tInterpret only length bytes of input\n" \ | ||
| 661 | "\t-o\t\tTwo-byte octal display\n" \ | ||
| 662 | "\t-s OFFSET\tSkip offset byte\n" \ | ||
| 663 | "\t-v\t\tdisplay all input data\n" \ | ||
| 664 | "\t-x\t\tTwo-byte hexadecimal display\n" | ||
| 665 | |||
| 649 | #define hostid_trivial_usage \ | 666 | #define hostid_trivial_usage \ |
| 650 | "" | 667 | "" |
| 651 | #define hostid_full_usage \ | 668 | #define hostid_full_usage \ |
| @@ -1249,6 +1266,12 @@ | |||
| 1249 | "Name: debian\n" \ | 1266 | "Name: debian\n" \ |
| 1250 | "Address: 127.0.0.1\n" | 1267 | "Address: 127.0.0.1\n" |
| 1251 | 1268 | ||
| 1269 | #define od_trivial_usage \ | ||
| 1270 | "[-aBbcDdeFfHhIiLlOovXx] [FILE]" | ||
| 1271 | #define od_full_usage \ | ||
| 1272 | "Write an unambiguous representation, octal bytes by default, of FILE\n"\ | ||
| 1273 | "to standard output. With no FILE, or when FILE is -, read standard input." | ||
| 1274 | |||
| 1252 | #define pidof_trivial_usage \ | 1275 | #define pidof_trivial_usage \ |
| 1253 | "process-name [process-name ...]" | 1276 | "process-name [process-name ...]" |
| 1254 | #define pidof_full_usage \ | 1277 | #define pidof_full_usage \ |
diff --git a/libbb/Makefile b/libbb/Makefile index 85ce95c90..f4f2d857f 100644 --- a/libbb/Makefile +++ b/libbb/Makefile | |||
| @@ -32,7 +32,7 @@ obj-n := | |||
| 32 | obj- := | 32 | obj- := |
| 33 | 33 | ||
| 34 | obj-y += ask_confirmation.o chomp.o concat_path_file.o copy_file.o \ | 34 | obj-y += ask_confirmation.o chomp.o concat_path_file.o copy_file.o \ |
| 35 | copy_file_chunk.o libc5.o device_open.o error_msg.o \ | 35 | copy_file_chunk.o dump.o libc5.o device_open.o error_msg.o \ |
| 36 | error_msg_and_die.o fgets_str.o find_mount_point.o find_pid_by_name.o \ | 36 | error_msg_and_die.o fgets_str.o find_mount_point.o find_pid_by_name.o \ |
| 37 | find_root_device.o full_read.o full_write.o get_console.o \ | 37 | find_root_device.o full_read.o full_write.o get_console.o \ |
| 38 | get_last_path_component.o get_line_from_file.o gz_open.o human_readable.o \ | 38 | get_last_path_component.o get_line_from_file.o gz_open.o human_readable.o \ |
diff --git a/libbb/dump.c b/libbb/dump.c new file mode 100644 index 000000000..10d5a8d63 --- /dev/null +++ b/libbb/dump.c | |||
| @@ -0,0 +1,863 @@ | |||
| 1 | /* | ||
| 2 | * Support code for the hexdump and od applets, | ||
| 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 <stdlib.h> | ||
| 26 | #include <string.h> | ||
| 27 | #include <ctype.h> /* for isdigit() */ | ||
| 28 | #include "dump.h" | ||
| 29 | #include "libbb.h" | ||
| 30 | |||
| 31 | enum _vflag vflag = FIRST; | ||
| 32 | FS *fshead; /* head of format strings */ | ||
| 33 | extern FS *fshead; /* head of format strings */ | ||
| 34 | extern int blocksize; | ||
| 35 | static FU *endfu; | ||
| 36 | static char **_argv; | ||
| 37 | static off_t savaddress; /* saved address/offset in stream */ | ||
| 38 | static off_t eaddress; /* end address */ | ||
| 39 | static off_t address; /* address/offset in stream */ | ||
| 40 | off_t skip; /* bytes to skip */ | ||
| 41 | off_t saveaddress; | ||
| 42 | int exitval; /* final exit value */ | ||
| 43 | int blocksize; /* data block size */ | ||
| 44 | int length = -1; /* max bytes to read */ | ||
| 45 | |||
| 46 | |||
| 47 | int size(FS *fs) | ||
| 48 | { | ||
| 49 | register FU *fu; | ||
| 50 | register int bcnt, cursize; | ||
| 51 | register char *fmt; | ||
| 52 | int prec; | ||
| 53 | |||
| 54 | /* figure out the data block size needed for each format unit */ | ||
| 55 | for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) { | ||
| 56 | if (fu->bcnt) { | ||
| 57 | cursize += fu->bcnt * fu->reps; | ||
| 58 | continue; | ||
| 59 | } | ||
| 60 | for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) { | ||
| 61 | if (*fmt != '%') | ||
| 62 | continue; | ||
| 63 | /* | ||
| 64 | * skip any special chars -- save precision in | ||
| 65 | * case it's a %s format. | ||
| 66 | */ | ||
| 67 | while (index(".#-+ 0123456789" + 1, *++fmt)); | ||
| 68 | if (*fmt == '.' && isdigit(*++fmt)) { | ||
| 69 | prec = atoi(fmt); | ||
| 70 | while (isdigit(*++fmt)); | ||
| 71 | } | ||
| 72 | switch(*fmt) { | ||
| 73 | case 'c': | ||
| 74 | bcnt += 1; | ||
| 75 | break; | ||
| 76 | case 'd': case 'i': case 'o': case 'u': | ||
| 77 | case 'x': case 'X': | ||
| 78 | bcnt += 4; | ||
| 79 | break; | ||
| 80 | case 'e': case 'E': case 'f': case 'g': case 'G': | ||
| 81 | bcnt += 8; | ||
| 82 | break; | ||
| 83 | case 's': | ||
| 84 | bcnt += prec; | ||
| 85 | break; | ||
| 86 | case '_': | ||
| 87 | switch(*++fmt) { | ||
| 88 | case 'c': case 'p': case 'u': | ||
| 89 | bcnt += 1; | ||
| 90 | break; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | cursize += bcnt * fu->reps; | ||
| 95 | } | ||
| 96 | return(cursize); | ||
| 97 | } | ||
| 98 | |||
| 99 | void rewrite(FS *fs) | ||
| 100 | { | ||
| 101 | enum { NOTOKAY, USEBCNT, USEPREC } sokay; | ||
| 102 | register PR *pr, **nextpr = NULL; | ||
| 103 | register FU *fu; | ||
| 104 | register char *p1, *p2; | ||
| 105 | char savech, *fmtp; | ||
| 106 | int nconv, prec = 0; | ||
| 107 | |||
| 108 | for (fu = fs->nextfu; fu; fu = fu->nextfu) { | ||
| 109 | /* | ||
| 110 | * break each format unit into print units; each | ||
| 111 | * conversion character gets its own. | ||
| 112 | */ | ||
| 113 | for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) { | ||
| 114 | /* NOSTRICT */ | ||
| 115 | pr = (PR *)xmalloc(sizeof(PR)); | ||
| 116 | if (!fu->nextpr) | ||
| 117 | fu->nextpr = pr; | ||
| 118 | else | ||
| 119 | *nextpr = pr; | ||
| 120 | |||
| 121 | /* skip preceding text and up to the next % sign */ | ||
| 122 | for (p1 = fmtp; *p1 && *p1 != '%'; ++p1); | ||
| 123 | |||
| 124 | /* only text in the string */ | ||
| 125 | if (!*p1) { | ||
| 126 | pr->fmt = fmtp; | ||
| 127 | pr->flags = F_TEXT; | ||
| 128 | break; | ||
| 129 | } | ||
| 130 | |||
| 131 | /* | ||
| 132 | * get precision for %s -- if have a byte count, don't | ||
| 133 | * need it. | ||
| 134 | */ | ||
| 135 | if (fu->bcnt) { | ||
| 136 | sokay = USEBCNT; | ||
| 137 | /* skip to conversion character */ | ||
| 138 | for (++p1; index(".#-+ 0123456789", *p1); ++p1); | ||
| 139 | } else { | ||
| 140 | /* skip any special chars, field width */ | ||
| 141 | while (index(".#-+ 0123456789" + 1, *++p1)); | ||
| 142 | if (*p1 == '.' && isdigit(*++p1)) { | ||
| 143 | sokay = USEPREC; | ||
| 144 | prec = atoi(p1); | ||
| 145 | while (isdigit(*++p1)); | ||
| 146 | } | ||
| 147 | else | ||
| 148 | sokay = NOTOKAY; | ||
| 149 | } | ||
| 150 | |||
| 151 | p2 = p1 + 1; /* set end pointer */ | ||
| 152 | |||
| 153 | /* | ||
| 154 | * figure out the byte count for each conversion; | ||
| 155 | * rewrite the format as necessary, set up blank- | ||
| 156 | * padding for end of data. | ||
| 157 | */ | ||
| 158 | switch(*p1) { | ||
| 159 | case 'c': | ||
| 160 | pr->flags = F_CHAR; | ||
| 161 | switch(fu->bcnt) { | ||
| 162 | case 0: case 1: | ||
| 163 | pr->bcnt = 1; | ||
| 164 | break; | ||
| 165 | default: | ||
| 166 | p1[1] = '\0'; | ||
| 167 | error_msg_and_die("bad byte count for conversion character %s.", p1); | ||
| 168 | } | ||
| 169 | break; | ||
| 170 | case 'd': case 'i': | ||
| 171 | pr->flags = F_INT; | ||
| 172 | goto sw1; | ||
| 173 | case 'l': | ||
| 174 | ++p2; | ||
| 175 | switch(p1[1]) { | ||
| 176 | case 'd': case 'i': | ||
| 177 | ++p1; | ||
| 178 | pr->flags = F_INT; | ||
| 179 | goto sw1; | ||
| 180 | case 'o': case 'u': case 'x': case 'X': | ||
| 181 | ++p1; | ||
| 182 | pr->flags = F_UINT; | ||
| 183 | goto sw1; | ||
| 184 | default: | ||
| 185 | p1[2] = '\0'; | ||
| 186 | error_msg_and_die("hexdump: bad conversion character %%%s.\n", p1); | ||
| 187 | } | ||
| 188 | /* NOTREACHED */ | ||
| 189 | case 'o': case 'u': case 'x': case 'X': | ||
| 190 | pr->flags = F_UINT; | ||
| 191 | sw1: switch(fu->bcnt) { | ||
| 192 | case 0: case 4: | ||
| 193 | pr->bcnt = 4; | ||
| 194 | break; | ||
| 195 | case 1: | ||
| 196 | pr->bcnt = 1; | ||
| 197 | break; | ||
| 198 | case 2: | ||
| 199 | pr->bcnt = 2; | ||
| 200 | break; | ||
| 201 | default: | ||
| 202 | p1[1] = '\0'; | ||
| 203 | error_msg_and_die("bad byte count for conversion character %s.", p1); | ||
| 204 | } | ||
| 205 | break; | ||
| 206 | case 'e': case 'E': case 'f': case 'g': case 'G': | ||
| 207 | pr->flags = F_DBL; | ||
| 208 | switch(fu->bcnt) { | ||
| 209 | case 0: case 8: | ||
| 210 | pr->bcnt = 8; | ||
| 211 | break; | ||
| 212 | case 4: | ||
| 213 | pr->bcnt = 4; | ||
| 214 | break; | ||
| 215 | default: | ||
| 216 | p1[1] = '\0'; | ||
| 217 | error_msg_and_die("bad byte count for conversion character %s.", p1); | ||
| 218 | } | ||
| 219 | break; | ||
| 220 | case 's': | ||
| 221 | pr->flags = F_STR; | ||
| 222 | switch(sokay) { | ||
| 223 | case NOTOKAY: | ||
| 224 | error_msg_and_die("%%s requires a precision or a byte count."); | ||
| 225 | case USEBCNT: | ||
| 226 | pr->bcnt = fu->bcnt; | ||
| 227 | break; | ||
| 228 | case USEPREC: | ||
| 229 | pr->bcnt = prec; | ||
| 230 | break; | ||
| 231 | } | ||
| 232 | break; | ||
| 233 | case '_': | ||
| 234 | ++p2; | ||
| 235 | switch(p1[1]) { | ||
| 236 | case 'A': | ||
| 237 | endfu = fu; | ||
| 238 | fu->flags |= F_IGNORE; | ||
| 239 | /* FALLTHROUGH */ | ||
| 240 | case 'a': | ||
| 241 | pr->flags = F_ADDRESS; | ||
| 242 | ++p2; | ||
| 243 | switch(p1[2]) { | ||
| 244 | case 'd': case 'o': case'x': | ||
| 245 | *p1 = p1[2]; | ||
| 246 | break; | ||
| 247 | default: | ||
| 248 | p1[3] = '\0'; | ||
| 249 | error_msg_and_die("hexdump: bad conversion character %%%s.\n", p1); | ||
| 250 | } | ||
| 251 | break; | ||
| 252 | case 'c': | ||
| 253 | pr->flags = F_C; | ||
| 254 | /* *p1 = 'c'; set in conv_c */ | ||
| 255 | goto sw2; | ||
| 256 | case 'p': | ||
| 257 | pr->flags = F_P; | ||
| 258 | *p1 = 'c'; | ||
| 259 | goto sw2; | ||
| 260 | case 'u': | ||
| 261 | pr->flags = F_U; | ||
| 262 | /* *p1 = 'c'; set in conv_u */ | ||
| 263 | sw2: switch(fu->bcnt) { | ||
| 264 | case 0: case 1: | ||
| 265 | pr->bcnt = 1; | ||
| 266 | break; | ||
| 267 | default: | ||
| 268 | p1[2] = '\0'; | ||
| 269 | error_msg_and_die("bad byte count for conversion character %s.", p1); | ||
| 270 | } | ||
| 271 | break; | ||
| 272 | default: | ||
| 273 | p1[2] = '\0'; | ||
| 274 | error_msg_and_die("hexdump: bad conversion character %%%s.\n", p1); | ||
| 275 | } | ||
| 276 | break; | ||
| 277 | default: | ||
| 278 | p1[1] = '\0'; | ||
| 279 | error_msg_and_die("hexdump: bad conversion character %%%s.\n", p1); | ||
| 280 | } | ||
| 281 | |||
| 282 | /* | ||
| 283 | * copy to PR format string, set conversion character | ||
| 284 | * pointer, update original. | ||
| 285 | */ | ||
| 286 | savech = *p2; | ||
| 287 | p1[1] = '\0'; | ||
| 288 | if (!(pr->fmt = strdup(fmtp))) | ||
| 289 | perror_msg_and_die("hexdump"); | ||
| 290 | *p2 = savech; | ||
| 291 | pr->cchar = pr->fmt + (p1 - fmtp); | ||
| 292 | fmtp = p2; | ||
| 293 | |||
| 294 | /* only one conversion character if byte count */ | ||
| 295 | if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++) { | ||
| 296 | error_msg_and_die("hexdump: byte count with multiple conversion characters.\n"); | ||
| 297 | } | ||
| 298 | } | ||
| 299 | /* | ||
| 300 | * if format unit byte count not specified, figure it out | ||
| 301 | * so can adjust rep count later. | ||
| 302 | */ | ||
| 303 | if (!fu->bcnt) | ||
| 304 | for (pr = fu->nextpr; pr; pr = pr->nextpr) | ||
| 305 | fu->bcnt += pr->bcnt; | ||
| 306 | } | ||
| 307 | /* | ||
| 308 | * if the format string interprets any data at all, and it's | ||
| 309 | * not the same as the blocksize, and its last format unit | ||
| 310 | * interprets any data at all, and has no iteration count, | ||
| 311 | * repeat it as necessary. | ||
| 312 | * | ||
| 313 | * if, rep count is greater than 1, no trailing whitespace | ||
| 314 | * gets output from the last iteration of the format unit. | ||
| 315 | */ | ||
| 316 | for (fu = fs->nextfu;; fu = fu->nextfu) { | ||
| 317 | if (!fu->nextfu && fs->bcnt < blocksize && | ||
| 318 | !(fu->flags&F_SETREP) && fu->bcnt) | ||
| 319 | fu->reps += (blocksize - fs->bcnt) / fu->bcnt; | ||
| 320 | if (fu->reps > 1) { | ||
| 321 | for (pr = fu->nextpr;; pr = pr->nextpr) | ||
| 322 | if (!pr->nextpr) | ||
| 323 | break; | ||
| 324 | for (p1 = pr->fmt, p2 = NULL; *p1; ++p1) | ||
| 325 | p2 = isspace(*p1) ? p1 : NULL; | ||
| 326 | if (p2) | ||
| 327 | pr->nospace = p2; | ||
| 328 | } | ||
| 329 | if (!fu->nextfu) | ||
| 330 | break; | ||
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 | static void doskip(char *fname, int statok) | ||
| 335 | { | ||
| 336 | struct stat sbuf; | ||
| 337 | |||
| 338 | if (statok) { | ||
| 339 | if (fstat(fileno(stdin), &sbuf)) { | ||
| 340 | perror_msg_and_die("hexdump: %s", fname); | ||
| 341 | } | ||
| 342 | if ( ( ! (S_ISCHR(sbuf.st_mode) || | ||
| 343 | S_ISBLK(sbuf.st_mode) || | ||
| 344 | S_ISFIFO(sbuf.st_mode)) ) && | ||
| 345 | skip >= sbuf.st_size) { | ||
| 346 | /* If size valid and skip >= size */ | ||
| 347 | skip -= sbuf.st_size; | ||
| 348 | address += sbuf.st_size; | ||
| 349 | return; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | if (fseek(stdin, skip, SEEK_SET)) { | ||
| 353 | perror_msg_and_die("hexdump: %s", fname); | ||
| 354 | } | ||
| 355 | savaddress = address += skip; | ||
| 356 | skip = 0; | ||
| 357 | } | ||
| 358 | |||
| 359 | int next(char **argv) | ||
| 360 | { | ||
| 361 | static int done; | ||
| 362 | int statok; | ||
| 363 | |||
| 364 | if (argv) { | ||
| 365 | _argv = argv; | ||
| 366 | return(1); | ||
| 367 | } | ||
| 368 | for (;;) { | ||
| 369 | if (*_argv) { | ||
| 370 | if (!(freopen(*_argv, "r", stdin))) { | ||
| 371 | perror_msg("%s", *_argv); | ||
| 372 | exitval = 1; | ||
| 373 | ++_argv; | ||
| 374 | continue; | ||
| 375 | } | ||
| 376 | statok = done = 1; | ||
| 377 | } else { | ||
| 378 | if (done++) | ||
| 379 | return(0); | ||
| 380 | statok = 0; | ||
| 381 | } | ||
| 382 | if (skip) | ||
| 383 | doskip(statok ? *_argv : "stdin", statok); | ||
| 384 | if (*_argv) | ||
| 385 | ++_argv; | ||
| 386 | if (!skip) | ||
| 387 | return(1); | ||
| 388 | } | ||
| 389 | /* NOTREACHED */ | ||
| 390 | } | ||
| 391 | |||
| 392 | static u_char * | ||
| 393 | get(void) | ||
| 394 | { | ||
| 395 | static int ateof = 1; | ||
| 396 | static u_char *curp, *savp; | ||
| 397 | register int n; | ||
| 398 | int need, nread; | ||
| 399 | u_char *tmpp; | ||
| 400 | |||
| 401 | if (!curp) { | ||
| 402 | curp = (u_char *)xmalloc(blocksize); | ||
| 403 | savp = (u_char *)xmalloc(blocksize); | ||
| 404 | } else { | ||
| 405 | tmpp = curp; | ||
| 406 | curp = savp; | ||
| 407 | savp = tmpp; | ||
| 408 | address = savaddress += blocksize; | ||
| 409 | } | ||
| 410 | for (need = blocksize, nread = 0;;) { | ||
| 411 | /* | ||
| 412 | * if read the right number of bytes, or at EOF for one file, | ||
| 413 | * and no other files are available, zero-pad the rest of the | ||
| 414 | * block and set the end flag. | ||
| 415 | */ | ||
| 416 | if (!length || (ateof && !next((char **)NULL))) { | ||
| 417 | if (need == blocksize) { | ||
| 418 | return((u_char *)NULL); | ||
| 419 | } | ||
| 420 | if (vflag != ALL && !bcmp(curp, savp, nread)) { | ||
| 421 | if (vflag != DUP) { | ||
| 422 | printf("*\n"); | ||
| 423 | } | ||
| 424 | return((u_char *)NULL); | ||
| 425 | } | ||
| 426 | bzero((char *)curp + nread, need); | ||
| 427 | eaddress = address + nread; | ||
| 428 | return(curp); | ||
| 429 | } | ||
| 430 | n = fread((char *)curp + nread, sizeof(u_char), | ||
| 431 | length == -1 ? need : MIN(length, need), stdin); | ||
| 432 | if (!n) { | ||
| 433 | if (ferror(stdin)) { | ||
| 434 | perror_msg("%s", _argv[-1]); | ||
| 435 | } | ||
| 436 | ateof = 1; | ||
| 437 | continue; | ||
| 438 | } | ||
| 439 | ateof = 0; | ||
| 440 | if (length != -1) { | ||
| 441 | length -= n; | ||
| 442 | } | ||
| 443 | if (!(need -= n)) { | ||
| 444 | if (vflag == ALL || vflag == FIRST || | ||
| 445 | bcmp(curp, savp, blocksize)) { | ||
| 446 | if (vflag == DUP || vflag == FIRST) { | ||
| 447 | vflag = WAIT; | ||
| 448 | } | ||
| 449 | return(curp); | ||
| 450 | } | ||
| 451 | if (vflag == WAIT) { | ||
| 452 | printf("*\n"); | ||
| 453 | } | ||
| 454 | vflag = DUP; | ||
| 455 | address = savaddress += blocksize; | ||
| 456 | need = blocksize; | ||
| 457 | nread = 0; | ||
| 458 | } else { | ||
| 459 | nread += n; | ||
| 460 | } | ||
| 461 | } | ||
| 462 | } | ||
| 463 | |||
| 464 | static void bpad(PR *pr) | ||
| 465 | { | ||
| 466 | register char *p1, *p2; | ||
| 467 | |||
| 468 | /* | ||
| 469 | * remove all conversion flags; '-' is the only one valid | ||
| 470 | * with %s, and it's not useful here. | ||
| 471 | */ | ||
| 472 | pr->flags = F_BPAD; | ||
| 473 | *pr->cchar = 's'; | ||
| 474 | for (p1 = pr->fmt; *p1 != '%'; ++p1); | ||
| 475 | for (p2 = ++p1; *p1 && index(" -0+#", *p1); ++p1); | ||
| 476 | while ((*p2++ = *p1++) != 0) ; | ||
| 477 | } | ||
| 478 | |||
| 479 | void conv_c(PR *pr, u_char *p) | ||
| 480 | { | ||
| 481 | char buf[10], *str; | ||
| 482 | |||
| 483 | switch(*p) { | ||
| 484 | case '\0': | ||
| 485 | str = "\\0"; | ||
| 486 | goto strpr; | ||
| 487 | /* case '\a': */ | ||
| 488 | case '\007': | ||
| 489 | str = "\\a"; | ||
| 490 | goto strpr; | ||
| 491 | case '\b': | ||
| 492 | str = "\\b"; | ||
| 493 | goto strpr; | ||
| 494 | case '\f': | ||
| 495 | str = "\\f"; | ||
| 496 | goto strpr; | ||
| 497 | case '\n': | ||
| 498 | str = "\\n"; | ||
| 499 | goto strpr; | ||
| 500 | case '\r': | ||
| 501 | str = "\\r"; | ||
| 502 | goto strpr; | ||
| 503 | case '\t': | ||
| 504 | str = "\\t"; | ||
| 505 | goto strpr; | ||
| 506 | case '\v': | ||
| 507 | str = "\\v"; | ||
| 508 | goto strpr; | ||
| 509 | default: | ||
| 510 | break; | ||
| 511 | } | ||
| 512 | if (isprint(*p)) { | ||
| 513 | *pr->cchar = 'c'; | ||
| 514 | (void)printf(pr->fmt, *p); | ||
| 515 | } else { | ||
| 516 | sprintf(str = buf, "%03o", (int)*p); | ||
| 517 | strpr: | ||
| 518 | *pr->cchar = 's'; | ||
| 519 | printf(pr->fmt, str); | ||
| 520 | } | ||
| 521 | } | ||
| 522 | |||
| 523 | void conv_u(PR *pr, u_char *p) | ||
| 524 | { | ||
| 525 | static char *list[] = { | ||
| 526 | "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", | ||
| 527 | "bs", "ht", "lf", "vt", "ff", "cr", "so", "si", | ||
| 528 | "dle", "dcl", "dc2", "dc3", "dc4", "nak", "syn", "etb", | ||
| 529 | "can", "em", "sub", "esc", "fs", "gs", "rs", "us", | ||
| 530 | }; | ||
| 531 | |||
| 532 | /* od used nl, not lf */ | ||
| 533 | if (*p <= 0x1f) { | ||
| 534 | *pr->cchar = 's'; | ||
| 535 | printf(pr->fmt, list[*p]); | ||
| 536 | } else if (*p == 0x7f) { | ||
| 537 | *pr->cchar = 's'; | ||
| 538 | printf(pr->fmt, "del"); | ||
| 539 | } else if (isprint(*p)) { | ||
| 540 | *pr->cchar = 'c'; | ||
| 541 | printf(pr->fmt, *p); | ||
| 542 | } else { | ||
| 543 | *pr->cchar = 'x'; | ||
| 544 | printf(pr->fmt, (int)*p); | ||
| 545 | } | ||
| 546 | } | ||
| 547 | |||
| 548 | void display(void) | ||
| 549 | { | ||
| 550 | // extern FU *endfu; | ||
| 551 | register FS *fs; | ||
| 552 | register FU *fu; | ||
| 553 | register PR *pr; | ||
| 554 | register int cnt; | ||
| 555 | register u_char *bp; | ||
| 556 | // off_t saveaddress; | ||
| 557 | u_char savech = 0, *savebp; | ||
| 558 | |||
| 559 | while ((bp = get()) != NULL) { | ||
| 560 | for (fs = fshead, savebp = bp, saveaddress = address; fs; | ||
| 561 | fs = fs->nextfs, bp = savebp, address = saveaddress) { | ||
| 562 | for (fu = fs->nextfu; fu; fu = fu->nextfu) { | ||
| 563 | if (fu->flags & F_IGNORE) { | ||
| 564 | break; | ||
| 565 | } | ||
| 566 | for (cnt = fu->reps; cnt; --cnt) { | ||
| 567 | for (pr = fu->nextpr; pr; address += pr->bcnt, | ||
| 568 | bp += pr->bcnt, pr = pr->nextpr) { | ||
| 569 | if (eaddress && address >= eaddress && | ||
| 570 | !(pr->flags&(F_TEXT|F_BPAD))) { | ||
| 571 | bpad(pr); | ||
| 572 | } | ||
| 573 | if (cnt == 1 && pr->nospace) { | ||
| 574 | savech = *pr->nospace; | ||
| 575 | *pr->nospace = '\0'; | ||
| 576 | } | ||
| 577 | // PRINT; | ||
| 578 | switch(pr->flags) { | ||
| 579 | case F_ADDRESS: | ||
| 580 | printf(pr->fmt, address); | ||
| 581 | break; | ||
| 582 | case F_BPAD: | ||
| 583 | printf(pr->fmt, ""); | ||
| 584 | break; | ||
| 585 | case F_C: | ||
| 586 | conv_c(pr, bp); | ||
| 587 | break; | ||
| 588 | case F_CHAR: | ||
| 589 | printf(pr->fmt, *bp); | ||
| 590 | break; | ||
| 591 | case F_DBL: { | ||
| 592 | double dval; | ||
| 593 | float fval; | ||
| 594 | switch(pr->bcnt) { | ||
| 595 | case 4: | ||
| 596 | bcopy((char *)bp, (char *)&fval, sizeof(fval)); | ||
| 597 | printf(pr->fmt, fval); | ||
| 598 | break; | ||
| 599 | case 8: | ||
| 600 | bcopy((char *)bp, (char *)&dval, sizeof(dval)); | ||
| 601 | printf(pr->fmt, dval); | ||
| 602 | break; | ||
| 603 | } | ||
| 604 | break; | ||
| 605 | } | ||
| 606 | case F_INT: { | ||
| 607 | int ival; | ||
| 608 | short sval; | ||
| 609 | switch(pr->bcnt) { | ||
| 610 | case 1: | ||
| 611 | printf(pr->fmt, (int)*bp); | ||
| 612 | break; | ||
| 613 | case 2: | ||
| 614 | bcopy((char *)bp, (char *)&sval, sizeof(sval)); | ||
| 615 | printf(pr->fmt, (int)sval); | ||
| 616 | break; | ||
| 617 | case 4: | ||
| 618 | bcopy((char *)bp, (char *)&ival, sizeof(ival)); | ||
| 619 | printf(pr->fmt, ival); | ||
| 620 | break; | ||
| 621 | } | ||
| 622 | break; | ||
| 623 | } | ||
| 624 | case F_P: | ||
| 625 | printf(pr->fmt, isprint(*bp) ? *bp : '.'); | ||
| 626 | break; | ||
| 627 | case F_STR: | ||
| 628 | printf(pr->fmt, (char *)bp); | ||
| 629 | break; | ||
| 630 | case F_TEXT: | ||
| 631 | printf(pr->fmt); | ||
| 632 | break; | ||
| 633 | case F_U: | ||
| 634 | conv_u(pr, bp); | ||
| 635 | break; | ||
| 636 | case F_UINT: { | ||
| 637 | u_int ival; | ||
| 638 | u_short sval; | ||
| 639 | switch(pr->bcnt) { | ||
| 640 | case 1: | ||
| 641 | printf(pr->fmt, (u_int)*bp); | ||
| 642 | break; | ||
| 643 | case 2: | ||
| 644 | bcopy((char *)bp, (char *)&sval, sizeof(sval)); | ||
| 645 | printf(pr->fmt, (u_int)sval); | ||
| 646 | break; | ||
| 647 | case 4: | ||
| 648 | bcopy((char *)bp, (char *)&ival, sizeof(ival)); | ||
| 649 | printf(pr->fmt, ival); | ||
| 650 | break; | ||
| 651 | } | ||
| 652 | break; | ||
| 653 | } | ||
| 654 | } | ||
| 655 | if (cnt == 1 && pr->nospace) { | ||
| 656 | *pr->nospace = savech; | ||
| 657 | } | ||
| 658 | } | ||
| 659 | } | ||
| 660 | } | ||
| 661 | } | ||
| 662 | } | ||
| 663 | if (endfu) { | ||
| 664 | /* | ||
| 665 | * if eaddress not set, error or file size was multiple of | ||
| 666 | * blocksize, and no partial block ever found. | ||
| 667 | */ | ||
| 668 | if (!eaddress) { | ||
| 669 | if (!address) { | ||
| 670 | return; | ||
| 671 | } | ||
| 672 | eaddress = address; | ||
| 673 | } | ||
| 674 | for (pr = endfu->nextpr; pr; pr = pr->nextpr) { | ||
| 675 | switch(pr->flags) { | ||
| 676 | case F_ADDRESS: | ||
| 677 | (void)printf(pr->fmt, eaddress); | ||
| 678 | break; | ||
| 679 | case F_TEXT: | ||
| 680 | (void)printf(pr->fmt); | ||
| 681 | break; | ||
| 682 | } | ||
| 683 | } | ||
| 684 | } | ||
| 685 | } | ||
| 686 | |||
| 687 | int dump(char **argv) | ||
| 688 | { | ||
| 689 | register FS *tfs; | ||
| 690 | |||
| 691 | /* figure out the data block size */ | ||
| 692 | for (blocksize = 0, tfs = fshead; tfs; tfs = tfs->nextfs) { | ||
| 693 | tfs->bcnt = size(tfs); | ||
| 694 | if (blocksize < tfs->bcnt) { | ||
| 695 | blocksize = tfs->bcnt; | ||
| 696 | } | ||
| 697 | } | ||
| 698 | /* rewrite the rules, do syntax checking */ | ||
| 699 | for (tfs = fshead; tfs; tfs = tfs->nextfs) { | ||
| 700 | rewrite(tfs); | ||
| 701 | } | ||
| 702 | |||
| 703 | next(argv); | ||
| 704 | display(); | ||
| 705 | |||
| 706 | return(exitval); | ||
| 707 | } | ||
| 708 | |||
| 709 | void add(char *fmt) | ||
| 710 | { | ||
| 711 | register char *p; | ||
| 712 | register char *p1; | ||
| 713 | register char *p2; | ||
| 714 | static FS **nextfs; | ||
| 715 | FS *tfs; | ||
| 716 | FU *tfu, **nextfu; | ||
| 717 | char *savep; | ||
| 718 | |||
| 719 | /* start new linked list of format units */ | ||
| 720 | /* NOSTRICT */ | ||
| 721 | tfs = (FS *)xmalloc(sizeof(FS)); | ||
| 722 | if (!fshead) { | ||
| 723 | fshead = tfs; | ||
| 724 | } else { | ||
| 725 | *nextfs = tfs; | ||
| 726 | } | ||
| 727 | nextfs = &tfs->nextfs; | ||
| 728 | nextfu = &tfs->nextfu; | ||
| 729 | |||
| 730 | /* take the format string and break it up into format units */ | ||
| 731 | for (p = fmt;;) { | ||
| 732 | /* skip leading white space */ | ||
| 733 | for (; isspace(*p); ++p); | ||
| 734 | if (!*p) { | ||
| 735 | break; | ||
| 736 | } | ||
| 737 | |||
| 738 | /* allocate a new format unit and link it in */ | ||
| 739 | /* NOSTRICT */ | ||
| 740 | tfu = (FU *)xmalloc(sizeof(FU)); | ||
| 741 | *nextfu = tfu; | ||
| 742 | nextfu = &tfu->nextfu; | ||
| 743 | tfu->reps = 1; | ||
| 744 | |||
| 745 | /* if leading digit, repetition count */ | ||
| 746 | if (isdigit(*p)) { | ||
| 747 | for (savep = p; isdigit(*p); ++p); | ||
| 748 | if (!isspace(*p) && *p != '/') { | ||
| 749 | error_msg_and_die("hexdump: bad format {%s}", fmt); | ||
| 750 | } | ||
| 751 | /* may overwrite either white space or slash */ | ||
| 752 | tfu->reps = atoi(savep); | ||
| 753 | tfu->flags = F_SETREP; | ||
| 754 | /* skip trailing white space */ | ||
| 755 | for (++p; isspace(*p); ++p); | ||
| 756 | } | ||
| 757 | |||
| 758 | /* skip slash and trailing white space */ | ||
| 759 | if (*p == '/') { | ||
| 760 | while (isspace(*++p)); | ||
| 761 | } | ||
| 762 | |||
| 763 | /* byte count */ | ||
| 764 | if (isdigit(*p)) { | ||
| 765 | for (savep = p; isdigit(*p); ++p); | ||
| 766 | if (!isspace(*p)) { | ||
| 767 | error_msg_and_die("hexdump: bad format {%s}", fmt); | ||
| 768 | } | ||
| 769 | tfu->bcnt = atoi(savep); | ||
| 770 | /* skip trailing white space */ | ||
| 771 | for (++p; isspace(*p); ++p); | ||
| 772 | } | ||
| 773 | |||
| 774 | /* format */ | ||
| 775 | if (*p != '"') { | ||
| 776 | error_msg_and_die("hexdump: bad format {%s}", fmt); | ||
| 777 | } | ||
| 778 | for (savep = ++p; *p != '"';) { | ||
| 779 | if (*p++ == 0) { | ||
| 780 | error_msg_and_die("hexdump: bad format {%s}", fmt); | ||
| 781 | } | ||
| 782 | } | ||
| 783 | if (!(tfu->fmt = malloc(p - savep + 1))) { | ||
| 784 | perror_msg_and_die("hexdump"); | ||
| 785 | } | ||
| 786 | strncpy(tfu->fmt, savep, p - savep); | ||
| 787 | tfu->fmt[p - savep] = '\0'; | ||
| 788 | // escape(tfu->fmt); | ||
| 789 | |||
| 790 | p1 = tfu->fmt; | ||
| 791 | |||
| 792 | /* alphabetic escape sequences have to be done in place */ | ||
| 793 | for (p2 = p1;; ++p1, ++p2) { | ||
| 794 | if (!*p1) { | ||
| 795 | *p2 = *p1; | ||
| 796 | break; | ||
| 797 | } | ||
| 798 | if (*p1 == '\\') { | ||
| 799 | switch(*++p1) { | ||
| 800 | case 'a': | ||
| 801 | /* *p2 = '\a'; */ | ||
| 802 | *p2 = '\007'; | ||
| 803 | break; | ||
| 804 | case 'b': | ||
| 805 | *p2 = '\b'; | ||
| 806 | break; | ||
| 807 | case 'f': | ||
| 808 | *p2 = '\f'; | ||
| 809 | break; | ||
| 810 | case 'n': | ||
| 811 | *p2 = '\n'; | ||
| 812 | break; | ||
| 813 | case 'r': | ||
| 814 | *p2 = '\r'; | ||
| 815 | break; | ||
| 816 | case 't': | ||
| 817 | *p2 = '\t'; | ||
| 818 | break; | ||
| 819 | case 'v': | ||
| 820 | *p2 = '\v'; | ||
| 821 | break; | ||
| 822 | default: | ||
| 823 | *p2 = *p1; | ||
| 824 | break; | ||
| 825 | } | ||
| 826 | } | ||
| 827 | } | ||
| 828 | |||
| 829 | p++; | ||
| 830 | } | ||
| 831 | } | ||
| 832 | /* | ||
| 833 | * Copyright (c) 1989 The Regents of the University of California. | ||
| 834 | * All rights reserved. | ||
| 835 | * | ||
| 836 | * Redistribution and use in source and binary forms, with or without | ||
| 837 | * modification, are permitted provided that the following conditions | ||
| 838 | * are met: | ||
| 839 | * 1. Redistributions of source code must retain the above copyright | ||
| 840 | * notice, this list of conditions and the following disclaimer. | ||
| 841 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 842 | * notice, this list of conditions and the following disclaimer in the | ||
| 843 | * documentation and/or other materials provided with the distribution. | ||
| 844 | * 3. All advertising materials mentioning features or use of this software | ||
| 845 | * must display the following acknowledgement: | ||
| 846 | * This product includes software developed by the University of | ||
| 847 | * California, Berkeley and its contributors. | ||
| 848 | * 4. Neither the name of the University nor the names of its contributors | ||
| 849 | * may be used to endorse or promote products derived from this software | ||
| 850 | * without specific prior written permission. | ||
| 851 | * | ||
| 852 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 853 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 854 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 855 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 856 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 857 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 858 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 859 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 860 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 861 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 862 | * SUCH DAMAGE. | ||
| 863 | */ | ||
diff --git a/util-linux/Makefile b/util-linux/Makefile index ddecf50d3..0971acadc 100644 --- a/util-linux/Makefile +++ b/util-linux/Makefile | |||
| @@ -30,6 +30,7 @@ obj-$(CONFIG_FDFLUSH) += fdflush.o | |||
| 30 | obj-$(CONFIG_FREERAMDISK) += freeramdisk.o | 30 | obj-$(CONFIG_FREERAMDISK) += freeramdisk.o |
| 31 | obj-$(CONFIG_FSCK_MINIX) += fsck_minix.o | 31 | obj-$(CONFIG_FSCK_MINIX) += fsck_minix.o |
| 32 | obj-$(CONFIG_GETOPT) += getopt.o | 32 | obj-$(CONFIG_GETOPT) += getopt.o |
| 33 | obj-$(CONFIG_HEXDUMP) += hexdump.o | ||
| 33 | obj-$(CONFIG_MKFS_MINIX) += mkfs_minix.o | 34 | obj-$(CONFIG_MKFS_MINIX) += mkfs_minix.o |
| 34 | obj-$(CONFIG_MKSWAP) += mkswap.o | 35 | obj-$(CONFIG_MKSWAP) += mkswap.o |
| 35 | obj-$(CONFIG_MORE) += more.o | 36 | obj-$(CONFIG_MORE) += more.o |
diff --git a/util-linux/config.in b/util-linux/config.in index 3eb8ee0b5..50a874d6c 100644 --- a/util-linux/config.in +++ b/util-linux/config.in | |||
| @@ -13,6 +13,7 @@ bool 'fdflush' CONFIG_FDFLUSH | |||
| 13 | bool 'freeramdisk' CONFIG_FREERAMDISK | 13 | bool 'freeramdisk' CONFIG_FREERAMDISK |
| 14 | bool 'fsck_minix' CONFIG_FSCK_MINIX | 14 | bool 'fsck_minix' CONFIG_FSCK_MINIX |
| 15 | bool 'getopt' CONFIG_GETOPT | 15 | bool 'getopt' CONFIG_GETOPT |
| 16 | bool 'hexdump' CONFIG_HEXDUMP | ||
| 16 | bool 'mkfs_minix' CONFIG_MKFS_MINIX | 17 | bool 'mkfs_minix' CONFIG_MKFS_MINIX |
| 17 | bool 'mkswap' CONFIG_MKSWAP | 18 | bool 'mkswap' CONFIG_MKSWAP |
| 18 | bool 'more' CONFIG_MORE | 19 | bool 'more' CONFIG_MORE |
diff --git a/util-linux/hexdump.c b/util-linux/hexdump.c new file mode 100644 index 000000000..4510a5e66 --- /dev/null +++ b/util-linux/hexdump.c | |||
| @@ -0,0 +1,169 @@ | |||
| 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. | ||
| 150 | * 3. All advertising materials mentioning features or use of this software | ||
| 151 | * must display the following acknowledgement: | ||
| 152 | * This product includes software developed by the University of | ||
| 153 | * California, Berkeley and its contributors. | ||
| 154 | * 4. Neither the name of the University nor the names of its contributors | ||
| 155 | * may be used to endorse or promote products derived from this software | ||
| 156 | * without specific prior written permission. | ||
| 157 | * | ||
| 158 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 159 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 160 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 161 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 162 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 163 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 164 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 165 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 166 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 167 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 168 | * SUCH DAMAGE. | ||
| 169 | */ | ||
