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

📄 zip.c

📁 《汇编源代码大全》
💻 C
📖 第 1 页 / 共 3 页
字号:
  }  else    a = 0;  /* Throw away the garbage in front of the zip file for -F */  if (fix) zipbeg = 0;  /* Open zip file and temporary output file */  diag("opening zip file and creating temporary zip file");  x = NULL;  tempzn = 0;  if (strcmp(zipfile, "-") == 0)  {#if defined(MSDOS) || defined(__human68k__)    /* Set stdout mode to binary for MSDOS systems */    setmode(fileno(stdout), O_BINARY);    tempzf = y = fdopen(fileno(stdout), FOPW);#else    tempzf = y = stdout;#endif    /* tempzip must be malloced so a later free won't barf */    tempzip = malloc(4);    if (tempzip == NULL)      err(ZE_MEM, "allocating temp filename");    strcpy(tempzip, "-");  }  else if (d) /* d true if just appending (-g) */  {    if ((y = fopen(zipfile, FOPM)) == NULL)      err(ZE_NAME, zipfile);    tempzip = zipfile;    tempzf = y;    if (fseek(y, cenbeg, SEEK_SET))      err(ferror(y) ? ZE_READ : ZE_EOF, zipfile);    tempzn = cenbeg;  }  else  {    if ((zfiles != NULL || zipbeg) && (x = fopen(zipfile, FOPR_EX)) == NULL)      err(ZE_NAME, zipfile);    if ((tempzip = tempname(zipfile)) == NULL)      err(ZE_MEM, "allocating temp filename");    if ((tempzf = y = fopen(tempzip, FOPW)) == NULL)      err(ZE_TEMP, tempzip);    if (zipbeg && (r = fcopy(x, y, zipbeg)) != ZE_OK)      err(r, r == ZE_TEMP ? tempzip : zipfile);    tempzn = zipbeg;  }#ifndef VMS  /* Use large buffer to speed up stdio: */  zipbuf = (char *)malloc(ZBSZ);  if (zipbuf == NULL)    err(ZE_MEM, tempzip);# ifdef _IOFBF  setvbuf(y, zipbuf, _IOFBF, ZBSZ);# else  setbuf(y, zipbuf);# endif /* _IOBUF */#endif /* VMS */  o = 0;                                /* no ZE_OPEN errors yet */  /* Process zip file, updating marked files */  if (zfiles != NULL)    diag("going through old zip file");  w = &zfiles;  while ((z = *w) != NULL)    if (z->mark)    {      /* if not deleting, zip it up */      if (action != DELETE)      {        if (noisy)        {          fprintf(mesg, "updating: %s", z->zname);          fflush(mesg);        }        if ((r = zipup(z, y)) != ZE_OK && r != ZE_OPEN)        {          if (noisy)          {            putc('\n', mesg);            fflush(mesg);          }          sprintf(errbuf, "was zipping %s", z->name);          err(r, errbuf);        }        if (r == ZE_OPEN)        {          o = 1;          if (noisy)          {            putc('\n', mesg);            fflush(mesg);          }          perror("zip warning");          warn("could not open for reading: ", z->name);          warn("will just copy entry over: ", z->zname);          if ((r = zipcopy(z, x, y)) != ZE_OK)          {            sprintf(errbuf, "was copying %s", z->zname);            err(r, errbuf);          }          z->mark = 0;        }        w = &z->nxt;      }      else      {        if (noisy)        {          fprintf(mesg, "deleting: %s\n", z->zname);          fflush(mesg);        }        v = z->nxt;                     /* delete entry from list */        free((voidp *)(z->name));        free((voidp *)(z->zname));        if (z->ext)          free((voidp *)(z->extra));        if (z->cext && z->cextra != z->extra)          free((voidp *)(z->cextra));        if (z->com)          free((voidp *)(z->comment));        farfree((voidp far *)z);        *w = v;        zcount--;      }    }    else    {      /* copy the original entry verbatim */      if (!d && (r = zipcopy(z, x, y)) != ZE_OK)      {        sprintf(errbuf, "was copying %s", z->zname);        err(r, errbuf);      }      w = &z->nxt;    }  /* Process the edited found list, adding them to the zip file */  diag("zipping up new entries, if any");  for (f = found; f != NULL; f = fexpel(f))  {    /* add a new zfiles entry and set the name */    if ((z = (struct zlist far *)farmalloc(sizeof(struct zlist))) == NULL)      err(ZE_MEM, "was adding files to zip file");    z->nxt = NULL;    z->name = f->name;    f->name = NULL;    z->zname = f->zname;    f->zname = NULL;    z->ext = z->cext = z->com = 0;    z->mark = 1;    z->dosflag = f->dosflag;    /* zip it up */    if (noisy)    {      fprintf(mesg, "  adding: %s", z->zname);      fflush(mesg);    }    if ((r = zipup(z, y)) != ZE_OK  && r != ZE_OPEN)    {      if (noisy)      {        putc('\n', mesg);        fflush(mesg);      }      sprintf(errbuf, "was zipping %s", z->name);      err(r, errbuf);    }    if (r == ZE_OPEN)    {      o = 1;      if (noisy)      {        putc('\n', mesg);        fflush(mesg);      }      perror("zip warning");      warn("could not open for reading: ", z->name);      free((voidp *)(z->name));      free((voidp *)(z->zname));      farfree((voidp far *)z);    }    else    {      *w = z;      w = &z->nxt;      zcount++;    }  }  if (key != NULL)  {    free((voidp *)key);    key = NULL;  }  /* Get one line comment for each new entry */  if (comadd)  {    if (comment_stream == NULL) {      comment_stream = fdopen(fileno(stderr), "r");    }    if ((e = malloc(MAXCOM + 1)) == NULL)      err(ZE_MEM, "was reading comment lines");    for (z = zfiles; z != NULL; z = z->nxt)      if (z->mark)      {        if (noisy)          fprintf(mesg, "Enter comment for %s:\n", z->name);        if (fgets(e, MAXCOM+1, comment_stream) != NULL)        {          if ((p = malloc((k = strlen(e))+1)) == NULL)          {            free((voidp *)e);            err(ZE_MEM, "was reading comment lines");          }          strcpy(p, e);          if (p[k-1] == '\n')            p[--k] = 0;          z->comment = p;          z->com = k;        }      }    free((voidp *)e);  }  /* Get multi-line comment for the zip file */  if (zipedit)  {    if (comment_stream == NULL) {      comment_stream = fdopen(fileno(stderr), "r");    }    if ((e = malloc(MAXCOM + 1)) == NULL)      err(ZE_MEM, "was reading comment lines");    if (noisy && zcomlen)    {      fputs("current zip file comment is:\n", mesg);      fwrite(zcomment, 1, zcomlen, mesg);      if (zcomment[zcomlen-1] != '\n')        putc('\n', mesg);      free((voidp *)zcomment);    }    zcomment = malloc(1);    *zcomment = 0;    if (noisy)      fputs("enter new zip file comment (end with .):\n", mesg);#if (defined(AMIGA) && (defined(LATTICE)||defined(__SASC)))      flushall();  /* tty input/output is out of sync here */#endif    while (fgets(e, MAXCOM+1, comment_stream) != NULL && strcmp(e, ".\n"))    {      if (e[(r = strlen(e)) - 1] == '\n')        e[--r] = 0;      if ((p = malloc((*zcomment ? strlen(zcomment) + 3 : 1) + r)) == NULL)      {        free((voidp *)e);        err(ZE_MEM, "was reading comment lines");      }      if (*zcomment)        strcat(strcat(strcpy(p, zcomment), "\r\n"), e);      else        strcpy(p, *e ? e : "\r\n");      free((voidp *)zcomment);      zcomment = p;    }    zcomlen = strlen(zcomment);    free((voidp *)e);  }  /* Write central directory and end header to temporary zip */  diag("writing central directory");  k = 0;                        /* keep count for end header */  c = tempzn;                   /* get start of central */  n = t = 0;  for (z = zfiles; z != NULL; z = z->nxt)  {    if ((r = putcentral(z, y)) != ZE_OK)      err(r, tempzip);    tempzn += 4 + CENHEAD + z->nam + z->cext + z->com;    n += z->len;    t += z->siz;    k++;  }  if (k == 0)    warn("zip file empty", "");  if (verbose)    fprintf(mesg, "total bytes=%lu, compressed=%lu -> %d%% savings\n",           n, t, percent(n, t));  t = tempzn - c;               /* compute length of central */  diag("writing end of central directory");  if ((r = putend(k, t, c, zcomlen, zcomment, y)) != ZE_OK)    err(r, tempzip);  tempzf = NULL;  if (fclose(y))    err(d ? ZE_WRITE : ZE_TEMP, tempzip);  if (x != NULL)    fclose(x);  /* Free some memory before spawning unzip */  lm_free();  /* Test new zip file before overwriting old one or removing input files */  if (test) {    check_zipfile(tempzip);  }  /* Replace old zip file with new zip file, leaving only the new one */  if (strcmp(zipfile, "-") && !d)  {    diag("replacing old zip file with new zip file");    if ((r = replace(zipfile, tempzip)) != ZE_OK)    {      warn("new zip file left as: ", tempzip);      free((voidp *)tempzip);      tempzip = NULL;      err(r, "was replacing the original zip file");    }    free((voidp *)tempzip);  }  tempzip = NULL;  if (a && strcmp(zipfile, "-")) {    setfileattr(zipfile, a);#ifdef VMS    /* If the zip file existed previously, restore its record format: */    if (x != NULL)      VMSmunch(zipfile, RESTORE_RTYPE, NULL);#endif  }  /* Finish up (process -o, -m, clean up).  Exit code depends on o. */  leave(o ? ZE_OPEN : ZE_OK);  return 0; /* just to avoid compiler warning */}/***************************************************************** | envargs - add default options from environment to command line |---------------------------------------------------------------- | Author: Bill Davidsen, original 10/13/91, revised 23 Oct 1991. | This program is in the public domain. |---------------------------------------------------------------- | Minor program notes: |  1. Yes, the indirection is a tad complex |  2. Parenthesis were added where not needed in some cases |     to make the action of the code less obscure. ****************************************************************/local voidenvargs(Pargc, Pargv, envstr)int *Pargc;char ***Pargv;char *envstr;{    char *getenv();    char *envptr;                               /* value returned by getenv */    char *bufptr;                               /* copy of env info */    int argc = 0;                               /* internal arg count */    int ch;                                             /* spare temp value */    char **argv;                                /* internal arg vector */    char **argvect;                             /* copy of vector address */    /* see if anything in the environment */    envptr = getenv(envstr);    if (envptr == NULL || *envptr == 0) return;    /* count the args so we can allocate room for them */    argc = count_args(envptr);    bufptr = malloc(1+strlen(envptr));    if (bufptr == NULL)        err(ZE_MEM, "Can't get memory for arguments");    strcpy(bufptr, envptr);    /* allocate a vector large enough for all args */    argv = (char **)malloc((argc+*Pargc+1)*sizeof(char *));    if (argv == NULL)        err(ZE_MEM, "Can't get memory for arguments");    argvect = argv;    /* copy the program name first, that's always true */    *(argv++) = *((*Pargv)++);    /* copy the environment args first, may be changed */    do {        *(argv++) = bufptr;        /* skip the arg and any trailing blanks */        while ((ch = *bufptr) != '\0' && ch != ' ') ++bufptr;        if (ch == ' ') *(bufptr++) = '\0';        while ((ch = *bufptr) != '\0' && ch == ' ') ++bufptr;    } while (ch);    /* now save old argc and copy in the old args */    argc += *Pargc;    while (--(*Pargc)) *(argv++) = *((*Pargv)++);    /* finally, add a NULL after the last arg, like UNIX */    *argv = NULL;    /* save the values and return */    *Pargv = argvect;    *Pargc = argc;}static intcount_args(s)char *s;{    int count = 0;    int ch;    do {        /* count and skip args */        ++count;        while ((ch = *s) != '\0' && ch != ' ') ++s;        while ((ch = *s) != '\0' && ch == ' ') ++s;    } while (ch);    return count;}

⌨️ 快捷键说明

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