home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / fileio.c < prev    next >
C/C++ Source or Header  |  1996-04-16  |  20KB  |  769 lines

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