Merge: Check return values of some library calls.
[bpt/emacs.git] / lib-src / hexl.c
1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010, 2011 Free Software Foundation, Inc.
4
5 Author: Keith Gabryelski
6 (according to authors.el)
7
8 This file is not considered part of GNU Emacs.
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <ctype.h>
30 #ifdef DOS_NT
31 #include <fcntl.h>
32 #if __DJGPP__ >= 2
33 #include <io.h>
34 #endif
35 #endif
36 #ifdef WINDOWSNT
37 #include <io.h>
38 #endif
39
40 #define DEFAULT_GROUPING 0x01
41 #define DEFAULT_BASE 16
42
43 #undef TRUE
44 #undef FALSE
45 #define TRUE (1)
46 #define FALSE (0)
47
48 int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
49 int group_by = DEFAULT_GROUPING;
50 char *progname;
51
52 void usage(void) NO_RETURN;
53
54 int
55 main (int argc, char **argv)
56 {
57 register long address;
58 char string[18];
59 FILE *fp;
60
61 progname = *argv++; --argc;
62
63 /*
64 ** -hex hex dump
65 ** -oct Octal dump
66 ** -group-by-8-bits
67 ** -group-by-16-bits
68 ** -group-by-32-bits
69 ** -group-by-64-bits
70 ** -iso iso character set.
71 ** -big-endian Big Endian
72 ** -little-endian Little Endian
73 ** -un || -de from hexl format to binary.
74 ** -- End switch list.
75 ** <filename> dump filename
76 ** - (as filename == stdin)
77 */
78
79 while (*argv && *argv[0] == '-' && (*argv)[1])
80 {
81 /* A switch! */
82 if (!strcmp (*argv, "--"))
83 {
84 --argc; argv++;
85 break;
86 }
87 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
88 {
89 un_flag = TRUE;
90 --argc; argv++;
91 }
92 else if (!strcmp (*argv, "-hex"))
93 {
94 base = 16;
95 --argc; argv++;
96 }
97 else if (!strcmp (*argv, "-iso"))
98 {
99 iso_flag = TRUE;
100 --argc; argv++;
101 }
102 else if (!strcmp (*argv, "-oct"))
103 {
104 base = 8;
105 --argc; argv++;
106 }
107 else if (!strcmp (*argv, "-big-endian"))
108 {
109 endian = 1;
110 --argc; argv++;
111 }
112 else if (!strcmp (*argv, "-little-endian"))
113 {
114 endian = 0;
115 --argc; argv++;
116 }
117 else if (!strcmp (*argv, "-group-by-8-bits"))
118 {
119 group_by = 0x00;
120 --argc; argv++;
121 }
122 else if (!strcmp (*argv, "-group-by-16-bits"))
123 {
124 group_by = 0x01;
125 --argc; argv++;
126 }
127 else if (!strcmp (*argv, "-group-by-32-bits"))
128 {
129 group_by = 0x03;
130 --argc; argv++;
131 }
132 else if (!strcmp (*argv, "-group-by-64-bits"))
133 {
134 group_by = 0x07;
135 endian = 0;
136 --argc; argv++;
137 }
138 else
139 {
140 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
141 *argv);
142 usage ();
143 }
144 }
145
146 do
147 {
148 if (*argv == NULL)
149 fp = stdin;
150 else
151 {
152 char *filename = *argv++;
153
154 if (!strcmp (filename, "-"))
155 fp = stdin;
156 else if ((fp = fopen (filename, "r")) == NULL)
157 {
158 perror (filename);
159 continue;
160 }
161 }
162
163 if (un_flag)
164 {
165 char buf[18];
166
167 #ifdef DOS_NT
168 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
169 if (!isatty (fileno (stdout)))
170 setmode (fileno (stdout), O_BINARY);
171 #else
172 (stdout)->_flag &= ~_IOTEXT; /* print binary */
173 _setmode (fileno (stdout), O_BINARY);
174 #endif
175 #endif
176 for (;;)
177 {
178 register int i, c = 0, d;
179
180 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
181
182 /* Skip 10 bytes. */
183 if (fread (buf, 1, 10, fp) != 10)
184 break;
185
186 for (i=0; i < 16; ++i)
187 {
188 if ((c = getc (fp)) == ' ' || c == EOF)
189 break;
190
191 d = getc (fp);
192 c = hexchar (c) * 0x10 + hexchar (d);
193 putchar (c);
194
195 if ((i&group_by) == group_by)
196 getc (fp);
197 }
198
199 if (c == ' ')
200 {
201 while ((c = getc (fp)) != '\n' && c != EOF)
202 ;
203
204 if (c == EOF)
205 break;
206 }
207 else
208 {
209 if (i < 16)
210 break;
211
212 /* Skip 18 bytes. */
213 if (fread (buf, 1, 18, fp) != 18)
214 break;
215 }
216 }
217 }
218 else
219 {
220 #ifdef DOS_NT
221 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
222 if (!isatty (fileno (fp)))
223 setmode (fileno (fp), O_BINARY);
224 #else
225 (fp)->_flag &= ~_IOTEXT; /* read binary */
226 _setmode (fileno (fp), O_BINARY);
227 #endif
228 #endif
229 address = 0;
230 string[0] = ' ';
231 string[17] = '\0';
232 for (;;)
233 {
234 register int i, c = 0;
235
236 for (i=0; i < 16; ++i)
237 {
238 if ((c = getc (fp)) == EOF)
239 {
240 if (!i)
241 break;
242
243 fputs (" ", stdout);
244 string[i+1] = '\0';
245 }
246 else
247 {
248 if (!i)
249 printf ("%08lx: ", address);
250
251 if (iso_flag)
252 string[i+1] =
253 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
254 else
255 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
256
257 printf ("%02x", c);
258 }
259
260 if ((i&group_by) == group_by)
261 putchar (' ');
262 }
263
264 if (i)
265 puts (string);
266
267 if (c == EOF)
268 break;
269
270 address += 0x10;
271
272 }
273 }
274
275 if (fp != stdin)
276 fclose (fp);
277
278 } while (*argv != NULL);
279 return EXIT_SUCCESS;
280 }
281
282 void
283 usage (void)
284 {
285 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
286 exit (EXIT_FAILURE);
287 }
288
289
290 /* hexl.c ends here */