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.
61 /* hey someone is probably piping to us ... */
65 if (*p != '+' && (argc < 2 ||
66 (!isdigit(p[0]) && (p[0] != 'x' || !ishexdigit(p[1])))))
71 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
76 if (p[0] == 'x' && ishexdigit(p[1])) {
79 } else if (p[0] == '0' && p[1] == 'x') {
84 /* skip over the number */
86 for (num = p; ishexdigit(*p); ++p);
88 for (num = p; isdigit(*p); ++p);
90 /* check for no number */
94 /* if terminates with a '.', base is decimal */
101 skip = strtol(num, &end, base ? base : 8);
103 /* if end isn't the same as p, we got a non-octal digit */
119 * If the offset uses a non-octal base, the base of
120 * the offset is changed as well. This isn't pretty,
123 #define TYPE_OFFSET 7
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';
135 static void odprecede(void)
137 static int first = 1;
141 add("\"%07.7_Ao\n\"");
142 add("\"%07.7_ao \"");
147 int od_main(int argc, char **argv)
150 extern enum _vflag vflag;
154 while ((ch = getopt(argc, argv, "aBbcDdeFfHhIiLlOoPpswvXx")) != EOF)
158 add("16/1 \"%3_u \" \"\\n\"");
163 add("8/2 \" %06o \" \"\\n\"");
167 add("16/1 \"%03o \" \"\\n\"");
171 add("16/1 \"%3_c \" \"\\n\"");
175 add("8/2 \" %05u \" \"\\n\"");
179 add("4/4 \" %010u \" \"\\n\"");
181 case 'e': /* undocumented in od */
184 add("2/8 \" %21.14e \" \"\\n\"");
189 add("4/4 \" %14.7e \" \"\\n\"");
194 add("4/4 \" %08x \" \"\\n\"");
199 add("8/2 \" %04x \" \"\\n\"");
205 add("4/4 \" %11d \" \"\\n\"");
209 add("8/2 \" %6d \" \"\\n\"");
213 add("4/4 \" %011o \" \"\\n\"");
224 error_msg("od: od(1) has been deprecated for hexdump(1).\n");
226 error_msg("od: hexdump(1) compatibility doesn't support the -%c option%s\n",
227 ch, ch == 's' ? "; see strings(1)." : ".");
233 add("\"%07.7_Ao\n\"");
234 add("\"%07.7_ao \" 8/2 \"%06o \" \"\\n\"");
240 odoffset(argc, &argv);
246 * Copyright (c) 1990 The Regents of the University of California.
247 * All rights reserved.
249 * Redistribution and use in source and binary forms, with or without
250 * modification, are permitted provided that the following conditions
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.
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