Back to index

plt-scheme  4.2.1
Defines | Functions | Variables
minigzip.c File Reference
#include <stdio.h>
#include "zlib.h"

Go to the source code of this file.

Defines

#define SET_BINARY_MODE(file)
#define GZ_SUFFIX   ".gz"
#define SUFFIX_LEN   (sizeof(GZ_SUFFIX)-1)
#define BUFLEN   16384
#define MAX_NAME_LEN   1024
#define local

Functions

void exit OF ((int))
int unlink OF ((const char *))
void error OF ((const char *msg))
void gz_compress OF ((FILE *in, gzFile out))
void gz_uncompress OF ((gzFile in, FILE *out))
void file_compress OF ((char *file, char *mode))
void file_uncompress OF ((char *file))
int main OF ((int argc, char *argv[]))
void error (char *msg) const
void gz_compress (FILE *in, gzFile out)
void gz_uncompress (gzFile in, FILE *out)
void file_compress (char *file, char *mode)
void file_uncompress (char *file)
int main (int argc, argv)

Variables

char * prog

Define Documentation

#define BUFLEN   16384

Definition at line 64 of file minigzip.c.

#define GZ_SUFFIX   ".gz"

Definition at line 60 of file minigzip.c.

#define local

Definition at line 71 of file minigzip.c.

#define MAX_NAME_LEN   1024

Definition at line 65 of file minigzip.c.

#define SET_BINARY_MODE (   file)

Definition at line 39 of file minigzip.c.

#define SUFFIX_LEN   (sizeof(GZ_SUFFIX)-1)

Definition at line 62 of file minigzip.c.


Function Documentation

void error ( char *  msg) const

Definition at line 89 of file minigzip.c.

{
    fprintf(stderr, "%s: %s\n", prog, msg);
    exit(1);
}

Here is the caller graph for this function:

void file_compress ( char *  file,
char *  mode 
)

Definition at line 195 of file minigzip.c.

{
    local char outfile[MAX_NAME_LEN];
    FILE  *in;
    gzFile out;

    strcpy(outfile, file);
    strcat(outfile, GZ_SUFFIX);

    in = fopen(file, "rb");
    if (in == NULL) {
        perror(file);
        exit(1);
    }
    out = gzopen(outfile, mode);
    if (out == NULL) {
        fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
        exit(1);
    }
    gz_compress(in, out);

    unlink(file);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void file_uncompress ( char *  file)

Definition at line 225 of file minigzip.c.

{
    local char buf[MAX_NAME_LEN];
    char *infile, *outfile;
    FILE  *out;
    gzFile in;
    int len = strlen(file);

    strcpy(buf, file);

    if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
        infile = file;
        outfile = buf;
        outfile[len-3] = '\0';
    } else {
        outfile = file;
        infile = buf;
        strcat(infile, GZ_SUFFIX);
    }
    in = gzopen(infile, "rb");
    if (in == NULL) {
        fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
        exit(1);
    }
    out = fopen(outfile, "wb");
    if (out == NULL) {
        perror(file);
        exit(1);
    }

    gz_uncompress(in, out);

    unlink(infile);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void gz_compress ( FILE *  in,
gzFile  out 
)

Definition at line 100 of file minigzip.c.

{
    local char buf[BUFLEN];
    int len;
    int err;

#ifdef USE_MMAP
    /* Try first compressing with mmap. If mmap fails (minigzip used in a
     * pipe), use the normal fread loop.
     */
    if (gz_compress_mmap(in, out) == Z_OK) return;
#endif
    for (;;) {
        len = fread(buf, 1, sizeof(buf), in);
        if (ferror(in)) {
            perror("fread");
            exit(1);
        }
        if (len == 0) break;

        if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
    }
    fclose(in);
    if (gzclose(out) != Z_OK) error("failed gzclose");
}

Here is the call graph for this function:

Here is the caller graph for this function:

void gz_uncompress ( gzFile  in,
FILE *  out 
)

Definition at line 168 of file minigzip.c.

{
    local char buf[BUFLEN];
    int len;
    int err;

    for (;;) {
        len = gzread(in, buf, sizeof(buf));
        if (len < 0) error (gzerror(in, &err));
        if (len == 0) break;

        if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
           error("failed fwrite");
       }
    }
    if (fclose(out)) error("failed fclose");

    if (gzclose(in) != Z_OK) error("failed gzclose");
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
argv   
)

Definition at line 270 of file minigzip.c.

{
    int uncompr = 0;
    gzFile file;
    char outmode[20];

    strcpy(outmode, "wb6 ");

    prog = argv[0];
    argc--, argv++;

    while (argc > 0) {
      if (strcmp(*argv, "-d") == 0)
       uncompr = 1;
      else if (strcmp(*argv, "-f") == 0)
       outmode[3] = 'f';
      else if (strcmp(*argv, "-h") == 0)
       outmode[3] = 'h';
      else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
              (*argv)[2] == 0)
       outmode[2] = (*argv)[1];
      else
       break;
      argc--, argv++;
    }
    if (argc == 0) {
        SET_BINARY_MODE(stdin);
        SET_BINARY_MODE(stdout);
        if (uncompr) {
            file = gzdopen(fileno(stdin), "rb");
            if (file == NULL) error("can't gzdopen stdin");
            gz_uncompress(file, stdout);
        } else {
            file = gzdopen(fileno(stdout), outmode);
            if (file == NULL) error("can't gzdopen stdout");
            gz_compress(stdin, file);
        }
    } else {
        do {
            if (uncompr) {
                file_uncompress(*argv);
            } else {
                file_compress(*argv, outmode);
            }
        } while (argv++, --argc);
    }
    exit(0);
    return 0; /* to avoid warning */
}

Here is the call graph for this function:

void exit OF ( (int )
int unlink OF ( (const char *)  )
void error OF ( (const char *msg)  )
void gz_compress OF ( (FILE *in, gzFile out)  )
void gz_uncompress OF ( (gzFile in, FILE *out)  )
void file_compress OF ( (char *file, char *mode)  )
void file_uncompress OF ( (char *file )
int main OF ( (int argc, char *argv[])  )

Variable Documentation

char* prog

Definition at line 74 of file minigzip.c.