home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / os2 / os2.c < prev    next >
C/C++ Source or Header  |  1996-04-25  |  16KB  |  599 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. #include "zip.h"
  12.  
  13. #include <time.h>
  14. #if defined(__IBMC__) || defined(MSC)
  15. #include <direct.h>
  16. #endif
  17.  
  18. #define MATCH shmatch
  19.  
  20. /* Extra malloc() space in names for cutpath() */
  21. #define PAD 0
  22. #define PATH_END '/'
  23.  
  24.  
  25. #include "os2zip.h"
  26.  
  27. /* Library functions not in (most) header files */
  28.  
  29. #ifndef UTIL    /* the companion #endif is a bit of ways down ... */
  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->zname))
  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. ztimbuf *t;             /* return value: access and modification time */
  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 and modification time
  371.    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->actime = t->modtime = 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.     if (fstat(fileno(stdin), &s) != 0)
  395.       error("fstat(stdin)");
  396.   } else if (LSSTAT(name, &s) != 0)
  397.              /* Accept about any file kind including directories
  398.               * (stored with trailing / with -r option)
  399.               */
  400.     return 0;
  401.  
  402.   if (a != NULL) {
  403.     *a = ((ulg)s.st_mode << 16) | (isstdin ? 0L : (ulg)GetFileMode(name));
  404.   }
  405.   if (n != NULL)
  406.     *n = (s.st_mode & S_IFMT) == S_IFREG ? s.st_size : -1L;
  407.   if (t != NULL) {
  408.     t->actime = s.st_atime;
  409.     t->modtime = s.st_mtime;
  410.   }
  411.  
  412.   return GetFileTime(name);
  413. }
  414.  
  415. int deletedir(d)
  416. char *d;                /* directory to delete */
  417. /* Delete the directory *d if it is empty, do nothing otherwise.
  418.    Return the result of rmdir(), delete(), or system().
  419.  */
  420. {
  421.     return rmdir(d);
  422. }
  423.  
  424. /******************************/
  425. /*  Function version_local()  */
  426. /******************************/
  427.  
  428. void version_local()
  429. {
  430.     static const char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";
  431. #if defined(__IBMC__) || defined(__WATCOMC__) || defined(_MSC_VER)
  432.     char buf[80];
  433. #endif
  434.  
  435.     printf(CompiledWith,
  436.  
  437. #ifdef __GNUC__
  438. #  ifdef __EMX__  /* __EMX__ is defined as "1" only (sigh) */
  439.       "emx+gcc ", __VERSION__,
  440. #  else
  441.       "gcc/2 ", __VERSION__,
  442. #  endif
  443. #else
  444. #ifdef __IBMC__
  445.       "IBM ",
  446. #  if (__IBMC__ < 200)
  447.       (sprintf(buf, "C Set/2 %d.%02d", __IBMC__/100,__IBMC__%100), buf),
  448. #  elif (__IBMC__ < 300)
  449.       (sprintf(buf, "C Set++ %d.%02d", __IBMC__/100,__IBMC__%100), buf),
  450. #  else
  451.       (sprintf(buf, "Visual Age C++ %d.%02d", __IBMC__/100,__IBMC__%100), buf),
  452. #  endif
  453. #else
  454. #ifdef __WATCOMC__
  455.       "Watcom C", (sprintf(buf, " (__WATCOMC__ = %d)", __WATCOMC__), buf),
  456. #else
  457. #ifdef __TURBOC__
  458. #  ifdef __BORLANDC__
  459.       "Borland C++",
  460. #    if (__BORLANDC__ < 0x0200)
  461.         " 1.0",
  462. #    else
  463. #    if (__BORLANDC__ == 0x0200)
  464.         " 2.0",
  465. #    else
  466. #    if (__BORLANDC__ == 0x0400)
  467.         " 3.0",
  468. #    else
  469. #    if (__BORLANDC__ == 0x0410)
  470.         " 3.1",
  471. #    else
  472. #    if (__BORLANDC__ == 0x0452)
  473.         " 4.0",
  474. #    else                    /* these two are guesses based on DOS version */
  475. #    if (__BORLANDC__ == 0x0460)
  476.         " 4.5",
  477. #    else
  478.         " later than 4.5",
  479. #    endif
  480. #    endif
  481. #    endif
  482. #    endif
  483. #    endif
  484. #    endif
  485. #  else
  486.       "Turbo C",
  487. #    if (__TURBOC__ >= 661)
  488.        "++ 1.0 or later",
  489. #    else
  490. #    if (__TURBOC__ == 661)
  491.        " 3.0?",
  492. #    else
  493. #    if (__TURBOC__ == 397)
  494.        " 2.0",
  495. #    else
  496.        " 1.0 or 1.5?",
  497. #    endif
  498. #    endif
  499. #    endif
  500. #  endif
  501. #else
  502. #ifdef MSC
  503.       "Microsoft C ",
  504. #  ifdef _MSC_VER
  505.       (sprintf(buf, "%d.%02d", _MSC_VER/100, _MSC_VER%100), buf),
  506. #  else
  507.       "5.1 or earlier",
  508. #  endif
  509. #else
  510.       "unknown compiler", "",
  511. #endif /* MSC */
  512. #endif /* __TURBOC__ */
  513. #endif /* __WATCOMC__ */
  514. #endif /* __IBMC__ */
  515. #endif /* __GNUC__ */
  516.  
  517.       "OS/2",
  518.  
  519. /* GRR:  does IBM C/2 identify itself as IBM rather than Microsoft? */
  520. #if (defined(MSC) || (defined(__WATCOMC__) && !defined(__386__)))
  521. #  if defined(M_I86HM) || defined(__HUGE__)
  522.       " (16-bit, huge)",
  523. #  else
  524. #  if defined(M_I86LM) || defined(__LARGE__)
  525.       " (16-bit, large)",
  526. #  else
  527. #  if defined(M_I86MM) || defined(__MEDIUM__)
  528.       " (16-bit, medium)",
  529. #  else
  530. #  if defined(M_I86CM) || defined(__COMPACT__)
  531.       " (16-bit, compact)",
  532. #  else
  533. #  if defined(M_I86SM) || defined(__SMALL__)
  534.       " (16-bit, small)",
  535. #  else
  536. #  if defined(M_I86TM) || defined(__TINY__)
  537.       " (16-bit, tiny)",
  538. #  else
  539.       " (16-bit)",
  540. #  endif
  541. #  endif
  542. #  endif
  543. #  endif
  544. #  endif
  545. #  endif
  546. #else
  547.       " 2.x (32-bit)",
  548. #endif
  549.  
  550. #ifdef __DATE__
  551.       " on ", __DATE__
  552. #else
  553.       "", ""
  554. #endif
  555.     );
  556.  
  557.     /* temporary debugging code for Borland compilers only */
  558. #ifdef __TURBOC__
  559.     printf("\t(__TURBOC__ = 0x%04x = %d)\n", __TURBOC__, __TURBOC__);
  560. #ifdef __BORLANDC__
  561.     printf("\t(__BORLANDC__ = 0x%04x)\n",__BORLANDC__);
  562. #else
  563.     printf("\tdebug(__BORLANDC__ not defined)\n");
  564. #endif
  565. #ifdef __TCPLUSPLUS__
  566.     printf("\t(__TCPLUSPLUS__ = 0x%04x)\n", __TCPLUSPLUS__);
  567. #else
  568.     printf("\tdebug(__TCPLUSPLUS__ not defined)\n");
  569. #endif
  570. #ifdef __BCPLUSPLUS__
  571.     printf("\t(__BCPLUSPLUS__ = 0x%04x)\n\n", __BCPLUSPLUS__);
  572. #else
  573.     printf("\tdebug(__BCPLUSPLUS__ not defined)\n\n");
  574. #endif
  575. #endif /* __TURBOC__ */
  576.  
  577. } /* end function version_local() */
  578.  
  579.  
  580. #if defined MY_ZCALLOC /* Special zcalloc function for MEMORY16 (MSDOS/OS2) */
  581.  
  582. #ifdef MSC  /* Microsoft C */
  583.  
  584. zvoid far *zcalloc (unsigned items, unsigned size)
  585. {
  586.     return (zvoid far *)halloc((long)items, size);
  587. }
  588.  
  589. zvoid zcfree (zvoid far *ptr)
  590. {
  591.     hfree((void huge *)ptr);
  592. }
  593.  
  594. #endif /* MSC */
  595.  
  596. #endif /* MY_ZCALLOC */
  597.  
  598. #endif /* !UTIL */
  599.