home *** CD-ROM | disk | FTP | other *** search
- /* Copyright 1988, Gail Zacharias. All rights reserved.
- * Permission is hereby granted to copy, reproduce, redistribute or
- * otherwise use this software provided there is no monetary profit
- * gained specifically from its use or reproduction, it is not sold,
- * rented, traded or otherwise marketed, and this copyright notice
- * and the software version number is included prominently in any copy
- * made.
- * This is mcompress version 1.0.
- *
- * Send comments, suggestions, bug reports (not bloody likely), feature
- * requests, etc. to gz@entity.com.
- *
- */
-
- /* This program demonstrates the use of the compstream library. It
- implements a simple unix-compatible compress/decompress MPW tool.
- It only handles data forks of files for now.
- */
-
- #include <stdio.h>
- #ifdef macintosh
- #include <Types.h>
- #include <Files.h>
- #include <CursorCtl.h>
- #endif macintosh
-
- extern long compinit(), uncompinit();
- extern int compwrite(), uncompread(), compclose(), uncompclose();
-
- stdwrite (stream, size, buf)
- FILE *stream;
- char *buf;
- {
- return (size == fwrite(buf, 1, size, stream)) ? 0 : -1;
- }
-
- stdread (stream, size, buf)
- FILE *stream;
- char *buf;
- {
- return fread(buf, 1, size, stream);
- }
-
- main (argc, argv)
- char **argv;
- {
- int block_compress = 1, nbits = 0, header = 1;
- int decomp = 0;
- int unix = 0;
- int i;
- FILE *inf, *outf;
- char buf[512];
- long cstream;
- int n;
-
- #ifdef macintosh
- InitCursorCtl(nil);
- #endif macintosh
-
- while (--argc > 0 && **++argv == '-') {
- char *argp = &argv[0][1];
- if (!*argp) break;
- do switch (*argp++) {
- case 'd': decomp = 1; break;
- case 'C': block_compress = 0; break;
- case 'n': header = 0; break;
- case 'b': if (*argp == '\0') {
- if (--argc == 0) goto badarg;
- argp = *++argv;
- }
- nbits = 0;
- while ('0' <= *argp && *argp <= '9')
- nbits = nbits*10 + (*argp++ - '0');
- if (*argp || nbits < 9 || nbits > 16) goto badarg;
- break;
- case 'u': unix = 1; /* do CR<->LF for Unix text files */
- break;
- /* case 'm': macbinary = 1; break; /* not yet */
- default: goto badarg;
- } while (*argp);
- }
- if (decomp && (!nbits && !header || nbits && header)) goto badarg;
- if (argc > 2) goto badarg;
- if (!nbits) nbits = 16;
- inf = stdin; outf = stdout;
- if (argc > 0 && strcmp("-", argv[0])) {
- if (!(inf = fopen(argv[0], "r"))) {
- fprintf(stderr, "mcompress: can't open \"%s\" for reading\n", argv[0]);
- return 1;
- }
- if (!decomp && argc == 1) {
- strcpy(buf, argv[0]);
- strcat(buf, ".Z");
- if (outf = fopen(buf, "r")) {
- fclose(outf);
- fprintf(stderr, "mcompress: will not overwrite \"%s\"\n", buf);
- return 1;
- }
- argv[1] = buf;
- argc++;
- }
- }
- if (argc > 1 && strcmp("-", argv[1])) {
- if (!(outf = fopen(argv[1], "w"))) {
- fprintf(stderr, "mcompress: can't open \"%s\" for writing\n", argv[1]);
- return 1;
- }
- }
- if (decomp) {
- if (header) {
- if (getc(inf) != 037 || getc(inf) != 0235 || (n = getc(inf)) == EOF) {
- fprintf(stderr, "mcompress: That's not a compressed file!\n");
- return 1;
- }
- nbits = n & 0x1F;
- block_compress = n & 0x80;
- }
- if (!(cstream = uncompinit(n & 0x1F, n & 0x80, stdread, inf))) {
- fprintf(stderr, "mcompress: not enough memory\n");
- return 1;
- }
- while ((n=uncompread(cstream, sizeof(buf), buf)) > 0) {
- #ifdef macintosh
- SpinCursor(1);
- if (unix) {
- /* convert LF->CRs */
- for (i = 0; i < sizeof(buf); i++)
- if ( *(buf + i) == '\r' )
- *(buf + i) = '\n';
- }
- fwrite(buf, 1, n, outf);
- }
- #endif macintosh
- uncompclose(cstream);
- #ifdef macintosh
- if (unix && outf != stdout ) {
- FInfo fInfoRec;
-
- c2pstr (argv[1]);
- (void) GetFInfo ((StringPtr) argv[1], 0, &fInfoRec);
- fInfoRec.fdType = 'TEXT';
- fInfoRec.fdCreator = 'MPS ';
- (void) SetFInfo ((StringPtr)argv[1], 0, &fInfoRec);
- }
- #endif macintosh
- }
- else {
- if (header) {
- buf[0] = 037;
- buf[1] = 0235;
- buf[2] = nbits;
- if (block_compress) buf[2] |= 0x80;
- stdwrite(outf, 3, buf);
- }
- if (!(cstream = compinit(nbits, block_compress, stdwrite, outf))) {
- fprintf(stderr, "mcompress: not enough memory\n");
- return 1;
- }
- while ((n=fread(buf, 1, sizeof(buf), inf)) > 0) {
- #ifdef macintosh
- SpinCursor(1);
- if (unix) {
- /* convert CR->LFs */
- for (i = 0; i < sizeof(buf); i++)
- if ( *(buf + i) == '\n' )
- *(buf + i) = '\r';
- }
- compwrite(cstream, n, buf);
- }
- #endif macintosh
- compclose(cstream);
- }
- fflush(outf);
- if (ferror(inf) || ferror(outf)) {
- fprintf(stderr, "mcompress: I/O error\n");
- return 2;
- }
- if (inf != stdin) fclose(inf);
- if (outf != stdout) fclose(outf);
- return 0;
-
- badarg:
- fprintf(stderr, "mcompress v1.0, by gz@entity.com. Usage:\n\
- mcompress [-dCnH] [-b maxbits] [inputfile [outputfile]]\n\
- -d = Do decompression rather than compression.\n\
- -b maxbits = max number of bits/code. Must be between 9 and 16.\n\
- The default is 16. Smaller values give worse results but require\n\
- less memory at runtime.\n\
- This option cannot be given for decompression unless -n is also specified.\n\
- -C = Do not perform 'block compression' (use only for compatibility with\n");
- fprintf(stderr, "\
- obsolete versions of unix compress).\n\
- -n = Do not create/expect compress file header (for obsolete compatibility)\n\
- If this option is specified for decompression, -b must also be given.\n\
- -u = Convert CR/LFs to/from Unix text files\n");
- fprintf(stderr, "\
- -H = show this help message\n\n\
- If no files are specified, reads standard input and writes standard output.\n\n\
- If inputfile is specified but no outputfile, the output defaults as follows:\n\
- for compress: the input filename with \".Z\" appended.\n\
- for decompress: standard output.\n\
- Only the data fork of the input file is processed.\n\n");
- fprintf(stderr, "\
- Note that unlike the unix version, the input file is NOT deleted.\n\n\
- This is freeware. If you paid money for this program, you got ripped off.\n");
- return 2;
- }
-
-