(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lib-src / hexl.c
blob7a2f127ae61a668a744b5937ed8814b7994284cb
1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989 Free Software Foundation, Inc
4 This file is not considered part of GNU Emacs.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <stdio.h>
25 #include <ctype.h>
26 #ifdef DOS_NT
27 #include <fcntl.h>
28 #if __DJGPP__ >= 2
29 #include <io.h>
30 #endif
31 #endif
32 #ifdef WINDOWSNT
33 #include <io.h>
34 #endif
36 #define DEFAULT_GROUPING 0x01
37 #define DEFAULT_BASE 16
39 #undef TRUE
40 #undef FALSE
41 #define TRUE (1)
42 #define FALSE (0)
44 int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
45 int group_by = DEFAULT_GROUPING;
46 char *progname;
48 void usage();
50 int
51 main (argc, argv)
52 int argc;
53 char *argv[];
55 register long address;
56 char string[18];
57 FILE *fp;
59 progname = *argv++; --argc;
62 ** -hex hex dump
63 ** -oct Octal dump
64 ** -group-by-8-bits
65 ** -group-by-16-bits
66 ** -group-by-32-bits
67 ** -group-by-64-bits
68 ** -iso iso character set.
69 ** -big-endian Big Endian
70 ** -little-endian Little Endian
71 ** -un || -de from hexl format to binary.
72 ** -- End switch list.
73 ** <filename> dump filename
74 ** - (as filename == stdin)
77 while (*argv && *argv[0] == '-' && (*argv)[1])
79 /* A switch! */
80 if (!strcmp (*argv, "--"))
82 --argc; argv++;
83 break;
85 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
87 un_flag = TRUE;
88 --argc; argv++;
90 else if (!strcmp (*argv, "-hex"))
92 base = 16;
93 --argc; argv++;
95 else if (!strcmp (*argv, "-iso"))
97 iso_flag = TRUE;
98 --argc; argv++;
100 else if (!strcmp (*argv, "-oct"))
102 base = 8;
103 --argc; argv++;
105 else if (!strcmp (*argv, "-big-endian"))
107 endian = 1;
108 --argc; argv++;
110 else if (!strcmp (*argv, "-little-endian"))
112 endian = 0;
113 --argc; argv++;
115 else if (!strcmp (*argv, "-group-by-8-bits"))
117 group_by = 0x00;
118 --argc; argv++;
120 else if (!strcmp (*argv, "-group-by-16-bits"))
122 group_by = 0x01;
123 --argc; argv++;
125 else if (!strcmp (*argv, "-group-by-32-bits"))
127 group_by = 0x03;
128 --argc; argv++;
130 else if (!strcmp (*argv, "-group-by-64-bits"))
132 group_by = 0x07;
133 endian = 0;
134 --argc; argv++;
136 else
138 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
139 *argv);
140 usage ();
146 if (*argv == NULL)
147 fp = stdin;
148 else
150 char *filename = *argv++;
152 if (!strcmp (filename, "-"))
153 fp = stdin;
154 else if ((fp = fopen (filename, "r")) == NULL)
156 perror (filename);
157 continue;
161 if (un_flag)
163 char buf[18];
165 #ifdef DOS_NT
166 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
167 if (!isatty (fileno (stdout)))
168 setmode (fileno (stdout), O_BINARY);
169 #else
170 (stdout)->_flag &= ~_IOTEXT; /* print binary */
171 _setmode (fileno (stdout), O_BINARY);
172 #endif
173 #endif
174 for (;;)
176 register int i, c = 0, d;
178 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
180 fread (buf, 1, 10, fp); /* skip 10 bytes */
182 for (i=0; i < 16; ++i)
184 if ((c = getc (fp)) == ' ' || c == EOF)
185 break;
187 d = getc (fp);
188 c = hexchar (c) * 0x10 + hexchar (d);
189 putchar (c);
191 if ((i&group_by) == group_by)
192 getc (fp);
195 if (c == ' ')
197 while ((c = getc (fp)) != '\n' && c != EOF)
200 if (c == EOF)
201 break;
203 else
205 if (i < 16)
206 break;
208 fread (buf, 1, 18, fp); /* skip 18 bytes */
212 else
214 #ifdef DOS_NT
215 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
216 if (!isatty (fileno (fp)))
217 setmode (fileno (fp), O_BINARY);
218 #else
219 (fp)->_flag &= ~_IOTEXT; /* read binary */
220 _setmode (fileno (fp), O_BINARY);
221 #endif
222 #endif
223 address = 0;
224 string[0] = ' ';
225 string[17] = '\0';
226 for (;;)
228 register int i, c = 0;
230 for (i=0; i < 16; ++i)
232 if ((c = getc (fp)) == EOF)
234 if (!i)
235 break;
237 fputs (" ", stdout);
238 string[i+1] = '\0';
240 else
242 if (!i)
243 printf ("%08lx: ", address);
245 if (iso_flag)
246 string[i+1] =
247 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
248 else
249 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
251 printf ("%02x", c);
254 if ((i&group_by) == group_by)
255 putchar (' ');
258 if (i)
259 puts (string);
261 if (c == EOF)
262 break;
264 address += 0x10;
269 if (fp != stdin)
270 fclose (fp);
272 } while (*argv != NULL);
273 return EXIT_SUCCESS;
276 void
277 usage ()
279 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
280 exit (EXIT_FAILURE);
283 /* arch-tag: 20e04fb7-926e-4e48-be86-64fe869ecdaa
284 (do not change this comment) */
286 /* hexl.c ends here */