blob: a29ed758e0986282054749f1cbca7be399aaf595 [file] [log] [blame]
Glenn L McGrath60281112001-11-02 11:39:46 +00001/*
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
31extern FS *fshead; /* head of format strings */
32extern int blocksize; /* data block size */
33extern 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
38static void
39odoffset(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
129static 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
141int 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.
Aaron Lehmann69d41782002-06-23 22:25:24 +0000251 * 3. Neither the name of the University nor the names of its contributors
Glenn L McGrath60281112001-11-02 11:39:46 +0000252 * may be used to endorse or promote products derived from this software
253 * without specific prior written permission.
254 *
255 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
256 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
257 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
258 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
260 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
263 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
264 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
265 * SUCH DAMAGE.
266 */