2 * od implementation for busybox
3 * Based on code from util-linux v 2.11l
6 * The Regents of the University of California. All rights reserved.
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.
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.
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
22 * Original copyright notice is retained at the end of this file.
31 extern FS *fshead; /* head of format strings */
32 extern int blocksize; /* data block size */
33 extern int length; /* max bytes to read */
35 #define ishexdigit(c) \
36 ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
39 odoffset(int argc, char ***argvp)
42 register char *num, *p;
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.
56 * We assumes it's a file if the offset is bad.
59 if (*p != '+' && (argc < 2 ||
60 (!isdigit(p[0]) && (p[0] != 'x' || !ishexdigit(p[1])))))
65 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
70 if (p[0] == 'x' && ishexdigit(p[1])) {
73 } else if (p[0] == '0' && p[1] == 'x') {
78 /* skip over the number */
80 for (num = p; ishexdigit(*p); ++p);
82 for (num = p; isdigit(*p); ++p);
84 /* check for no number */
88 /* if terminates with a '.', base is decimal */
95 skip = strtol(num, &end, base ? base : 8);
97 /* if end isn't the same as p, we got a non-octal digit */
113 * If the offset uses a non-octal base, the base of
114 * the offset is changed as well. This isn't pretty,
117 #define TYPE_OFFSET 7
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';
129 static void odprecede(void)
131 static int first = 1;
135 add("\"%07.7_Ao\n\"");
136 add("\"%07.7_ao \"");
141 int od_main(int argc, char **argv)
144 extern enum _vflag vflag;
148 while ((ch = getopt(argc, argv, "aBbcDdeFfHhIiLlOoPpswvXx")) != EOF)
152 add("16/1 \"%3_u \" \"\\n\"");
157 add("8/2 \" %06o \" \"\\n\"");
161 add("16/1 \"%03o \" \"\\n\"");
165 add("16/1 \"%3_c \" \"\\n\"");
169 add("8/2 \" %05u \" \"\\n\"");
173 add("4/4 \" %010u \" \"\\n\"");
175 case 'e': /* undocumented in od */
178 add("2/8 \" %21.14e \" \"\\n\"");
183 add("4/4 \" %14.7e \" \"\\n\"");
188 add("4/4 \" %08x \" \"\\n\"");
193 add("8/2 \" %04x \" \"\\n\"");
199 add("4/4 \" %11d \" \"\\n\"");
203 add("8/2 \" %6d \" \"\\n\"");
207 add("4/4 \" %011o \" \"\\n\"");
218 error_msg("od: od(1) has been deprecated for hexdump(1).\n");
220 error_msg("od: hexdump(1) compatibility doesn't support the -%c option%s\n",
221 ch, ch == 's' ? "; see strings(1)." : ".");
227 add("\"%07.7_Ao\n\"");
228 add("\"%07.7_ao \" 8/2 \"%06o \" \"\\n\"");
234 odoffset(argc, &argv);
240 * Copyright (c) 1990 The Regents of the University of California.
241 * All rights reserved.
243 * Redistribution and use in source and binary forms, with or without
244 * modification, are permitted provided that the following conditions
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.
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