static char sccsid
[] = "@(#)compact.c 4.4 (Berkeley) 8/11/83";
* Adaptive Huffman code input to output
* Does not prepend decoding tree
* Written by Colin L. Mc Master (UCB) February 28, 1979
for (head
= dir
+ (j
= 513); j
--; ) {
bottom
= dirp
-> pt
= dict
;
dict
[0] . top
[0] = dict
[0] . top
[1] = dirp
;
in
[EF
] . flags
= FBIT
| SEEN
;
(bottom
-> top
[1]) -> next
= flist
;
bottom
-> top
[1] = dirp
;
for (l
= 0; l
< (LNAME
- 3) && (*cp
= argv
[i
][l
]); l
++)
if (l
>= (LNAME
- 3) || (l
- m
) > MAXNAMLEN
- 1) {
fprintf (stderr
, "%s: File name too long\n", argv
[i
]);
if (i
== argc
- 1) break;
if ((uncfp
= fopen (argv
[i
], "r")) == NULL
) {
if (i
== argc
- 1) break;
fstat (fileno (uncfp
), &status
);
if ((status
.st_mode
& 040000) == 040000) {
fprintf (stderr
, "%s: Can't compact a directory\n", argv
[i
]);
if (i
< argc
) goto closein
;
if ((d
. integ
= getc (uncfp
)) != EOF
) {
if ((c
. integ
= getc (uncfp
)) != EOF
) {
d
. chars
. hib
= c
. integ
& 0377;
if ((d
. integ
&= 0177777) == COMPACTED
) {
fprintf (stderr
, "%s: Already compacted.\n", argv
[i
]);
if (i
< argc
) goto closein
;
if (d
. integ
== PACKED
) {
fprintf (stderr
, "%s: Already packed using program pack. Use unpack.\n", argv
[i
]);
if (i
< argc
) goto closein
;
*cp
++ = '.'; *cp
++ = 'C'; *cp
= '\0';
if ((cfp
= fopen (fname
, "w")) == NULL
) {
chmod (fname
, status
.st_mode
);
putc (c
. chars
. lob
, cfp
);
putc (c
. chars
. hib
, cfp
);
c
. integ
= d
. integ
& 0377;
in
[NC
] . fp
= in
[EF
] . fp
= dict
[0] . sp
[0] . p
= bottom
= dict
+ 1;
bottom
-> count
[0] = bottom
-> count
[1] = dict
[0] . count
[1] = 1;
dirp
-> next
= dict
[0] . top
[1] = bottom
-> top
[0] = bottom
-> top
[1] = dirq
= NEW
;
dict
[0] . fath
. fp
= NULL
;
dirq
-> pt
= bottom
-> fath
. fp
= in
[c
. integ
] . fp
= dict
;
in
[c
. integ
] . flags
= (FBIT
| SEEN
);
dict
[0] . fath
. flags
= RLEAF
;
bottom
-> fath
. flags
= (LLEAF
| RLEAF
);
dict
[0] . count
[0] = 2;
dict
[0] . sp
[1] . ch
= c
. integ
;
bottom
-> sp
[0] . ch
= NC
;
bottom
-> sp
[1] . ch
= EF
;
for (c
. integ
= ((d
. integ
>> 8) & 0377); c
. integ
!= EOF
; c
. integ
= getc (uncfp
)) {
if (in
[c
. integ
] . flags
& SEEN
) encode (c
. integ
);
for (j
= 8; j
--; m
>>= 1) {
if (m
& c
. integ
) d
. integ
++;
if ((bits
&= 017) == 0) {
putc (d
. chars
. hib
, cfp
);
putc (d
. chars
. lob
, cfp
);
d
. integ
<<= (16 - bits
);
putc (d
. chars
. hib
, cfp
);
putc (d
. chars
. lob
, cfp
);
if (ferror (uncfp
) || ferror (cfp
))
if (i
< argc
) fprintf (stderr
, "%s: ", argv
[i
]);
if (i
< argc
) fprintf (stderr
, "Not compacted. ");
fprintf (stderr
, "Does not save bytes.\n");
while ((ic
- oc
) > 21474) {
n
= 100000 * (ic
- oc
) / ic
+ 5;
c
. integ
= m
/ 10 + '0';
if (i
< argc
) fprintf (stderr
, "%s: ", argv
[i
]);
fprintf (stderr
, "Compression : %4ld.%c%c%%\n", n
/ 1000, c
. chars
. lob
, bits
);
closeboth
: fclose (cfp
);
closein
: fclose (uncfp
);
if (i
== argc
- 1) break;
for (j
= 256; j
--; ) in
[j
] . flags
= 0;
fail
: fprintf (stderr
, "Unsuccessful compact of standard input to standard output.\n");
register struct node
*pp
;
int stack
[17], stp
, stbits
;
stack
[stp
= 0] = in
[c
. integ
] . flags
& FBIT
;
pp
= in
[c
. integ
] . fp
;
while (pp
-> fath
. fp
) {
if (pp
-> fath
. flags
& FBIT
) stack
[stp
]++;
if ((stbits
&= 017) == 0) stp
++;
/* pop the output stack */
for (j
= 0; j
< stbits
; j
++) {
if (stack
[stp
] & 01) d
. integ
++;
if ((bits
&= 017) == 0) {
putc (d
. chars
. hib
, cfp
);
putc (d
. chars
. lob
, cfp
);
if (ferror (cfp
)) return;