lisp/ChangeLog: Add missing entry.
[emacs.git] / lib-src / hexl.c
blob91fadd81f5a4f21893d5cb888d210c24cdfe78e2
1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010 Free Software Foundation, Inc.
5 Author: Keith Gabryelski
6 (according to authors.el)
8 This file is not considered part of GNU Emacs.
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.
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.
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/>. */
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
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
40 #define DEFAULT_GROUPING 0x01
41 #define DEFAULT_BASE 16
43 #undef TRUE
44 #undef FALSE
45 #define TRUE (1)
46 #define FALSE (0)
48 int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
49 int group_by = DEFAULT_GROUPING;
50 char *progname;
52 void usage();
54 int
55 main (argc, argv)
56 int argc;
57 char *argv[];
59 register long address;
60 char string[18];
61 FILE *fp;
63 progname = *argv++; --argc;
66 ** -hex hex dump
67 ** -oct Octal dump
68 ** -group-by-8-bits
69 ** -group-by-16-bits
70 ** -group-by-32-bits
71 ** -group-by-64-bits
72 ** -iso iso character set.
73 ** -big-endian Big Endian
74 ** -little-endian Little Endian
75 ** -un || -de from hexl format to binary.
76 ** -- End switch list.
77 ** <filename> dump filename
78 ** - (as filename == stdin)
81 while (*argv && *argv[0] == '-' && (*argv)[1])
83 /* A switch! */
84 if (!strcmp (*argv, "--"))
86 --argc; argv++;
87 break;
89 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
91 un_flag = TRUE;
92 --argc; argv++;
94 else if (!strcmp (*argv, "-hex"))
96 base = 16;
97 --argc; argv++;
99 else if (!strcmp (*argv, "-iso"))
101 iso_flag = TRUE;
102 --argc; argv++;
104 else if (!strcmp (*argv, "-oct"))
106 base = 8;
107 --argc; argv++;
109 else if (!strcmp (*argv, "-big-endian"))
111 endian = 1;
112 --argc; argv++;
114 else if (!strcmp (*argv, "-little-endian"))
116 endian = 0;
117 --argc; argv++;
119 else if (!strcmp (*argv, "-group-by-8-bits"))
121 group_by = 0x00;
122 --argc; argv++;
124 else if (!strcmp (*argv, "-group-by-16-bits"))
126 group_by = 0x01;
127 --argc; argv++;
129 else if (!strcmp (*argv, "-group-by-32-bits"))
131 group_by = 0x03;
132 --argc; argv++;
134 else if (!strcmp (*argv, "-group-by-64-bits"))
136 group_by = 0x07;
137 endian = 0;
138 --argc; argv++;
140 else
142 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
143 *argv);
144 usage ();
150 if (*argv == NULL)
151 fp = stdin;
152 else
154 char *filename = *argv++;
156 if (!strcmp (filename, "-"))
157 fp = stdin;
158 else if ((fp = fopen (filename, "r")) == NULL)
160 perror (filename);
161 continue;
165 if (un_flag)
167 char buf[18];
169 #ifdef DOS_NT
170 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
171 if (!isatty (fileno (stdout)))
172 setmode (fileno (stdout), O_BINARY);
173 #else
174 (stdout)->_flag &= ~_IOTEXT; /* print binary */
175 _setmode (fileno (stdout), O_BINARY);
176 #endif
177 #endif
178 for (;;)
180 register int i, c = 0, d;
182 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
184 fread (buf, 1, 10, fp); /* skip 10 bytes */
186 for (i=0; i < 16; ++i)
188 if ((c = getc (fp)) == ' ' || c == EOF)
189 break;
191 d = getc (fp);
192 c = hexchar (c) * 0x10 + hexchar (d);
193 putchar (c);
195 if ((i&group_by) == group_by)
196 getc (fp);
199 if (c == ' ')
201 while ((c = getc (fp)) != '\n' && c != EOF)
204 if (c == EOF)
205 break;
207 else
209 if (i < 16)
210 break;
212 fread (buf, 1, 18, fp); /* skip 18 bytes */
216 else
218 #ifdef DOS_NT
219 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
220 if (!isatty (fileno (fp)))
221 setmode (fileno (fp), O_BINARY);
222 #else
223 (fp)->_flag &= ~_IOTEXT; /* read binary */
224 _setmode (fileno (fp), O_BINARY);
225 #endif
226 #endif
227 address = 0;
228 string[0] = ' ';
229 string[17] = '\0';
230 for (;;)
232 register int i, c = 0;
234 for (i=0; i < 16; ++i)
236 if ((c = getc (fp)) == EOF)
238 if (!i)
239 break;
241 fputs (" ", stdout);
242 string[i+1] = '\0';
244 else
246 if (!i)
247 printf ("%08lx: ", address);
249 if (iso_flag)
250 string[i+1] =
251 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
252 else
253 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
255 printf ("%02x", c);
258 if ((i&group_by) == group_by)
259 putchar (' ');
262 if (i)
263 puts (string);
265 if (c == EOF)
266 break;
268 address += 0x10;
273 if (fp != stdin)
274 fclose (fp);
276 } while (*argv != NULL);
277 return EXIT_SUCCESS;
280 void
281 usage ()
283 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
284 exit (EXIT_FAILURE);
287 /* arch-tag: 20e04fb7-926e-4e48-be86-64fe869ecdaa
288 (do not change this comment) */
290 /* hexl.c ends here */