home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 036 / less232.zip / ZFILES.C < prev   
C/C++ Source or Header  |  1994-09-18  |  3KB  |  143 lines

  1. #include "less.h"
  2.  
  3. #if OS2
  4. #define INCL_DOSPROCESS
  5. #include <os2.h>
  6. #include <process.h>
  7. #endif
  8.  
  9. typedef struct 
  10. {
  11.     unsigned char magic[2];
  12.     char *cmd;
  13.     char *opt;
  14. }  
  15. CompressMethod;
  16.  
  17. static CompressMethod zTable[] = 
  18. {
  19.   0x1F, 0x9D, "compress", "-dc",    /* compress */
  20.   0x1F, 0x9F, "freeze", "-dc",        /* freeze 2.x */
  21.   0x1F, 0x9E, "gzip", "-dc",        /* freeze 1.x */
  22.   0x1F, 0xA0, "gzip", "-dc",        /* sco compress */
  23.   0x1F, 0x1E, "gzip", "-dc",        /* pack */
  24.   0x1F, 0x8B, "gzip", "-dc",        /* gzip */
  25. };
  26. #define MethodCnt sizeof(zTable)/sizeof(CompressMethod)
  27.  
  28. int isZfile(name)
  29. char *name;
  30. {
  31.   int method, res, file;
  32.   unsigned char magic[2];
  33.  
  34.   file = open(name, O_RDONLY | O_BINARY);
  35.   res = read(file, magic, 2);
  36.   close(file);
  37.  
  38.   if (res != 2)
  39.     return -1;
  40.  
  41.   for (method=0; method < MethodCnt; method++)
  42.     if (zTable[method].magic[0] == magic[0] &&
  43.         zTable[method].magic[1] == magic[1])
  44.         return method;
  45.  
  46.   return -1;
  47. }
  48.  
  49. static int pid[64];
  50.  
  51. int Zopen(name, method)
  52. char *name;
  53. int method;
  54. {
  55.   int ph[2], file, old0, old1, old2, p;
  56.   char *cmd, *opt;
  57.  
  58.   if (method < 0 || method >= MethodCnt)
  59.     return -1;
  60.  
  61.   cmd = zTable[method].cmd;
  62.   opt = zTable[method].opt;
  63.  
  64. #if OS2
  65.   if ((file = open(name, O_RDONLY|O_BINARY)) == -1)
  66.     return -1;
  67.  
  68.   old0 = dup(0);
  69.   fcntl(old0, F_SETFD, 1);
  70.   old1 = dup(1);
  71.   fcntl(old1, F_SETFD, 1);
  72.   old2 = dup(2);
  73.   fcntl(old1, F_SETFD, 2);
  74.  
  75.   if (pipe(ph))
  76.     return -1;
  77.  
  78.   fcntl(ph[0], F_SETFD, 1);
  79.   setmode(ph[0], O_BINARY);
  80.   setmode(ph[1], O_BINARY);
  81.  
  82.   dup2(file, 0);
  83.   close(file);
  84.   dup2(ph[1], 1);
  85.   dup2(ph[1], 2);
  86.   close(ph[1]);
  87.  
  88.   p = spawnlp(P_NOWAIT, cmd, cmd, opt, NULL);
  89.  
  90.   dup2(old0, 0);
  91.   close(old0);
  92.   dup2(old1, 1);
  93.   close(old1);
  94.   dup2(old2, 2);
  95.   close(old2);
  96.  
  97.   if (p == -1)
  98.     return close(ph[0]), -1;
  99. #else
  100.   if (pipe(ph))
  101.     return -1;
  102.   
  103.   if ((p = fork()) == -1)
  104.     return -1;
  105.  
  106.   if (p == 0)
  107.   {
  108.     if ((file = open(name, O_RDONLY)) == -1)
  109.       exit(1);
  110.  
  111.     dup2(file, 0);
  112.     dup2(ph[1], 1);
  113.     dup2(ph[1], 2);
  114.     close(ph[1]);
  115.     close(ph[0]);
  116.     close(file);
  117.  
  118.     if (execlp(cmd, cmd, opt, NULL) == -1)
  119.       exit(1);
  120.   }
  121.   else
  122.     close(ph[1]);
  123. #endif
  124.  
  125.   pid[ph[0]] = p;
  126.  
  127.   return ph[0];
  128. }
  129.  
  130. void Zclose(int handle)
  131. {
  132. #if OS2
  133.   RESULTCODES rc;
  134.   PID p;
  135.   close(handle);
  136.   DosWaitChild(0, 0, &rc, &p, pid[handle]);
  137. #else
  138.   close(handle);
  139.   kill(pid[handle], SIGPIPE);
  140.   waitpid(pid[handle], NULL, 0);
  141. #endif
  142. }
  143.