📄 gzip.c
字号:
} DBGPrintfo(("version(out)\r\n"));}#ifdef __TURBOC__extern ush ptr_offset ;#endifextern int pkzip; /* set for a pkzip file */extern int ext_header; /* set if extended local header */extern int in_exit;//extern char *nextchar;char *nextchar;extern int opterr;//extern int first_nonopt;//extern int last_nonopt;int first_nonopt;int last_nonopt;void InitVariables(void){ optind = 0; optarg = 0; ascii = 0; /* convert end-of-lines to local OS conventions */ to_stdout = 0; /* output to stdout (-c) */ decompress = 0; /* decompress (-d) */ force = 0; /* don't ask questions, compress links (-f) */ no_name = -1; /* don't save or restore the original file name */ no_time = -1; /* don't save or restore the original file time */ recursive = 0; /* recurse through directories (-r) */ list = 0; /* list the file contents (-l) */ verbose = 0; /* be verbose (-v) */ quiet = 0; /* be very quiet (-q) */ do_lzw = 0; /* generate output compatible with old compress (-Z) */ test = 0; /* test .gz file integrity */ maxbits = BITS; /* max bits per code for LZW */ method = DEFLATED; /* compression method */ level = 6; /* compression level */ exit_code = OK; /* program exit code */ args = NULL; /* argv pointer if GZIP env variable defined */ total_in = 0; /* input bytes for all files */ total_out = 0; /* output bytes for all files */ remove_ofname = 0; /* remove output file on error */ pkzip = 0; /* set for a pkzip file */ ext_header = 0; /* set if extended local header */ in_exit = 0; nextchar = 0; opterr = 1; first_nonopt = 0; last_nonopt = 0; work = zip; /* function to call */ ifd = 0; ofd = 0;#ifdef __TURBOC__ ptr_offset = 0;#endif}char *in_buf;int in_size;int in_rd_count;char *out_buf;int out_buf_size;int *out_size;int *outUnitSize = 0;char **pout;extern int GetFreeErrCountValue2(void);/* ======================================================================== *///int main (argc, argv) //int argc; //char **argv;int gzip (int argc, char **argv, char *in, int isize, char **out, int *outBufUnit, int *osize){ DBGPrintfi(("main(In)\r\n")); { int file_count; /* number of files to precess */ int proglen; /* length of progname */ int optc; /* current option */ in_buf = in; in_size = isize; in_rd_count = 0; pout = out; outUnitSize = outBufUnit; *pout = 0; out_size = osize; InitVariables(); EXPAND(argc, argv); /* wild card expansion if necessary */ progname = basename(argv[0]); proglen = (int)strlen(progname); /* Suppress .exe for MSDOS, OS/2 and VMS: */ if (proglen > 4 && strequ(progname+proglen-4, ".exe")) { progname[proglen-4] = '\0'; } /* Add options in GZIP environment variable if there is one */ env = add_envopt(&argc, &argv, OPTIONS_VAR); if ( env == (char *)-1 ) { DBGPrintfo(("main(out17)\r\n")); return -1; } if (env != NULL) args = argv; foreground = signal(SIGINT, SIG_IGN) != SIG_IGN; if (foreground) { (void) signal (SIGINT, (sig_type)abort_gzip); }#ifdef SIGTERM if (signal(SIGTERM, SIG_IGN) != SIG_IGN) { (void) signal(SIGTERM, (sig_type)abort_gzip); }#endif#ifdef SIGHUP if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { (void) signal(SIGHUP, (sig_type)abort_gzip); }#endif#ifndef GNU_STANDARD /* For compatibility with old compress, use program name as an option. * If you compile with -DGNU_STANDARD, this program will behave as * gzip even if it is invoked under the name gunzip or zcat. * * Systems which do not support links can still use -d or -dc. * Ignore an .exe extension for MSDOS, OS/2 and VMS. */ if ( strncmp(progname, "un", 2) == 0 /* ungzip, uncompress */ || strncmp(progname, "gun", 3) == 0) { /* gunzip */ decompress = 1; } else if (strequ(progname+1, "cat") /* zcat, pcat, gcat */ || strequ(progname, "gzcat")) { /* gzcat */ decompress = to_stdout = 1; }#endif strncpy(z_suffix, Z_SUFFIX, sizeof(z_suffix)-1); z_len = (int)strlen(z_suffix); while ((optc = getopt_long (argc, argv, "ab:cdfhH?lLmMnNqrS:tvVZ123456789", longopts, (int *)0)) != EOF) { switch (optc) { case 'a': ascii = 1; break; case 'b': maxbits = atoi(optarg); break; case 'c': to_stdout = 1; break; case 'd': decompress = 1; break; case 'f': force++; break; case 'h': case 'H': case '?': help(); do_exit(OK); return 0; // by bskim case 'l': list = decompress = to_stdout = 1; break; case 'L': license(); do_exit(OK); return 0; case 'm': /* undocumented, may change later */ no_time = 1; break; case 'M': /* undocumented, may change later */ no_time = 0; break; case 'n': no_name = no_time = 1; break; case 'N': no_name = no_time = 0; break; case 'q': quiet = 1; verbose = 0; break; case 'r':#ifdef NO_DIR fprintf(stderr, "%s: -r not supported on this system\n", progname); usage(); do_exit(ERROR); DBGPrintfo(("main(out16)\r\n")); return -1; // by bskim#else recursive = 1; break;#endif case 'S':#ifdef NO_MULTIPLE_DOTS if (*optarg == '.') optarg++;#endif z_len = (int)strlen(optarg); strcpy(z_suffix, optarg); break; case 't': test = decompress = to_stdout = 1; break; case 'v': verbose++; quiet = 0; break; case 'V': version(); do_exit(OK); DBGPrintfo(("main(out15)\r\n")); return 0; // by bskim case 'Z':#ifdef LZW do_lzw = 1; break;#else fprintf(stderr, "%s: -Z not supported in this version\n", progname); usage(); do_exit(ERROR); return -1; // by bskim#endif case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': level = optc - '0'; break; default: /* Error message already emitted by getopt_long. */ usage(); do_exit(ERROR); DBGPrintfo(("main(out14)\r\n")); return -1; } } /* loop on all arguments */ /* By default, save name and timestamp on compression but do not * restore them on decompression. */ if (no_time < 0) no_time = decompress; if (no_name < 0) no_name = decompress; file_count = argc - optind;#if O_BINARY#else if (ascii && !quiet) { fprintf(stderr, "%s: option --ascii ignored on this system\n", progname); }#endif if ((z_len == 0 && !decompress) || z_len > MAX_SUFFIX) { fprintf(stderr, "%s: incorrect suffix '%s'\n", progname, optarg); do_exit(ERROR); DBGPrintfo(("main(out13)\r\n")); return -1; } if (do_lzw && !decompress) work = lzw; /* Allocate all global buffers (for DYN_ALLOC option) */ ALLOC(uch, inbuf, INBUFSIZ +INBUF_EXTRA); ALLOC(uch, outbuf, OUTBUFSIZ+OUTBUF_EXTRA); ALLOC(ush, d_buf, DIST_BUFSIZE); ALLOC(uch, window, 2L*WSIZE);#ifndef MAXSEG_64K ALLOC(ush, tab_prefix, 1L<<BITS);#else ALLOC(ush, tab_prefix0, 1L<<(BITS-1)); ALLOC(ush, tab_prefix1, 1L<<(BITS-1));#endif /* And get to work */ if (file_count != 0) { if (to_stdout && !test && !list && (!decompress || !ascii)) { SET_BINARY_MODE(fileno(stdout)); } while (optind < argc) { if ( treat_file(argv[optind++]) < 0 ) { DBGPrintfo(("main(out12)\r\n")); exit_code = -1; goto exit_gzip; } } } else { /* Standard input */ if ( treat_stdin() < 0 ) { DBGPrintfo(("main(out11)\r\n")); exit_code = -1; goto exit_gzip; } } if (list && !quiet && file_count > 1) { if ( do_list(-1, -1) < 0 ) /* print totals */ { DBGPrintfo(("main(out10)\r\n")); exit_code = -1; goto exit_gzip; } } exit_gzip: do_exit(exit_code); DBGPrintfo(("main(out1)\r\n")); return exit_code; /* just to avoid lint warning */ }}/* ======================================================================== * Compress or decompress stdin */local int treat_stdin(){ DBGPrintfi(("treat_stdin(In)\r\n")); { strcpy(ifname, "stdin"); strcpy(ofname, "stdout"); /* Get the time stamp on the input file. */ time_stamp = 0; /* time unknown by default */ ifile_size = -1L; /* convention for unknown size */ clear_bufs(); /* clear input and output buffers */ to_stdout = 1; part_nb = 0; if (decompress) { method = get_method(ifd); if (method < 0) { do_exit(exit_code); /* error message already emitted */ DBGPrintfo(("main(out02)\r\n")); return -1; } } /* Actually do the compression/decompression. Loop over zipped members. */ for (;;) { int ret; ret = (*work)(0, 0); if ( ret != OK ) { DBGPrintfo(("treat_stdin(out2)\r\n")); return -1; } if (!decompress || last_member || inptr == insize) break; /* end of file */ method = get_method(ifd); if (method < 0) { DBGPrintfo(("treat_stdin(out21)\r\n")); return -1; /* error message already emitted */ } bytes_out = 0; /* required for length check */ } if (verbose) { if (test) { fprintf(stderr, " OK\n"); } else if (!decompress) { display_ratio(bytes_in-(bytes_out-header_bytes), bytes_in, stderr); fprintf(stderr, "\n");#ifdef DISPLAY_STDIN_RATIO } else { display_ratio(bytes_out-(bytes_in-header_bytes), bytes_out,stderr); fprintf(stderr, "\n");#endif } } } DBGPrintfo(("treat_stdin(out)\r\n")); return 0;}#if 0local int treat_stdin(){ DBGPrintfi(("treat_stdin(In)\r\n")); { if (!force && !list && isatty(fileno((FILE *)(decompress ? stdin : stdout)))) { /* Do not send compressed data to the terminal or read it from * the terminal. We get here when user invoked the program * without parameters, so be helpful. According to the GNU standards: * * If there is one behavior you think is most useful when the output * is to a terminal, and another that you think is most useful when * the output is a file or a pipe, then it is usually best to make * the default behavior the one that is useful with output to a * terminal, and have an option for the other behavior. * * Here we use the --force option to get the other behavior. */ fprintf(stderr, "%s: compressed data not %s a terminal. Use -f to force %scompression.\n", progname, decompress ? "read from" : "written to", decompress ? "de" : ""); fprintf(stderr,"For help, type: %s -h\n", progname); do_exit(ERROR); DBGPrintfo(("main(out0)\r\n")); return -1; } if (decompress || !ascii) { SET_BINARY_MODE(fileno(stdin)); } if (!test && !list && (!decompress || !ascii)) { SET_BINARY_MODE(fileno(stdout)); } strcpy(ifname, "stdin"); strcpy(ofname, "stdout"); /* Get the time stamp on the input file. */ time_stamp = 0; /* time unknown by default */#ifndef NO_STDIN_FSTAT if (list || !no_time) { if (fstat(fileno(stdin), &istat) != 0) { error("fstat(stdin)"); DBGPrintfo(("main(out01)\r\n")); return -1; }# ifdef NO_PIPE_TIMESTAMP if (S_ISREG(istat.st_mode))# endif time_stamp = istat.st_mtime;#endif /* NO_STDIN_FSTAT */ } ifile_size = -1L; /* convention for unknown size */ clear_bufs(); /* clear input and output buffers */ to_stdout = 1; part_nb = 0; if (decompress) { method = get_method(ifd); if (method < 0) { do_exit(exit_code); /* error message already emitted */ DBGPrintfo(("main(out02)\r\n")); return -1; } } if (list) { do_list(ifd, method); DBGPrintfo(("treat_stdin(out1)\r\n")); return 1; } /* Actually do the compression/decompression. Loop over zipped members. */ for (;;) { int ret; ret = (*work)(fileno(stdin), fileno(stdout)); if ( ret != OK ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -