*** empty log message ***
[emacs.git] / lib-src / hexl.c
blob95a7f82e1c4b9a2824083fb3c1658f27f0fa6053
1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is not considered part of GNU Emacs.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdio.h>
27 #include <ctype.h>
28 #ifdef DOS_NT
29 #include <fcntl.h>
30 #if __DJGPP__ >= 2
31 #include <io.h>
32 #endif
33 #endif
34 #ifdef WINDOWSNT
35 #include <io.h>
36 #endif
38 #define DEFAULT_GROUPING 0x01
39 #define DEFAULT_BASE 16
41 #undef TRUE
42 #undef FALSE
43 #define TRUE (1)
44 #define FALSE (0)
46 int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
47 int group_by = DEFAULT_GROUPING;
48 char *progname;
50 void usage();
52 int
53 main (argc, argv)
54 int argc;
55 char *argv[];
57 register long address;
58 char string[18];
59 FILE *fp;
61 progname = *argv++; --argc;
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)
79 while (*argv && *argv[0] == '-' && (*argv)[1])
81 /* A switch! */
82 if (!strcmp (*argv, "--"))
84 --argc; argv++;
85 break;
87 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
89 un_flag = TRUE;
90 --argc; argv++;
92 else if (!strcmp (*argv, "-hex"))
94 base = 16;
95 --argc; argv++;
97 else if (!strcmp (*argv, "-iso"))
99 iso_flag = TRUE;
100 --argc; argv++;
102 else if (!strcmp (*argv, "-oct"))
104 base = 8;
105 --argc; argv++;
107 else if (!strcmp (*argv, "-big-endian"))
109 endian = 1;
110 --argc; argv++;
112 else if (!strcmp (*argv, "-little-endian"))
114 endian = 0;
115 --argc; argv++;
117 else if (!strcmp (*argv, "-group-by-8-bits"))
119 group_by = 0x00;
120 --argc; argv++;
122 else if (!strcmp (*argv, "-group-by-16-bits"))
124 group_by = 0x01;
125 --argc; argv++;
127 else if (!strcmp (*argv, "-group-by-32-bits"))
129 group_by = 0x03;
130 --argc; argv++;
132 else if (!strcmp (*argv, "-group-by-64-bits"))
134 group_by = 0x07;
135 endian = 0;
136 --argc; argv++;
138 else
140 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
141 *argv);
142 usage ();
148 if (*argv == NULL)
149 fp = stdin;
150 else
152 char *filename = *argv++;
154 if (!strcmp (filename, "-"))
155 fp = stdin;
156 else if ((fp = fopen (filename, "r")) == NULL)
158 perror (filename);
159 continue;
163 if (un_flag)
165 char buf[18];
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 (;;)
178 register int i, c = 0, d;
180 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
182 fread (buf, 1, 10, fp); /* skip 10 bytes */
184 for (i=0; i < 16; ++i)
186 if ((c = getc (fp)) == ' ' || c == EOF)
187 break;
189 d = getc (fp);
190 c = hexchar (c) * 0x10 + hexchar (d);
191 putchar (c);
193 if ((i&group_by) == group_by)
194 getc (fp);
197 if (c == ' ')
199 while ((c = getc (fp)) != '\n' && c != EOF)
202 if (c == EOF)
203 break;
205 else
207 if (i < 16)
208 break;
210 fread (buf, 1, 18, fp); /* skip 18 bytes */
214 else
216 #ifdef DOS_NT
217 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
218 if (!isatty (fileno (fp)))
219 setmode (fileno (fp), O_BINARY);
220 #else
221 (fp)->_flag &= ~_IOTEXT; /* read binary */
222 _setmode (fileno (fp), O_BINARY);
223 #endif
224 #endif
225 address = 0;
226 string[0] = ' ';
227 string[17] = '\0';
228 for (;;)
230 register int i, c = 0;
232 for (i=0; i < 16; ++i)
234 if ((c = getc (fp)) == EOF)
236 if (!i)
237 break;
239 fputs (" ", stdout);
240 string[i+1] = '\0';
242 else
244 if (!i)
245 printf ("%08lx: ", address);
247 if (iso_flag)
248 string[i+1] =
249 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
250 else
251 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
253 printf ("%02x", c);
256 if ((i&group_by) == group_by)
257 putchar (' ');
260 if (i)
261 puts (string);
263 if (c == EOF)
264 break;
266 address += 0x10;
271 if (fp != stdin)
272 fclose (fp);
274 } while (*argv != NULL);
275 return EXIT_SUCCESS;
278 void
279 usage ()
281 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
282 exit (EXIT_FAILURE);
285 /* arch-tag: 20e04fb7-926e-4e48-be86-64fe869ecdaa
286 (do not change this comment) */
288 /* hexl.c ends here */