From: Ian Darwin Date: Tue, 8 Sep 1992 15:01:08 +0000 (+0000) Subject: Hurray! Zoulos wrote in the missing glop to my framework, to actually X-Git-Tag: FILE3_27~260 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6de84b3797473c68adcb4dc5676a486e5b9e52e2;p=file Hurray! Zoulos wrote in the missing glop to my framework, to actually *do* the uncompression of a .Z file. --- diff --git a/src/compress.c b/src/compress.c index eefebf50..300c58d5 100644 --- a/src/compress.c +++ b/src/compress.c @@ -3,6 +3,13 @@ * is_compress() returns 0 if uncompressed, number of bits if compressed. * uncompress(old, n, newch) - uncompress old into new, return sizeof new */ +#include +#include +#include +#include +#include + +#include "file.h" /* Check for compression, return nbits. Algorithm, in magic(4) format: * 0 string \037\235 compressed data @@ -11,7 +18,7 @@ */ int is_compress(p, b) -unsigned char *p; +const unsigned char *p; int *b; { @@ -23,12 +30,56 @@ int *b; } int -uncompress(old, n, newch) -unsigned char *old, **newch; +uncompress(old, newch, n) +const unsigned char *old; +unsigned char **newch; int n; { - *newch = old; /* TODO write this */ - **newch = 0; /* prevent infinite loop, skeleton version only */ - return n; + int fdin[2], fdout[2]; + + if (pipe(fdin) == -1 || pipe(fdout) == -1) { + error("cannot create pipe (%s).\n", strerror(errno)); + /*NOTREACHED*/ + } + switch (fork()) { + case 0: /* child */ + (void) close(0); + (void) dup(fdin[0]); + (void) close(fdin[0]); + (void) close(fdin[1]); + + (void) close(1); + (void) dup(fdout[1]); + (void) close(fdout[0]); + (void) close(fdout[1]); + + execlp("uncompress", "uncompress", "-c", NULL); + error("could not execute `uncompress' (%s).\n", + strerror(errno)); + /*NOTREACHED*/ + case -1: + error("could not fork (%s).\n", strerror(errno)); + /*NOTREACHED*/ + + default: /* parent */ + (void) close(fdin[0]); + (void) close(fdout[1]); + if (write(fdin[1], old, n) != n) { + error("write failed (%s).\n", strerror(errno)); + /*NOTREACHED*/ + } + (void) close(fdin[1]); + if ((*newch = (unsigned char *) malloc(n)) == NULL) { + error("out of memory.\n"); + /*NOTREACHED*/ + } + if ((n = read(fdout[0], *newch, n)) <= 0) { + free(*newch); + error("read failed (%s).\n", strerror(errno)); + /*NOTREACHED*/ + } + (void) close(fdout[0]); + (void) wait(NULL); + return n; + } } -