comparison lib-src/hexl.c @ 5406:061f4f90f874

Convert lib-src/ to GPLv3.
author Mike Sperber <sperber@deinprogramm.de>
date Mon, 18 Oct 2010 14:02:19 +0200
parents abe6d1db359e
children
comparison
equal deleted inserted replaced
5405:2aa9cd456ae7 5406:061f4f90f874
1 /* Synched up with: FSF 19.28. */ 1 /* Convert files for Emacs Hexl mode.
2 2 Copyright (C) 1989, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010 Free Software Foundation, Inc.
4
5 Author: Keith Gabryelski
6 (according to authors.el)
7
8 This file is not considered part of GNU Emacs.
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
24 #ifdef HAVE_CONFIG_H
3 #include <config.h> 25 #include <config.h>
26 #endif
4 27
5 #include <stdio.h> 28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
6 #include <ctype.h> 31 #include <ctype.h>
7 #ifdef WIN32_NATIVE 32 #ifdef DOS_NT
33 #include <fcntl.h>
34 #if __DJGPP__ >= 2
8 #include <io.h> 35 #include <io.h>
9 #include <fcntl.h> 36 #endif
10 #endif 37 #endif
11 38 #ifdef WINDOWSNT
12 #if __STDC__ || defined(STDC_HEADERS) 39 #include <io.h>
13 #include <stdlib.h>
14 #ifdef HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <string.h>
18 #endif 40 #endif
19 41
20 #define DEFAULT_GROUPING 0x01 42 #define DEFAULT_GROUPING 0x01
21 #define DEFAULT_BASE 16 43 #define DEFAULT_BASE 16
22 44
27 49
28 int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1; 50 int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
29 int group_by = DEFAULT_GROUPING; 51 int group_by = DEFAULT_GROUPING;
30 char *progname; 52 char *progname;
31 53
32 void usage (void); 54 void usage(void);
33 55
34 int 56 int
35 main (int argc, char *argv[]) 57 main (int argc, char *argv[])
36 { 58 {
37 register long address; 59 register long address;
38 char string[18]; 60 char string[18];
39 FILE *fp; 61 FILE *fp;
40 62
41 progname = *argv++; --argc; 63 progname = *argv++; --argc;
42 64
43 /* 65 /*
44 ** -hex hex dump 66 ** -hex hex dump
45 ** -oct Octal dump 67 ** -oct Octal dump
46 ** -group-by-8-bits 68 ** -group-by-8-bits
47 ** -group-by-16-bits 69 ** -group-by-16-bits
48 ** -group-by-32-bits 70 ** -group-by-32-bits
49 ** -group-by-64-bits 71 ** -group-by-64-bits
50 ** -iso iso character set. 72 ** -iso iso character set.
51 ** -big-endian Big Endian 73 ** -big-endian Big Endian
52 ** -little-endian Little Endian 74 ** -little-endian Little Endian
53 ** -un || -de from hexl format to binary. 75 ** -un || -de from hexl format to binary.
54 ** -- End switch list. 76 ** -- End switch list.
55 ** <filename> dump filename 77 ** <filename> dump filename
56 ** - (as filename == stdin) 78 ** - (as filename == stdin)
57 */ 79 */
58 80
59 while (*argv && *argv[0] == '-' && (*argv)[1]) 81 while (*argv && *argv[0] == '-' && (*argv)[1])
60 { 82 {
61 /* A switch! */ 83 /* A switch! */
62 if (!strcmp (*argv, "--")) 84 if (!strcmp (*argv, "--"))
63 { 85 {
115 endian = 0; 137 endian = 0;
116 --argc; argv++; 138 --argc; argv++;
117 } 139 }
118 else 140 else
119 { 141 {
120 (void) fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname, 142 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
121 *argv); 143 *argv);
122 usage (); 144 usage ();
123 } 145 }
124 } 146 }
125 147
142 164
143 if (un_flag) 165 if (un_flag)
144 { 166 {
145 char buf[18]; 167 char buf[18];
146 168
147 #ifdef WIN32_NATIVE 169 #ifdef DOS_NT
148 _setmode (_fileno (stdout), O_BINARY); 170 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
171 if (!isatty (fileno (stdout)))
172 setmode (fileno (stdout), O_BINARY);
173 #else
174 (stdout)->_flag &= ~_IOTEXT; /* print binary */
175 _setmode (fileno (stdout), O_BINARY);
176 #endif
149 #endif 177 #endif
150 for (;;) 178 for (;;)
151 { 179 {
152 register int i, c = 0, d; 180 register int i, c = 0, d;
153 181
185 } 213 }
186 } 214 }
187 } 215 }
188 else 216 else
189 { 217 {
190 #ifdef WIN32_NATIVE 218 #ifdef DOS_NT
191 _setmode (_fileno (fp), O_BINARY); 219 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
220 if (!isatty (fileno (fp)))
221 setmode (fileno (fp), O_BINARY);
222 #else
223 (fp)->_flag &= ~_IOTEXT; /* read binary */
224 _setmode (fileno (fp), O_BINARY);
225 #endif
192 #endif 226 #endif
193 address = 0; 227 address = 0;
194 string[0] = ' '; 228 string[0] = ' ';
195 string[17] = '\0'; 229 string[17] = '\0';
196 for (;;) 230 for (;;)
208 string[i+1] = '\0'; 242 string[i+1] = '\0';
209 } 243 }
210 else 244 else
211 { 245 {
212 if (!i) 246 if (!i)
213 (void) printf ("%08lx: ", address); 247 printf ("%08lx: ", address);
214 248
215 if (iso_flag) 249 if (iso_flag)
216 string[i+1] = 250 string[i+1] =
217 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c; 251 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
218 else 252 else
219 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c; 253 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
220 254
221 (void) printf ("%02x", c); 255 printf ("%02x", c);
222 } 256 }
223 257
224 if ((i&group_by) == group_by) 258 if ((i&group_by) == group_by)
225 putchar (' '); 259 putchar (' ');
226 } 260 }
235 269
236 } 270 }
237 } 271 }
238 272
239 if (fp != stdin) 273 if (fp != stdin)
240 (void) fclose (fp); 274 fclose (fp);
241 275
242 } while (*argv != NULL); 276 } while (*argv != NULL);
243 return 0; 277 return EXIT_SUCCESS;
244 } 278 }
245 279
246 void 280 void
247 usage (void) 281 usage (void)
248 { 282 {
249 fprintf (stderr, "Usage: %s [-de] [-iso]\n", progname); 283 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
250 exit (1); 284 exit (EXIT_FAILURE);
251 } 285 }
286
287 /* hexl.c ends here */