home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / util.c < prev    next >
C/C++ Source or Header  |  1996-04-01  |  15KB  |  497 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.  *  util.c by Mark Adler.
  13.  */
  14.  
  15. #include "zip.h"
  16. #include <ctype.h>
  17.  
  18. #ifdef MSDOS16
  19. #  include <dos.h>
  20. #endif
  21.  
  22. uch upper[256], lower[256];
  23. /* Country-dependent case map table */
  24.  
  25.  
  26. #ifndef UTIL /* UTIL picks out namecmp code (all utils) */
  27.  
  28. /* Local functions */
  29. local int recmatch OF((uch *, uch *));
  30. local int count_args OF((char *s));
  31.  
  32. #ifdef MSDOS16
  33.   local unsigned ident OF((unsigned chr));
  34. #endif
  35.  
  36. #ifdef NO_MKTIME
  37. #include "mktime.c"
  38. #endif
  39.  
  40. char *isshexp(p)
  41. char *p;                /* candidate sh expression */
  42. /* If p is a sh expression, a pointer to the first special character is
  43.    returned.  Otherwise, NULL is returned. */
  44. {
  45.   for (; *p; p++)
  46.     if (*p == '\\' && *(p+1))
  47.       p++;
  48. #ifdef VMS
  49.     else if (*p == '%' || *p == '*')
  50. #else /* !VMS */
  51.     else if (*p == '?' || *p == '*' || *p == '[')
  52. #endif /* ?VMS */
  53.       return p;
  54.   return NULL;
  55. }
  56.  
  57.  
  58. local int recmatch(p, s)
  59. uch *p;       /* sh pattern to match */
  60. uch *s;       /* string to match it to */
  61. /* Recursively compare the sh pattern p with the string s and return 1 if
  62.    they match, and 0 or 2 if they don't or if there is a syntax error in the
  63.    pattern.  This routine recurses on itself no deeper than the number of
  64.    characters in the pattern. */
  65. {
  66.   unsigned int c;       /* pattern char or start of range in [-] loop */
  67.  
  68.   /* Get first character, the pattern for new recmatch calls follows */
  69.   c = *p++;
  70.  
  71.   /* If that was the end of the pattern, match if string empty too */
  72.   if (c == 0)
  73.     return *s == 0;
  74.  
  75.   /* '?' (or '%') matches any character (but not an empty string) */
  76. #ifdef VMS
  77.   if (c == '%')
  78. #else /* !VMS */
  79.   if (c == '?')
  80. #endif /* ?VMS */
  81.     return *s ? recmatch(p, s + 1) : 0;
  82.  
  83.   /* '*' matches any number of characters, including zero */
  84. #ifdef AMIGA
  85.   if (c == '#' && *p == '?')            /* "#?" is Amiga-ese for "*" */
  86.     c = '*', p++;
  87. #endif /* AMIGA */
  88.   if (c == '*')
  89.   {
  90.     if (*p == 0)
  91.       return 1;
  92.     for (; *s; s++)
  93.       if ((c = recmatch(p, s)) != 0)
  94.         return (int)c;
  95.     return 2;           /* 2 means give up--shmatch will return false */
  96.   }
  97.  
  98. #ifndef VMS             /* No bracket matching in VMS */
  99.   /* Parse and process the list of characters and ranges in brackets */
  100.   if (c == '[')
  101.   {
  102.     int e;              /* flag true if next char to be taken literally */
  103.     uch *q;   /* pointer to end of [-] group */
  104.     int r;              /* flag true to match anything but the range */
  105.  
  106.     if (*s == 0)                        /* need a character to match */
  107.       return 0;
  108.     p += (r = (*p == '!' || *p == '^')); /* see if reverse */
  109.     for (q = p, e = 0; *q; q++)         /* find closing bracket */
  110.       if (e)
  111.         e = 0;
  112.       else
  113.         if (*q == '\\')
  114.           e = 1;
  115.         else if (*q == ']')
  116.           break;
  117.     if (*q != ']')                      /* nothing matches if bad syntax */
  118.       return 0;
  119.     for (c = 0, e = *p == '-'; p < q; p++)      /* go through the list */
  120.     {
  121.       if (e == 0 && *p == '\\')         /* set escape flag if \ */
  122.         e = 1;
  123.       else if (e == 0 && *p == '-')     /* set start of range if - */
  124.         c = *(p-1);
  125.       else
  126.       {
  127.         uch cc = case_map(*s);
  128.         if (*(p+1) != '-')
  129.           for (c = c ? c : *p; c <= *p; c++)    /* compare range */
  130.             if (case_map(c) == cc)
  131.               return r ? 0 : recmatch(q + 1, s + 1);
  132.         c = e = 0;                      /* clear range, escape flags */
  133.       }
  134.     }
  135.     return r ? recmatch(q + 1, s + 1) : 0;      /* bracket match failed */
  136.   }
  137. #endif /* !VMS */
  138.  
  139.   /* If escape ('\'), just compare next character */
  140.   if (c == '\\')
  141.     if ((c = *p++) == 0)                /* if \ at end, then syntax error */
  142.       return 0;
  143.  
  144.   /* Just a character--compare it */
  145.   return case_map(c) == case_map(*s) ? recmatch(p, ++s) : 0;
  146. }
  147.  
  148.  
  149. int shmatch(p, s)
  150. char *p;                /* sh pattern to match */
  151. char *s;                /* string to match it to */
  152. /* Compare the sh pattern p with the string s and return true if they match,
  153.    false if they don't or if there is a syntax error in the pattern. */
  154. {
  155.   return recmatch((uch *) p, (uch *) s) == 1;
  156. }
  157.  
  158.  
  159. #ifdef DOS
  160.  
  161. int dosmatch(p, s)
  162. char *p;                /* dos pattern to match */
  163. char *s;                /* string to match it to */
  164. /* Break the pattern and string into name and extension parts and match
  165.    each separately using shmatch(). */
  166. {
  167.   char *p1, *p2;        /* pattern sections */
  168.   char *s1, *s2;        /* string sections */
  169.   int plen = strlen(p); /* length of pattern */
  170.   int r;                /* result */
  171.  
  172.   if ((p1 = malloc(plen + 1)) == NULL ||
  173.       (s1 = malloc(strlen(s) + 1)) == NULL)
  174.   {
  175.     if (p1 != NULL)
  176.       free((zvoid *)p1);
  177.     return 0;
  178.   }
  179.   strcpy(p1, p);
  180.   strcpy(s1, s);
  181.   if ((p2 = strrchr(p1, '.')) != NULL)
  182.     *p2++ = '\0';
  183.   else if (plen && p1[plen - 1] == '*')
  184.     p2 = "*";
  185.   else
  186.     p2 = "";
  187.   if ((s2 = strrchr(s1, '.')) != NULL)
  188.     *s2++ = '\0';
  189.   else
  190.     s2 = "";
  191.   r = shmatch(p2, s2) && shmatch(p1, s1);
  192.   free((zvoid *)p1);
  193.   free((zvoid *)s1);
  194.   return r;
  195. }
  196. #endif /* DOS */
  197.  
  198. zvoid far **search(b, a, n, cmp)
  199. zvoid *b;               /* pointer to value to search for */
  200. zvoid far **a;          /* table of pointers to values, sorted */
  201. extent n;               /* number of pointers in a[] */
  202. int (*cmp) OF((const zvoid *, const zvoid far *)); /* comparison function */
  203.  
  204. /* Search for b in the pointer list a[0..n-1] using the compare function
  205.    cmp(b, c) where c is an element of a[i] and cmp() returns negative if
  206.    *b < *c, zero if *b == *c, or positive if *b > *c.  If *b is found,
  207.    search returns a pointer to the entry in a[], else search() returns
  208.    NULL.  The nature and size of *b and *c (they can be different) are
  209.    left up to the cmp() function.  A binary search is used, and it is
  210.    assumed that the list is sorted in ascending order. */
  211. {
  212.   zvoid far **i;        /* pointer to midpoint of current range */
  213.   zvoid far **l;        /* pointer to lower end of current range */
  214.   int r;                /* result of (*cmp)() call */
  215.   zvoid far **u;        /* pointer to upper end of current range */
  216.  
  217.   l = (zvoid far **)a;  u = l + (n-1);
  218.   while (u >= l) {
  219.     i = l + ((unsigned)(u - l) >> 1);
  220.     if ((r = (*cmp)(b, (char *)*(struct zlist **)i)) < 0)
  221.       u = i - 1;
  222.     else if (r > 0)
  223.       l = i + 1;
  224.     else
  225.       return (zvoid far **)i;
  226.   }
  227.   return NULL;          /* If b were in list, it would belong at l */
  228. }
  229.  
  230. #endif /* !UTIL */
  231.  
  232. #ifdef MSDOS16
  233.  
  234. local unsigned ident(unsigned chr)
  235. {
  236.    return chr; /* in al */
  237. }
  238.  
  239. void init_upper()
  240. {
  241.   static struct country {
  242.     uch ignore[18];
  243.     int (far *casemap)(int);
  244.     uch filler[16];
  245.   } country_info;
  246.  
  247.   struct country far *info = &country_info;
  248.   union REGS regs;
  249.   struct SREGS sregs;
  250.   int c;
  251.  
  252.   regs.x.ax = 0x3800; /* get country info */
  253.   regs.x.dx = FP_OFF(info);
  254.   sregs.ds  = FP_SEG(info);
  255.   intdosx(®s, ®s, &sregs);
  256.   for (c = 0; c < 128; c++) {
  257.     upper[c] = (uch) toupper(c);
  258.     lower[c] = (uch) c;
  259.   }
  260.   for (; c < sizeof(upper); c++) {
  261.     upper[c] = (uch) (*country_info.casemap)(ident(c));
  262.     /* ident() required because casemap takes its parameter in al */
  263.     lower[c] = (uch) c;
  264.   }
  265.   for (c = 0; c < sizeof(upper); c++ ) {
  266.     int u = upper[c];
  267.     if (u != c && lower[u] == (uch) u) {
  268.       lower[u] = (uch)c;
  269.     }
  270.   }
  271.   for (c = 'A'; c <= 'Z'; c++) {
  272.     lower[c] = (uch) (c - 'A' + 'a');
  273.   }
  274. }
  275. #else /* !MSDOS16 */
  276. #  ifndef OS2
  277.  
  278. void init_upper()
  279. {
  280.   int c;
  281. #if defined(ATARI) || defined(CMS_MVS)
  282. #include <ctype.h>
  283. /* this should be valid for all other platforms too.   (HD 11/11/95) */
  284.   for (c = 0; c< sizeof(upper); c++) {
  285.     upper[c] = islower(c) ? toupper(c) : c;
  286.     lower[c] = isupper(c) ? tolower(c) : c;
  287.   }
  288. #else
  289.   for (c = 0; c < sizeof(upper); c++) upper[c] = lower[c] = c;
  290.   for (c = 'a'; c <= 'z';        c++) upper[c] = c - 'a' + 'A';
  291.   for (c = 'A'; c <= 'Z';        c++) lower[c] = c - 'A' + 'a';
  292. #endif
  293. }
  294. #  endif /* !OS2 */
  295.  
  296. #endif /* ?MSDOS16 */
  297.  
  298. int namecmp(string1, string2)
  299.   char *string1, *string2;
  300. /* Compare the two strings ignoring case, and correctly taking into
  301.  * account national language characters. For operating systems with
  302.  * case sensitive file names, this function is equivalent to strcmp.
  303.  */
  304. {
  305.   int d;
  306.  
  307.   for (;;)
  308.   {
  309.     d = (int) (uch) case_map(*string1)
  310.       - (int) (uch) case_map(*string2);
  311.  
  312.     if (d || *string1 == 0 || *string2 == 0)
  313.       return d;
  314.  
  315.     string1++;
  316.     string2++;
  317.   }
  318. }
  319.  
  320. #ifdef EBCDIC
  321.  
  322. #include "ebcdic.h"
  323.  
  324. char *strtoasc(char *str1, char *str2)
  325. {
  326.   char *old;
  327.   old = str1;
  328.   while (*str1++ = (char)ascii[(uch)(*str2++)]);
  329.   return old;
  330. }
  331.  
  332. char *strtoebc(char *str1, char *str2)
  333. {
  334.   char *old;
  335.   old = str1;
  336.   while (*str1++ = (char)ebcdic[(uch)(*str2++)]);
  337.   return old;
  338. }
  339.  
  340. #endif /* EBCDIC */
  341.  
  342. #ifndef UTIL
  343.  
  344. extern char *getenv OF((const char *));
  345.  
  346. /*****************************************************************
  347.  | envargs - add default options from environment to command line
  348.  |----------------------------------------------------------------
  349.  | Author: Bill Davidsen, original 10/13/91, revised 23 Oct 1991.
  350.  | This program is in the public domain.
  351.  |----------------------------------------------------------------
  352.  | Minor program notes:
  353.  |  1. Yes, the indirection is a tad complex
  354.  |  2. Parenthesis were added where not needed in some cases
  355.  |     to make the action of the code less obscure.
  356.  ****************************************************************/
  357.  
  358. void envargs(Pargc, Pargv, envstr, envstr2)
  359.     int *Pargc;
  360.     char ***Pargv;
  361.     char *envstr;
  362.     char *envstr2;
  363. {
  364.     char *envptr;                               /* value returned by getenv */
  365.     char *bufptr;                               /* copy of env info */
  366.     int argc = 0;                               /* internal arg count */
  367.     int ch;                                             /* spare temp value */
  368.     char **argv;                                /* internal arg vector */
  369.     char **argvect;                             /* copy of vector address */
  370.  
  371.     /* see if anything in the environment */
  372.     if ((envptr = getenv(envstr)) == NULL || *envptr == 0)      /* usual var */
  373.         if ((envptr = getenv(envstr2)) == NULL || *envptr == 0) /* alternate */
  374.             return;
  375.  
  376.     /* count the args so we can allocate room for them */
  377.     argc = count_args(envptr);
  378.     bufptr = malloc(1+strlen(envptr));
  379.     if (bufptr == NULL)
  380.         ziperr(ZE_MEM, "Can't get memory for arguments");
  381.  
  382.     strcpy(bufptr, envptr);
  383.  
  384.     /* allocate a vector large enough for all args */
  385.     argv = (char **)malloc((argc+*Pargc+1)*sizeof(char *));
  386.     if (argv == NULL)
  387.         ziperr(ZE_MEM, "Can't get memory for arguments");
  388.     argvect = argv;
  389.  
  390.     /* copy the program name first, that's always true */
  391.     *(argv++) = *((*Pargv)++);
  392.  
  393.     /* copy the environment args first, may be changed */
  394.     do {
  395.         *(argv++) = bufptr;
  396.         /* skip the arg and any trailing blanks */
  397.         while ((ch = *bufptr) != '\0' && ch != ' ') ++bufptr;
  398.         if (ch == ' ') *(bufptr++) = '\0';
  399.         while ((ch = *bufptr) != '\0' && ch == ' ') ++bufptr;
  400.     } while (ch);
  401.  
  402.     /* now save old argc and copy in the old args */
  403.     argc += *Pargc;
  404.     while (--(*Pargc)) *(argv++) = *((*Pargv)++);
  405.  
  406.     /* finally, add a NULL after the last arg, like UNIX */
  407.     *argv = NULL;
  408.  
  409.     /* save the values and return */
  410.     *Pargv = argvect;
  411.     *Pargc = argc;
  412. }
  413.  
  414. local int count_args(s)
  415.     char *s;
  416. {
  417.     int count = 0;
  418.     int ch;
  419.  
  420.     do {
  421.         /* count and skip args */
  422.         ++count;
  423.         while ((ch = *s) != '\0' && ch != ' ') ++s;
  424.         while ((ch = *s) != '\0' && ch == ' ') ++s;
  425.     } while (ch);
  426.  
  427.     return count;
  428. }
  429.  
  430.  
  431. /* Extended argument processing -- by Rich Wales
  432.  * This function currently deals only with the MKS shell, but could be
  433.  * extended later to understand other conventions.
  434.  *
  435.  * void expand_args(int *argcp, char ***argvp)
  436.  *
  437.  *    Substitutes the extended command line argument list produced by
  438.  *    the MKS Korn Shell in place of the command line info from DOS.
  439.  *
  440.  *    The MKS shell gets around DOS's 128-byte limit on the length of
  441.  *    a command line by passing the "real" command line in the envi-
  442.  *    ronment.  The "real" arguments are flagged by prepending a tilde
  443.  *    (~) to each one.
  444.  *
  445.  *    This "expand_args" routine creates a new argument list by scanning
  446.  *    the environment from the beginning, looking for strings begin-
  447.  *    ning with a tilde character.  The new list replaces the original
  448.  *    "argv" (pointed to by "argvp"), and the number of arguments
  449.  *    in the new list replaces the original "argc" (pointed to by
  450.  *    "argcp").
  451.  */
  452. void expand_args(argcp, argvp)
  453.       int *argcp;
  454.       char ***argvp;
  455. {
  456. #ifdef DOS
  457.  
  458. /* Do NEVER include (re)definiton of `environ' variable with any version
  459.    of MSC or BORLAND/Turbo C. These compilers supply an incompatible
  460.    definition in <stdlib.h>.  */
  461. #if defined(__GO32__) || defined(__EMX__)
  462.       extern char **environ;          /* environment */
  463. #endif /* __GO32__ || __EMX__ */
  464.       char        **envp;             /* pointer into environment */
  465.       char        **newargv;          /* new argument list */
  466.       char        **argp;             /* pointer into new arg list */
  467.       int           newargc;          /* new argument count */
  468.  
  469.       /* sanity check */
  470.       if (environ == NULL
  471.           || argcp == NULL
  472.           || argvp == NULL || *argvp == NULL)
  473.               return;
  474.       /* find out how many environment arguments there are */
  475.       for (envp = environ, newargc = 0;
  476.            *envp != NULL && (*envp)[0] == '~';
  477.            envp++, newargc++) ;
  478.       if (newargc == 0)
  479.               return;                 /* no environment arguments */
  480.       /* set up new argument list */
  481.       newargv = (char **) malloc(sizeof(char **) * (newargc+1));
  482.       if (newargv == NULL)
  483.               return;                 /* malloc failed */
  484.       for (argp = newargv, envp = environ;
  485.            *envp != NULL && (*envp)[0] == '~';
  486.            *argp++ = &(*envp++)[1]) ;
  487.       *argp = NULL;                   /* null-terminate the list */
  488.       /* substitute new argument list in place of old one */
  489.       *argcp = newargc;
  490.       *argvp = newargv;
  491. #else /* ?DOS */
  492.       if (argcp || argvp) return;
  493. #endif /* ?DOS */
  494. }
  495.  
  496. #endif /* UTIL */
  497.