annotate utils/gen-hex-lines.c @ 62:6519e5c4b3f3

top Makefile: add utils
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 25 Sep 2024 18:48:58 +0000
parents ffbbce856ac7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
61
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program reads an arbitrary binary file and converts it into ASCII hex
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * output with a specified number of bytes per line.
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdlib.h>
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 main(argc, argv)
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 char **argv;
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 {
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 FILE *inf, *outf;
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 int c, lcnt, bpl;
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 if (argc != 4) {
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 fprintf(stderr,
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 "usage: %s input.bin output.hex bytes-per-line\n",
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 argv[0]);
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 exit(1);
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 }
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 inf = fopen(argv[1], "r");
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 if (!inf) {
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 perror(argv[1]);
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 exit(1);
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 }
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 outf = fopen(argv[2], "w");
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 if (!outf) {
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 perror(argv[2]);
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 exit(1);
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 }
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 bpl = atoi(argv[3]);
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 lcnt = 0;
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 for (;;) {
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 c = getc(inf);
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 if (c < 0)
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 break;
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 fprintf(outf, "%02X", c);
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 lcnt++;
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 if (lcnt >= bpl) {
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 putc('\n', outf);
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 lcnt = 0;
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 }
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 if (lcnt)
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 putc('\n', outf);
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 exit(0);
ffbbce856ac7 utils/gen-hex-lines: import from vband-misc repository
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }