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

  1. /*
  2.  
  3.  Copyright (C) 1990-1997 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel 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. #include "zip.h"
  12.  
  13. #ifndef UTIL    /* the companion #endif is a bit of ways down ... */
  14.  
  15. #include <time.h>
  16. #include <utime.h>
  17.  
  18. #ifndef MATCH
  19. # define MATCH shmatch
  20. #endif
  21.  
  22. #include <errno.h>
  23.  
  24. #ifdef S_IWRITE
  25. # undef S_IWRITE
  26. #endif /* S_IWRITE */
  27. #define S_IWRITE S_IWUSR
  28.  
  29. #define MSDOS_DIR_ATTR 0x10
  30.  
  31. local struct stat zipstatb;
  32. local int zipstate = -1;
  33. local char *label = NULL;
  34. local ulg label_time = 0;
  35. local ulg label_mode = 0;
  36. local time_t label_utim = 0;
  37.  
  38.  
  39. typedef time_t statime;
  40. #define PAD 0
  41. #define PATH_END '/'
  42.  
  43. /* Local functions */
  44.  
  45. int procname(n)
  46. char *n;                /* name to process */
  47. /* Process a name or sh expression to operate on (or exclude).  Return
  48.    an error code in the ZE_ class. */
  49. {
  50.   char *a;              /* path and name for recursion */
  51.   int m;                /* matched flag */
  52.   char *p;              /* path for recursion */
  53.   struct stat s;        /* result of stat() */
  54.   struct zlist far *z;  /* steps through zfiles list */
  55.  
  56.   if (strcmp(n, "-") == 0)   /* if compressing stdin */
  57.     return newname(n, 0);
  58.   else if (SSTAT(n, &s) )
  59.   {
  60.     /* Not a file or directory--search for shell expression in zip file */
  61.     p = ex2in(n, 0, (int *)NULL);       /* shouldn't affect matching chars */
  62.  
  63.     m = 1;
  64.     for (z = zfiles; z != NULL; z = z->nxt) {
  65.       if (MATCH(p, z->iname))
  66.       {
  67.         z->mark = pcount ? filter(z->zname) : 1;
  68.         if (verbose)
  69.             fprintf(mesg, "zip diagnostic: %scluding %s\n",
  70.                z->mark ? "in" : "ex", z->name);
  71.         m = 0;
  72.       }
  73.     }
  74.     free(p);
  75.     return m ? ZE_MISS : ZE_OK;
  76.   }
  77.  
  78.   /* Live name--use if file, recurse if directory */
  79.  
  80.   if ((s.st_mode & S_IFDIR) == 0)
  81.   {
  82.     /* add or remove name of file */
  83.     if ((m = newname(n, 0)) != ZE_OK)
  84.       return m;
  85.   }
  86.   return ZE_OK;
  87. }
  88.  
  89.  
  90.  
  91. char *ex2in(x, isdir, pdosflag)
  92. char *x;                /* external file name */
  93. int isdir;              /* input: x is a directory */
  94. int *pdosflag;          /* output: force MSDOS file attributes? */
  95. /* Convert the external file name to a zip file name, returning the malloc'ed
  96.    string or NULL if not enough memory. */
  97. {
  98.   char *n;              /* internal file name (malloc'ed) */
  99.   char *t;              /* shortened name */
  100.   int dosflag;
  101.  
  102.   t = ql2Unix(x);
  103.  
  104.   dosflag = dosify; /* default for non-DOS and non-OS/2 */
  105.  
  106.   /* Make changes, if any, to the copied name (leave original intact) */
  107.  
  108.   if (!pathput)
  109.     t = last(t, PATH_END);
  110.  
  111.   /* Discard directory names with zip -rj */
  112.   if (*t == '\0')
  113.     return t;
  114.  
  115.   /* Malloc space for internal name and copy it */
  116.   if ((n = malloc(strlen(t) + 1)) == NULL)
  117.     return NULL;
  118.   strcpy(n, t);
  119.  
  120.   if (dosify)
  121.     msname(n);
  122.   /* Returned malloc'ed name */
  123.   if (pdosflag)
  124.     *pdosflag = dosflag;
  125.   return n;
  126. }
  127.  
  128.  
  129. char *in2ex(n)
  130. char *n;                /* internal file name */
  131. /* Convert the zip file name to an external file name, returning the malloc'ed
  132.    string or NULL if not enough memory. */
  133. {
  134.   char *x;              /* external file name */
  135.  
  136.   if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  137.        return NULL;
  138.   strcpy(x, Unix2ql(n, NULL));
  139.   return x;
  140. }
  141.  
  142.  
  143. void stamp(f, d)
  144. char *f;                /* name of file to change */
  145. ulg d;                  /* dos-style time to change it to */
  146. /* Set last updated and accessed time of file f to the DOS time d. */
  147. {
  148.   struct utimbuf u;     /* argument for utime()  const ?? */
  149.  
  150.   /* Convert DOS time to time_t format in u */
  151.   u.actime = u.modtime = dos2unixtime(d);
  152.   utime(f, &u);
  153. }
  154.  
  155. ulg filetime(f, a, n, t)
  156. char *f;                /* name of file to get info on */
  157. ulg *a;                 /* return value: file attributes */
  158. long *n;                /* return value: file size */
  159. iztimes *t;             /* return value: access, modific. and creation times */
  160. /* If file *f does not exist, return 0.  Else, return the file's last
  161.    modified date and time as an MSDOS date and time.  The date and
  162.    time is returned in a long with the date most significant to allow
  163.    unsigned integer comparison of absolute times.  Also, if a is not
  164.    a NULL pointer, store the file attributes there, with the high two
  165.    bytes being the Unix attributes, and the low byte being a mapping
  166.    of that to DOS attributes.  If n is not NULL, store the file size
  167.    there.  If t is not NULL, the file's access, modification and creation
  168.    times are stored there as UNIX time_t values.
  169.    If f is "-", use standard input as the file. If f is a device, return
  170.    a file size of -1 */
  171. {
  172.   struct stat s;        /* results of stat() */
  173.   char name[FNMAX];
  174.   int len = strlen(f);
  175.  
  176.   if (f == label) {
  177.     if (a != NULL)
  178.       *a = label_mode;
  179.     if (n != NULL)
  180.       *n = -2L; /* convention for a label name */
  181.     if (t != NULL)
  182.       t->atime = t->mtime = t->ctime = label_utim;
  183.     return label_time;
  184.   }
  185.   strcpy(name, f);
  186.   if (name[len - 1] == '/')
  187.     name[len - 1] = '\0';
  188.   /* not all systems allow stat'ing a file with / appended */
  189.   if (strcmp(f, "-") == 0) {
  190.     if (fstat(fileno(stdin), &s) != 0)
  191.       error("fstat(stdin)");
  192.   } else if (LSSTAT(name, &s) != 0)
  193.              /* Accept about any file kind including directories
  194.               * (stored with trailing / with -r option)
  195.               */
  196.     return 0;
  197.  
  198.   if (a != NULL) {
  199.     *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);
  200.     if ((s.st_mode & S_IFMT) == S_IFDIR) {
  201.       *a |= MSDOS_DIR_ATTR;
  202.     }
  203.   }
  204.   if (n != NULL)
  205.     *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  206.   if (t != NULL) {
  207.     t->atime = s.st_atime;
  208.     t->mtime = s.st_mtime;
  209.     t->ctime = s.st_ctime;
  210.   }
  211.  
  212.   return unix2dostime(&s.st_mtime);
  213. }
  214.  
  215.  
  216. int deletedir(d)
  217. char *d;                /* directory to delete */
  218. /* Delete the directory *d if it is empty, do nothing otherwise.
  219.    Return the result of rmdir(), delete(), or system().
  220.    For VMS, d must be in format [x.y]z.dir;1  (not [x.y.z]).
  221.  */
  222. {
  223.     return rmdir(d);
  224. }
  225.  
  226. #endif /* !UTIL */
  227.  
  228.  
  229. void version_local()
  230. {
  231.     puts ("Compiled with c68 v4.2x on " __DATE__);
  232. }
  233.