Fixed calendar-string-spread to allow 0 or 1 string; this simplified several
[emacs.git] / lib-src / hexl.c
blobb090dae335be4b52037095c78b736fe9bfafc8d9
1 #include <stdio.h>
2 #include <ctype.h>
3 #ifdef MSDOS
4 #include <fcntl.h>
5 #endif
7 #define DEFAULT_GROUPING 0x01
8 #define DEFAULT_BASE 16
10 #undef TRUE
11 #undef FALSE
12 #define TRUE (1)
13 #define FALSE (0)
15 int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
16 int group_by = DEFAULT_GROUPING;
17 char *progname;
19 void usage();
21 int
22 main (argc, argv)
23 int argc;
24 char *argv[];
26 register long address;
27 char string[18];
28 FILE *fp;
30 progname = *argv++; --argc;
33 ** -hex hex dump
34 ** -oct Octal dump
35 ** -group-by-8-bits
36 ** -group-by-16-bits
37 ** -group-by-32-bits
38 ** -group-by-64-bits
39 ** -iso iso character set.
40 ** -big-endian Big Endian
41 ** -little-endian Little Endian
42 ** -un || -de from hexl format to binary.
43 ** -- End switch list.
44 ** <filename> dump filename
45 ** - (as filename == stdin)
48 while (*argv && *argv[0] == '-' && (*argv)[1])
50 /* A switch! */
51 if (!strcmp (*argv, "--"))
53 --argc; argv++;
54 break;
56 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
58 un_flag = TRUE;
59 --argc; argv++;
61 else if (!strcmp (*argv, "-hex"))
63 base = 16;
64 --argc; argv++;
66 else if (!strcmp (*argv, "-iso"))
68 iso_flag = TRUE;
69 --argc; argv++;
71 else if (!strcmp (*argv, "-oct"))
73 base = 8;
74 --argc; argv++;
76 else if (!strcmp (*argv, "-big-endian"))
78 endian = 1;
79 --argc; argv++;
81 else if (!strcmp (*argv, "-little-endian"))
83 endian = 0;
84 --argc; argv++;
86 else if (!strcmp (*argv, "-group-by-8-bits"))
88 group_by = 0x00;
89 --argc; argv++;
91 else if (!strcmp (*argv, "-group-by-16-bits"))
93 group_by = 0x01;
94 --argc; argv++;
96 else if (!strcmp (*argv, "-group-by-32-bits"))
98 group_by = 0x03;
99 --argc; argv++;
101 else if (!strcmp (*argv, "-group-by-64-bits"))
103 group_by = 0x07;
104 endian = 0;
105 --argc; argv++;
107 else
109 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
110 *argv);
111 usage ();
117 if (*argv == NULL)
118 fp = stdin;
119 else
121 char *filename = *argv++;
123 if (!strcmp (filename, "-"))
124 fp = stdin;
125 else if ((fp = fopen (filename, "r")) == NULL)
127 perror (filename);
128 continue;
132 if (un_flag)
134 char buf[18];
136 #ifdef MSDOS
137 (stdout)->_flag &= ~_IOTEXT; /* print binary */
138 _setmode (fileno (stdout), O_BINARY);
139 #endif
140 for (;;)
142 register int i, c, d;
144 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
146 fread (buf, 1, 10, fp); /* skip 10 bytes */
148 for (i=0; i < 16; ++i)
150 if ((c = getc (fp)) == ' ' || c == EOF)
151 break;
153 d = getc (fp);
154 c = hexchar (c) * 0x10 + hexchar (d);
155 putchar (c);
157 if ((i&group_by) == group_by)
158 getc (fp);
161 if (c == ' ')
163 while ((c = getc (fp)) != '\n' && c != EOF)
166 if (c == EOF)
167 break;
169 else
171 if (i < 16)
172 break;
174 fread (buf, 1, 18, fp); /* skip 18 bytes */
178 else
180 #ifdef MSDOS
181 (fp)->_flag &= ~_IOTEXT; /* read binary */
182 _setmode (fileno (fp), O_BINARY);
183 #endif
184 address = 0;
185 string[0] = ' ';
186 string[17] = '\0';
187 for (;;)
189 register int i, c;
191 for (i=0; i < 16; ++i)
193 if ((c = getc (fp)) == EOF)
195 if (!i)
196 break;
198 fputs (" ", stdout);
199 string[i+1] = '\0';
201 else
203 if (!i)
204 printf ("%08x: ", address);
206 if (iso_flag)
207 string[i+1] =
208 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
209 else
210 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
212 printf ("%02x", c);
215 if ((i&group_by) == group_by)
216 putchar (' ');
219 if (i)
220 puts (string);
222 if (c == EOF)
223 break;
225 address += 0x10;
230 if (fp != stdin)
231 fclose (fp);
233 } while (*argv != NULL);
234 return 0;
237 void
238 usage ()
240 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
241 exit (1);