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
78
|
/*
* Copyright (C) 2024 dwlr <dweller@cabin.digital>
*
* BSD 3-Clause License (BSD-3-Clause)
* See LICENSE for details
*/
#define _XOPEN_SOURCE 500
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char** argv)
{
int rc = 0;
int ret = EXIT_SUCCESS;
FILE* in = stdin;
FILE* out = stdout;
const char* name = "stdin";
char buffer[4096] = {0};
size_t got = 0;
if(argc == 2)
{
name = argv[1];
in = fopen(name, "r");
if(!in)
{
perror("fopen");
exit(EXIT_FAILURE);
}
rc = snprintf(buffer, sizeof(buffer) - 1, "%s.h", name);
if(rc < 0)
{
perror("snprintf");
exit(EXIT_FAILURE);
}
out = fopen(buffer, "w");
if(!out)
{
perror("fopen");
fclose(in);
exit(EXIT_FAILURE);
}
}
else if(argc > 2) fprintf(stderr, "warning: ignoring excess paramters\n");
for(;;)
{
size_t i;
got = fread(buffer, 1, sizeof(buffer), in);
rc = ferror(in);
if(rc)
{
perror("fread");
ret = EXIT_FAILURE;
goto end;
}
for(i = 0; i < got; i++) fprintf(out, "'\\x%02x',", (unsigned char)buffer[i]);
rc = feof(in);
if(rc) break;
}
fprintf(out, "\n");
end:
fclose(in);
fclose(out);
return ret;
}
|