?? zip.c
字號(hào):
/* Copyright (C) 1990-1993 Mark Adler, Richard B. Wales, Jean-loup Gailly, Kai Uwe Rommel and Igor Mandrichenko. Permission is granted to any individual or institution to use, copy, or redistribute this software so long as all of the original files are included, that it is not sold for profit, and that this copyright notice is retained.*//* * zip.c by Mark Adler. */#include "revision.h"#include "zip.h"#include "crypt.h"#ifdef VMS# include "VMSmunch.h"#endif#if (defined(MSDOS) && !defined(__GO32__)) || defined(__human68k__)# include <process.h>#endif#include <signal.h>#ifdef MACOS# include <console.h>#endif#define MAXCOM 256 /* Maximum one-line comment size *//* Local option flags */#define DELETE 0#define ADD 1#define UPDATE 2#define FRESHEN 3local int action = ADD; /* one of ADD, UPDATE, FRESHEN, or DELETE */local int comadd = 0; /* 1=add comments for new files */local int zipedit = 0; /* 1=edit zip comment and all file comments */local int latest = 0; /* 1=set zip file time to time of latest file */local ulg before = 0; /* 0=ignore, else exclude files before this time */local int test = 0; /* 1=test zip file with unzip -t */local int tempdir = 0; /* 1=use temp directory (-b) *//* Temporary zip file name and file pointer */local char *tempzip;local FILE *tempzf;/* Local functions */local void freeup OF((void));local void leave OF((int));local void handler OF((int));local void license OF((void));local void help OF((void));local void zipstdout OF((void));local void check_zipfile OF((char *zipname));local void get_filters OF((int argc, char **argv)); int main OF((int, char **));local int count_args OF((char *s));local void envargs OF((int *Pargc, char ***Pargv, char *envstr));local void freeup()/* Free all allocations in the found list and the zfiles list */{ struct flist far *f; /* steps through found list */ struct zlist far *z; /* pointer to next entry in zfiles list */ for (f = found; f != NULL; f = fexpel(f)) ; while (zfiles != NULL) { z = zfiles->nxt; free((voidp *)(zfiles->name)); if (zfiles->zname != zfiles->name) free((voidp *)(zfiles->zname)); if (zfiles->ext) free((voidp *)(zfiles->extra)); if (zfiles->cext && zfiles->cextra != zfiles->extra) free((voidp *)(zfiles->cextra)); if (zfiles->com) free((voidp *)(zfiles->comment)); farfree((voidp far *)zfiles); zfiles = z; zcount--; }}local void leave(e)int e; /* exit code *//* Process -o and -m options (if specified), free up malloc'ed stuff, and exit with the code e. */{ int r; /* return value from trash() */ ulg t; /* latest time in zip file */ struct zlist far *z; /* pointer into zfile list */ /* If latest, set time to zip file to latest file in zip file */ if (latest && zipfile && strcmp(zipfile, "-")) { diag("changing time of zip file to time of latest file in it"); /* find latest time in zip file */ if (zfiles == NULL) warn("zip file is empty, can't make it as old as latest entry", ""); else { t = 0; for (z = zfiles; z != NULL; z = z->nxt) /* Ignore directories in time comparisons */ if (t < z->tim && z->zname[z->nam-1] != '/') t = z->tim; /* set modified time of zip file to that time */ if (t != 0) stamp(zipfile, t); else warn( "zip file has only directories, can't make it as old as latest entry", ""); } } if (tempath != NULL) { free((voidp *)tempath); tempath = NULL; } if (zipfile != NULL) { free((voidp *)zipfile); zipfile = NULL; } /* If dispose, delete all files in the zfiles list that are marked */ if (dispose) { diag("deleting files that were added to zip file"); if ((r = trash()) != ZE_OK) err(r, "was deleting moved files and directories"); } /* Done! */ freeup();#ifdef VMS exit(0);#else /* !VMS */ exit(e);#endif /* ?VMS */}void err(c, h)int c; /* error code from the ZE_ class */char *h; /* message about how it happened *//* Issue a message for the error, clean up files and memory, and exit. */{ static int error_level; if (error_level++ > 0) exit(0); /* avoid recursive err() */ if (h != NULL) { if (PERR(c)) perror("zip error"); fflush(mesg); fprintf(stderr, "\nzip error: %s (%s)\n", errors[c-1], h); } if (tempzip != NULL) { if (tempzip != zipfile) { if (tempzf != NULL) fclose(tempzf);#ifndef DEBUG destroy(tempzip);#endif free((voidp *)tempzip); } else { /* -g option, attempt to restore the old file */ int k = 0; /* keep count for end header */ ulg cb = cenbeg; /* get start of central */ struct zlist far *z; /* steps through zfiles linked list */ fprintf(stderr, "attempting to restore %s to its previous state\n", zipfile); fseek(tempzf, cenbeg, SEEK_SET); tempzn = cenbeg; for (z = zfiles; z != NULL; z = z->nxt) { putcentral(z, tempzf); tempzn += 4 + CENHEAD + z->nam + z->cext + z->com; k++; } putend(k, tempzn - cb, cb, zcomlen, zcomment, tempzf); tempzf = NULL; fclose(tempzf); } } if (key != NULL) free((voidp *)key); if (tempath != NULL) free((voidp *)tempath); if (zipfile != NULL) free((voidp *)zipfile); freeup();#ifdef VMS c = 0;#endif exit(c);}void error(h) char *h;/* Internal error, should never happen */{ err(ZE_LOGIC, h);}local void handler(s)int s; /* signal number (ignored) *//* Upon getting a user interrupt, turn echo back on for tty and abort cleanly using err(). */{#if !defined(MSDOS) && !defined(__human68k__) echon(); putc('\n', stderr);#endif /* !MSDOS */ err(ZE_ABORT, "aborting"); s++; /* keep some compilers happy */}void warn(a, b)char *a, *b; /* message strings juxtaposed in output *//* Print a warning message to stderr and return. */{ if (noisy) fprintf(stderr, "zip warning: %s%s\n", a, b);}local void license()/* Print license information to stdout. */{ extent i; /* counter for copyright array */ for (i = 0; i < sizeof(copyright)/sizeof(char *); i++) { printf(copyright[i], "zip"); putchar('\n'); } for (i = 0; i < sizeof(disclaimer)/sizeof(char *); i++) puts(disclaimer[i]);}local void help()/* Print help (along with license info) to stdout. */{ extent i; /* counter for help array */ /* help array */ static char *text[] = {"","Zip %s (%s). Usage:","zip [-options] [-b path] [-t mmddyy] [-n suffixes] [zipfile list] [-xi list]"," The default action is to add or replace zipfile entries from list, which"," can include the special name - to compress standard input."," If zipfile and list are omitted, zip compresses stdin to stdout."," -f freshen: only changed files -u update: only changed or new files"," -d delete entries in zipfile -m move into zipfile (delete files)"," -k simulate PKZIP made zipfile -g allow growing existing zipfile"," -r recurse into directories -j junk (don't record) directory names"," -0 store only -l convert LF to CR LF (-ll CR LF to LF)"," -1 compress faster -9 compress better"," -q quiet operation -v verbose operation"," -c add one-line comments -z add zipfile comment"," -b use \"path\" for temp file -t only do files after \"mmddyy\""," -@ read names from stdin -o make zipfile as old as latest entry"," -x exclude the following names -i include only the following names",#ifdef VMS" \"-F\" fix zipfile(-FF try harder) \"-D\" do not add directory entries"," \"-T\" test zipfile integrity \"-L\" show software license"," -w append the VMS version number to the name stored in the zip file"," \"-V\" save VMS file attributes",#else" -F fix zipfile (-FF try harder) -D do not add directory entries"," -T test zipfile integrity -L show software license",#endif /* VMS */#ifdef OS2" -E use the .LONGNAME Extended attribute (if found) as filename",#endif /* OS2 */#ifdef S_IFLNK" -y store symbolic links as the link instead of the referenced file",#endif /* !S_IFLNK */#if defined(MSDOS) || defined(OS2)" -$ include volume label -S include system and hidden files",#endif#ifdef CRYPT" -e encrypt (-ee verify key) -n don't compress these suffixes"#else" -h show this help -n don't compress these suffixes"#endif }; for (i = 0; i < sizeof(copyright)/sizeof(char *); i++) { printf(copyright[i], "zip"); putchar('\n'); } for (i = 0; i < sizeof(text)/sizeof(char *); i++) { printf(text[i], VERSION, REVDATE); putchar('\n'); }}/* Do command line expansion for MSDOS, VMS, ATARI, and AMIGA but not OS/2 */#if defined(MSVMS) || defined(AMIGA)# define PROCNAME(n) (action==ADD||action==UPDATE?wild(n):procname(n))#else /* !MSVMS */# define PROCNAME(n) procname(n)#endif /* ?MSVMS */local void zipstdout()/* setup for writing zip file on stdout */{ int r; mesg = stderr; if (isatty(1)) err(ZE_PARMS, "cannot write zip file to terminal"); if ((zipfile = malloc(4)) == NULL) err(ZE_MEM, "was processing arguments"); strcpy(zipfile, "-"); if ((r = readzipfile()) != ZE_OK) err(r, zipfile);}local void check_zipfile(zipname) char *zipname; /* Invoke unzip -t on the given zip file */{#if (defined(MSDOS) && !defined(__GO32__)) || defined(__human68k__) if (spawnlp(P_WAIT, "unzip", "unzip", verbose ? "-t" : "-tqq", zipname, NULL)) {#else char cmd[128]; strcpy(cmd, "unzip -t "); if (!verbose) strcat(cmd, "-qq "); strcat(cmd, zipname);# ifdef VMS if (!system(cmd)) {# else if (system(cmd)) {# endif#endif fprintf(mesg, "test of %s FAILED\n", zipfile); err(ZE_TEST, "original files unmodified"); } if (noisy) fprintf(mesg, "test of %s OK\n", zipfile);}local void get_filters(argc, argv) int argc; /* number of tokens in command line */ char **argv; /* command line tokens *//* Counts number of -i or -x patterns, sets patterns and pcount */{ int i; int flag = 0; pcount = 0; for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { if (strrchr(argv[i], 'i') != NULL) { flag = 'i'; } else if (strrchr(argv[i], 'x') != NULL) { flag = 'x'; } else { flag = 0; } } else if (flag) { if (patterns != NULL) { patterns[pcount].zname = ex2in(argv[i], 0, (int *)NULL); patterns[pcount].select = flag; if (flag == 'i') icount++; } pcount++; } } if (pcount == 0 || patterns != NULL) return; patterns = (struct plist*) malloc(pcount * sizeof(struct plist)); if (patterns == NULL) err(ZE_MEM, "was creating pattern list"); get_filters(argc, argv);}int main(argc, argv)int argc; /* number of tokens in command line */char **argv; /* command line tokens *//* Add, update, freshen, or delete zip entries in a zip file. See the command help in help() above. */{ int a; /* attributes of zip file */ ulg c; /* start of central directory */ int d; /* true if just adding to a zip file */ char *e; /* malloc'd comment buffer */ struct flist far *f; /* steps through found linked list */ int i; /* arg counter, root directory flag */ int k; /* next argument type, marked counter, comment size, entry count */ ulg n; /* total of entry len's */ int o; /* true if there were any ZE_OPEN errors */ char *p; /* steps through option arguments */ char *pp; /* temporary pointer */ int r; /* temporary variable */ int s; /* flag to read names from stdin */ ulg t; /* file time, length of central directory */ struct zlist far *v; /* temporary variable */ struct zlist far * far *w; /* pointer to last link in zfiles list */
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -