msdos.c

来自「infozip2.2源码」· C语言 代码 · 共 938 行 · 第 1/2 页

C
938
字号
/* Copyright (C) 1990-1997 Mark Adler, Richard B. Wales, Jean-loup Gailly, Onno van der Linden, Christian Spieler and Kai Uwe Rommel. 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.*/#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(__DJGPP__) || (__DJGPP__ < 2))#    define GetFileMode(name) bdosptr(0x43, (name), 0)#  else#    include <libc/dosio.h>#  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)#endif /* __EMX__ *//* Keep this =>SYNCHRONIZED<= with the corresponding definition in fileio.c */#if defined(__GO32__) || defined(__EMX__)#define MATCH shmatch#else#define MATCH dosmatch#endif /* __GO32__ */#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 *));/* 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;  _dos_getfileattr(name, &attr);  return 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) + 5)) == 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 creationtime (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;}#define ONENAMELEN 12/* 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);        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);    if (subwild != NULL) {        /* this "+ 1" dodges the  ^^^ hole left by *glue == 0 */        *(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)) {            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(newwhole);            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);      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);    /* 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;}int procname(n)char *n;                /* name to process *//* 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);  else if (*n == '\0') return ZE_MISS;  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 */    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))      {        z->mark = pcount ? filter(z->zname) : 1;        if (verbose)            fprintf(mesg, "zip diagnostic: %scluding %s\n",               z->mark ? "in" : "ex", z->name);        m = 0;      }    }    free((zvoid *)p);    return m ? ZE_MISS : ZE_OK;  }  /* Live name--use if file, recurse if directory */  for (p = n; *p; p++)          /* use / consistently */    if (*p == '\\')      *p = '/';  if ((s.st_mode & S_IFDIR) == 0)  {    /* add or remove name of file */    if ((m = newname(n, 0)) != ZE_OK)      return m;  } else {    /* Add trailing / to the directory name */    if ((p = malloc(strlen(n)+2)) == NULL)      return ZE_MEM;    if (strcmp(n, ".") == 0 || strcmp(n, "/.") == 0) {      *p = '\0';  /* avoid "./" prefix and do not create zip entry */    } else {      strcpy(p, n);      a = p + strlen(p);      if (a[-1] != '/')        strcpy(a, "/");      if (dirnames && (m = newname(p, 1)) != ZE_OK) {        free((zvoid *)p);        return m;      }    }    /* recurse into directory */    if (recurse)    {      if ((d = malloc(sizeof(ff_dir))) == NULL ||          (m = initDirSearch(n, d)) == ZE_MEM)      {        if (d != NULL)          free((zvoid *)d);        free((zvoid *)p);        return ZE_MEM;      }      for (e = d->ff_name, ff_status = m;           ff_status == 0;           ff_status = FNEXT(d))      {        if (strcmp(e, ".") && strcmp(e, ".."))        {          if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)          {            free((zvoid *)d);            free((zvoid *)p);            return ZE_MEM;          }          strcat(strcpy(a, p), e);          if ((m = procname(a)) != ZE_OK)   /* recurse on name */          {            if (m == ZE_MISS)              zipwarn("name not matched: ", a);            else              ziperr(m, a);          }          free((zvoid *)a);        }      }      free((zvoid *)d);    }    free((zvoid *)p);  } /* (s.st_mode & S_IFDIR) == 0) */  return ZE_OK;}char *ex2in(x, isdir, pdosflag)char *x;                /* external file name */int isdir;              /* input: x is a directory */int *pdosflag;          /* output: force MSDOS file attributes? *//* Convert the external file name to a zip file name, returning the malloc'ed   string or NULL if not enough memory. */{  char *n;              /* internal file name (malloc'ed) */  char *t;              /* shortened name */  int dosflag;  dosflag = 1;  /* Find starting point in name before doing malloc */  t = *x && *(x + 1) == ':' ? x + 2 : x;  while (*t == '/' || *t == '\\')    t++;  /* Skip leading "./" as well */  while (*t == '.' && (t[1] == '/' || t[1] == '\\'))    t += 2;  /* Make changes, if any, to the copied name (leave original intact) */  for (n = t; *n; n++)    if (*n == '\\')      *n = '/';  if (!pathput)    t = last(t, PATH_END);  /* Malloc space for internal name and copy it */  if ((n = malloc(strlen(t) + 1)) == NULL)    return NULL;  strcpy(n, t);  if (isdir == 42) return n;      /* avoid warning on unused variable */  if (dosify)    msname(n);  else#if defined(__DJGPP__) && __DJGPP__ >= 2    if (_USE_LFN == 0)#endif      strlwr(n);  if (pdosflag)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?