-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinhex.cpp
56 lines (46 loc) · 1.99 KB
/
binhex.cpp
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
#include "stdafx.h"
#include <stdio.h>
int main(int argc, char** argv)
{
if (argc <= 1)
{
printf("usage: binhex <input file> [output file]\n");
return 0;
}
FILE* input = 0;
fopen_s(&input, argv[1], "rb");
if (!input)
{
printf("binhex: failed to open '%s'\n", argv[1]);
return 0;
}
FILE* output = stdout;
if (argv[2])
{
fopen_s(&output, argv[2], "w");
if (!output)
{
printf("binhex: failed to open output file '%s'. writing to stdout instead.\n", argv[2]);
output = stdout;
}
}
fseek(input, 0, SEEK_END);
long filesize = ftell(input);
fseek(input, 0, SEEK_SET);
for (int i = 0; i < filesize; i++)
{
unsigned char c;
size_t numRead = fread_s(&c, sizeof(c), 1, 1, input);
if (numRead != 1 || feof(input))
{
printf("binhex: eof while reading from file\n");
break;
}
fprintf_s(output, "%02x", c);
}
fprintf_s(output, "\n");
fclose(input);
if (output != stdout)
fclose(output);
return 0;
}