-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextpand.c
70 lines (59 loc) · 1.36 KB
/
textpand.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
/*
* textpand - uncompress input using builtin frequency table
*
* this file is part of the makeutil package:
* https://github.com/alanpost/makeutil
*
* this file is hereby placed in the public domain.
*/
#include "tree.h"
#include <stdio.h>
#include <stdlib.h>
extern char freq[];
extern size_t _freq_size;
extern int forarg();
static
textpand(filename, file)
char *filename;
FILE *file;
{
struct forest forest;
struct tree *head, *tree;
int ch;
tree=head=huffman_tree(&forest, &freq[0], _freq_size);
while(EOF!=(ch=fgetc(file))) {
unsigned i=0U;
while(i<8U) {
if(NULL==(tree=tree->node[(ch>>i++)&0x1U])) {
fputs("error: fell off tree\n", stderr);
exit(EXIT_FAILURE);
}
if(leaf(tree)) {
if(EOF==tree->ch) break;
if(EOF==fputc(tree->ch, stdout)) {
perror("error: write");
exit(EXIT_FAILURE);
}
tree=head;
}
}
}
if(ferror(file)) {
perror("error: read");
exit(EXIT_FAILURE);
}
if(EOF==fflush(stdout)) {
perror("error: write");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
main(argc, argv)
char *argv[];
{
exit(forarg(argc, argv, textpand, "rb"));
}
/* ..__
* `' "
*/