admin/notes/tags small update
[emacs.git] / lib-src / hexl.c
blobf8fb11f8218c0352d8e5ea3a0f442e1f9ff22f39
1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001-2012 Free Software Foundation, Inc.
4 Author: Keith Gabryelski
5 (according to authors.el)
7 This file is not considered part of GNU Emacs.
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <stdio.h>
28 #include <ctype.h>
29 #ifdef DOS_NT
30 #include <fcntl.h>
31 #if __DJGPP__ >= 2
32 #include <io.h>
33 #endif
34 #endif
35 #ifdef WINDOWSNT
36 #include <io.h>
37 #endif
39 #define DEFAULT_GROUPING 0x01
40 #define DEFAULT_BASE 16
42 #undef TRUE
43 #undef FALSE
44 #define TRUE (1)
45 #define FALSE (0)
47 int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
48 int group_by = DEFAULT_GROUPING;
49 char *progname;
51 void usage (void) NO_RETURN;
53 int
54 main (int argc, char **argv)
56 register long address;
57 char string[18];
58 FILE *fp;
60 progname = *argv++; --argc;
63 ** -hex hex dump
64 ** -oct Octal dump
65 ** -group-by-8-bits
66 ** -group-by-16-bits
67 ** -group-by-32-bits
68 ** -group-by-64-bits
69 ** -iso iso character set.
70 ** -big-endian Big Endian
71 ** -little-endian Little Endian
72 ** -un || -de from hexl format to binary.
73 ** -- End switch list.
74 ** <filename> dump filename
75 ** - (as filename == stdin)
78 while (*argv && *argv[0] == '-' && (*argv)[1])
80 /* A switch! */
81 if (!strcmp (*argv, "--"))
83 --argc; argv++;
84 break;
86 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
88 un_flag = TRUE;
89 --argc; argv++;
91 else if (!strcmp (*argv, "-hex"))
93 base = 16;
94 --argc; argv++;
96 else if (!strcmp (*argv, "-iso"))
98 iso_flag = TRUE;
99 --argc; argv++;
101 else if (!strcmp (*argv, "-oct"))
103 base = 8;
104 --argc; argv++;
106 else if (!strcmp (*argv, "-big-endian"))
108 endian = 1;
109 --argc; argv++;
111 else if (!strcmp (*argv, "-little-endian"))
113 endian = 0;
114 --argc; argv++;
116 else if (!strcmp (*argv, "-group-by-8-bits"))
118 group_by = 0x00;
119 --argc; argv++;
121 else if (!strcmp (*argv, "-group-by-16-bits"))
123 group_by = 0x01;
124 --argc; argv++;
126 else if (!strcmp (*argv, "-group-by-32-bits"))
128 group_by = 0x03;
129 --argc; argv++;
131 else if (!strcmp (*argv, "-group-by-64-bits"))
133 group_by = 0x07;
134 endian = 0;
135 --argc; argv++;
137 else
139 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
140 *argv);
141 usage ();
147 if (*argv == NULL)
148 fp = stdin;
149 else
151 char *filename = *argv++;
153 if (!strcmp (filename, "-"))
154 fp = stdin;
155 else if ((fp = fopen (filename, "r")) == NULL)
157 perror (filename);
158 continue;
162 if (un_flag)
164 char buf[18];
166 #ifdef DOS_NT
167 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
168 if (!isatty (fileno (stdout)))
169 setmode (fileno (stdout), O_BINARY);
170 #else
171 (stdout)->_flag &= ~_IOTEXT; /* print binary */
172 _setmode (fileno (stdout), O_BINARY);
173 #endif
174 #endif
175 for (;;)
177 register int i, c = 0, d;
179 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
181 /* Skip 10 bytes. */
182 if (fread (buf, 1, 10, fp) != 10)
183 break;
185 for (i=0; i < 16; ++i)
187 if ((c = getc (fp)) == ' ' || c == EOF)
188 break;
190 d = getc (fp);
191 c = hexchar (c) * 0x10 + hexchar (d);
192 putchar (c);
194 if ((i&group_by) == group_by)
195 getc (fp);
198 if (c == ' ')
200 while ((c = getc (fp)) != '\n' && c != EOF)
203 if (c == EOF)
204 break;
206 else
208 if (i < 16)
209 break;
211 /* Skip 18 bytes. */
212 if (fread (buf, 1, 18, fp) != 18)
213 break;
217 else
219 #ifdef DOS_NT
220 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
221 if (!isatty (fileno (fp)))
222 setmode (fileno (fp), O_BINARY);
223 #else
224 (fp)->_flag &= ~_IOTEXT; /* read binary */
225 _setmode (fileno (fp), O_BINARY);
226 #endif
227 #endif
228 address = 0;
229 string[0] = ' ';
230 string[17] = '\0';
231 for (;;)
233 register int i, c = 0;
235 for (i=0; i < 16; ++i)
237 if ((c = getc (fp)) == EOF)
239 if (!i)
240 break;
242 fputs (" ", stdout);
243 string[i+1] = '\0';
245 else
247 if (!i)
248 printf ("%08lx: ", address);
250 if (iso_flag)
251 string[i+1] =
252 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
253 else
254 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
256 printf ("%02x", c);
259 if ((i&group_by) == group_by)
260 putchar (' ');
263 if (i)
264 puts (string);
266 if (c == EOF)
267 break;
269 address += 0x10;
274 if (fp != stdin)
275 fclose (fp);
277 } while (*argv != NULL);
278 return EXIT_SUCCESS;
281 void
282 usage (void)
284 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
285 exit (EXIT_FAILURE);
289 /* hexl.c ends here */