home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip22.zip / os2 / os2.c < prev    next >
C/C++ Source or Header  |  1997-08-19  |  13KB  |  457 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. #include "zip.h"
  12.  
  13. #ifndef UTIL    /* the companion #endif is a bit of ways down ... */
  14.  
  15. #include <time.h>
  16. #if defined(__IBMC__) || defined(MSC)
  17. #include <direct.h>
  18. #endif
  19.  
  20. #define MATCH shmatch
  21.  
  22. /* Extra malloc() space in names for cutpath() */
  23. #define PAD 0
  24. #define PATH_END '/'
  25.  
  26.  
  27. #include "os2zip.h"
  28.  
  29. /* Library functions not in (most) header files */
  30.  
  31. extern char *label;
  32. local ulg label_time = 0;
  33. local ulg label_mode = 0;
  34. local time_t label_utim = 0;
  35.  
  36. /* Local functions */
  37. local char *readd OF((DIR *));
  38.  
  39.  
  40. local char *readd(d)
  41. DIR *d;                 /* directory stream to read from */
  42. /* Return a pointer to the next name in the directory stream d, or NULL if
  43.    no more entries or an error occurs. */
  44. {
  45.   struct dirent *e;
  46.  
  47.   e = readdir(d);
  48.   return e == NULL ? (char *) NULL : e->d_name;
  49. }
  50.  
  51. int wild(w)
  52. char *w;                /* path/pattern to match */
  53. /* If not in exclude mode, expand the pattern based on the contents of the
  54.    file system.  Return an error code in the ZE_ class. */
  55. {
  56.   DIR *d;               /* stream for reading directory */
  57.   char *e;              /* name found in directory */
  58.   int r;                /* temporary variable */
  59.   char *n;              /* constructed name from directory */
  60.   int f;                /* true if there was a match */
  61.   char *a;              /* alloc'ed space for name */
  62.   char *p;              /* path */
  63.   char *q;              /* name */
  64.   char v[5];            /* space for device current directory */
  65.  
  66.   if (volume_label == 1) {
  67.     volume_label = 2;
  68.     label = getVolumeLabel((w != NULL && w[1] == ':') ? to_up(w[0]) : '\0',
  69.                            &label_time, &label_mode, &label_utim);
  70.     if (label != NULL) {
  71.        newname(label, 0);
  72.     }
  73.     if (w == NULL || (w[1] == ':' && w[2] == '\0')) return ZE_OK;
  74.     /* "zip -$ foo a:" can be used to force drive name */
  75.   }
  76.  
  77.   if (w == NULL)
  78.     return ZE_OK;
  79.  
  80.   /* special handling of stdin request */
  81.   if (strcmp(w, "-") == 0)   /* if compressing stdin */
  82.     return newname(w, 0);
  83.  
  84.   /* Allocate and copy pattern */
  85.   if ((p = a = malloc(strlen(w) + 1)) == NULL)
  86.     return ZE_MEM;
  87.   strcpy(p, w);
  88.  
  89.   /* catch special case: treat "*.*" as "*" for DOS-impaired people */
  90.   r = strlen(p);
  91.   if (strcmp(p + r - 3, "*.*") == 0)
  92.     p[r - 2] = '\0';
  93.  
  94.   /* Normalize path delimiter as '/'. */
  95.   for (q = p; *q; q++)                  /* use / consistently */
  96.     if (*q == '\\')
  97.       *q = '/';
  98.  
  99.   /* Only name can have special matching characters */
  100.   if ((q = isshexp(p)) != NULL &&
  101.       (strrchr(q, '/') != NULL || strrchr(q, ':') != NULL))
  102.   {
  103.     free((zvoid *)a);
  104.     return ZE_PARMS;
  105.   }
  106.  
  107.   /* Separate path and name into p and q */
  108.   if ((q = strrchr(p, '/')) != NULL && (q == p || q[-1] != ':'))
  109.   {
  110.     *q++ = '\0';                        /* path/name -> path, name */
  111.     if (*p == '\0')                     /* path is just / */
  112.       p = strcpy(v, "/.");
  113.   }
  114.   else if ((q = strrchr(p, ':')) != NULL)
  115.   {                                     /* has device and no or root path */
  116.     *q++ = '\0';
  117.     p = strcat(strcpy(v, p), ":");      /* copy device as path */
  118.     if (*q == '/')                      /* -> device:/., name */
  119.     {
  120.       strcat(p, "/");
  121.       q++;
  122.     }
  123.     strcat(p, ".");
  124.   }
  125.   else if (recurse && (strcmp(p, ".") == 0 ||  strcmp(p, "..") == 0))
  126.   {                                    /* current or parent directory */
  127.     /* I can't understand Mark's code so I am adding a hack here to get
  128.      * "zip -r foo ." to work. Allow the dubious "zip -r foo .." but
  129.      * reject "zip -rm foo ..".
  130.      */
  131.     if (dispose && strcmp(p, "..") == 0)
  132.        ziperr(ZE_PARMS, "cannot remove parent directory");
  133.     q = "*";
  134.   }
  135.   else                                  /* no path or device */
  136.   {
  137.     q = p;
  138.     p = strcpy(v, ".");
  139.   }
  140.   if (recurse && *q == '\0') {
  141.     q = "*";
  142.   }
  143.   /* Search that level for matching names */
  144.   if ((d = opendir(p)) == NULL)
  145.   {
  146.     free((zvoid *)a);
  147.     return ZE_MISS;
  148.   }
  149.   if ((r = strlen(p)) > 1 &&
  150.       (strcmp(p + r - 2, ":.") == 0 || strcmp(p + r - 2, "/.") == 0))
  151.     *(p + r - 1) = '\0';
  152.   f = 0;
  153.   while ((e = readd(d)) != NULL) {
  154.     if (strcmp(e, ".") && strcmp(e, "..") && MATCH(q, e))
  155.     {
  156.       f = 1;
  157.       if (strcmp(p, ".") == 0) {                /* path is . */
  158.         r = procname(e);                        /* name is name */
  159.         if (r) {
  160.            f = 0;
  161.            break;
  162.         }
  163.       } else
  164.       {
  165.         if ((n = malloc(strlen(p) + strlen(e) + 2)) == NULL)
  166.         {
  167.           free((zvoid *)a);
  168.           closedir(d);
  169.           return ZE_MEM;
  170.         }
  171.         n = strcpy(n, p);
  172.         if (n[r = strlen(n) - 1] != '/' && n[r] != ':')
  173.           strcat(n, "/");
  174.         r = procname(strcat(n, e));             /* name is path/name */
  175.         free((zvoid *)n);
  176.         if (r) {
  177.           f = 0;
  178.           break;
  179.         }
  180.       }
  181.     }
  182.   }
  183.   closedir(d);
  184.  
  185.   /* Done */
  186.   free((zvoid *)a);
  187.   return f ? ZE_OK : ZE_MISS;
  188. }
  189.  
  190. int procname(n)
  191. char *n;                /* name to process */
  192. /* Process a name or sh expression to operate on (or exclude).  Return
  193.    an error code in the ZE_ class. */
  194. {
  195.   char *a;              /* path and name for recursion */
  196.   DIR *d;               /* directory stream from opendir() */
  197.   char *e;              /* pointer to name from readd() */
  198.   int m;                /* matched flag */
  199.   char *p;              /* path for recursion */
  200.   struct stat s;        /* result of stat() */
  201.   struct zlist far *z;  /* steps through zfiles list */
  202.  
  203.   if (n == NULL)        /* volume_label request in freshen|delete mode ?? */
  204.     return ZE_OK;
  205.  
  206.   if (strcmp(n, "-") == 0)   /* if compressing stdin */
  207.     return newname(n, 0);
  208.   else if (LSSTAT(n, &s)
  209. #if defined(__TURBOC__) || defined(__WATCOMC__)
  210.            /* For these 2 compilers, stat() succeeds on wild card names! */
  211.            || isshexp(n)
  212. #endif
  213.           )
  214.   {
  215.     /* Not a file or directory--search for shell expression in zip file */
  216.     p = ex2in(n, 0, (int *)NULL);       /* shouldn't affect matching chars */
  217.     m = 1;
  218.     for (z = zfiles; z != NULL; z = z->nxt) {
  219.       if (MATCH(p, z->iname))
  220.       {
  221.         z->mark = pcount ? filter(z->zname) : 1;
  222.         if (verbose)
  223.             fprintf(mesg, "zip diagnostic: %scluding %s\n",
  224.                z->mark ? "in" : "ex", z->name);
  225.         m = 0;
  226.       }
  227.     }
  228.     free((zvoid *)p);
  229.     return m ? ZE_MISS : ZE_OK;
  230.   }
  231.  
  232.   /* Live name--use if file, recurse if directory */
  233.   for (p = n; *p; p++)          /* use / consistently */
  234.     if (*p == '\\')
  235.       *p = '/';
  236.   if ((s.st_mode & S_IFDIR) == 0)
  237.   {
  238.     /* add or remove name of file */
  239.     if ((m = newname(n, 0)) != ZE_OK)
  240.       return m;
  241.   } else {
  242.     /* Add trailing / to the directory name */
  243.     if ((p = malloc(strlen(n)+2)) == NULL)
  244.       return ZE_MEM;
  245.     if (strcmp(n, ".") == 0 || strcmp(n, "/.") == 0) {
  246.       *p = '\0';  /* avoid "./" prefix and do not create zip entry */
  247.     } else {
  248.       strcpy(p, n);
  249.       a = p + strlen(p);
  250.       if (a[-1] != '/')
  251.         strcpy(a, "/");
  252.       if (dirnames && (m = newname(p, 1)) != ZE_OK) {
  253.         free((zvoid *)p);
  254.         return m;
  255.       }
  256.     }
  257.     /* recurse into directory */
  258.     if (recurse && (d = opendir(n)) != NULL)
  259.     {
  260.       while ((e = readd(d)) != NULL) {
  261.         if (strcmp(e, ".") && strcmp(e, ".."))
  262.         {
  263.           if ((a = malloc(strlen(p) + strlen(e) + 1)) == NULL)
  264.           {
  265.             closedir(d);
  266.             free((zvoid *)p);
  267.             return ZE_MEM;
  268.           }
  269.           strcat(strcpy(a, p), e);
  270.           if ((m = procname(a)) != ZE_OK)   /* recurse on name */
  271.           {
  272.             if (m == ZE_MISS)
  273.               zipwarn("name not matched: ", a);
  274.             else
  275.               ziperr(m, a);
  276.           }
  277.           free((zvoid *)a);
  278.         }
  279.       }
  280.       closedir(d);
  281.     }
  282.     free((zvoid *)p);
  283.   } /* (s.st_mode & S_IFDIR) == 0) */
  284.   return ZE_OK;
  285. }
  286.  
  287. char *ex2in(x, isdir, pdosflag)
  288. char *x;                /* external file name */
  289. int isdir;              /* input: x is a directory */
  290. int *pdosflag;          /* output: force MSDOS file attributes? */
  291. /* Convert the external file name to a zip file name, returning the malloc'ed
  292.    string or NULL if not enough memory. */
  293. {
  294.   char *n;              /* internal file name (malloc'ed) */
  295.   char *t;              /* shortened name */
  296.   int dosflag;
  297.  
  298.   dosflag = dosify || IsFileSystemFAT(x) || (x == label);
  299.   if (!dosify && use_longname_ea && (t = GetLongPathEA(x)) != NULL)
  300.   {
  301.     x = t;
  302.     dosflag = 0;
  303.   }
  304.  
  305.   /* Find starting point in name before doing malloc */
  306.   t = *x && *(x + 1) == ':' ? x + 2 : x;
  307.   while (*t == '/' || *t == '\\')
  308.     t++;
  309.  
  310.   /* Make changes, if any, to the copied name (leave original intact) */
  311.   for (n = t; *n; n++)
  312.     if (*n == '\\')
  313.       *n = '/';
  314.  
  315.   if (!pathput)
  316.     t = last(t, PATH_END);
  317.  
  318.   /* Malloc space for internal name and copy it */
  319.   if ((n = malloc(strlen(t) + 1)) == NULL)
  320.     return NULL;
  321.   strcpy(n, t);
  322.  
  323.   if (dosify)
  324.     msname(n);
  325.  
  326.   /* Returned malloc'ed name */
  327.   if (pdosflag)
  328.     *pdosflag = dosflag;
  329.   return n;
  330. }
  331.  
  332.  
  333. char *in2ex(n)
  334. char *n;                /* internal file name */
  335. /* Convert the zip file name to an external file name, returning the malloc'ed
  336.    string or NULL if not enough memory. */
  337. {
  338.   char *x;              /* external file name */
  339.  
  340.   if ((x = malloc(strlen(n) + 1 + PAD)) == NULL)
  341.     return NULL;
  342.   strcpy(x, n);
  343.  
  344.   if ( !IsFileNameValid(x) )
  345.     ChangeNameForFAT(x);
  346.   return x;
  347. }
  348.  
  349.  
  350. void stamp(f, d)
  351. char *f;                /* name of file to change */
  352. ulg d;                  /* dos-style time to change it to */
  353. /* Set last updated and accessed time of file f to the DOS time d. */
  354. {
  355.   SetFileTime(f, d);
  356. }
  357.  
  358. ulg filetime(f, a, n, t)
  359. char *f;                /* name of file to get info on */
  360. ulg *a;                 /* return value: file attributes */
  361. long *n;                /* return value: file size */
  362. iztimes *t;             /* return value: access, modific. and creation times */
  363. /* If file *f does not exist, return 0.  Else, return the file's last
  364.    modified date and time as an MSDOS date and time.  The date and
  365.    time is returned in a long with the date most significant to allow
  366.    unsigned integer comparison of absolute times.  Also, if a is not
  367.    a NULL pointer, store the file attributes there, with the high two
  368.    bytes being the Unix attributes, and the low byte being a mapping
  369.    of that to DOS attributes.  If n is not NULL, store the file size
  370.    there.  If t is not NULL, the file's access, modification and creation
  371.    times are stored there as UNIX time_t values.
  372.    If f is "-", use standard input as the file. If f is a device, return
  373.    a file size of -1 */
  374. {
  375.   struct stat s;        /* results of stat() */
  376.   char name[FNMAX];
  377.   int len = strlen(f), isstdin = !strcmp(f, "-");
  378.  
  379.   if (f == label) {
  380.     if (a != NULL)
  381.       *a = label_mode;
  382.     if (n != NULL)
  383.       *n = -2L; /* convention for a label name */
  384.     if (t != NULL)
  385.       t->atime = t->mtime = t->ctime = label_utim;
  386.     return label_time;
  387.   }
  388.   strcpy(name, f);
  389.   if (name[len - 1] == '/')
  390.     name[len - 1] = '\0';
  391.   /* not all systems allow stat'ing a file with / appended */
  392.  
  393.   if (isstdin) {
  394.     /* it is common for some PC based compilers to 
  395.        fail with fstat() on devices or pipes */
  396.     if (fstat(fileno(stdin), &s) != 0) {
  397.       s.st_mode = S_IFREG; s.st_size = -1L;
  398.     }
  399.     time(&s.st_ctime);
  400.     s.st_atime = s.st_mtime = s.st_ctime;
  401.   } else if (LSSTAT(name, &s) != 0)
  402.              /* Accept about any file kind including directories
  403.               * (stored with trailing / with -r option)
  404.               */
  405.     return 0;
  406.  
  407.   if (a != NULL) {
  408.     *a = ((ulg)s.st_mode << 16) | (isstdin ? 0L : (ulg)GetFileMode(name));
  409.   }
  410.   if (n != NULL)
  411.     *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  412. #ifdef __WATCOMC__
  413.   /* of course, Watcom always has to make an exception */
  414.   if (s.st_atime == 312764400)
  415.     s.st_atime = s.st_mtime;
  416.   if (s.st_ctime == 312764400)
  417.     s.st_ctime = s.st_mtime;
  418. #endif
  419.   if (t != NULL) {
  420.     t->atime = s.st_atime;
  421.     t->mtime = s.st_mtime;
  422.     t->ctime = s.st_ctime;
  423.   }
  424.  
  425.   return GetFileTime(name);
  426. }
  427.  
  428. int deletedir(d)
  429. char *d;                /* directory to delete */
  430. /* Delete the directory *d if it is empty, do nothing otherwise.
  431.    Return the result of rmdir(), delete(), or system().
  432.  */
  433. {
  434.     return rmdir(d);
  435. }
  436.  
  437.  
  438. #if defined MY_ZCALLOC /* Special zcalloc function for MEMORY16 (MSDOS/OS2) */
  439.  
  440. #ifdef MSC  /* Microsoft C */
  441.  
  442. zvoid far *zcalloc (unsigned items, unsigned size)
  443. {
  444.     return (zvoid far *)halloc((long)items, size);
  445. }
  446.  
  447. zvoid zcfree (zvoid far *ptr)
  448. {
  449.     hfree((void huge *)ptr);
  450. }
  451.  
  452. #endif /* MSC */
  453.  
  454. #endif /* MY_ZCALLOC */
  455.  
  456. #endif /* !UTIL */
  457.