Back to index

python3.2  3.2.2
Defines | Functions | Variables
minigzip.c File Reference
#include "zlib.h"
#include <stdio.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

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 131 of file minigzip.c.

#define GZ_SUFFIX   ".gz"

Definition at line 127 of file minigzip.c.

#define local

Definition at line 138 of file minigzip.c.

#define MAX_NAME_LEN   1024

Definition at line 132 of file minigzip.c.

#define SET_BINARY_MODE (   file)

Definition at line 40 of file minigzip.c.

#define SUFFIX_LEN   (sizeof(GZ_SUFFIX)-1)

Definition at line 129 of file minigzip.c.


Function Documentation

void error ( char *  msg) const

Definition at line 156 of file minigzip.c.

{
    fprintf(stderr, "%s: %s\n", prog, msg);
    exit(1);
}
void file_compress ( char *  file,
char *  mode 
)

Definition at line 262 of file minigzip.c.

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

    if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
        fprintf(stderr, "%s: filename too long\n", prog);
        exit(1);
    }

    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 297 of file minigzip.c.

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

    if (len + strlen(GZ_SUFFIX) >= sizeof(buf)) {
        fprintf(stderr, "%s: filename too long\n", prog);
        exit(1);
    }

    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 167 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 = (int)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 235 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 349 of file minigzip.c.

{
    int copyout = 0;
    int uncompr = 0;
    gzFile file;
    char *bname, outmode[20];

    strcpy(outmode, "wb6 ");

    prog = argv[0];
    bname = strrchr(argv[0], '/');
    if (bname)
      bname++;
    else
      bname = argv[0];
    argc--, argv++;

    if (!strcmp(bname, "gunzip"))
      uncompr = 1;
    else if (!strcmp(bname, "zcat"))
      copyout = uncompr = 1;

    while (argc > 0) {
      if (strcmp(*argv, "-c") == 0)
        copyout = 1;
      else 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 (strcmp(*argv, "-r") == 0)
        outmode[3] = 'R';
      else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
               (*argv)[2] == 0)
        outmode[2] = (*argv)[1];
      else
        break;
      argc--, argv++;
    }
    if (outmode[3] == ' ')
        outmode[3] = 0;
    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 {
        if (copyout) {
            SET_BINARY_MODE(stdout);
        }
        do {
            if (uncompr) {
                if (copyout) {
                    file = gzopen(*argv, "rb");
                    if (file == NULL)
                        fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
                    else
                        gz_uncompress(file, stdout);
                } else {
                    file_uncompress(*argv);
                }
            } else {
                if (copyout) {
                    FILE * in = fopen(*argv, "rb");

                    if (in == NULL) {
                        perror(*argv);
                    } else {
                        file = gzdopen(fileno(stdout), outmode);
                        if (file == NULL) error("can't gzdopen stdout");

                        gz_compress(in, file);
                    }

                } else {
                    file_compress(*argv, outmode);
                }
            }
        } while (argv++, --argc);
    }
    return 0;
}

Here is the call graph for this function:

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 141 of file minigzip.c.