-
Notifications
You must be signed in to change notification settings - Fork 1
/
icecat.c
107 lines (89 loc) · 1.98 KB
/
icecat.c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "ice.h"
char *me;
static void
icecat (char *file)
{
char header[ICE_HEADER_SIZE];
char *destination;
char *source;
FILE *f;
f = fopen (file, "rb");
if (f == NULL)
{
fprintf (stderr, "%s: error opening file %s: %s\n",
me, file, strerror (errno));
goto end;
}
if (fread (header, ICE_HEADER_SIZE, 1, f) != 1)
{
fprintf (stderr, "%s: %s: error reading header: %s\n",
me, file, strerror (errno));
goto close_file;
}
if (!is_ice_data (header))
{
fprintf (stderr, "%s: %s: not in ICE format\n", me, file);
goto close_file;
}
source = malloc (ice_crunched_length (header));
if (source == NULL)
{
fprintf (stderr, "%s: %s: out of memory\n", me, file);
goto close_file;
}
rewind (f);
if (fread (source, ice_crunched_length (header), 1, f) != 1)
{
fprintf (stderr, "%s: %s: read error: %s\n",
me, file, strerror (errno));
goto free_source;
}
destination = malloc (ice_decrunched_length (header));
if (destination == NULL)
{
fprintf (stderr, "%s: %s: out of memory\n", me, file);
goto free_source;
}
if (ice_decrunch (source, destination) == 0)
{
fprintf (stderr, "%s: %s: decrunch error\n", me, file);
goto free_destination;
}
if (fwrite (destination, ice_decrunched_length (header), 1, stdout) != 1)
{
fprintf (stderr, "%s: %s: write error: %s\n",
me, file, strerror (errno));
goto free_destination;
}
free_destination:
free (destination);
free_source:
free (source);
close_file:
fclose (f);
end:
return;
}
static void
usage (FILE *file, int status)
{
fprintf (file, "Usage: %s file ...\n", me);
exit (status);
}
int
main (int argc, char **argv)
{
int i;
me = argv[0];
if (argc <= 1)
usage (stderr, 1);
for (i = 1; i < argc; i++)
{
icecat (argv[i]);
}
return 0;
}