]> granicus.if.org Git - file/commitdiff
Hurray! Zoulos wrote in the missing glop to my framework, to actually
authorIan Darwin <ian@darwinsys.com>
Tue, 8 Sep 1992 15:01:08 +0000 (15:01 +0000)
committerIan Darwin <ian@darwinsys.com>
Tue, 8 Sep 1992 15:01:08 +0000 (15:01 +0000)
*do* the uncompression of a .Z file.

src/compress.c

index eefebf5017ec58e27ec72db785a1170d8ee31427..300c58d519cd5964cc1e040338a9f1c83beafa49 100644 (file)
@@ -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 <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/wait.h>
+
+#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;
+       }
 }
-