home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / os / linux / 10530 < prev    next >
Encoding:
Text File  |  1992-09-11  |  6.2 KB  |  262 lines

  1. Path: sparky!uunet!sun-barr!news2me.ebay.sun.com!exodus.Eng.Sun.COM!appserv.Eng.Sun.COM!slovax.Eng.Sun.COM!lm
  2. From: lm@slovax.Eng.Sun.COM (Larry McVoy)
  3. Newsgroups: comp.os.linux
  4. Subject: compress each file in tar - a partial fix
  5. Date: 12 Sep 1992 05:41:27 GMT
  6. Organization: Sun Microsystems, Mountain View
  7. Lines: 249
  8. Distribution: world
  9. Message-ID: <lb30o7INN8ng@appserv.Eng.Sun.COM>
  10. Reply-To: lm@sun.com (Larry McVoy)
  11. NNTP-Posting-Host: slovax
  12.  
  13.  
  14. Recently, there was some discussion about compressing each file on
  15. input and (optionally) decompressing each file on output.  I haven't
  16. solved that problem but here is a tool that might.  The general idea is
  17. that you can take an fd, call compress(fd, COMPRESS) or compress(fd,
  18. UNCOMPRESS) and the fd turns into a compressed stream or an
  19. uncompressed stream.  That should be sufficient to do the tar hacks
  20. (which I might go do one of these days).
  21.  
  22. Anyway, can people take a look at this and tell me what they think?
  23. I used named pipse, if Linux doesn't grok those but does grok pipe()
  24. then I'll convert it over.  If people seem to like it, I'll polish it 
  25. a bit, make a man page, and submit to the glibc folks.
  26.  
  27. --lm
  28.  
  29. #! /bin/sh
  30. # Contents:  README compress.h compressfd.c zcat.c zcomp.c
  31. # Wrapped by lm@slovax on Fri Sep 11 22:36:24 1992
  32. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  33. if test -f 'README' -a "${1}" != "-c" ; then 
  34.   echo shar: Will not clobber existing file \"'README'\"
  35. else
  36. echo shar: Extracting \"'README'\" \(450 characters\)
  37. sed "s/^X//" >'README' <<'END_OF_FILE'
  38. XCompress the stream.  Take a file descriptor and turn the byte stream
  39. Xinto a compressed byte stream, using named pipes.  You lose the ability
  40. Xto seek but most people don't seek.
  41. X
  42. X
  43. XUsage
  44. X    #include    "compress.h"
  45. X    #include    <fcntl.h>
  46. X
  47. X    compress(fd, flag);
  48. X
  49. XExample
  50. X
  51. X    fd = open("xxx.Z", O_WRONLY|O_CREAT, 0664);
  52. X    compress(fd, COMPRESS);
  53. X
  54. X    normal writes of fd here...
  55. X
  56. X
  57. X    fd = open("xxx.Z", O_RDONLY);
  58. X    compress(fd, UNCOMPRESS);
  59. X
  60. X    normal reads of fd here...
  61. END_OF_FILE
  62. if test 450 -ne `wc -c <'README'`; then
  63.     echo shar: \"'README'\" unpacked with wrong size!
  64. fi
  65. # end of 'README'
  66. fi
  67. if test -f 'compress.h' -a "${1}" != "-c" ; then 
  68.   echo shar: Will not clobber existing file \"'compress.h'\"
  69. else
  70. echo shar: Extracting \"'compress.h'\" \(167 characters\)
  71. sed "s/^X//" >'compress.h' <<'END_OF_FILE'
  72. X#ifndef    _COMPRESS_H
  73. X#define    _COMPRESS_H    "@(#)compress.h    1.1"
  74. X
  75. X#define    COMPRESS    1
  76. X#define    UNCOMPRESS    2
  77. X
  78. X/* could add others, such as pack et al */
  79. X
  80. X#endif    _COMPRESS_H
  81. END_OF_FILE
  82. if test 166 -ne `wc -c <'compress.h'`; then
  83.     echo shar: \"'compress.h'\" unpacked with wrong size!
  84. fi
  85. # end of 'compress.h'
  86. fi
  87. if test -f 'compressfd.c' -a "${1}" != "-c" ; then 
  88.   echo shar: Will not clobber existing file \"'compressfd.c'\"
  89. else
  90. echo shar: Extracting \"'compressfd.c'\" \(1753 characters\)
  91. sed "s/^X//" >'compressfd.c' <<'END_OF_FILE'
  92. X/*
  93. X * @(#)compressfd.c    1.2 (c) 1992 Larry McVoy, may be freely used.
  94. X *
  95. X * Turn a byte stream into a compressed/uncompressed byte stream.
  96. X * You lose the ability to seek.
  97. X *
  98. X * In all cases,
  99. X *    create a named pipe
  100. X *    fork
  101. X *    child:
  102. X *        set up stdin / stdout
  103. X *        exec [un]compress
  104. X *    parent:
  105. X *        unlinks pipe after opening it
  106. X *        dups pipe to fd
  107. X */
  108. X#include "compress.h"
  109. X#include <fcntl.h>
  110. X#include <sys/param.h>
  111. X
  112. Xcompress(fd, flag)
  113. X    int    fd;    /* byte stream to compress */
  114. X    int    flag;    /* COMPRESS, UNCOMPRESS */
  115. X{
  116. X    char    fifo[MAXPATHLEN];
  117. X    char    *av[5];
  118. X    int    fd2;
  119. X    int    mode;
  120. X
  121. X    mode = fcntl(fd, F_GETFL, 0);
  122. X    mode &= O_RDONLY|O_WRONLY;
  123. X    if (mode != O_RDONLY && mode != O_WRONLY) {
  124. X        return (-1);
  125. X    }
  126. X    if (flag != COMPRESS && flag != UNCOMPRESS) {
  127. X        return (-1);
  128. X    }
  129. X    sprintf(fifo, "/usr/tmp/comp%d", getpid());
  130. X    if (mkfifo(fifo, 0644)) {
  131. X        return (-1);
  132. X    }
  133. X    switch (fork()) {
  134. X        case -1:    /* no more processes, skip it */
  135. X        (void) unlink(fifo);
  136. X        return (-1);
  137. X        case 0:    /* child */
  138. X        /*
  139. X         * RDONLY:
  140. X         *    parent reads from pipe
  141. X         *    child writes to pipe, reads from fd
  142. X         * WRONLY:
  143. X         *    parent writes to pipe
  144. X         *    child reads from pipe, writes to fd
  145. X         */
  146. X        if (mode == O_RDONLY) {
  147. X            close(0);
  148. X            dup(fd);    /* puts it at 0 */
  149. X            close(fd);
  150. X            close(1);
  151. X            if ((fd = open(fifo, O_WRONLY)) == -1)
  152. X                exit(1);
  153. X        } else {
  154. X            close(1);
  155. X            dup(fd);    /* puts it at 1 */
  156. X            close(fd);
  157. X            close(0);
  158. X            if ((fd = open(fifo, O_RDONLY)) == -1)
  159. X                exit(1);
  160. X        }
  161. X        if (flag == COMPRESS)
  162. X            execlp("compress", "compress", "-f", 0);
  163. X        else
  164. X            execlp("uncompress", "uncompress", "-f", 0);
  165. X        perror("compress");
  166. X        exit(1);
  167. X        /*NOTREACHED*/
  168. X        default:    /* parent */
  169. X        fd2 = open(fifo, mode);
  170. X        dup2(fd2, fd);
  171. X        close(fd2);
  172. X        unlink(fifo);
  173. X        return (0);
  174. X    }
  175. X    /*NOTREACHED*/
  176. X}
  177. END_OF_FILE
  178. if test 1753 -ne `wc -c <'compressfd.c'`; then
  179.     echo shar: \"'compressfd.c'\" unpacked with wrong size!
  180. fi
  181. # end of 'compressfd.c'
  182. fi
  183. if test -f 'zcat.c' -a "${1}" != "-c" ; then 
  184.   echo shar: Will not clobber existing file \"'zcat.c'\"
  185. else
  186. echo shar: Extracting \"'zcat.c'\" \(344 characters\)
  187. sed "s/^X//" >'zcat.c' <<'END_OF_FILE'
  188. X#include "compress.h"
  189. X#include <fcntl.h>
  190. X#include <stdio.h>
  191. X
  192. Xmain(ac, av)
  193. X    char    **av;
  194. X{
  195. X    while (++av, --ac)
  196. X        zcat(av[0]);
  197. X}
  198. X
  199. Xzcat(f)
  200. X    char    *f;
  201. X{
  202. X    char    buf[10000];
  203. X    int    fd, n;
  204. X
  205. X    fd = open(f, O_RDONLY);
  206. X    compress(fd, UNCOMPRESS);
  207. X    while ((n = read(fd, buf, sizeof(buf))) > 0) {
  208. X        write(1, buf, n);
  209. X    }
  210. X    if (n < 0)
  211. X        perror("read");
  212. X    close(fd);
  213. X}
  214. END_OF_FILE
  215. if test 344 -ne `wc -c <'zcat.c'`; then
  216.     echo shar: \"'zcat.c'\" unpacked with wrong size!
  217. fi
  218. # end of 'zcat.c'
  219. fi
  220. if test -f 'zcomp.c' -a "${1}" != "-c" ; then 
  221.   echo shar: Will not clobber existing file \"'zcomp.c'\"
  222. else
  223. echo shar: Extracting \"'zcomp.c'\" \(359 characters\)
  224. sed "s/^X//" >'zcomp.c' <<'END_OF_FILE'
  225. X
  226. X#include "compress.h"
  227. X#include <fcntl.h>
  228. X#include <stdio.h>
  229. X
  230. Xmain(ac, av)
  231. X    char    **av;
  232. X{
  233. X    while (++av, --ac)
  234. X        zcat(av[0]);
  235. X}
  236. X
  237. Xzcat(f)
  238. X    char    *f;
  239. X{
  240. X    char    buf[10000];
  241. X    int    fd, n;
  242. X
  243. X    fd = open(f, O_WRONLY|O_CREAT, 0644);
  244. X    compress(fd, UNCOMPRESS);
  245. X    while ((n = read(0, buf, sizeof(buf))) > 0) {
  246. X        write(fd, buf, n);
  247. X    }
  248. X    if (n < 0)
  249. X        perror("read");
  250. X    close(fd);
  251. X}
  252. END_OF_FILE
  253. if test 359 -ne `wc -c <'zcomp.c'`; then
  254.     echo shar: \"'zcomp.c'\" unpacked with wrong size!
  255. fi
  256. # end of 'zcomp.c'
  257. fi
  258. echo shar: End of shell archive.
  259. exit 0
  260. ---
  261. Larry McVoy            (415) 336-7627             lm@sun.com
  262.