(outline-end-of-subtree): Stop before final newline.
[emacs.git] / lib-src / hexl.c
blob20504211b55c52cfb404ada07b54ddc3d57ca67c
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 extern void exit (), perror ();
17 int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
18 int group_by = DEFAULT_GROUPING;
19 char *progname;
21 main (argc, argv)
22 int argc;
23 char *argv[];
25 register long address;
26 char string[18];
27 FILE *fp;
29 progname = *argv++; --argc;
32 ** -hex hex dump
33 ** -oct Octal dump
34 ** -group-by-8-bits
35 ** -group-by-16-bits
36 ** -group-by-32-bits
37 ** -group-by-64-bits
38 ** -iso iso character set.
39 ** -big-endian Big Endian
40 ** -little-endian Little Endian
41 ** -un || -de from hexl format to binary.
42 ** -- End switch list.
43 ** <filename> dump filename
44 ** - (as filename == stdin)
47 while (*argv && *argv[0] == '-' && (*argv)[1])
49 /* A switch! */
50 if (!strcmp (*argv, "--"))
52 --argc; argv++;
53 break;
55 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
57 un_flag = TRUE;
58 --argc; argv++;
60 else if (!strcmp (*argv, "-hex"))
62 base = 16;
63 --argc; argv++;
65 else if (!strcmp (*argv, "-iso"))
67 iso_flag = TRUE;
68 --argc; argv++;
70 else if (!strcmp (*argv, "-oct"))
72 base = 8;
73 --argc; argv++;
75 else if (!strcmp (*argv, "-big-endian"))
77 endian = 1;
78 --argc; argv++;
80 else if (!strcmp (*argv, "-little-endian"))
82 endian = 0;
83 --argc; argv++;
85 else if (!strcmp (*argv, "-group-by-8-bits"))
87 group_by = 0x00;
88 --argc; argv++;
90 else if (!strcmp (*argv, "-group-by-16-bits"))
92 group_by = 0x01;
93 --argc; argv++;
95 else if (!strcmp (*argv, "-group-by-32-bits"))
97 group_by = 0x03;
98 --argc; argv++;
100 else if (!strcmp (*argv, "-group-by-64-bits"))
102 group_by = 0x07;
103 endian = 0;
104 --argc; argv++;
106 else
108 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
109 *argv);
110 usage ();
116 if (*argv == NULL)
117 fp = stdin;
118 else
120 char *filename = *argv++;
122 if (!strcmp (filename, "-"))
123 fp = stdin;
124 else if ((fp = fopen (filename, "r")) == NULL)
126 perror (filename);
127 continue;
131 if (un_flag)
133 char buf[18];
135 #ifdef MSDOS
136 (stdout)->_flag &= ~_IOTEXT; /* print binary */
137 _setmode (fileno (stdout), O_BINARY);
138 #endif
139 for (;;)
141 register int i, c, d;
143 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
145 fread (buf, 1, 10, fp); /* skip 10 bytes */
147 for (i=0; i < 16; ++i)
149 if ((c = getc (fp)) == ' ' || c == EOF)
150 break;
152 d = getc (fp);
153 c = hexchar (c) * 0x10 + hexchar (d);
154 putchar (c);
156 if ((i&group_by) == group_by)
157 getc (fp);
160 if (c == ' ')
162 while ((c = getc (fp)) != '\n' && c != EOF)
165 if (c == EOF)
166 break;
168 else
170 if (i < 16)
171 break;
173 fread (buf, 1, 18, fp); /* skip 18 bytes */
177 else
179 #ifdef MSDOS
180 (fp)->_flag &= ~_IOTEXT; /* read binary */
181 _setmode (fileno (fp), O_BINARY);
182 #endif
183 address = 0;
184 string[0] = ' ';
185 string[17] = '\0';
186 for (;;)
188 register int i, c;
190 for (i=0; i < 16; ++i)
192 if ((c = getc (fp)) == EOF)
194 if (!i)
195 break;
197 fputs (" ", stdout);
198 string[i+1] = '\0';
200 else
202 if (!i)
203 printf ("%08x: ", address);
205 if (iso_flag)
206 string[i+1] =
207 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
208 else
209 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
211 printf ("%02x", c);
214 if ((i&group_by) == group_by)
215 putchar (' ');
218 if (i)
219 puts (string);
221 if (c == EOF)
222 break;
224 address += 0x10;
229 if (fp != stdin)
230 fclose (fp);
232 } while (*argv != NULL);
233 return 0;
236 usage ()
238 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
239 exit (1);