📄 zip.c
字号:
FILE *x, *y; /* input and output zip files */ struct zlist far *z; /* steps through zfiles linked list */ char *zipbuf; /* stdio buffer for the zip file */ FILE *comment_stream; /* set to stderr if anything is read from stdin */#if defined(__IBMC__) && defined(__DEBUG_ALLOC__) { extern void DebugMalloc(void); atexit(DebugMalloc); }#endif mesg = (FILE *) stdout; /* cannot be made at link time for VMS */ comment_stream = (FILE *)stdin; init_upper(); /* build case map table */#ifdef MACOS argc = ccommand(&argv);#endif /* Process arguments */ diag("processing arguments"); if (argc == 1 && isatty(1)) { help(); exit(0); } envargs(&argc, &argv, "ZIPOPT"); /* get options from environment */ zipfile = tempzip = NULL; tempzf = NULL; d = 0; /* disallow adding to a zip file */ signal(SIGINT, handler);#ifdef SIGTERM /* AMIGADOS and others have no SIGTERM */ signal(SIGTERM, handler);#endif k = 0; /* Next non-option argument type */ s = 0; /* set by -@ if -@ is early */ get_filters(argc, argv); /* scan first the -x and -i patterns */ for (i = 1; i < argc; i++) { if (argv[i][0] == '-') if (argv[i][1]) for (p = argv[i]+1; *p; p++) switch(*p) { case '0': method = STORE; level = 0; break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': /* Set the compression efficacy */ level = *p - '0'; break; case 'b': /* Specify path for temporary file */ tempdir = 1; if (k != 0) err(ZE_PARMS, "use -b before zip file name"); else k = 1; /* Next non-option is path */ break; case 'c': /* Add comments for new files in zip file */ comadd = 1; break; case 'd': /* Delete files from zip file */ if (action != ADD) err(ZE_PARMS, "specify just one action"); action = DELETE; break; case 'D': /* Do not add directory entries */ dirnames = 0; break; case 'e': /* Encrypt */#ifndef CRYPT err(ZE_PARMS, "encryption not supported");#else /* CRYPT */ e = key == NULL ? (char *)NULL : key; if ((key = malloc(PWLEN+1)) == NULL) err(ZE_MEM, "was getting encryption password"); if (getp(e == NULL ? "Enter password: " : "Verify password: ", key, PWLEN+1) == NULL) err(ZE_PARMS, "stderr is not a tty"); if (e != NULL) { r = strcmp(key, e); free((voidp *)e); if (r) err(ZE_PARMS, "password not verified"); }#endif /* ?CRYPT */ break; case 'F': /* fix the zip file */ fix++; latest = 1; break; case 'f': /* Freshen zip file--overwrite only */ if (action != ADD) err(ZE_PARMS, "specify just one action"); action = FRESHEN; break; case 'g': /* Allow appending to a zip file */ d = 1; break; case 'h': case 'H': case '?': /* Help */ help(); leave(ZE_OK); case 'j': /* Junk directory names */ pathput = 0; break; case 'k': /* Make entries using DOS names (k for Katz) */ dosify = 1; break; case 'l': /* Translate end-of-line */ translate_eol++; break; case 'L': /* Show license, version */ license(); leave(ZE_OK); case 'm': /* Delete files added or updated in zip file */ dispose = 1; break; case 'n': /* Don't compress files with a special suffix */ special = NULL; /* will be set at next argument */ break; case 'o': /* Set zip file time to time of latest file in it */ latest = 1; break; case 'p': /* Store path with name */ break; /* (do nothing as annoyance avoidance) */ case 'q': /* Quiet operation */ noisy = 0; break; case 'r': /* Recurse into subdirectories */ recurse = 1; break; case 'S': hidden_files = 1; break; case 't': /* Exclude files earlier than specified date */ if (before) err(ZE_PARMS, "can only have one -t"); k = 2; break; case 'T': /* test zip file */ test = 1; break; case 'u': /* Update zip file--overwrite only if newer */ if (action != ADD) err(ZE_PARMS, "specify just one action"); action = UPDATE; break; case 'v': /* Mention oddities in zip file structure */ verbose++; break;#ifdef VMS case 'V': /* Store in VMS format */ vms_native = 1; break; case 'w': /* Append the VMS version number */ vmsver = 1; break;#endif /* VMS */ case 'i': /* Include only the following files */ case 'x': /* Exclude following files */ if (k != 4 && (k != 3 || (action != UPDATE && action != FRESHEN))) err(ZE_PARMS, "nothing to select from"); k = 5; break;#ifdef S_IFLNK case 'y': /* Store symbolic links as such */ linkput = 1; break;#endif /* S_IFLNK */ case 'z': /* Edit zip file comment */ zipedit = 1; break;#if defined(MSDOS) || defined(OS2) case '$': /* Include volume label */ volume_label = 1; break;#endif case '@': /* read file names from stdin */ comment_stream = NULL; if (k < 3) /* zip file not read yet */ s = 1; /* defer -@ until after zipfile read */ else if (strcmp(zipfile, "-") == 0) err(ZE_PARMS, "can't use - and -@ together"); else /* zip file read--do it now */ while ((pp = getnam(errbuf)) != NULL) { if ((r = PROCNAME(pp)) != ZE_OK) if (r == ZE_MISS) warn("name not matched: ", pp); else err(r, pp); } break;#ifdef OS2 case 'E': /* use the .LONGNAME EA (if any) as the file's name. */ use_longname_ea = 1; break;#endif default: { sprintf(errbuf, "no such option: %c", *p); err(ZE_PARMS, errbuf); } } else /* just a dash */ switch (k) { case 0: zipstdout(); k = 3; if (s) err(ZE_PARMS, "can't use - and -@ together"); break; case 1: err(ZE_PARMS, "invalid path"); break; case 2: err(ZE_PARMS, "invalid time"); break; case 3: case 4: comment_stream = NULL; if ((r = PROCNAME(argv[i])) != ZE_OK) if (r == ZE_MISS) warn("name not matched: ", argv[i]); else err(r, argv[i]); if (k == 3) k = 4; } else /* not an option */ { if (special == NULL) special = argv[i]; else if (k == 5) break; /* -i and -x arguments already scanned */ else switch (k) { case 0: if ((zipfile = ziptyp(argv[i])) == NULL) err(ZE_MEM, "was processing arguments"); if ((r = readzipfile()) != ZE_OK) err(r, zipfile); k = 3; if (s) { while ((pp = getnam(errbuf)) != NULL) { if ((r = PROCNAME(pp)) != ZE_OK) if (r == ZE_MISS) warn("name not matched: ", pp); else err(r, pp); } s = 0; } break; case 1: if ((tempath = malloc(strlen(argv[i]) + 1)) == NULL) err(ZE_MEM, "was processing arguments"); strcpy(tempath, argv[i]); k = 0; break; case 2: { int yy, mm, dd; /* results of sscanf() */ if (sscanf(argv[i], "%2d%2d%2d", &mm, &dd, &yy) != 3 || mm < 1 || mm > 12 || dd < 1 || dd > 31) err(ZE_PARMS, "invalid date entered for -t option"); before = dostime(yy + (yy < 80 ? 2000 : 1900), mm, dd, 0, 0, 0); k = 0; break; } case 3: case 4: if ((r = PROCNAME(argv[i])) != ZE_OK) if (r == ZE_MISS) warn("name not matched: ", argv[i]); else err(r, argv[i]); if (k == 3) k = 4; } } } if (k < 3) { /* zip used as filter */ zipstdout(); comment_stream = NULL; if ((r = procname("-")) != ZE_OK) if (r == ZE_MISS) warn("name not matched: ", "-"); else err(r, "-"); k = 4; if (s) err(ZE_PARMS, "can't use - and -@ together"); } /* Clean up selections */ if (k == 3 && (action == UPDATE || action == FRESHEN)) { for (z = zfiles; z != NULL; z = z->nxt) { /* if -u or -f with no args, do all */ z->mark = pcount ? filter(z->zname) : 1; } } if ((r = check_dup()) != ZE_OK) /* remove duplicates in found list */ if (r == ZE_PARMS) err(r, "cannot repeat names in zip file"); else err(r, "was processing list of files"); if (zcount) free((voidp *)zsort); /* Check option combinations */ if (special == NULL) err(ZE_PARMS, "missing suffix list"); if (level == 9 || !strcmp(special, ";") || !strcmp(special, ":")) special = NULL; /* compress everything */ if (action == DELETE && (method != BEST || dispose || recurse || key != NULL || comadd || zipedit)) err(ZE_PARMS, "invalid option(s) used with -d"); if (linkput && dosify) { warn("can't use -y with -k, -y ignored", ""); linkput = 0; } if (test && !strcmp(zipfile, "-")) { warn("can't use -T on stdout, -T ignored", ""); } if ((action != ADD || d) && !strcmp(zipfile, "-")) err(ZE_PARMS, "can't use -d,-f,-u or -g on stdout\n");#ifdef VMS if (vms_native && translate_eol) err(ZE_PARMS, "can't use -V with -l");#endif if (zcount == 0 && (action != ADD || d)) warn(zipfile, " not found or empty"); /* If -b not specified, make temporary path the same as the zip file */#if defined(MSDOS) || defined(__human68k__) || defined(AMIGA) if (tempath == NULL && ((p = strrchr(zipfile, '/')) != NULL ||# ifdef MSDOS (p = strrchr(zipfile, '\\')) != NULL ||# endif (p = strrchr(zipfile, ':')) != NULL)) { if (*p == ':') p++;#else if (tempath == NULL && (p = strrchr(zipfile, '/')) != NULL) {#endif if ((tempath = malloc((int)(p - zipfile) + 1)) == NULL) err(ZE_MEM, "was processing arguments"); r = *p; *p = 0; strcpy(tempath, zipfile); *p = (char)r; } /* For each marked entry, if not deleting, check if it exists, and if updating or freshening, compare date with entry in old zip file. Unmark if it doesn't exist or is too old, else update marked count. */ diag("stating marked entries"); k = 0; /* Initialize marked count */ for (z = zfiles; z != NULL; z = z->nxt) if (z->mark) { if (action != DELETE && ((t = filetime(z->name, (ulg *)NULL, (long *)NULL)) == 0 || t < before || ((action == UPDATE || action == FRESHEN) && t <= z->tim))) { z->mark = 0; z->trash = t && t >= before; /* delete if -um or -fm */ if (verbose) { fprintf(mesg, "zip diagnostic: %s %s\n", z->name, z->trash ? "up to date" : "missing or early"); } } else k++;#if 0 fprintf(mesg, "t %ld, z->tim %ld, t-tim %ld\n", t, z->tim, t-z->tim); /* ??? */#endif } /* Remove entries from found list that do not exist or are too old */ diag("stating new entries"); for (f = found; f != NULL;) if (action == DELETE || action == FRESHEN || (t = filetime(f->name, (ulg *)NULL, (long *)NULL)) == 0 || t < before || (namecmp(f->name, zipfile) == 0 && strcmp(zipfile, "-"))) f = fexpel(f); else f = f->nxt; /* Make sure there's something left to do */ if (k == 0 && found == NULL && !(zfiles != NULL && (latest || zipedit))) { if (test && (zfiles != NULL || zipbeg != 0)) { check_zipfile(zipfile); leave(ZE_OK); } if (action == UPDATE || action == FRESHEN) leave(ZE_OK); else if (zfiles == NULL && latest) err(ZE_NAME, zipfile); else err(ZE_NONE, zipfile); } d = (d && k == 0 && (zipbeg || zfiles != NULL)); /* d true if appending */ /* Before we get carried away, make sure zip file is writeable. This * has the undesired side effect of leaving one empty junk file on a WORM, * so when the zipfile does not exist already and when -b is specified, * the writability check is made in replace(). */ if (strcmp(zipfile, "-")) { if (tempdir && zfiles == NULL && zipbeg == 0) { a = 0; } else { x = zfiles == NULL && zipbeg == 0 ? fopen(zipfile, FOPW) : fopen(zipfile, FOPM); /* Note: FOPW and FOPM expand to several parameters for VMS */ if (x == NULL) err(ZE_CREAT, zipfile); fclose(x); a = getfileattr(zipfile); if (zfiles == NULL && zipbeg == 0) destroy(zipfile); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -