⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msdos.c

📁 给出了 zip 压缩算法的完整实现过程。
💻 C
📖 第 1 页 / 共 3 页
字号:
      {        z->mark = pcount ? filter(z->zname, caseflag) : 1;        if (z->mark) z->dosflag = 1;    /* force DOS attribs for incl. names */        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, caseflag)) != 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, caseflag)) != 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_dos(a, caseflag, getDirEntryAttr(d)))              != 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;}int procname(n, caseflag)char *n;                /* name to process */int caseflag;           /* true to force case-sensitive match */{  return procname_dos(n, caseflag, MSDOS_INVALID_ATTR);}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 */  /* Strip drive specification */  t = *x && *(x + 1) == ':' ? x + 2 : x;  /* Strip "//host/share/" part of a UNC name */  if ((!strncmp(x,"//",2) || !strncmp(x,"\\\\",2)) &&      (x[2] != '\0' && x[2] != '/' && x[2] != '\\')) {    n = x + 2;    while (*n != '\0' && *n != '/' && *n != '\\')      n++;              /* strip host name */    if (*n != '\0') {      n++;      while (*n != '\0' && *n != '/' && *n != '\\')        n++;            /* strip `share' name */    }    if (*n != '\0')      t = n + 1;  }  /* Strip leading "/" to convert an absolute path into a relative path */  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)    *pdosflag = dosflag;  return n;}char *in2ex(n)char *n;                /* internal file name *//* Convert the zip file name to an external file name, returning the malloc'ed   string or NULL if not enough memory. */{  char *x;              /* external file name */  if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)      return NULL;  strcpy(x, n);  return x;}void stamp(f, d)char *f;                /* name of file to change */ulg d;                  /* dos-style time to change it to *//* Set last updated and accessed time of file f to the DOS time d. */{#if defined(__TURBOC__) || defined(__GO32__)  int h;                /* file handle */  if ((h = open(f, 0)) != -1)  {    setftime(h, (struct ftime *)(void *)&d);    close(h);  }#else /* !__TURBOC__ && !__GO32__ */  ztimbuf u;            /* argument for utime() */  /* Convert DOS time to time_t format in u.actime and u.modtime */  u.actime = u.modtime = dos2unixtime(d);  /* Set updated and accessed times of f */  utime(f, &u);#endif /* ?(__TURBOC__ || __GO32__) */}ulg filetime(f, a, n, t)char *f;                /* name of file to get info on */ulg *a;                 /* return value: file attributes */long *n;                /* return value: file size */iztimes *t;             /* return value: access, modific. and creation times *//* If file *f does not exist, return 0.  Else, return the file's last   modified date and time as an MSDOS date and time.  The date and   time is returned in a long with the date most significant to allow   unsigned integer comparison of absolute times.  Also, if a is not   a NULL pointer, store the file attributes there, with the high two   bytes being the Unix attributes, and the low byte being a mapping   of that to DOS attributes.  If n is not NULL, store the file size   there.  If t is not NULL, the file's access, modification and creation   times are stored there as UNIX time_t values.   If f is "-", use standard input as the file. If f is a device, return   a file size of -1 */{  struct stat s;        /* results of stat() */  char *name;  unsigned int len = strlen(f);  int isstdin = !strcmp(f, "-");  if (f == label) {    if (a != NULL)      *a = label_mode;    if (n != NULL)      *n = -2L; /* convention for a label name */    if (t != NULL)      t->atime = t->mtime = t->ctime = label_utim;    return label_time;  }  if ((name = malloc(len + 1)) == NULL) {    ZIPERR(ZE_MEM, "filetime");  }  strcpy(name, f);  if (name[len - 1] == '/')    name[len - 1] = '\0';  /* not all systems allow stat'ing a file with / appended */  if (isstdin) {    if (fstat(fileno(stdin), &s) != 0) {      free(name);      error("fstat(stdin)");    }    time((time_t *)&s.st_mtime);       /* some fstat()s return time zero */  } else if (LSSTAT(name, &s) != 0) {             /* Accept about any file kind including directories              * (stored with trailing / with -r option)              */    free(name);    return 0;  }  if (a != NULL) {    *a = ((ulg)s.st_mode << 16) | (isstdin ? 0L : (ulg)GetFileMode(name));#if (S_IFREG != 0x8000)    /* kludge to work around non-standard S_IFREG flag used in DJGPP V2.x */    if ((s.st_mode & S_IFMT) == S_IFREG) *a |= 0x80000000L;#endif  }  if (n != NULL)    *n = (s.st_mode & S_IFREG) != 0 ? s.st_size : -1L;  if (t != NULL) {    t->atime = s.st_atime;    t->mtime = s.st_mtime;    t->ctime = s.st_ctime;  }  free(name);  return unix2dostime((time_t *)&s.st_mtime);}int deletedir(d)char *d;                /* directory to delete *//* Delete the directory *d if it is empty, do nothing otherwise.   Return the result of rmdir(), delete(), or system(). */{    return rmdir(d);}int set_extra_field(z, z_utim)  struct zlist far *z;  iztimes *z_utim;  /* create extra field and change z->att if desired */{#ifdef USE_EF_UT_TIME#ifdef IZ_CHECK_TZ  if (!zp_tz_is_valid) return ZE_OK;    /* skip silently if no valid TZ info */#endif  if ((z->extra = (char *)malloc(EB_HEADSIZE+EB_UT_LEN(1))) == NULL)    return ZE_MEM;  z->extra[0]  = 'U';  z->extra[1]  = 'T';  z->extra[2]  = EB_UT_LEN(1);          /* length of data part of e.f. */  z->extra[3]  = 0;  z->extra[4]  = EB_UT_FL_MTIME;  z->extra[5]  = (char)(z_utim->mtime);  z->extra[6]  = (char)(z_utim->mtime >> 8);  z->extra[7]  = (char)(z_utim->mtime >> 16);  z->extra[8]  = (char)(z_utim->mtime >> 24);  z->cext = z->ext = (EB_HEADSIZE+EB_UT_LEN(1));  z->cextra = z->extra;  return ZE_OK;#else /* !USE_EF_UT_TIME */  return (int)(z-z);#endif /* ?USE_EF_UT_TIME */}#ifdef MY_ZCALLOC       /* Special zcalloc function for MEMORY16 (MSDOS/OS2) */#if defined(__TURBOC__) && !defined(OS2)/* Small and medium model are for now limited to near allocation with * reduced MAX_WBITS and MAX_MEM_LEVEL *//* Turbo C malloc() does not allow dynamic allocation of 64K bytes * and farmalloc(64K) returns a pointer with an offset of 8, so we * must fix the pointer. Warning: the pointer must be put back to its * original form in order to free it, use zcfree(). */#define MAX_PTR 10/* 10*64K = 640K */local int next_ptr = 0;typedef struct ptr_table_s {    zvoid far *org_ptr;    zvoid far *new_ptr;} ptr_table;local ptr_table table[MAX_PTR];/* This table is used to remember the original form of pointers * to large buffers (64K). Such pointers are normalized with a zero offset. * Since MSDOS is not a preemptive multitasking OS, this table is not * protected from concurrent access. This hack doesn't work anyway on * a protected system like OS/2. Use Microsoft C instead. */zvoid far *zcalloc (unsigned items, unsigned size){    zvoid far *buf;    ulg bsize = (ulg)items*size;    if (bsize < (65536L-16L)) {        buf = farmalloc(bsize);        if (*(ush*)&buf != 0) return buf;    } else {        buf = farmalloc(bsize + 16L);    }    if (buf == NULL || next_ptr >= MAX_PTR) return NULL;    table[next_ptr].org_ptr = buf;    /* Normalize the pointer to seg:0 */    *((ush*)&buf+1) += ((ush)((uch*)buf-NULL) + 15) >> 4;    *(ush*)&buf = 0;    table[next_ptr++].new_ptr = buf;    return buf;}zvoid zcfree (zvoid far *ptr){    int n;    if (*(ush*)&ptr != 0) { /* object < 64K */        farfree(ptr);        return;    }    /* Find the original pointer */    for (n = next_ptr - 1; n >= 0; n--) {        if (ptr != table[n].new_ptr) continue;        farfree(table[n].org_ptr);        while (++n < next_ptr) {            table[n-1] = table[n];        }        next_ptr--;        return;

⌨️ 快捷键说明

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