home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip22.zip / fileio.c < prev    next >
C/C++ Source or Header  |  1997-10-08  |  23KB  |  868 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1997 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel, Onno van der Linden and Igor Mandrichenko.
  5.  Permission is granted to any individual or institution to use, copy, or
  6.  redistribute this software so long as all of the original files are included,
  7.  that it is not sold for profit, and that this copyright notice is retained.
  8.  
  9. */
  10.  
  11. /*
  12.  *  fileio.c by Mark Adler.
  13.  *
  14.  */
  15.  
  16. #include "zip.h"
  17.  
  18. #include <time.h>
  19.  
  20. #ifdef QDOS
  21. # ifndef MATCH
  22. #  define MATCH shmatch
  23. # endif
  24. #endif
  25.  
  26. #ifdef OSF
  27. #define EXDEV 18   /* avoid a bug in the DEC OSF/1 header files. */
  28. #else
  29. #include <errno.h>
  30. #endif
  31.  
  32. #ifdef NO_ERRNO
  33. extern int errno;
  34. #endif
  35.  
  36. #if (defined(DOS) && !defined(__GO32__) && !defined(__EMX__)) || defined(WIN32)
  37. #define MATCH dosmatch
  38. #else
  39. #define MATCH shmatch
  40. #endif
  41.  
  42. #if defined(VMS) || defined(TOPS20)
  43. #  define PAD 5
  44. #else
  45. #  define PAD 0
  46. #endif
  47.  
  48. #ifdef NO_RENAME
  49. int rename OF((ZCONST char *, ZCONST char *));
  50. #endif
  51.  
  52.  
  53. #ifndef UTIL    /* the companion #endif is a bit of ways down ... */
  54.  
  55. /* Local functions */
  56. local int fqcmp  OF((ZCONST zvoid *, ZCONST zvoid *));
  57. local int fqcmpz OF((ZCONST zvoid *, ZCONST zvoid *));
  58.  
  59. /* Local module level variables. */
  60. char *label = NULL;                /* global, but only used in `system'.c */
  61. local struct stat zipstatb;
  62. #ifndef WINDLL
  63. local int zipstate = -1;
  64. #else
  65. int zipstate;
  66. #endif
  67. /* -1 unknown, 0 old zip file exists, 1 new zip file */
  68.  
  69. char *getnam(n, fp)
  70. char *n;                /* where to put name (must have >=FNMAX+1 bytes) */
  71. FILE *fp;
  72. /* Read a \n or \r delimited name from stdin into n, and return
  73.    n.  If EOF, then return NULL.  Also, if the name read is too big, return
  74.    NULL. */
  75. {
  76.   int c;                /* last character read */
  77.   char *p;              /* pointer into name area */
  78.  
  79.   p = n;
  80.   while ((c = getc(fp)) == '\n' || c == '\r')
  81.     ;
  82.   if (c == EOF)
  83.     return NULL;
  84.   do {
  85.     if (p - n >= FNMAX)
  86.       return NULL;
  87.     *p++ = (char) c;
  88.     c = getc(fp);
  89.   } while (c != EOF && (c != '\n' && c != '\r'));
  90.   *p = 0;
  91.   return n;
  92. }
  93.  
  94. struct flist far *fexpel(f)
  95. struct flist far *f;    /* entry to delete */
  96. /* Delete the entry *f in the doubly-linked found list.  Return pointer to
  97.    next entry to allow stepping through list. */
  98. {
  99.   struct flist far *t;  /* temporary variable */
  100.  
  101.   t = f->nxt;
  102.   *(f->lst) = t;                        /* point last to next, */
  103.   if (t != NULL)
  104.     t->lst = f->lst;                    /* and next to last */
  105.   if (f->name != NULL)                  /* free memory used */
  106.     free((zvoid *)(f->name));
  107.   if (f->zname != NULL)
  108.     free((zvoid *)(f->zname));
  109.   if (f->iname != NULL)
  110.     free((zvoid *)(f->iname));
  111.   farfree((zvoid far *)f);
  112.   fcount--;                             /* decrement count */
  113.   return t;                             /* return pointer to next */
  114. }
  115.  
  116.  
  117. local int fqcmp(a, b)
  118. ZCONST zvoid *a, *b;          /* pointers to pointers to found entries */
  119. /* Used by qsort() to compare entries in the found list by name. */
  120. {
  121.   return strcmp((*(struct flist far **)a)->name,
  122.                 (*(struct flist far **)b)->name);
  123. }
  124.  
  125.  
  126. local int fqcmpz(a, b)
  127. ZCONST zvoid *a, *b;          /* pointers to pointers to found entries */
  128. /* Used by qsort() to compare entries in the found list by zname. */
  129. {
  130.   return strcmp((*(struct flist far **)a)->zname,
  131.                 (*(struct flist far **)b)->zname);
  132. }
  133.  
  134.  
  135. char *last(p, c)
  136. char *p;                /* sequence of path components */
  137. int c;                  /* path components separator character */
  138. /* Return a pointer to the start of the last path component. For a directory
  139.  * name terminated by the character in c, the return value is an empty string.
  140.  */
  141. {
  142.   char *t;              /* temporary variable */
  143.  
  144.   if ((t = strrchr(p, c)) != NULL)
  145.     return t + 1;
  146.   else
  147. #ifndef AOS_VS
  148.     return p;
  149. #else
  150. /* We want to allow finding of end of path in either AOS/VS-style pathnames
  151.  * or Unix-style pathnames.  This presents a few little problems ...
  152.  */
  153.   {
  154.     if (*p == '='  ||  *p == '^')      /* like ./ and ../ respectively */
  155.       return p + 1;
  156.     else
  157.       return p;
  158.   }
  159. #endif
  160. }
  161.  
  162.  
  163. char *msname(n)
  164. char *n;
  165. /* Reduce all path components to MSDOS upper case 8.3 style names.  Probably
  166.    should also check for invalid characters, but I don't know which ones
  167.    those are.  We at least remove spaces and colons. */
  168. {
  169.   int c;                /* current character */
  170.   int f;                /* characters in current component */
  171.   char *p;              /* source pointer */
  172.   char *q;              /* destination pointer */
  173.  
  174.   p = q = n;
  175.   f = 0;
  176.   while ((c = (unsigned char)*p++) != 0)
  177.     if (c == ' ' || c == ':' /* or anything else illegal */ )
  178.       q++;                              /* char is discarded */
  179.     else if (c == '/')
  180.     {
  181.       *q++ = (char)c;
  182.       f = 0;                            /* new component */
  183.     }
  184. #ifdef __human68k__
  185.     else if (iskanji(c))
  186.     {
  187.       if (f == 7 || f == 11)
  188.         f++;
  189.       else if (*p != '\0' && f < 12 && f != 8)
  190.       {
  191.         *q++ = c;
  192.         *q++ = *p++;
  193.         f += 2;
  194.       }
  195.     }
  196. #endif /* __human68k__ */
  197.     else if (c == '.')
  198.       if (f < 9)
  199.       {
  200.         *q++ = (char)c;
  201.         f = 9;                          /* now in file type */
  202.       }
  203.       else
  204.         f = 12;                         /* now just excess characters */
  205.     else
  206.       if (f < 12 && f != 8)
  207.       {
  208.         *q++ = (char)(to_up(c));
  209.         f++;                            /* do until end of name or type */
  210.       }
  211.   *q = 0;
  212.   return n;
  213. }
  214.  
  215. int check_dup()
  216. /* Sort the found list and remove duplicates.
  217.    Return an error code in the ZE_ class. */
  218. {
  219.   struct flist far *f;          /* steps through found linked list */
  220.   extent j, k;                  /* indices for s */
  221.   struct flist far **s;         /* sorted table */
  222.   struct flist far **nodup;     /* sorted table without duplicates */
  223.  
  224.   /* sort found list, remove duplicates */
  225.   if (fcount)
  226.   {
  227.     if ((s = (struct flist far **)malloc(
  228.          fcount * sizeof(struct flist far *))) == NULL)
  229.       return ZE_MEM;
  230.     for (j = 0, f = found; f != NULL; f = f->nxt)
  231.       s[j++] = f;
  232.     /* Check names as given (f->name) */
  233.     qsort((char *)s, fcount, sizeof(struct flist far *), fqcmp);
  234.     for (k = j = fcount - 1; j > 0; j--)
  235.       if (strcmp(s[j - 1]->name, s[j]->name) == 0)
  236.         /* remove duplicate entry from list */
  237.         fexpel(s[j]);           /* fexpel() changes fcount */
  238.       else
  239.         /* copy valid entry into destination position */
  240.         s[k--] = s[j];
  241.     s[k] = s[0];                /* First entry is always valid */
  242.     nodup = &s[k];              /* Valid entries are at end of array s */
  243.  
  244.     /* sort only valid items and check for unique internal names */
  245.     qsort((char *)nodup, fcount, sizeof(struct flist far *), fqcmpz);
  246.     for (j = 1; j < fcount; j++)
  247.       if (strcmp(nodup[j - 1]->zname, nodup[j]->zname) == 0)
  248.       {
  249.         zipwarn("  first full name: ", nodup[j - 1]->name);
  250.         zipwarn(" second full name: ", nodup[j]->name);
  251.         zipwarn("name in zip file repeated: ", nodup[j]->zname);
  252.         return ZE_PARMS;
  253.       }
  254.     free((zvoid *)s);
  255.   }
  256.   return ZE_OK;
  257. }
  258.  
  259. int filter(name)
  260.   char *name;
  261.   /* Scan the -i and -x lists for matches to the given name.
  262.      Return true if the name must be included, false otherwise.
  263.      Give precedence to -x over -i.
  264.    */
  265. {
  266.    int n, slashes;
  267.    int include = icount ? 0 : 1;
  268.    char *p, *q;
  269.  
  270.    if (pcount == 0) return 1;
  271.  
  272.    for (n = 0; n < pcount; n++) {
  273.       if (!patterns[n].zname[0])        /* it can happen... */
  274.           continue;
  275.       p = name;
  276.       if (patterns[n].select == 'R') {
  277.          /* With -R patterns, if the pattern has N path components (that is, */
  278.          /* N-1 slashes), then we test only the last N components of name.   */
  279.          slashes = 0;
  280.          for (q = patterns[n].zname; (q = strchr(q, '/')) != NULL; q++)
  281.             slashes++;
  282.          for (q = p + strlen(p); q > p; q--)
  283.             if (q[-1] == '/')
  284.                if (!slashes--) {
  285.                   p = q;
  286.                   break;
  287.                }
  288.       }
  289.       if (MATCH(patterns[n].zname, p)) {
  290.          if (patterns[n].select == 'x') return 0;
  291.          include = 1;
  292.       }
  293.    }
  294.    return include;
  295. }
  296.  
  297. int newname(name, isdir)
  298. char *name;                /* name to add (or exclude) */
  299. int  isdir;             /* true for a directory */
  300. /* Add (or exclude) the name of an existing disk file.  Return an error
  301.    code in the ZE_ class. */
  302. {
  303.   char *iname, *zname;  /* Internal name, external version of iname */
  304.   char *undosm;
  305.   struct flist far *f;  /* where in found, or new found entry */
  306.   struct zlist far *z;  /* where in zfiles (if found) */
  307.   int dosflag;
  308.  
  309.   /* Search for name in zip file.  If there, mark it, else add to
  310.      list of new names to do (or remove from that list). */
  311.   if ((iname = ex2in(name, isdir, &dosflag)) == NULL)
  312.     return ZE_MEM;
  313.  
  314.   /* Discard directory names with zip -rj */
  315.   if (*iname == '\0') {
  316. #ifndef AMIGA
  317. /* A null string is a legitimate external directory name in AmigaDOS; also,
  318.  * a command like "zip -r zipfile FOO:" produces an empty internal name.
  319.  */
  320. # ifndef RISCOS
  321.  /* If extensions needs to be swapped, we will have empty directory names
  322.     instead of the original directory. For example, zipping 'c.', 'c.main'
  323.     should zip only 'main.c' while 'c.' will be converted to '\0' by ex2in. */
  324.  
  325.     if (pathput && !recurse) error("empty name without -j or -r");
  326.  
  327. # endif /* !RISCOS */
  328. #endif /* !AMIGA */
  329.     free((zvoid *)iname);
  330.     return ZE_OK;
  331.   }
  332.   undosm = iname;
  333.   if (dosflag || !pathput) {
  334.     int save_dosify = dosify, save_pathput = pathput;
  335.     dosify = 0;
  336.     pathput = 1;
  337.     if ((undosm = ex2in(name, isdir, NULL)) == NULL)
  338.       undosm = iname;
  339.     dosify = save_dosify;
  340.     pathput = save_pathput;
  341.   }
  342.   if ((zname = in2ex(iname)) == NULL)
  343.     return ZE_MEM;
  344.   if ((z = zsearch(zname)) != NULL) {
  345.     if (pcount && !filter(undosm)) {
  346.       /* Do not clear z->mark if "exclude", because, when "dosify || !pathput"
  347.        * is in effect, two files with different filter options may hit the
  348.        * same z entry.
  349.        */
  350.       if (verbose)
  351.         fprintf(mesg, "excluding %s\n", zname);
  352.       free((zvoid *)iname);
  353.       free((zvoid *)zname);
  354.     } else {
  355.       z->mark = 1;
  356.       if ((z->name = malloc(strlen(name) + 1 + PAD)) == NULL) {
  357.         if (undosm != iname)
  358.           free((zvoid *)undosm);
  359.         free((zvoid *)iname);
  360.         free((zvoid *)zname);
  361.         return ZE_MEM;
  362.       }
  363.       strcpy(z->name, name);
  364.       z->dosflag = dosflag;
  365.  
  366. #ifdef FORCE_NEWNAME
  367.       free((zvoid *)(z->iname));
  368.       z->iname = iname;
  369. #else
  370.       /* Better keep the old name. Useful when updating on MSDOS a zip file
  371.        * made on Unix.
  372.        */
  373.       free((zvoid *)iname);
  374.       free((zvoid *)zname);
  375. #endif /* ? FORCE_NEWNAME */
  376.     }
  377.     if (name == label) {
  378.        label = z->name;
  379.     }
  380.   } else if (pcount == 0 || filter(undosm)) {
  381.  
  382.     /* Check that we are not adding the zip file to itself. This
  383.      * catches cases like "zip -m foo ../dir/foo.zip".
  384.      */
  385. #ifndef CMS_MVS
  386. /* Version of stat() for CMS/MVS isn't complete enough to see if       */
  387. /* files match.  Just let ZIP.C compare the filenames.  That's good    */
  388. /* enough for CMS anyway since there aren't paths to worry about.      */
  389.     struct stat statb;
  390.  
  391.     if (zipstate == -1)
  392.        zipstate = strcmp(zipfile, "-") != 0 &&
  393.                    stat(zipfile, &zipstatb) == 0;
  394.  
  395.     if (zipstate == 1 && (statb = zipstatb, stat(name, &statb) == 0
  396.       && zipstatb.st_mode  == statb.st_mode
  397.       && zipstatb.st_ino   == statb.st_ino
  398.       && zipstatb.st_dev   == statb.st_dev
  399.       && zipstatb.st_uid   == statb.st_uid
  400.       && zipstatb.st_gid   == statb.st_gid
  401.       && zipstatb.st_size  == statb.st_size
  402.       && zipstatb.st_mtime == statb.st_mtime
  403.       && zipstatb.st_ctime == statb.st_ctime)) {
  404.       /* Don't compare a_time since we are reading the file */
  405.          if (verbose)
  406.            fprintf(mesg, "file matches zip file -- skipping\n");
  407.          if (undosm != iname)
  408.            free((zvoid *)undosm);
  409.          free((zvoid *)iname);
  410.          free((zvoid *)zname);
  411.          return ZE_OK;
  412.     }
  413. #endif  /* CMS_MVS */
  414.  
  415.     /* allocate space and add to list */
  416.     if ((f = (struct flist far *)farmalloc(sizeof(struct flist))) == NULL ||
  417.         (f->name = malloc(strlen(name) + 1 + PAD)) == NULL)
  418.     {
  419.       if (f != NULL)
  420.         farfree((zvoid far *)f);
  421.       if (undosm != iname)
  422.         free((zvoid *)undosm);
  423.       free((zvoid *)iname);
  424.       free((zvoid *)zname);
  425.       return ZE_MEM;
  426.     }
  427.     strcpy(f->name, name);
  428.     f->iname = iname;
  429.     f->zname = zname;
  430.     f->dosflag = dosflag;
  431.     *fnxt = f;
  432.     f->lst = fnxt;
  433.     f->nxt = NULL;
  434.     fnxt = &f->nxt;
  435.     fcount++;
  436.     if (name == label) {
  437.       label = f->name;
  438.     }
  439.   }
  440.   if (undosm != iname)
  441.     free((zvoid *)undosm);
  442.   return ZE_OK;
  443. }
  444.  
  445.  
  446. #if !defined(OS2) && !defined(VMS)
  447.  
  448. time_t dos2unixtime(dostime)
  449. ulg dostime;            /* DOS time to convert */
  450. /* Return the Unix time_t value (GMT/UTC time) for the DOS format (local)
  451.  * time dostime, where dostime is a four byte value (date in most significant
  452.  * word, time in least significant word), see dostime() function.
  453.  */
  454. {
  455.   struct tm *t;         /* argument for mktime() */
  456.   ZCONST time_t clock = time(NULL);
  457.  
  458.   t = localtime(&clock);
  459.   t->tm_isdst = -1;     /* let mktime() determine if DST is in effect */
  460.   /* Convert DOS time to UNIX time_t format */
  461.   t->tm_sec  = (((int)dostime) <<  1) & 0x3e;
  462.   t->tm_min  = (((int)dostime) >>  5) & 0x3f;
  463.   t->tm_hour = (((int)dostime) >> 11) & 0x1f;
  464.   t->tm_mday = (int)(dostime >> 16) & 0x1f;
  465.   t->tm_mon  = ((int)(dostime >> 21) & 0x0f) - 1;
  466.   t->tm_year = ((int)(dostime >> 25) & 0x7f) + 80;
  467.  
  468.   return mktime(t);
  469. }
  470.  
  471. #endif /* !OS2 && !VMS */
  472.  
  473.  
  474. ulg dostime(y, n, d, h, m, s)
  475. int y;                  /* year */
  476. int n;                  /* month */
  477. int d;                  /* day */
  478. int h;                  /* hour */
  479. int m;                  /* minute */
  480. int s;                  /* second */
  481. /* Convert the date y/n/d and time h:m:s to a four byte DOS date and
  482.    time (date in high two bytes, time in low two bytes allowing magnitude
  483.    comparison). */
  484. {
  485.   return y < 1980 ? dostime(1980, 1, 1, 0, 0, 0) :
  486.         (((ulg)y - 1980) << 25) | ((ulg)n << 21) | ((ulg)d << 16) |
  487.         ((ulg)h << 11) | ((ulg)m << 5) | ((ulg)s >> 1);
  488. }
  489.  
  490.  
  491. ulg unix2dostime(t)
  492. time_t *t;             /* unix time to convert */
  493. /* Return the Unix time t in DOS format, rounded up to the next two
  494.    second boundary. */
  495. {
  496.   time_t t_even;
  497.   struct tm *s;         /* result of localtime() */
  498.  
  499.   t_even = (*t + 1) & (~1);     /* Round up to even seconds. */
  500.   s = localtime(&t_even);       /* Use local time since MSDOS does. */
  501.   return dostime(s->tm_year + 1900, s->tm_mon + 1, s->tm_mday,
  502.                  s->tm_hour, s->tm_min, s->tm_sec);
  503. }
  504.  
  505. int issymlnk(a)
  506. ulg a;                  /* Attributes returned by filetime() */
  507. /* Return true if the attributes are those of a symbolic link */
  508. {
  509. #ifndef QDOS
  510. #ifdef S_IFLNK
  511.   return ((a >> 16) & S_IFMT) == S_IFLNK;
  512. #else /* !S_IFLNK */
  513.   return (int)a & 0;    /* avoid warning on unused parameter */
  514. #endif /* ?S_IFLNK */
  515. #else
  516.   return 0;
  517. #endif
  518. }
  519.  
  520. #endif /* !UTIL */
  521.  
  522. int destroy(f)
  523. char *f;                /* file to delete */
  524. /* Delete the file *f, returning non-zero on failure. */
  525. {
  526.   return unlink(f);
  527. }
  528.  
  529.  
  530. int replace(d, s)
  531. char *d, *s;            /* destination and source file names */
  532. /* Replace file *d by file *s, removing the old *s.  Return an error code
  533.    in the ZE_ class. This function need not preserve the file attributes,
  534.    this will be done by setfileattr() later.
  535.  */
  536. {
  537.   struct stat t;        /* results of stat() */
  538.   int copy = 0;
  539.   int d_exists;
  540.  
  541. #if defined(VMS) || defined(CMS_MVS)
  542.   /* stat() is broken on VMS remote files (accessed through Decnet).
  543.    * This patch allows creation of remote zip files, but is not sufficient
  544.    * to update them or compress remote files */
  545.   unlink(d);
  546. #else /* !(VMS || CMS_MVS) */
  547.   d_exists = (LSTAT(d, &t) == 0);
  548.   if (d_exists)
  549.   {
  550.     /*
  551.      * respect existing soft and hard links!
  552.      */
  553.     if (t.st_nlink > 1
  554. # ifdef S_IFLNK
  555.         || (t.st_mode & S_IFMT) == S_IFLNK
  556. # endif
  557.         )
  558.        copy = 1;
  559.     else if (unlink(d))
  560.        return ZE_CREAT;                 /* Can't erase zip file--give up */
  561.   }
  562. #endif /* ?(VMS || CMS_MVS) */
  563.   if (!copy) {
  564.       if (rename(s, d)) {               /* Just move s on top of d */
  565.           copy = 1;                     /* failed ? */
  566. #if !defined(VMS) && !defined(ATARI) && !defined(AZTEC_C)
  567. #if !defined(CMS_MVS) && !defined(RISCOS) && !defined(QDOS)
  568.     /* For VMS, ATARI, AMIGA Aztec, VM_CMS, MVS, RISCOS,
  569.        always assume that failure is EXDEV */
  570.           if (errno != EXDEV
  571. #  ifdef ENOTSAM
  572.            && errno != ENOTSAM /* Used at least on Turbo C */
  573. #  endif
  574.               ) return ZE_CREAT;
  575. #endif /* !CMS_MVS && !RISCOS */
  576. #endif /* !VMS && !ATARI && !AZTEC_C */
  577.       }
  578.   }
  579.  
  580.   if (copy) {
  581.     FILE *f, *g;      /* source and destination files */
  582.     int r;            /* temporary variable */
  583.  
  584. #ifdef RISCOS
  585.     if (SWI_OS_FSControl_26(s,d,0xA1)!=NULL) {
  586. #endif
  587.  
  588.     if ((f = fopen(s, FOPR)) == NULL) {
  589.       fprintf(stderr," replace: can't open %s\n", s);
  590.       return ZE_TEMP;
  591.     }
  592.     if ((g = fopen(d, FOPW)) == NULL)
  593.     {
  594.       fclose(f);
  595.       return ZE_CREAT;
  596.     }
  597.     r = fcopy(f, g, (ulg)-1L);
  598.     fclose(f);
  599.     if (fclose(g) || r != ZE_OK)
  600.     {
  601.       unlink(d);
  602.       return r ? (r == ZE_TEMP ? ZE_WRITE : r) : ZE_WRITE;
  603.     }
  604.     unlink(s);
  605. #ifdef RISCOS
  606.     }
  607. #endif
  608.   }
  609.   return ZE_OK;
  610. }
  611.  
  612.  
  613. int getfileattr(f)
  614. char *f;                /* file path */
  615. /* Return the file attributes for file f or 0 if failure */
  616. {
  617.   struct stat s;
  618.  
  619.   return SSTAT(f, &s) == 0 ? (int) s.st_mode : 0;
  620. }
  621.  
  622.  
  623. int setfileattr(f, a)
  624. char *f;                /* file path */
  625. int a;                  /* attributes returned by getfileattr() */
  626. /* Give the file f the attributes a, return non-zero on failure */
  627. {
  628. #if defined(TOPS20) || defined (CMS_MVS)
  629.   return 0;
  630. #else
  631.   return chmod(f, a);
  632. #endif
  633. }
  634.  
  635.  
  636. char *tempname(zip)
  637. char *zip;              /* path name of zip file to generate temp name for */
  638.  
  639. /* Return a temporary file name in its own malloc'ed space, using tempath. */
  640. {
  641.   char *t = zip;   /* malloc'ed space for name (use zip to avoid warning) */
  642.  
  643. #ifdef CMS_MVS
  644.   if ((t = malloc(strlen(tempath)+L_tmpnam+2)) == NULL)
  645.     return NULL;
  646.   tmpnam(t);
  647. #  ifdef VM_CMS
  648.   /* Remove filemode and replace with tempath, if any. */
  649.   /* Otherwise A-disk is used by default */
  650.   *(strrchr(t, ' ')+1) = '\0';
  651.   if (tempath!=NULL)
  652.      strcat(t, tempath);
  653.   return t;
  654. #  else   /* !VM_CMS */
  655.   /* For MVS */
  656.   if (tempath != NULL)
  657.   {
  658.     if ((t = malloc(strlen(tempath)+L_tmpnam+2)) == NULL)
  659.       return NULL;
  660.     strcpy(t, tempath);
  661.     if (t[strlen(t)]!='.')
  662.       strcat(t, ".");
  663.     strcat(t,tmpnam(NULL));
  664.     return t;
  665. #  endif  /* !VM_CMS */
  666. #else /* !CMS_MVS */
  667. #ifdef TANDEM
  668.   char cur_subvol [FILENAME_MAX];
  669.   char temp_subvol [FILENAME_MAX];
  670.   char *zptr;
  671.   char *ptr;
  672.   char *cptr = &cur_subvol[0];
  673.   char *tptr = &temp_subvol[0];
  674.   short err;
  675.  
  676.   t = (char *)malloc(NAMELEN); /* malloc here as you cannot free */
  677.                                /* tmpnam allocated storage later */
  678.  
  679.   zptr = strrchr(zip, TANDEM_DELIMITER);
  680.  
  681.   if (zptr != NULL) {
  682.     /* ZIP file specifies a Subvol so make temp file there so it can just
  683.        be renamed at end */
  684.  
  685.     *tptr = *cptr = '\0';
  686.     strcat(cptr, getenv("DEFAULTS"));
  687.  
  688.     strncat(tptr, zip, _min(FILENAME_MAX, (zptr - zip)) ); /* temp subvol */
  689.     strncat(t,zip, _min(NAMELEN, ((zptr - zip) + 1)) );    /* temp stem   */
  690.  
  691.     err = chvol(tptr);
  692.     ptr = t + strlen(t);  /* point to end of stem */
  693.     tmpnam(ptr);          /* Add filename part to temp subvol */
  694.     err = chvol(cptr);
  695.   }
  696.   else
  697.     t = tmpnam(t);
  698.  
  699.   return t;
  700.  
  701. #else /* !CMS_MVS && !TANDEM */
  702. /*
  703.  * Do something with TMPDIR, TMP, TEMP ????
  704.  */
  705.   if (tempath != NULL)
  706.   {
  707.     if ((t = malloc(strlen(tempath)+12)) == NULL)
  708.       return NULL;
  709.     strcpy(t, tempath);
  710. #if (!defined(VMS) && !defined(TOPS20))
  711. #  ifdef AMIGA
  712.     {
  713.           char c = t[strlen(t)-1];
  714.           if (c != '/' && c != ':')
  715.             strcat(t, "/");
  716.     }
  717. #  else /* !AMIGA */
  718. #    ifdef RISCOS
  719.        if (t[strlen(t)-1] != '.')
  720.          strcat(t, ".");
  721. #    else /* !RISCOS */
  722. #      ifdef QDOS
  723.          if (t[strlen(t)-1] != '_')
  724.            strcat(t, "_");
  725. #      else
  726.        if (t[strlen(t)-1] != '/')
  727.          strcat(t, "/");
  728. #      endif /* QDOS */
  729. #    endif /* RISCOS */
  730. #  endif  /* ?AMIGA */
  731. #endif /* !VMS && !TOPS20 */
  732.   }
  733.   else
  734.   {
  735.     if ((t = malloc(12)) == NULL)
  736.       return NULL;
  737.     *t = 0;
  738.   }
  739. #ifdef NO_MKTEMP
  740.   {
  741.     char *p = t + strlen(t);
  742.     sprintf(p, "%08lx", (ulg)time(NULL));
  743.     return t;
  744.   }
  745. #else
  746.   strcat(t, "ziXXXXXX"); /* must use lowercase for Linux dos file system */
  747.   return mktemp(t);
  748. #endif /* NO_MKTEMP */
  749. #endif /* TANDEM */
  750. #endif /* CMS_MVS */
  751. }
  752.  
  753.  
  754. int fcopy(f, g, n)
  755. FILE *f, *g;            /* source and destination files */
  756. ulg n;                  /* number of bytes to copy or -1 for all */
  757. /* Copy n bytes from file *f to file *g, or until EOF if n == -1.  Return
  758.    an error code in the ZE_ class. */
  759. {
  760.   char *b;              /* malloc'ed buffer for copying */
  761.   extent k;             /* result of fread() */
  762.   ulg m;                /* bytes copied so far */
  763.  
  764.   if ((b = malloc(CBSZ)) == NULL)
  765.     return ZE_MEM;
  766.   m = 0;
  767.   while (n == (ulg)(-1L) || m < n)
  768.   {
  769.     if ((k = fread(b, 1, n == (ulg)(-1) ?
  770.                    CBSZ : (n - m < CBSZ ? (extent)(n - m) : CBSZ), f)) == 0)
  771.       if (ferror(f))
  772.       {
  773.         free((zvoid *)b);
  774.         return ZE_READ;
  775.       }
  776.       else
  777.         break;
  778.     if (fwrite(b, 1, k, g) != k)
  779.     {
  780.       free((zvoid *)b);
  781.       fprintf(stderr," fcopy: write error\n");
  782.       return ZE_TEMP;
  783.     }
  784.     m += k;
  785.   }
  786.   free((zvoid *)b);
  787.   return ZE_OK;
  788. }
  789.  
  790. #ifdef NO_RENAME
  791. int rename(from, to)
  792. ZCONST char *from;
  793. ZCONST char *to;
  794. {
  795.     unlink(to);
  796.     if (link(from, to) == -1)
  797.         return -1;
  798.     if (unlink(from) == -1)
  799.         return -1;
  800.     return 0;
  801. }
  802.  
  803. #endif /* NO_RENAME */
  804.  
  805.  
  806. #ifdef ZMEM
  807.  
  808. /************************/
  809. /*  Function memset()  */
  810. /************************/
  811.  
  812. /*
  813.  * memset - for systems without it
  814.  *  bill davidsen - March 1990
  815.  */
  816.  
  817. char *
  818. memset(buf, init, len)
  819. register char *buf;     /* buffer loc */
  820. register int init;      /* initializer */
  821. register unsigned int len;   /* length of the buffer */
  822. {
  823.     char *start;
  824.  
  825.     start = buf;
  826.     while (len--) *(buf++) = init;
  827.     return(start);
  828. }
  829.  
  830.  
  831. /************************/
  832. /*  Function memcpy()  */
  833. /************************/
  834.  
  835. char *
  836. memcpy(dst,src,len)           /* v2.0f */
  837. register char *dst, *src;
  838. register unsigned int len;
  839. {
  840.     char *start;
  841.  
  842.     start = dst;
  843.     while (len--)
  844.         *dst++ = *src++;
  845.     return(start);
  846. }
  847.  
  848.  
  849. /************************/
  850. /*  Function memcmp()  */
  851. /************************/
  852.  
  853. int
  854. memcmp(b1,b2,len)                     /* jpd@usl.edu -- 11/16/90 */
  855. register char *b1, *b2;
  856. register unsigned int len;
  857. {
  858.  
  859.     if (len) do {             /* examine each byte (if any) */
  860.       if (*b1++ != *b2++)
  861.         return (*((uch *)b1-1) - *((uch *)b2-1));  /* exit when miscompare */
  862.        } while (--len);
  863.  
  864.     return(0);        /* no miscompares, yield 0 result */
  865. }
  866.  
  867. #endif  /* ZMEM */
  868.