1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#define WIDTH 10
void usage(const char *progname) {
fprintf(stderr, "usage: %s <file> [-r <file>] [-w <width>]\n", progname);
}
int main(int argc, char *argv[]) {
FILE *fp;
uint8_t buf[1];
int i = 0;
int opt;
char *redir = NULL;
unsigned int width = WIDTH;
bool r = false;
if (argc < 2) {
usage(argv[0]);
return EXIT_FAILURE;
}
while ((opt = getopt(argc, argv, "w:r:")) != -1) {
switch (opt) {
case 'w':
width = atoi(optarg);
break;
case 'r':
redir = optarg;
break;
default:
usage(argv[0]);
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
fp = fopen(argv[0], "rb");
if (fp == NULL) {
fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
printf("echo -ne \"");
while(fread(buf, 1, 1, fp)) {
i++;
printf("\\x%02x", buf[0]);
if (i % width == 0) {
if (redir) {
printf("\" %s %s\n", r ? ">>" : ">", redir);
r = true;
} else {
printf("\"\n");
}
printf("echo -ne \"");
}
}
if (i % WIDTH != 0)
printf("\"\n");
fclose(fp);
return EXIT_SUCCESS;
}
|