1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001-2014 Free Software Foundation, Inc.
4 Author: Keith Gabryelski
5 (according to authors.el)
7 This file is not considered part of GNU Emacs.
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
37 #define DEFAULT_GROUPING 0x01
38 #define DEFAULT_BASE 16
40 int base
= DEFAULT_BASE
;
41 bool un_flag
= false, iso_flag
= false, endian
= true;
42 int group_by
= DEFAULT_GROUPING
;
45 _Noreturn
void usage (void);
48 main (int argc
, char **argv
)
50 register long address
;
54 progname
= *argv
++; --argc
;
63 ** -iso iso character set.
64 ** -big-endian Big Endian
65 ** -little-endian Little Endian
66 ** -un || -de from hexl format to binary.
67 ** -- End switch list.
68 ** <filename> dump filename
69 ** - (as filename == stdin)
72 while (*argv
&& *argv
[0] == '-' && (*argv
)[1])
75 if (!strcmp (*argv
, "--"))
80 else if (!strcmp (*argv
, "-un") || !strcmp (*argv
, "-de"))
85 else if (!strcmp (*argv
, "-hex"))
90 else if (!strcmp (*argv
, "-iso"))
95 else if (!strcmp (*argv
, "-oct"))
100 else if (!strcmp (*argv
, "-big-endian"))
105 else if (!strcmp (*argv
, "-little-endian"))
110 else if (!strcmp (*argv
, "-group-by-8-bits"))
115 else if (!strcmp (*argv
, "-group-by-16-bits"))
120 else if (!strcmp (*argv
, "-group-by-32-bits"))
125 else if (!strcmp (*argv
, "-group-by-64-bits"))
133 fprintf (stderr
, "%s: invalid switch: \"%s\".\n", progname
,
145 char *filename
= *argv
++;
147 if (!strcmp (filename
, "-"))
149 else if ((fp
= fopen (filename
, "r")) == NULL
)
161 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
162 if (!isatty (fileno (stdout
)))
163 setmode (fileno (stdout
), O_BINARY
);
165 (stdout
)->_flag
&= ~_IOTEXT
; /* print binary */
166 _setmode (fileno (stdout
), O_BINARY
);
171 register int i
, c
= 0, d
;
173 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
176 if (fread (buf
, 1, 10, fp
) != 10)
179 for (i
=0; i
< 16; ++i
)
181 if ((c
= getc (fp
)) == ' ' || c
== EOF
)
185 c
= hexchar (c
) * 0x10 + hexchar (d
);
188 if ((i
&group_by
) == group_by
)
194 while ((c
= getc (fp
)) != '\n' && c
!= EOF
)
206 if (fread (buf
, 1, 18, fp
) != 18)
214 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
215 if (!isatty (fileno (fp
)))
216 setmode (fileno (fp
), O_BINARY
);
218 (fp
)->_flag
&= ~_IOTEXT
; /* read binary */
219 _setmode (fileno (fp
), O_BINARY
);
227 register int i
, c
= 0;
229 for (i
=0; i
< 16; ++i
)
231 if ((c
= getc (fp
)) == EOF
)
242 printf ("%08lx: ", address
);
246 (c
< 0x20 || (c
>= 0x7F && c
< 0xa0)) ? '.' :c
;
248 string
[i
+1] = (c
< 0x20 || c
>= 0x7F) ? '.' : c
;
253 if ((i
&group_by
) == group_by
)
271 } while (*argv
!= NULL
);
278 fprintf (stderr
, "usage: %s [-de] [-iso]\n", progname
);
283 /* hexl.c ends here */