📄 msdos.c
字号:
/* Copyright (c) 1990-2006 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*/#include "zip.h"#ifndef UTIL /* little or no material in this file is used by UTIL */#include <dos.h>#include <time.h>#if defined(__GO32__) || defined(__TURBOC__)# include <dir.h> /* prototypes of find*() */ typedef struct ffblk ff_dir;# define FATTR (hidden_files ? FA_HIDDEN+FA_SYSTEM+FA_DIREC : FA_DIREC)# define FFIRST(n,d,a) findfirst(n,(struct ffblk *)d,a)# define FNEXT(d) findnext((struct ffblk *)d)# if (defined(__TURBOC__) || (defined(__DJGPP__) && (__DJGPP__ >=2)))# if (defined(__DJGPP__) && (__DJGPP__ == 2) && (__DJGPP_MINOR__ == 0))# include <libc/dosio.h># endif# define GetFileMode(name) _chmod(name, 0)# define SetFileMode(name, attr) _chmod(name, 1, attr)# else /* DJGPP v1.x */# define GetFileMode(name) bdosptr(0x43, (name), 0)# endif#endif /* __GO32__ || __TURBOC__ */#if defined(MSC) || defined(__WATCOMC__) typedef struct find_t ff_dir;# define FATTR (hidden_files ? _A_HIDDEN+_A_SYSTEM+_A_SUBDIR : _A_SUBDIR)# ifndef FA_LABEL# define FA_LABEL _A_VOLID# endif# define FFIRST(n,d,a) _dos_findfirst(n,a,(struct find_t *)d)# define FNEXT(d) _dos_findnext((struct find_t *)d)# define ff_name name# define ff_fdate wr_date# define ff_ftime wr_time# define ff_attrib attrib#endif /* MSC || __WATCOMC__ */#ifdef __EMX__# ifdef EMX_OBSOLETE /* emx 0.9b or earlier */# define size_t xxx_size_t# define wchar_t xxx_wchar_t# define tm xxx_tm# include <sys/emx.h># undef size_t# undef wchar_t# undef tm# else /* !EMX_OBSOLETE */ /* emx 0.9c or newer */# include <emx/syscalls.h># endif /* ?EMX_OBSOLETE */ typedef struct _find ff_dir;# define FATTR (hidden_files ? _A_HIDDEN+_A_SYSTEM+_A_SUBDIR : _A_SUBDIR)# define FA_LABEL _A_VOLID# define FFIRST(n,d,a) __findfirst(n,a,d)# define FNEXT(d) __findnext(d)# define ff_name name# define ff_fdate date# define ff_ftime time# define ff_attrib attr# define GetFileMode(name) __chmod(name, 0, 0)# define SetFileMode(name, attr) __chmod(name, 1, attr)#endif /* __EMX__ */#ifndef SetFileMode# define SetFileMode(name, attr) _dos_setfileattr(name, attr)#endif#define PAD 0#define PATH_END '/'/* Library functions not in (most) header files */int rmdir OF((const char *));int utime OF((char *, ztimbuf *));/* Local functions */#ifndef GetFileModeint GetFileMode OF((char *name));#endif /* !GetFileMode */local int initDirSearch OF((char *name, ff_dir *ff_context_p));local char *getVolumeLabel OF((int, ulg *, ulg *, time_t *));local int wild_recurse OF((char *, char *));local int procname_dos OF((char *n, int caseflag, unsigned attribs));local int is_running_on_windows OF((void));#define MSDOS_INVALID_ATTR 0xFF#define getDirEntryAttr(d) ((d)->ff_attrib)/* Module level variables */extern char *label;local ulg label_time = 0;local ulg label_mode = 0;local time_t label_utim = 0;/* Module level constants */local ZCONST char wild_match_all[] = "*.*";#ifndef GetFileModeint GetFileMode(char *name){ unsigned int attr = 0; return (_dos_getfileattr(name, &attr) ? -1 : attr);}#endif /* !GetFileMode */local int initDirSearch(name, ff_context_p) char *name; /* name of directory to scan */ ff_dir *ff_context_p; /* pointer to FFIRST/FNEXT context structure */{ int r; /* FFIRST return value */ char *p, *q; /* temporary copy of name, and aux pointer */ if ((p = malloc(strlen(name) + (2 + sizeof(wild_match_all)))) == NULL) return ZE_MEM; strcpy(p, name); q = p + strlen(p); if (q[-1] == ':') *q++ = '.'; if ((q - p) > 0 && *(q - 1) != '/') *q++ = '/'; strcpy(q, wild_match_all); r = FFIRST(p, ff_context_p, FATTR); free((zvoid *)p); return (r ? ZE_MISS : ZE_OK);}local char *getVolumeLabel(drive, vtime, vmode, vutim) int drive; /* drive name: 'A' .. 'Z' or '\0' for current drive */ ulg *vtime; /* volume label creation time (DOS format) */ ulg *vmode; /* volume label file mode */ time_t *vutim;/* volume label creation time (UNIX format) *//* If a volume label exists for the given drive, return its name and set its time and mode. The returned name must be static data. */{ static char vol[14]; ff_dir d; char *p; if (drive) { vol[0] = (char)drive; strcpy(vol+1, ":/"); } else { strcpy(vol, "/"); } strcat(vol, wild_match_all); if (FFIRST(vol, &d, FA_LABEL) == 0) { strncpy(vol, d.ff_name, sizeof(vol)-1); vol[sizeof(vol)-1] = '\0'; /* just in case */ if ((p = strchr(vol, '.')) != NULL) /* remove dot, though PKZIP doesn't */ strcpy(p, p + 1); *vtime = ((ulg)d.ff_fdate << 16) | ((ulg)d.ff_ftime & 0xffff); *vmode = (ulg)d.ff_attrib; *vutim = dos2unixtime(*vtime); return vol; } return NULL;}#ifdef MSDOS16#define ONENAMELEN 12 /* no 16-bit compilers supports LFN */#else#define ONENAMELEN 255#endif/* whole is a pathname with wildcards, wildtail points somewhere in the *//* middle of it. All wildcards to be expanded must come AFTER wildtail. */local int wild_recurse(whole, wildtail)char *whole;char *wildtail;{ ff_dir dir; char *subwild, *name, *newwhole = NULL, *glue = NULL, plug = 0, plug2; ush newlen, amatch = 0; int e = ZE_MISS; if (!isshexp(wildtail)) { struct stat s; /* dummy buffer for stat() */ if (!LSSTAT(whole, &s)) /* file exists ? */ return procname(whole, 0); else return ZE_MISS; /* woops, no wildcards! */ } /* back up thru path components till existing dir found */ do { name = wildtail + strlen(wildtail) - 1; for (;;) if (name-- <= wildtail || *name == PATH_END) { subwild = name + 1; plug2 = *subwild; *subwild = 0; break; } if (glue) *glue = plug; glue = subwild; plug = plug2; e = initDirSearch(whole, &dir); } while (e == ZE_MISS && subwild > wildtail); wildtail = subwild; /* skip past non-wild components */ if (e != ZE_OK) { if (glue) *glue = plug; goto ohforgetit; } subwild = strchr(wildtail + 1, PATH_END); /* this "+ 1" dodges the ^^^ hole left by *glue == 0 */ if (subwild != NULL) { *(subwild++) = 0; /* wildtail = one component pattern */ newlen = strlen(whole) + strlen(subwild) + (ONENAMELEN + 2); } else newlen = strlen(whole) + (ONENAMELEN + 1); if ((newwhole = malloc(newlen)) == NULL) { if (glue) *glue = plug; e = ZE_MEM; goto ohforgetit; } strcpy(newwhole, whole); newlen = strlen(newwhole); if (glue) *glue = plug; /* repair damage to whole */ if (!isshexp(wildtail)) { e = ZE_MISS; /* non-wild name not found */ goto ohforgetit; } do { if (strcmp(dir.ff_name, ".") && strcmp(dir.ff_name, "..") && MATCH(wildtail, dir.ff_name, 0)) { strcpy(newwhole + newlen, dir.ff_name); if (subwild) { name = newwhole + strlen(newwhole); *(name++) = PATH_END; strcpy(name, subwild); e = wild_recurse(newwhole, name); } else e = procname_dos(newwhole, 0, getDirEntryAttr(&dir)); newwhole[newlen] = 0; if (e == ZE_OK) amatch = 1; else if (e != ZE_MISS) break; } } while (FNEXT(&dir) == 0); ohforgetit: if (subwild) *--subwild = PATH_END; if (newwhole) free(newwhole); if (e == ZE_MISS && amatch) e = ZE_OK; return e;}int wild(w)char *w; /* path/pattern to match *//* If not in exclude mode, expand the pattern based on the contents of the file system. Return an error code in the ZE_ class. */{ char *p; /* path */ char *q; /* diskless path */ int e; /* result */ if (volume_label == 1) { volume_label = 2; label = getVolumeLabel((w != NULL && w[1] == ':') ? to_up(w[0]) : '\0', &label_time, &label_mode, &label_utim); if (label != NULL) (void)newname(label, 0, 0); if (w == NULL || (w[1] == ':' && w[2] == '\0')) return ZE_OK; /* "zip -$ foo a:" can be used to force drive name */ } /* special handling of stdin request */ if (strcmp(w, "-") == 0) /* if compressing stdin */ return newname(w, 0, 0); /* Allocate and copy pattern, leaving room to add "." if needed */ if ((p = malloc(strlen(w) + 2)) == NULL) return ZE_MEM; strcpy(p, w); /* Normalize path delimiter as '/' */ for (q = p; *q; q++) /* use / consistently */ if (*q == '\\') *q = '/'; /* Separate the disk part of the path */ q = strchr(p, ':'); if (q != NULL) { if (strchr(++q, ':')) /* sanity check for safety of wild_recurse */ return ZE_MISS; } else q = p; /* Normalize bare disk names */ if (q > p && !*q) strcpy(q, "."); /* Here we go */ e = wild_recurse(p, q); free((zvoid *)p); return e;}local int procname_dos(n, caseflag, attribs)char *n; /* name to process */int caseflag; /* true to force case-sensitive match */unsigned attribs; /* file attributes, if available *//* Process a name or sh expression to operate on (or exclude). Return an error code in the ZE_ class. */{ char *a; /* path and name for recursion */ ff_dir *d; /* control structure for FFIRST/FNEXT */ char *e; /* pointer to name from readd() */ int m; /* matched flag */ int ff_status; /* return value of FFIRST/FNEXT */ char *p; /* path for recursion */ struct stat s; /* result of stat() */ struct zlist far *z; /* steps through zfiles list */ if (n == NULL) /* volume_label request in freshen|delete mode ?? */ return ZE_OK; if (strcmp(n, "-") == 0) /* if compressing stdin */ return newname(n, 0, caseflag); else if (*n == '\0') return ZE_MISS; else if (attribs != MSDOS_INVALID_ATTR) { /* Avoid calling stat() for performance reasons when it is already known (from a previous directory scan) that the passed name corresponds to a "real existing" file. The only information needed further down in this function is the distinction between directory entries and other (typically normal file) entries. This distinction can be derived from the file's attributes that the directory lookup has already provided "for free". */ s.st_mode = ((attribs & MSDOS_DIR_ATTR) ? S_IFDIR : S_IFREG); } else if (LSSTAT(n, &s)#ifdef __TURBOC__ /* For this compiler, stat() succeeds on wild card names! */ || isshexp(n)#endif ) { /* Not a file or directory--search for shell expression in zip file */ if (caseflag) { p = malloc(strlen(n) + 1); if (p != NULL) strcpy(p, n); } else p = ex2in(n, 0, (int *)NULL); /* shouldn't affect matching chars */ m = 1; for (z = zfiles; z != NULL; z = z->nxt) { if (MATCH(p, z->iname, caseflag))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -