📄 fileio.c
字号:
/* Copyright (c) 1990-2005 Info-ZIP. All rights reserved. See the accompanying file LICENSE, version 2005-Feb-10 or later (the contents of which are also included in zip.h) for terms of use. If, for some reason, all these files are missing, the Info-ZIP license also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html*//* * fileio.c by Mark Adler */#define __FILEIO_C#include "zip.h"#ifdef MACOS# include "helpers.h"#endif#include <time.h>#ifdef NO_MKTIMEtime_t mktime OF((struct tm *));#endif#ifdef OSF#define EXDEV 18 /* avoid a bug in the DEC OSF/1 header files. */#else#include <errno.h>#endif#ifdef NO_ERRNOextern int errno;#endif#if defined(VMS) || defined(TOPS20)# define PAD 5#else# define PAD 0#endif#ifdef NO_RENAMEint rename OF((ZCONST char *, ZCONST char *));#endif#ifndef UTIL /* the companion #endif is a bit of ways down ... *//* Local functions */local int fqcmp OF((ZCONST zvoid *, ZCONST zvoid *));local int fqcmpz OF((ZCONST zvoid *, ZCONST zvoid *));/* Local module level variables. */char *label = NULL; /* global, but only used in `system'.c */local struct stat zipstatb;#if (!defined(MACOS) && !defined(WINDLL))local int zipstate = -1;#elseint zipstate;#endif/* -1 unknown, 0 old zip file exists, 1 new zip file */char *getnam(n, fp)char *n; /* where to put name (must have >=FNMAX+1 bytes) */FILE *fp;/* Read a \n or \r delimited name from stdin into n, and return n. If EOF, then return NULL. Also, if the name read is too big, return NULL. */{ int c; /* last character read */ char *p; /* pointer into name area */ p = n; while ((c = getc(fp)) == '\n' || c == '\r') ; if (c == EOF) return NULL; do { if (p - n >= FNMAX) return NULL; *p++ = (char) c; c = getc(fp); } while (c != EOF && (c != '\n' && c != '\r'));#ifdef WIN32/* * WIN32 strips off trailing spaces and periods in filenames * XXX what about a filename that only consists of spaces ? * Answer: on WIN32, a filename must contain at least one non-space char */ while (p > n) { if ((c = p[-1]) != ' ' && c != '.') break; --p; }#endif *p = 0; return n;}struct flist far *fexpel(f)struct flist far *f; /* entry to delete *//* Delete the entry *f in the doubly-linked found list. Return pointer to next entry to allow stepping through list. */{ struct flist far *t; /* temporary variable */ t = f->nxt; *(f->lst) = t; /* point last to next, */ if (t != NULL) t->lst = f->lst; /* and next to last */ if (f->name != NULL) /* free memory used */ free((zvoid *)(f->name)); if (f->zname != NULL) free((zvoid *)(f->zname)); if (f->iname != NULL) free((zvoid *)(f->iname)); farfree((zvoid far *)f); fcount--; /* decrement count */ return t; /* return pointer to next */}local int fqcmp(a, b)ZCONST zvoid *a, *b; /* pointers to pointers to found entries *//* Used by qsort() to compare entries in the found list by name. */{ return strcmp((*(struct flist far **)a)->name, (*(struct flist far **)b)->name);}local int fqcmpz(a, b)ZCONST zvoid *a, *b; /* pointers to pointers to found entries *//* Used by qsort() to compare entries in the found list by iname. */{ return strcmp((*(struct flist far **)a)->iname, (*(struct flist far **)b)->iname);}char *last(p, c)char *p; /* sequence of path components */int c; /* path components separator character *//* Return a pointer to the start of the last path component. For a directory * name terminated by the character in c, the return value is an empty string. */{ char *t; /* temporary variable */ if ((t = strrchr(p, c)) != NULL) return t + 1; else#ifndef AOS_VS return p;#else/* We want to allow finding of end of path in either AOS/VS-style pathnames * or Unix-style pathnames. This presents a few little problems ... */ { if (*p == '=' || *p == '^') /* like ./ and ../ respectively */ return p + 1; else return p; }#endif}char *msname(n)char *n;/* Reduce all path components to MSDOS upper case 8.3 style names. */{ int c; /* current character */ int f; /* characters in current component */ char *p; /* source pointer */ char *q; /* destination pointer */ p = q = n; f = 0; while ((c = (unsigned char)*POSTINCSTR(p)) != 0) if (c == ' ' || c == ':' || c == '"' || c == '*' || c == '+' || c == ',' || c == ';' || c == '<' || c == '=' || c == '>' || c == '?' || c == '[' || c == ']' || c == '|') continue; /* char is discarded */ else if (c == '/') { *POSTINCSTR(q) = (char)c; f = 0; /* new component */ }#ifdef __human68k__ else if (ismbblead(c) && *p) { if (f == 7 || f == 11) f++; else if (*p && f < 12 && f != 8) { *q++ = c; *q++ = *p++; f += 2; } }#endif /* __human68k__ */ else if (c == '.') { if (f == 0) continue; /* leading dots are discarded */ else if (f < 9) { *POSTINCSTR(q) = (char)c; f = 9; /* now in file type */ } else f = 12; /* now just excess characters */ } else if (f < 12 && f != 8) { f += CLEN(p); /* do until end of name or type */ *POSTINCSTR(q) = (char)(to_up(c)); } *q = 0; return n;}int check_dup()/* Sort the found list and remove duplicates. Return an error code in the ZE_ class. */{ struct flist far *f; /* steps through found linked list */ extent j, k; /* indices for s */ struct flist far **s; /* sorted table */ struct flist far **nodup; /* sorted table without duplicates */ /* sort found list, remove duplicates */ if (fcount) { extent fl_size = fcount * sizeof(struct flist far *); if ((fl_size / sizeof(struct flist far *)) != fcount || (s = (struct flist far **)malloc(fl_size)) == NULL) return ZE_MEM; for (j = 0, f = found; f != NULL; f = f->nxt) s[j++] = f; /* Check names as given (f->name) */ qsort((char *)s, fcount, sizeof(struct flist far *), fqcmp); for (k = j = fcount - 1; j > 0; j--) if (strcmp(s[j - 1]->name, s[j]->name) == 0) /* remove duplicate entry from list */ fexpel(s[j]); /* fexpel() changes fcount */ else /* copy valid entry into destination position */ s[k--] = s[j]; s[k] = s[0]; /* First entry is always valid */ nodup = &s[k]; /* Valid entries are at end of array s */ /* sort only valid items and check for unique internal names (f->iname) */ qsort((char *)nodup, fcount, sizeof(struct flist far *), fqcmpz); for (j = 1; j < fcount; j++) if (strcmp(nodup[j - 1]->iname, nodup[j]->iname) == 0) { zipwarn(" first full name: ", nodup[j - 1]->name); zipwarn(" second full name: ", nodup[j]->name);#ifdef EBCDIC strtoebc(nodup[j]->iname, nodup[j]->iname);#endif zipwarn("name in zip file repeated: ", nodup[j]->iname);#ifdef EBCDIC strtoasc(nodup[j]->iname, nodup[j]->iname);#endif return ZE_PARMS; } free((zvoid *)s); } return ZE_OK;}int filter(name, casesensitive) char *name; int casesensitive; /* Scan the -R, -i and -x lists for matches to the given name. Return TRUE if the name must be included, FALSE otherwise. Give precedence to -x over -i and -R. Note that if both R and i patterns are given then must have a match for both. This routine relies on the following global variables: patterns array of match pattern structures pcount total number of patterns icount number of -i patterns Rcount number of -R patterns These data are set up by the command line parsing code. */{ unsigned int n; int slashes; char *p, *q; /* without -i patterns, every name matches the "-i select rules" */ int imatch = (icount == 0); /* without -R patterns, every name matches the "-R select rules" */ int Rmatch = (Rcount == 0); if (pcount == 0) return TRUE; for (n = 0; n < pcount; n++) { if (!patterns[n].zname[0]) /* it can happen... */ continue; p = name; switch (patterns[n].select) { case 'R': if (Rmatch) /* one -R match is sufficient, skip this pattern */ continue; /* With -R patterns, if the pattern has N path components (that is, N-1 slashes), then we test only the last N components of name. */ slashes = 0; for (q = patterns[n].zname; (q = MBSCHR(q, '/')) != NULL; INCSTR(q)) slashes++; /* The name may have M path components (M-1 slashes) */ for (q = p; (q = MBSCHR(q, '/')) != NULL; INCSTR(q)) slashes--; /* Now, "slashes" contains the difference "N-M" between the number of path components in the pattern (N) and in the name (M). */ if (slashes < 0) /* We found "M > N" --> skip the first (M-N) path components of the name. */ for (q = p; (q = MBSCHR(q, '/')) != NULL; INCSTR(q)) if (++slashes == 0) { p = q + 1; /* q points at '/', mblen("/") is 1 */ break; } break; case 'i': if (imatch) /* one -i match is sufficient, skip this pattern */ continue; break; } if (MATCH(patterns[n].zname, p, casesensitive)) { switch (patterns[n].select) { case 'x': /* The -x match takes precedence over everything else */ return FALSE; case 'R': Rmatch = TRUE; break; default: /* this must be a type -i match */ imatch = TRUE; break; } } } return imatch && Rmatch;}int newname(name, isdir, casesensitive)char *name; /* name to add (or exclude) */int isdir; /* true for a directory */int casesensitive; /* true for case-sensitive matching *//* Add (or exclude) the name of an existing disk file. Return an error code in the ZE_ class. */{ char *iname, *zname; /* internal name, external version of iname */ char *undosm; /* zname version with "-j" and "-k" options disabled */ struct flist far *f; /* where in found, or new found entry */ struct zlist far *z; /* where in zfiles (if found) */ int dosflag; /* Search for name in zip file. If there, mark it, else add to list of new names to do (or remove from that list). */ if ((iname = ex2in(name, isdir, &dosflag)) == NULL) return ZE_MEM; /* Discard directory names with zip -rj */ if (*iname == '\0') {#ifndef AMIGA/* A null string is a legitimate external directory name in AmigaDOS; also, * a command like "zip -r zipfile FOO:" produces an empty internal name. */# ifndef RISCOS /* If extensions needs to be swapped, we will have empty directory names instead of the original directory. For example, zipping 'c.', 'c.main' should zip only 'main.c' while 'c.' will be converted to '\0' by ex2in. */ if (pathput && !recurse) error("empty name without -j or -r");# endif /* !RISCOS */#endif /* !AMIGA */ free((zvoid *)iname); return ZE_OK; } undosm = NULL; if (dosflag || !pathput) { int save_dosify = dosify, save_pathput = pathput; dosify = 0; pathput = 1; /* zname is temporarly mis-used as "undosmode" iname pointer */ if ((zname = ex2in(name, isdir, NULL)) != NULL) { undosm = in2ex(zname); free(zname); } dosify = save_dosify; pathput = save_pathput; } if ((zname = in2ex(iname)) == NULL) return ZE_MEM; if (undosm == NULL) undosm = zname; if ((z = zsearch(zname)) != NULL) { if (pcount && !filter(undosm, casesensitive)) { /* Do not clear z->mark if "exclude", because, when "dosify || !pathput" * is in effect, two files with different filter options may hit the * same z entry. */ if (verbose) fprintf(mesg, "excluding %s\n", zname); free((zvoid *)iname); free((zvoid *)zname); } else { z->mark = 1; if ((z->name = malloc(strlen(name) + 1 + PAD)) == NULL) { if (undosm != zname) free((zvoid *)undosm); free((zvoid *)iname); free((zvoid *)zname); return ZE_MEM; } strcpy(z->name, name); z->dosflag = dosflag;#ifdef FORCE_NEWNAME free((zvoid *)(z->iname)); z->iname = iname;#else /* Better keep the old name. Useful when updating on MSDOS a zip file * made on Unix. */ free((zvoid *)iname); free((zvoid *)zname);#endif /* ? FORCE_NEWNAME */ } if (name == label) { label = z->name; } } else if (pcount == 0 || filter(undosm, casesensitive)) { /* Check that we are not adding the zip file to itself. This * catches cases like "zip -m foo ../dir/foo.zip". */#ifndef CMS_MVS/* Version of stat() for CMS/MVS isn't complete enough to see if *//* files match. Just let ZIP.C compare the filenames. That's good *//* enough for CMS anyway since there aren't paths to worry about. */ struct stat statb; if (zipstate == -1) zipstate = strcmp(zipfile, "-") != 0 && stat(zipfile, &zipstatb) == 0; if (zipstate == 1 && (statb = zipstatb, stat(name, &statb) == 0 && zipstatb.st_mode == statb.st_mode#ifdef VMS && memcmp(zipstatb.st_ino, statb.st_ino, sizeof(statb.st_ino)) == 0 && strcmp(zipstatb.st_dev, statb.st_dev) == 0 && zipstatb.st_uid == statb.st_uid#else /* !VMS */ && zipstatb.st_ino == statb.st_ino && zipstatb.st_dev == statb.st_dev && zipstatb.st_uid == statb.st_uid && zipstatb.st_gid == statb.st_gid#endif /* ?VMS */ && zipstatb.st_size == statb.st_size && zipstatb.st_mtime == statb.st_mtime && zipstatb.st_ctime == statb.st_ctime)) { /* Don't compare a_time since we are reading the file */ if (verbose) fprintf(mesg, "file matches zip file -- skipping\n"); if (undosm != zname) free((zvoid *)zname); if (undosm != iname) free((zvoid *)undosm); free((zvoid *)iname); return ZE_OK; }#endif /* CMS_MVS */ /* allocate space and add to list */ if ((f = (struct flist far *)farmalloc(sizeof(struct flist))) == NULL || fcount + 1 < fcount || (f->name = malloc(strlen(name) + 1 + PAD)) == NULL) { if (f != NULL) farfree((zvoid far *)f); if (undosm != zname) free((zvoid *)undosm); free((zvoid *)iname); free((zvoid *)zname); return ZE_MEM; } strcpy(f->name, name); f->iname = iname; f->zname = zname; f->dosflag = dosflag; *fnxt = f; f->lst = fnxt; f->nxt = NULL; fnxt = &f->nxt; fcount++; if (name == label) { label = f->name; } } if (undosm != zname) free((zvoid *)undosm); return ZE_OK;}ulg dostime(y, n, d, h, m, s)int y; /* year */int n; /* month */int d; /* day */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -