2 * dump.c HEXDUMP utility
4 * Copyright 2001,2002,2005,2009 by Charles Wilson
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * See the COPYING file for full license information.
29 #if defined(__WIN32__) && !defined(__CYGWIN__)
34 static const char versionID
[] = PACKAGE_VERSION
;
35 static const char revID
[] =
37 static const char copyrightID
[] =
38 "Copyright (c) 2009\nCharles S. Wilson. All rights reserved.\nLicensed under GPLv3+\n";
40 static void printTopDescription (FILE * f
, char *name
);
41 static void printBottomDescription (FILE * f
, char *name
);
42 static const char *getVersion (void);
43 static void usage (FILE * f
, char *name
);
44 static void help (FILE * f
, char *name
);
45 static void version (FILE * f
, char *name
);
46 static void license (FILE * f
, char *name
);
47 static void puthex (long n
, int digits
, int pos
);
48 static void dumpfile (FILE * f
);
50 static char *program_name
;
51 static poptContext optCon
;
56 main (int argc
, const char **argv
)
62 struct poptOption helpOptionsTable
[] = {
63 {"help", '?', POPT_ARG_NONE
, NULL
, '?',
64 "Show this help message", NULL
},
65 {"usage", '\0', POPT_ARG_NONE
, NULL
, 'u',
66 "Display brief usage message", NULL
},
67 {"version", '\0', POPT_ARG_NONE
, NULL
, 'v',
68 "Display version information", NULL
},
69 {"license", '\0', POPT_ARG_NONE
, NULL
, 'l',
70 "Display licensing information", NULL
},
71 {NULL
, '\0', 0, NULL
, 0, NULL
, NULL
}
74 struct poptOption opt
[] = {
75 {NULL
, '\0', POPT_ARG_INCLUDE_TABLE
, helpOptionsTable
, 0,
76 "Help options", NULL
},
77 {NULL
, '\0', 0, NULL
, 0, NULL
, NULL
}
80 if ((program_name
= strdup (argv
[0])) == NULL
)
82 fprintf (stderr
, "%s: memory allocation error\n", argv
[0]);
85 optCon
= poptGetContext (NULL
, argc
, argv
, opt
, 0);
86 poptSetOtherOptionHelp (optCon
, "[OPTION...] [files...]");
87 while ((rc
= poptGetNextOpt (optCon
)) > 0)
92 help (stdout
, program_name
);
95 usage (stdout
, program_name
);
98 version (stdout
, program_name
);
101 license (stdout
, program_name
);
107 fprintf (stderr
, "%s: bad argument %s: %s\n",
108 program_name
, poptBadOption (optCon
, POPT_BADOPTION_NOALIAS
),
113 rest
= poptGetArgs (optCon
);
121 FILE *f
= fopen (*rest
, "rb");
122 printf ("%s:\n", *rest
);
129 printf ("*** Can't open %s!!\n", *rest
);
134 if (argc < 2) dumpfile(stdin);
137 FILE *f = fopen(*++argv, "rb");
138 printf("%s:\n",*argv);
143 else printf("*** Can't open %s!!\n", *argv);
148 poptFreeContext (optCon
);
160 printTopDescription (FILE * f
, char *name
)
162 fprintf (f
, "%s is part of cygutils version %s\n", name
, getVersion ());
163 fprintf (f
, " Prints a hexdump of stdin or specified files to stdout\n\n");
167 printBottomDescription (FILE * f
, char *name
)
170 fprintf (f
, "Other arguments\n");
172 " [files...] dump each file specified; if none, use stdin\n");
176 printLicense (FILE * f
, char *name
)
179 "This program is free software; you can redistribute it and/or\n");
181 "modify it under the terms of the GNU General Public License\n");
183 "as published by the Free Software Foundation; either version 2\n");
184 fprintf (f
, "of the License, or (at your option) any later version.\n");
187 "This program is distributed in the hope that it will be useful,\n");
189 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
191 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
192 fprintf (f
, "GNU General Public License for more details.\n");
195 "You should have received a copy of the GNU General Public License\n");
197 "along with this program; if not, write to the Free Software\n");
199 "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
201 fprintf (f
, "See the COPYING file for license information.\n");
205 usage (FILE * f
, char *name
)
207 poptPrintUsage (optCon
, f
, 0);
211 help (FILE * f
, char *name
)
213 printTopDescription (f
, name
);
214 poptPrintHelp (optCon
, f
, 0);
215 printBottomDescription (f
, name
);
219 version (FILE * f
, char *name
)
221 printTopDescription (f
, name
);
225 license (FILE * f
, char *name
)
227 printTopDescription (f
, name
);
228 printLicense (f
, name
);
232 puthex (long n
, int digits
, int pos
)
235 puthex (n
/ 16, digits
- 1, pos
);
236 line
[pos
+ digits
- 1] = "0123456789abcdef"[n
% 16];
247 for (i
= 0; i
< 50; i
++)
251 puthex (address
, 8, 0);
254 for (i
= 0; i
< 16; i
++)
256 puthex (c
& 0xff, 2, 10 + i
* 2 + i
/ 2);
258 if (isprint (c
& 0x7f))
259 line
[50 + i
] = c
& 0x7f;
260 if ((c
= getc (f
)) == EOF
)
263 if (address
&& ((address
% 256) == 0))
265 #if defined(__WIN32__) && !defined(__CYGWIN__)
266 if (isatty (STDOUT_FILENO
))
275 (" Addr 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 2 4 6 8 A C E ");
277 ("-------- ---- ---- ---- ---- ---- ---- ---- ---- ----------------");