home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / cku196.zip / ckclib.c < prev    next >
C/C++ Source or Header  |  2000-01-02  |  51KB  |  1,868 lines

  1. char * cklibv = "C-Kermit library, 7.0.009, 29 Nov 1999";
  2.  
  3. #define CKCLIB_C
  4.  
  5. /* C K C L I B . C  --  C-Kermit Library routines. */
  6.  
  7. /*
  8.   Author: Frank da Cruz <fdc@columbia.edu>,
  9.   Columbia University Academic Information Systems, New York City.
  10.  
  11.   Copyright (C) 1999, 2000,
  12.     Trustees of Columbia University in the City of New York.
  13.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  14.     copyright text in the ckcmai.c module for disclaimer and permissions.
  15. */
  16.  
  17. /*
  18.   General-purpose, system/platform/compiler-independent routines for use
  19.   by all modules.  Most are replacements for commonly used C library
  20.   functions that are not found on every platform, and/or that lack needed
  21.   functionality (e.g. caseless string search/compare).
  22.  
  23.     ckstrncpy()  - Similar to strncpy() but different (see comments).
  24.     chartostr()  - Converts a char to a string (self or ctrl char name).
  25.     ckstrchr()   - Portable strchr().
  26.     cklower()    - Lowercase a string (in place).
  27.     ckindex()    - Left or right index.
  28.     ckitoa()     - Converts int to string.
  29.     ckltoa()     - Converts long to string.
  30.     ckmatch()    - Pattern matching.
  31.     ckmemcpy()   - Portable memcpy().
  32.     ckrchar()    - Rightmost character of a string.
  33.     ckstrcmp()   - Possibly caseless string comparison.
  34.     ckstrpre()   - Caseless string prefix comparison.
  35.     sh_sort()    - Sorts an array of strings, many options.
  36.     brstrip()    - Strips enclosing braces.
  37.     makelist()   - Splits "{{item}{item}...}" into an array.
  38.     makestr()    - Careful malloc() front end.
  39.     xmakestr()   - ditto (see comments).
  40.     fileselect() - Select a file based on size, date, excption list, etc.
  41.     radix()      - Convert number radix (2-36).
  42.     b8tob64()    - Convert data to base 64.
  43.     b64tob8()    - Convert base 64 to data.
  44.     chknum()     - Checks if string is an integer.
  45.     rdigits()    - Checks if string is composed only of digits.
  46.     isfloat()    - Checks if string is a valid floating-point number.
  47.     parnam()     - Returns parity name string.
  48.     hhmmss()     - Converts seconds to hh:mm:ss string.
  49.     lset()       - Write fixed-length field left-adjusted into a record.
  50.     rset()       - Write fixed-length field right-adjusted into a record.
  51.     ulongtohex() - Converts an unsigned long to a hex string.
  52.     hextoulong() - Converts a hex string to an unsigned long.
  53.  
  54.   Prototypes are in ckclib.h.
  55. */
  56. #include "ckcsym.h"
  57. #include "ckcdeb.h"
  58. #include "ckcasc.h"
  59.  
  60. char *
  61. ccntab[] = {    /* Names of ASCII (C0) control characters 0-31 */
  62.     "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
  63.     "BS",  "HT",  "LF",  "VT",  "FF",  "CR",  "SO",  "SI",
  64.     "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
  65.     "CAN", "EM",  "SUB", "ESC", "FS",  "GS",  "RS",  "US"
  66. };
  67.  
  68. char *
  69. c1tab[] = {    /* Names of ISO 6429 (C1) control characters 0-32 */
  70.     "XXX", "XXX", "BPH", "NBH", "IND", "NEL", "SSA", "ESA",
  71.     "HTS", "HTJ", "VTS", "PLD", "PLU", "RI",  "SS2", "SS3",
  72.     "DCS", "PU1", "PU2", "STS", "CCH", "MW",  "SPA", "EPA",
  73.     "SOS", "XXX", "SCI", "CSI", "ST",  "OSC", "PM",  "APC", "NBS"
  74. };
  75.  
  76. /*  C K S T R N C P Y */
  77.  
  78. /*
  79.   Copies a NUL-terminated string into a buffer whose total length is given,
  80.   ensuring that the result is NUL-terminated even if it had to be truncated.
  81.  
  82.   Call with:
  83.     dest = pointer to destination buffer
  84.     src  = pointer to source string
  85.     len  = length of destination buffer (the actual length, not one less).
  86.  
  87.   Returns:
  88.     int, The number of bytes copied, 0 or more.
  89.  
  90.   NOTE: This is NOT a replacement for strncpy():
  91.    . strncpy() does not require its source string to be NUL-terminated.
  92.    . strncpy() does not necessarily NUL-terminate its result.
  93.    . strncpy() right-pads dest with NULs if it is longer than src.
  94.    . strncpy() treats the length argument as the number of bytes to copy.
  95.    . ckstrncpy() treats the length argument as the size of the dest buffer.
  96.    . ckstrncpy() doesn't dump core if given NULL string pointers.
  97.    . ckstrncpy() returns a number.
  98. */
  99. int
  100. #ifdef CK_ANSIC
  101. ckstrncpy(char * dest, const char * src, int len)
  102. #else
  103. ckstrncpy(dest,src,len) char * dest, * src; int len;
  104. #endif /* CK_ANSIC */
  105. {
  106.     int i, x;
  107.     if (len < 1 || !src || !dest) {    /* Nothing or nowhere to copy */
  108.     if (dest) *dest = NUL;
  109.     return(0);
  110.     }
  111. #ifndef NOCKSTRNCPY
  112.     for (i = 0; src[i] && (i < len-1); i++) /* Args OK, copy */
  113.       dest[i] = src[i];
  114.     dest[i] = NUL;
  115. #else
  116.     i = strlen(src);
  117.     if (i > len) i = len;
  118.     strncpy(dest,src,i);
  119.     dest[len] = NUL;
  120. #endif /* NOCKSTRNCPY */
  121.     return(i);
  122. }
  123.  
  124. /*  C H A R T O S T R  */
  125.  
  126. /*  Convert a character to a string, interpreting controls.  */
  127.  
  128. char *
  129. chartostr(x) int x; {            /* Call with char x */
  130.     static char buf[2];            /* Returns string pointer. */
  131.     if (x < 32)
  132.       return(ccntab[x]);
  133.     if (x == 127)
  134.       return("DEL");
  135.     if (x > 127 && x < 161)
  136.       return(c1tab[x]);
  137.     if (x == 0xAD)
  138.       return("SHY");
  139.     buf[1] = NUL;
  140.     buf[0] = (unsigned)(x & 0xff);
  141.     return((char *)buf);
  142. }
  143.  
  144. /*  C K R C H A R */
  145.  
  146. /*  Returns the rightmost character of the given null-terminated string */
  147.  
  148. int
  149. ckrchar(s) char * s; {
  150.     register CHAR c = '\0', *p;
  151.     p = (CHAR *)s;
  152.     if (!p) p = (CHAR *)"";        /* Null pointer == empty string */
  153.     if (!*p) return(0);
  154.     while (*p)                /* Crawl to end of string */
  155.       c = *p++;
  156.     return((unsigned)(c & 0xff));    /* Return final character */
  157. }
  158.  
  159. /*  C K S T R C H R  */
  160.  
  161. /*  Replacement for strchr(), which is not universal.  */
  162. /*  Call with:
  163.      s = pointer to string to look in.
  164.      c = character to look for.
  165.     Returns:
  166.      NULL if c not found in s or upon any kind of error, or:
  167.      pointer to first occurrence of c in s.
  168. */
  169. char *
  170. #ifdef CK_ANSIC
  171. ckstrchr(char * s, char c)
  172. #else
  173. ckstrchr(s,c) char *s, c;
  174. #endif /* CK_ANSIC */
  175. /* ckstrchr */ {
  176.     if (!s)
  177.       return(NULL);
  178.     while (*s && *s != c)
  179.       s++;
  180.     return((*s == c) ? s : NULL);
  181. }
  182.  
  183. /*  C K L O W E R  --  Lowercase a string  */
  184.  
  185. /* Returns the length of the string */
  186.  
  187. int
  188. cklower(s) char *s; {
  189.     int n = 0;
  190.     if (!s) return(0);
  191.     while (*s) {
  192.         if (isupper(*s)) *s = (char) tolower(*s);
  193.         s++, n++;
  194.     }
  195.     return(n);
  196. }
  197.  
  198. /*  C K L T O A  --  Long to string  --  FOR DISCIPLINED USE ONLY  */
  199.  
  200. #define NUMBUF 1024
  201. static char numbuf[NUMBUF+32] = { NUL, NUL };
  202. static int numbp = 0;
  203. /*
  204.   ckltoa() and ckitoa() are like atol() and atoi() in the reverse direction,
  205.   returning a pointer to the string representation of the given number without
  206.   the caller having to worry about allocating or defining a buffer first.
  207.   They manage their own internal buffer, so successive calls return different
  208.   pointers.  However, to keep memory consumption from growing without bound,
  209.   the buffer recycles itself.  So after several hundred calls (depending on
  210.   the size of the numbers), some of the earlier pointers might well find
  211.   themselves referencing something different.  Moral: You can't win in C.
  212.   Therefore, these routines are intended mainly for generating numeric strings
  213.   for short-term use, e.g. for passing numbers in string form as parameters to
  214.   functions.  For long-term use, the result must be copied to a safe place.
  215. */
  216. char *
  217. #ifdef CK_ANSIC
  218. ckltoa(long n)
  219. #else
  220. ckltoa(n) long n;
  221. #endif /* CK_ANSIC */
  222. /* ckltoa */ {
  223.     char buf[32];            /* Internal working buffer */
  224.     char * p, * s, * q;
  225.     int k, x, sign = 0;
  226.     if (n < 0L) {            /* Sign */
  227.     n = 0L - n;
  228.     sign = 1;
  229.     }
  230.     buf[31] = NUL;
  231.     for (k = 30; k > 0; k--) {        /* Convert number to string */
  232.     x = n % 10L;
  233.     buf[k] = x + '0';
  234.     n = n / 10L;
  235.     if (!n)
  236.       break;
  237.     }
  238.     if (sign) buf[--k] = '-';        /* Add sign if necessary */
  239.     p = numbuf + numbp;
  240.     q = p;
  241.     s = buf + k;
  242.     while (*p++ = *s++ ) ;        /* Copy */
  243.     if (numbp >= NUMBUF)        /* Update pointer */
  244.       numbp = 0;
  245.     else
  246.       numbp += k;
  247.     return(q);                /* Return pointer */
  248. }
  249.  
  250.  
  251. /*  C K I T O A  --  Int to string  -- FOR DISCIPLINED USE ONLY  */
  252.  
  253. char *
  254. ckitoa(n) int n; {            /* See comments with ckltoa(). */
  255.     long nn;
  256.     nn = n;
  257.     return(ckltoa(nn));
  258. }
  259.  
  260.  
  261. /*  C K I N D E X  --  C-Kermit's index function  */
  262. /*
  263.   We can't depend on C libraries to have one, so here is our own.
  264.   Call with:
  265.     s1 - String to look for.
  266.     s2 - String to look in.
  267.      t - Offset from right or left of s2, 0 based; -1 for rightmost char in s2.
  268.      r - 0 for left-to-right search, non-0 for right-to-left.
  269.   icase  0 for case independence, non-0 if alphabetic case matters.
  270.   Returns 0 if string not found, otherwise a 1-based result.
  271.   Also returns 0 on any kind of error, e.g. junk parameters.
  272. */
  273. int
  274. ckindex(s1,s2,t,r,icase) char *s1, *s2; int t, r, icase; {
  275.     int len1, len2, i, j, x, ot = t;    /* ot = original t */
  276.     char * s;
  277.  
  278.     if (!s1 || !s2) return(0);
  279.     len1 = (int)strlen(s1);        /* length of string to look for */
  280.     len2 = (int)strlen(s = s2);        /* length of string to look in */
  281.     if (t < 0) t = len2 - 1;
  282.  
  283.     if (len1 < 0) return(0);        /* paranoia */
  284.     if (len2 < 0) return(0);
  285.     j = len2 - len1;            /* length difference */
  286.  
  287.     if (j < 0 || (r == 0 && t > j))    /* search string is longer */
  288.       return(0);
  289.     if (r == 0) {            /* Index */
  290.     s = s2 + t;            /* Point to beginning of target */
  291.     for (i = 0; i <= (j - t); i++) { /* Now compare */
  292.         x = ckstrcmp(s1,s++,len1,icase);
  293.         if (!x)
  294.           return(i+1+t);
  295.     }
  296.     } else {                /* Reverse Index */
  297.         i = len2 - len1;        /* Where to start looking */
  298.         if (ot > 0)            /* Figure in offset if any */
  299.       i -= t;
  300.     for (j = i; j > -1; j--) {
  301.         if (!ckstrcmp(s1,&s2[j],len1,icase))
  302.           return(j+1);
  303.     }
  304.     }
  305.     return(0);
  306. }
  307.  
  308. /*  B R S T R I P  --  Strip enclosing braces from arg string, in place */
  309. /*
  310.   Call with:
  311.     Pointer to string that can be poked.
  312.   Returns:
  313.     Pointer to string without enclosing braces.
  314.     If original string was not braced, this is the arg pointer;
  315.     otherwise it is 1 + the arg pointer, with the matching closing
  316.     brace zero'd out.  If the string starts with a brace but does not
  317.     end with a matching brace, the original pointer to the original
  318.     string is returned.  If the arg pointer is NULL, a pointer to an
  319.     empty string is returned.
  320. */
  321. char *
  322. brstrip(p) char *p; {
  323.     if (!p) return("");
  324.     if (*p == '{') {
  325.     int x;
  326.     x = (int)strlen(p) - 1;
  327.     if (p[x] == '}') {
  328.         p[x] = NUL;
  329.         p++;
  330.     }
  331.     }
  332.     return(p);
  333. }
  334.  
  335.  
  336. /*  M A K E L I S T  ---  Breaks {{s1}{s2}..{sn}} into an array of strings */
  337. /*
  338.   Call with:
  339.     s    = pointer to string to break up.
  340.     list = array of string pointers.
  341.     len  = number of elements in array.
  342.   NOTE: The array must be preinitialized to all NULL pointers.
  343.   If any array element is not NULL, it is assumed to have been malloc'd
  344.   and is therefore freed.  Do NOT call this function with an unitialized
  345.   array, or with an array that has had any static elements assigned to it.
  346. */
  347. VOID
  348. makelist(s,list,len) char * s; char *list[]; int len; {
  349.     int i, n, q, bc = 0;
  350.     char *p = NULL, *s2 = NULL;
  351.     debug(F110,"makelist s",s,0);
  352.     if (!s) {                /* Check for null or empty string */
  353.     list[0] = NULL;
  354.     return;
  355.     }
  356.     n = strlen(s);
  357.     if (n == 0) {
  358.     list[0] = NULL;
  359.     return;
  360.     }
  361.     if (s2 = (char *)malloc(n+1)) {    /* Safe copy for poking */
  362.     strcpy(s2,s);            /* (no need for ckstrncpy here) */
  363.     s = s2;
  364.     }
  365.     s = brstrip(s);            /* Strip braces */
  366.     n = strlen(s);            /* Get length */
  367.     if (*s != '{') {            /* Outer braces only */
  368.     if (p = (char *)malloc(n+1)) {    /* So just one pattern */
  369.         strcpy(p,s);        /* (no need for ckstrncpy here) */
  370.         if (list[0])
  371.           free(list[0]);
  372.         list[0] = p;
  373.     }
  374.     if (s2) free(s2);
  375.     return;
  376.     }
  377.     q = 0;                /* Inner ones too */
  378.     i = 0;                /* so a list of patterns. */
  379.     n = 0;
  380.     while (*s && i < len) {
  381.     if (*s == CMDQ) {        /* Quote... */
  382.         q = 1;
  383.         s++;
  384.         n++;
  385.         continue;
  386.     }
  387.     if (*s == '{' && !q) {        /* Opening brace */
  388.         if (bc++ == 0) {        /* Beginning of a group */
  389.         p = ++s;
  390.         n = 0;
  391.         } else {            /* It's a brace inside the group */
  392.         n++;
  393.         s++;
  394.         }
  395.         continue;
  396.     } else if (*s == '}' && !q) {    /* Closing brace */
  397.         if (--bc == 0) {        /* End of a group */
  398.         *s++ = NUL;
  399.         debug(F111,"makelist element",p,i);
  400.         if (list[i])
  401.           free(list[i]);
  402.         if (list[i] = (char *)malloc(n+1)) {
  403.             ckstrncpy(list[i],p,n+1); /* Note: n+1 */
  404.             i++;
  405.         }
  406.         while (*s == SP) s++;
  407.         p = s;
  408.         n = 0;
  409.         continue;
  410.         } else {            /* Within a group */
  411.         n++;
  412.         s++;
  413.         }
  414.     } else {            /* Regular character */
  415.         q = 0;
  416.         s++;
  417.         n++;
  418.     }
  419.     }
  420.     if (*p && i < len) {        /* Last one */
  421.     if (list[i])
  422.       free(list[i]);
  423.     if (list[i] = (char *)malloc(n+1)) {
  424.         ckstrncpy(list[i],p,n+1);
  425.         debug(F111,"makelist last element",p,i);
  426.     }
  427.     }
  428.     if (s2) free(s2);
  429. }
  430.  
  431. /*
  432.    M A K E S T R  --  Creates a dynamically allocated string.
  433.  
  434.    Makes a new copy of string s and sets pointer p to its address.
  435.    Handles degenerate cases, like when buffers overlap or are the same,
  436.    one or both arguments are NULL, etc.
  437.  
  438.    The target pointer must be either NULL or else a pointer to a previously
  439.    malloc'ed buffer.  If not, expect a core dump or segmentation fault.
  440.  
  441.    Note: The caller can tell whether this routine failed as follows:
  442.  
  443.      malloc(&p,q);
  444.      if (q & !p) { makestr() failed };
  445. */
  446. VOID
  447. #ifdef CK_ANSIC
  448. makestr(char **p, const char *s)
  449. #else
  450. makestr(p,s) char **p, *s;
  451. #endif
  452. /* makestr */ {
  453.     int x;
  454.     char *q = NULL;
  455.  
  456.     if (*p == s)            /* The two pointers are the same. */
  457.       return;                /* Don't do anything. */
  458.  
  459.     if (!s) {                /* New definition is null? */
  460.     if (*p)                /* Free old storage. */
  461.       free(*p);
  462.     *p = NULL;            /* Return null pointer. */
  463.     return;
  464.     }
  465.     if ((x = strlen(s)) >= 0) {        /* Get length, even of empty string. */
  466.     q = malloc(x+1);        /* Get and point to temp storage. */
  467.     if (q) {
  468.         strcpy(q,s);        /* (no need for ckstrncpy() here) */
  469.     }
  470. #ifdef DEBUG
  471.     else {                /* This would be a really bad error */
  472.         char tmp[24];        /* So get a good record of it. */
  473.         if (x > 23) {
  474.         ckstrncpy(tmp,s,20);
  475.         strcpy(tmp+20,"...");
  476.         tmp[23] = NUL;
  477.         } else {
  478.         ckstrncpy(tmp,s,24);
  479.         }
  480.         debug(F110,"MAKESTR MALLOC FAILURE ",s,0);
  481.     }
  482. #endif /* DEBUG */
  483.     } else
  484.       q = NULL;                /* Length of string is zero */
  485.  
  486.     if (*p) {                /* Now free the original storage. */
  487. #ifdef BETATEST
  488.     memset(*p,0xFF,sizeof(**p));    /* (not portable) */
  489. #endif /* BETATEST */
  490.     free(*p);
  491.     }
  492. #ifdef COMMENT
  493.     *q = NULL;                /* Set up return pointer */
  494.     if (q)
  495.       *p = q;
  496. #else
  497.     *p = q;                /* This is exactly the same */
  498. #endif /* COMMENT */
  499.  
  500. }
  501.  
  502. /*  X M A K E S T R  --  Non-destructive makestr() if s is NULL.  */
  503.  
  504. VOID
  505. #ifdef CK_ANSIC
  506. xmakestr(char **p, const char *s)
  507. #else
  508. xmakestr(p,s) char **p, *s;
  509. #endif
  510. /* xmakestr */ {
  511.     if (s) makestr(p,s);
  512. }
  513.  
  514. /* C K M E M C P Y  --  Portable (but slow) memcpy() */
  515.  
  516. /* Copies n bytes from s to p, allowing for overlap. */
  517. /* For use when real memcpy() not available. */
  518.  
  519. VOID
  520. ckmemcpy(p,s,n) char *p, *s; int n; {
  521.     char * q = NULL;
  522.     register int i;
  523.     int x;
  524.  
  525.     if (!s || !p || n <= 0 || p == s)    /* Verify args */
  526.       return;
  527.     x = p - s;                /* Check for overlap */
  528.     if (x < 0)
  529.       x = 0 - x;
  530.     if (x < n) {            /* They overlap */
  531.     q = p;
  532.     if (!(p = (char *)malloc(n)))    /* So use a temporary buffer */
  533.       return;
  534.     }
  535.     for (i = 0; i < n; i++)        /* Copy n bytes */
  536.       p[i] = s[i];
  537.     if (q) {                /* If we used a temporary buffer */
  538.     for (i = 0; i < n; i++)        /* copy from it to destination */
  539.       q[i] = p[i];
  540.     if (p) free(p);            /* and free the temporary buffer */
  541.     }
  542. }
  543.  
  544.  
  545. /*  C K S T R C M P  --  String comparison with case-matters selection */
  546. /*
  547.   Call with pointers to the two strings, s1 and s2, a length, n,
  548.   and c == 0 for caseless comparison, nonzero for case matters.
  549.   Call with n == -1 to compare without a length limit.
  550.   Compares up to n characters of the two strings and returns:
  551.     1 if s1 > s2
  552.     0 if s1 = s2
  553.    -1 if s1 < s2
  554. */
  555. int
  556. ckstrcmp(s1,s2,n,c) char *s1, *s2; int n, c; {
  557.     CHAR t1, t2;
  558.     if (n == 0) return(0);
  559.     if (!s1) s1 = "";            /* Watch out for null pointers. */
  560.     if (!s2) s2 = "";
  561.     if (!*s1) return(*s2 ? -1 : 0);
  562.     if (!*s2) return(1);
  563.     while (n--) {
  564.     t1 = (CHAR) *s1++;        /* Get next character from each. */
  565.     t2 = (CHAR) *s2++;
  566.     if (!t1) return(t2 ? -1 : 0);
  567.     if (!t2) return(1);
  568.     if (!c) {            /* If case doesn't matter */
  569.         if (isupper(t1)) t1 = tolower(t1); /* Convert case. */
  570.         if (isupper(t2)) t2 = tolower(t2);
  571.     }
  572.     if (t1 < t2) return(-1);    /* s1 < s2 */
  573.     if (t1 > t2) return(1);        /* s1 > s2 */
  574.     }
  575.     return(0);                /* They're equal */
  576. }
  577.  
  578. /*  C K S T R P R E  --  Caseless string prefix comparison  */
  579.  
  580. /* Returns position of the first char in the 2 strings that doesn't match */
  581.  
  582. int
  583. ckstrpre(s1,s2) char *s1, *s2; {
  584.     CHAR t1, t2;
  585.     int n = 0;
  586.     if (!s1) s1 = "";
  587.     if (!s2) s2 = "";
  588.     while (1) {
  589.     t1 = (CHAR) *s1++;
  590.     t2 = (CHAR) *s2++;
  591.     if (!t1 || !t2) return(n);
  592.     if (isupper(t1)) t1 = tolower(t1);
  593.     if (isupper(t2)) t2 = tolower(t2);
  594.     if (t1 != t2)
  595.       return(n);
  596.     n++;
  597.     }
  598. }
  599.  
  600. #define GLOBBING
  601.  
  602. /*  C K M A T C H  --  Match a string against a pattern  */
  603. /*
  604.   Call with a pattern containing * and/or ? metacharacters.
  605.   icase is 1 if case-sensitive, 0 otherwise.
  606.   opts is a bitmask:
  607.     Bit 0: 1 to match strings starting with '.', else 0.
  608.     Bit 1: 1 = file globbing (dirseps are fences, etc), 0 = ordinary string.
  609.     Bit 2 (and beyond): Undefined.
  610.   Works only with NUL-terminated strings.
  611.   Pattern may contain any number of ? and/or *.
  612.   If CKREGEX is defined, also [abc], [a-z], and/or {string,string,...}.
  613.  
  614.   Returns:
  615.     0 if string does not match pattern,
  616.     1 if it does.
  617.  
  618.   To be done:
  619.     Find a way to identify the piece of the string that matched the pattern,
  620.     as in Snobol "LINE (PAT . RESULT)".  Some prelinary attempts are commented
  621.     out (see "mstart"); these fail because they always indicate the entire
  622.     string.  The piece we want (I think) is the the part that matches the
  623.     first non-* segment of the pattern through the final non-* part.  If this
  624.     can be done, we can streamline INPUT and friends considerably, and also
  625.     add regexp support to functions like \findex(\fpattern(a*b),\%s).  INPUT
  626.     accomplishes this now by repeated calls to ckmatch, which is overkill.
  627. */
  628. #ifdef COMMENT
  629. char * ckmstring = NULL;
  630. #endif /* COMMENT */
  631.  
  632. int
  633. ckmatch(pattern, string, icase, opts) char *pattern,*string; int icase, opts; {
  634.     int q = 0, i = 0, k = -1, x, flag = 0;
  635.     CHAR cp;                /* Current character from pattern */
  636.     CHAR cs;                /* Current character from string */
  637.     char * psave = NULL;
  638.     int dot, globbing;
  639.  
  640. #ifdef COMMENT
  641.     char * mstart = NULL;        /* Pointer to beginning of match */
  642. #endif /* COMMENT */
  643.  
  644.     dot = opts & 1;
  645.     globbing = opts & 2;
  646.  
  647. #ifdef COMMENT
  648.     makestr(&ckmstring,NULL);
  649. #endif /* COMMENT */
  650.     if (!pattern) pattern = "";
  651.     if (!*pattern) return(1);        /* Null pattern always matches */
  652.     if (!string) string = "";
  653.  
  654.     debug(F110,"ckmatch string",string,0);
  655.     debug(F111,"ckmatch pattern",pattern,opts);
  656.  
  657. #ifdef COMMENT
  658.     mstart = string;
  659. #endif /* COMMENT */
  660.  
  661. #ifdef UNIX
  662.     if (!dot) {                /* For UNIX file globbing */
  663.     if (*string == '.' && *pattern != '.' && !matchdot) {
  664.         if (
  665. #ifdef CKREGEX
  666.         *pattern != '{' && *pattern != '['
  667. #else
  668.         1
  669. #endif /* CKREGEX */
  670.         ) {
  671.         debug(F110,"ckmatch skip",string,0);
  672.         return(0);
  673.         }
  674.     }
  675.     }
  676. #endif /* UNIX */
  677.     while (1) {
  678.     k++;
  679.     cp = *pattern;            /* Character from pattern */
  680.     cs = *string;            /* Character from string */
  681.  
  682.     if (!cs) {            /* End of string - done. */
  683.         x = (!cp || (cp == '*' && !*(pattern+1))) ? 1 : 0;
  684. #ifdef COMMENT
  685.         if (x) makestr(&ckmstring,mstart);
  686. #endif /* COMMENT */
  687.         return(x);
  688.     }
  689.         if (!icase) {            /* If ignoring case */
  690.         if (isupper(cp))        /* convert both to lowercase. */
  691.           cp = tolower(cp);
  692.         if (isupper(cs))
  693.           cs = tolower(cs);
  694.         }
  695.     if (q) {            /* This character was quoted */
  696.         q = 0;            /* Turn off quote flag */
  697.         if (cs == cp)        /* Compare directly */
  698.           pattern++, string++;    /* no metacharacters */
  699.         continue;
  700.     }
  701.     if (cp == CMDQ && !q) {        /* Quote in pattern */
  702.         q = 1;            /* Set flag */
  703.         pattern++;            /* Advance to next pattern character */
  704.         cp = *pattern;        /* Case conversion... */
  705.         if (!icase)
  706.           if (isupper(cp))
  707.         cp = tolower(cp);
  708.         if (cp != cs)        /* Literal char so compare now */
  709.           return(0);        /* No match, done. */
  710.         string++, pattern++;    /* They match so advance pointers */
  711.         continue;            /* and continue. */
  712.     }
  713.     if (cs && cp == '?') {        /* '?' matches any char */
  714.         pattern++, string++;
  715.         continue;
  716. #ifdef CKREGEX
  717.     } else if (cp == '[') {        /* Have bracket */
  718.         int q = 0;            /* My own private q */
  719.         char * psave = NULL;    /* and backup pointer */
  720.         CHAR clist[256];        /* Character list from brackets */
  721.         CHAR c, c1, c2;
  722.         for (i = 0; i < 256; i++)    /* memset() etc not portable */
  723.           clist[i] = NUL;
  724.         psave = ++pattern;        /* Where pattern starts */
  725.         for (flag = 0; !flag; pattern++) { /* Loop thru pattern */
  726.         c = (unsigned)*pattern;    /* Current char */
  727.         if (q) {        /* Quote within brackets */
  728.             q = 0;
  729.             clist[c] = 1;
  730.             continue;
  731.         }
  732.         if (!icase)        /* Case conversion */
  733.           if (isupper(c))
  734.             c = tolower(c);
  735.         switch (c) {        /* Handle unquoted character */
  736.           case NUL:        /* End of string */
  737.             return(0);        /* No matching ']' so fail */
  738.           case CMDQ:        /* Next char is quoted */
  739.             q = 1;        /* Set flag */
  740.             continue;        /* and continue. */
  741.           case '-':        /* A range is specified */
  742.             c1 = (pattern > psave) ? (unsigned)*(pattern-1) : NUL;
  743.             c2 = (unsigned)*(pattern+1);
  744.             if (c2 == ']') c2 = NUL;
  745.             if (c1 == NUL) c1 = c2;
  746.             for (c = c1; c <= c2; c++)
  747.               clist[c] = 1;
  748.             continue;
  749.           case ']':        /* End of bracketed sequence */
  750.             flag = 1;        /* Done with FOR loop */
  751.             break;        /* Compare what we have */
  752.           default:        /* Just a char */
  753.             clist[c] = 1;    /* Record it */
  754.             continue;
  755.         }
  756.         }
  757.         if (!clist[(unsigned)cs])     /* Match? */
  758.           return(0);        /* Nope, done. */
  759.         string++;            /* Yes, advance string pointer */
  760.         continue;            /* and go on. */
  761.     } else if (cp == '{') {        /* Braces with list of strings */
  762.         char * p, * s, * s2, * buf = NULL;
  763.         int n, bc = 0;
  764.         int len = 0;
  765.         for (p = pattern++; *p; p++) {
  766.         if (*p == '{') bc++;
  767.         if (*p == '}') bc--;
  768.         if (bc < 1) break;
  769.         }
  770.         if (bc != 0) {        /* Braces don't match */
  771.         return(0);        /* Fail */
  772.         } else {            /* Braces do match */
  773.         int q = 0, done = 0;
  774.         len = *p ? strlen(p+1) : 0; /* Length of rest of pattern */
  775.         n = p - pattern;    /* Size of list in braces */
  776.         if (buf = (char *)malloc(n+1)) { /* Copy so we can poke it */
  777.             char * tp = NULL;
  778.             int k;
  779.             ckstrncpy(buf,pattern,n+1);
  780.             n = 0;
  781.             for (s = s2 = buf; 1; s++) { /* Loop through segments */
  782.             n++;
  783.             if (q) {    /* This char is quoted */
  784.                 q = 0;
  785.                 if (!*s)
  786.                   done = 1;
  787.                 continue;
  788.             }
  789.             if (*s == CMDQ && !q) {    /* Quote next char */
  790.                 q = 1;
  791.                 continue;
  792.             }
  793.             if (!*s || *s == ',') {    /* End of this segment */
  794.                 if (!*s)    /* If end of buffer */
  795.                   done = 1;    /* then end of last segment */
  796.                 *s = NUL;    /* Overwrite comma with NUL */
  797.                 if (!*s2) {    /* Empty segment, no advancement */
  798.                 k = 0;
  799.                 } else if (tp = (char *)malloc(n+len+1)) {
  800.                 strcpy(tp,s2);  /* Current segment */
  801.                 strcat(tp,p+1);    /* Add rest of pattern */
  802.                 tp[n+len] = NUL;
  803.                 k = ckmatch(tp,string,icase,opts);
  804.                 free(tp);
  805.                 if (k > 0) { /* If it matched we're done */
  806. #ifdef COMMENT
  807.                     makestr(&ckmstring,mstart);
  808. #endif /* COMMENT */
  809.                     return(1);
  810.                 }
  811.                 } else {    /* Malloc failure, just compare */
  812.                 k = !ckstrcmp(tp,string,n-1,icase);
  813.                 }
  814.                 if (k) {    /* Successful comparison */
  815.                 string += n-1; /* Advance pointers */
  816.                 pattern = p+1;
  817.                 break;
  818.                 }
  819.                 if (done)    /* If no more segments */
  820.                   break;    /* break out of segment loop. */
  821.                 s2 = s+1;    /* Otherwise, on to next segment */
  822.                 n = 0;
  823.             }
  824.             }
  825.             free(buf);
  826.         }
  827.         }
  828. #endif /* CKREGEX */
  829.     } else if (cp == '*') {        /* Asterisk */
  830.         char * p, * s = NULL;
  831.         int k, n, q = 0;
  832.         while (*pattern == '*')    /* Collapse successive asterisks */
  833.           pattern++;
  834.         psave = pattern;        /* First non-asterisk after asterisk */
  835.         for (n = 0, p = psave; *p; p++,n++) { /* Find next meta char */
  836.         if (!q) {
  837.             if (*p == '?' || *p == '*' || *p == CMDQ
  838. #ifdef CKREGEX
  839.             || *p == '[' || *p == '{'
  840. #endif /* CKREGEX */
  841.             )
  842.               break;
  843. #ifdef GLOBBING
  844.             if (globbing
  845. #ifdef UNIXOROSK
  846.             && *p == '/'
  847. #else
  848. #ifdef VMS
  849.             && (*p == '.' || *p == ']' ||
  850.                 *p == '<' || *p == '>' ||
  851.                 *p == ':' || *p == ';')
  852. #else
  853. #ifdef datageneral
  854.             && *p == ':'
  855. #else
  856. #ifdef STRATUS
  857.             && *p == '>'
  858. #endif /* STRATUS */
  859. #endif /* datageneral */
  860. #endif /* VMS */
  861. #endif /* UNIXOROSK */
  862.             )
  863.               break;
  864. #endif /* GLOBBING */
  865.         }
  866.         }
  867.         if (n > 0) {        /* Literal string to match  */
  868.         s = (char *)malloc(n+1);
  869.         if (s) {
  870.             ckstrncpy(s,psave,n+1); /* Copy cuz no poking original */
  871.             debug(F111,"XXX",s,n+1);
  872.             if (*p == '*')
  873.               k = ckindex(s,string,0,0,icase); /* 1-based index() */
  874.             else
  875.               k = ckindex(s,string,-1,1,icase); /* 1-based rindex() */
  876.             free(s);
  877.             if (k < 1)
  878.               return(0);
  879.             string += k + n - 1;
  880.             pattern += n;
  881.             continue;
  882.         }
  883.         } else if (!*p) {        /* Asterisk at end matches the rest */
  884.         if (!globbing) {    /* (if not filename globbing) */
  885. #ifdef COMMENT
  886.             makestr(&ckmstring,mstart);
  887. #endif /* COMMENT */
  888.             return(1);
  889.         }
  890. #ifdef GLOBBING
  891.         while (*string) {    /* Globbing so don't cross fields */
  892.             if (globbing
  893. #ifdef UNIXOROSK
  894.             && *string == '/'
  895. #else
  896. #ifdef VMS
  897.             && (*string == '.' || *string == ']' ||
  898.                 *string == '<' || *string == '>' ||
  899.                 *string == ':' || *string == ';')
  900. #else
  901. #ifdef datageneral
  902.             && *string == ':'
  903. #else
  904. #ifdef STRATUS
  905.             && *string == '>'
  906. #endif /* STRATUS */
  907. #endif /* datageneral */
  908. #endif /* VMS */
  909. #endif /* UNIXOROSK */
  910.             )
  911.               return(0);
  912.             string++;
  913.         }
  914. #endif /* GLOBBING */
  915. #ifdef COMMENT
  916.         makestr(&ckmstring,mstart);
  917. #endif /* COMMENT */
  918.         return(1);
  919.  
  920.         } else {            /* A meta char follows asterisk */
  921.         while (*string && (k = ckmatch(p,string,icase,opts) < 1))
  922.           string++;
  923. #ifdef COMMENT
  924.         if (*string) makestr(&ckmstring,mstart);
  925. #endif /* COMMENT */
  926.         return(*string ? 1 : 0);
  927.         }
  928.     } else if (cs == cp) {
  929.         pattern++, string++;
  930.         continue;
  931.     } else
  932.       return(0);
  933.     }
  934. }
  935.  
  936.  
  937. #ifdef CKFLOAT
  938. /*  I S F L O A T  -- Verify that arg represents a floating-point number */
  939.  
  940. /*
  941.   Portable replacement for atof(), strtod(), scanf(), etc.
  942.  
  943.   Call with:
  944.     s = pointer to string
  945.     flag == 0 means entire string must be a (floating-pointing) number.
  946.     flag != 0 means to terminate scan on first character that is not legal.
  947.  
  948.   Returns:
  949.     1 if result is a floating point number;
  950.     0 if not or if string empty.
  951.  
  952.   Side effect:
  953.     Sets global floatval to floating-point value if successful.
  954.  
  955.   Number need not contain a decimal point -- integer is subcase of float.
  956.   Scientific notation not supported.
  957. */
  958. CKFLOAT floatval = 0.0;            /* For returning value */
  959.  
  960. int
  961. isfloat(s,flag) char *s; int flag; {
  962.     int state = 0;
  963.     int sign = 0;
  964.     char c;
  965.     CKFLOAT d = 0.0, f = 0.0;
  966.  
  967.     if (!s) return(0);
  968.     if (!*s) return(0);
  969.  
  970.     while (isspace(*s)) s++;
  971.  
  972.     if (*s == '-') {            /* Handle optional sign */
  973.     sign = 1;
  974.     s++;
  975.     } else if (*s == '+')
  976.       s++;
  977.     while (c = *s++) {            /* Handle numeric part */
  978.     switch (state) {
  979.       case 0:            /* Mantissa... */
  980.         if (isdigit(c)) {
  981.         f = f * 10.0 + (CKFLOAT)(c - '0');
  982.         continue;
  983.         } else if (c == '.') {
  984.         state = 1;
  985.         d = 1.0;
  986.         continue;
  987.         }
  988.         if (flag)            /* Not digit or period */
  989.           goto done;        /* break if flag != 0 */
  990.         return(0);            /* otherwise fail. */
  991.       case 1:            /* Fraction... */
  992.         if (isdigit(c)) {
  993.         d *= 10.0;
  994.         f += (CKFLOAT)(c - '0') / d;
  995.         continue;
  996.         }
  997.       default:
  998.         if (flag)            /* Illegal character */
  999.           goto done;        /* Break */
  1000.         return(0);            /* or fail, depending on flag */
  1001.     }
  1002.     }
  1003.   done:
  1004.     if (sign) f = 0.0 - f;        /* Apply sign to result */
  1005.     floatval = f;            /* Set result */
  1006.     return(1);                /* Succeed */
  1007. }
  1008. #endif /* CKFLOAT */
  1009.  
  1010. /* Sorting routines... */
  1011.  
  1012. #ifdef USE_QSORT
  1013. /*
  1014.   Quicksort works but it's not measurably faster than shell sort,
  1015.   probably because it makes a lot more comparisons, since
  1016.   it was originally designed for sorting an array of integers.
  1017.   It would need more thorough testing and debugging before production use.
  1018. */
  1019. static int            /* Internal comparison routine for ckqsort() */
  1020. compare(s1,s2,k,r,c) char *s1, *s2; int k, r, c; {
  1021.     int x;
  1022.     char *t, *t1, *t2;
  1023. #ifdef CKFLOAT
  1024.     CKFLOAT f1, f2;
  1025. #else
  1026.     long n1, n2;
  1027. #endif /* CKFLOAT */
  1028.  
  1029.     t = t2 = s1;            /* String 1 */
  1030.     if (!t)                /* If it's NULL */
  1031.       t2 = "";                /* make it the empty string */
  1032.     if (k > 0 && *t2) {
  1033.     if ((int)strlen(t2) < k)    /* If key too big */
  1034.       t2 = "";            /* make key the empty string */
  1035.     else                /* Key is in string */
  1036.       t2 += k;            /* so point to key position */
  1037.     }
  1038.     t1 = s2;
  1039.     if (!t1)                /* Same deal for s2 */
  1040.       t1 = "";
  1041.     if (k > 0 && *t1) {
  1042.     if ((int)strlen(t1) < k)
  1043.       t1 = "";
  1044.     else
  1045.       t1 += k;
  1046.     }
  1047.     if (c == 2) {            /* Numeric comparison */
  1048.     x = 0;
  1049. #ifdef CKFLOAT
  1050.     f2 = 0.0;
  1051.     f1 = 0.0;
  1052.     if (isfloat(t1,1)) {
  1053.         f1 = floatval;
  1054.         if (isfloat(t2,1))
  1055.           f2 = floatval;
  1056.         else
  1057.           f1 = 0.0;
  1058.     }
  1059.     if (f2 < f1)
  1060.       x = 1;
  1061.     else
  1062.       x = -1;
  1063. #else
  1064.     n2 = 0L;
  1065.     n1 = 0L;
  1066.     if (rdigits(t1)) {
  1067.         n1 = atol(t1);
  1068.         if (rdigits(t2))
  1069.           n2 = atol(t2);
  1070.         else
  1071.           n1 = 0L;
  1072.     }
  1073.     if (n2 < n1)
  1074.       x = 1;
  1075.     else
  1076.       x = -1;
  1077. #endif /* CKFLOAT */
  1078.     } else {
  1079.     x = ckstrcmp(t1,t2,-1,c);
  1080.     }
  1081.     return(x);
  1082. }
  1083.  
  1084. /* It's called sh_sort() but it's really quicksort... */
  1085.  
  1086. VOID
  1087. sh_sort(s,s2,n,k,r,how) char **s, **s2; int n, k, r, how; {
  1088.     int x, lp, up, p, lv[16], uv[16], m, c;
  1089.     char * y, * y2;
  1090.  
  1091.     if (!s) return;
  1092.     if (n < 2) return;
  1093.     if (k < 0) k = 0;
  1094.  
  1095.     lv[0] = 0;
  1096.     uv[0] = n-1;
  1097.     p = 0;
  1098. stb:                    /* Hmmm, looks like Fortran... */
  1099.     if (p < 0)
  1100.     return;
  1101. stc:
  1102.     lp = lv[p];
  1103.     up = uv[p];
  1104.     m = up - lp + 1;
  1105.     if (m < 2) {
  1106.     p--;
  1107.     goto stb;
  1108.     }
  1109.     if (m == 2) {
  1110.     x = compare(s[lp],s[up],k,r,how);
  1111.     if (x > 0) {
  1112.         y = s[lp];
  1113.         s[lp] = s[up];
  1114.         s[up] = y;
  1115.         if (s2) {
  1116.         y2 = s2[lp];
  1117.         s2[lp] = s2[up];
  1118.         s2[up] = y2;
  1119.         }
  1120.     }
  1121.     p--;
  1122.     goto stb;
  1123.     }
  1124.     c = (lp+up) / 2;
  1125.     if (m < 10)
  1126.       goto std;
  1127.     x = compare(s[lp],s[c],k,r,how);
  1128.     if (x < 1) {
  1129.     if (s[c] <= s[up]) {
  1130.         goto std;
  1131.         } else {
  1132.         x = compare(s[lp],s[up],k,r,how);
  1133.         if (x < 1)
  1134.           c = up;
  1135.         else
  1136.           c = lp;
  1137.         goto std;
  1138.     }
  1139.     } else {
  1140.     x = compare(s[up],s[c],k,r,how);
  1141.     if (x < 1) {
  1142.         goto std;
  1143.     } else {
  1144.         x = compare(s[lp],s[up],k,r,how);
  1145.         if (x < 1)
  1146.           c = lp;
  1147.         else
  1148.           c = up;
  1149.         goto std;
  1150.     }
  1151.     }
  1152. std:
  1153.     y = s[c];
  1154.     s[c] = s[up];
  1155.     if (s2) {
  1156.     y2 = s2[c];
  1157.     s2[c] = s2[up];
  1158.     }
  1159.     lp--;
  1160. stf:
  1161.     if ((up - lp) < 2)
  1162.       goto stk;
  1163.     lp++;
  1164.     x = compare(s[lp],y,k,r,how);
  1165.     if (x < 1)
  1166.       goto stf;
  1167.     s[up] = s[lp];
  1168. sth:
  1169.     if ((up - lp) < 2)
  1170.       goto stj;
  1171.     up--;
  1172.     x = compare(s[up],y,k,r,how);
  1173.     if (x > 0)
  1174.       goto sth;
  1175.     s[lp] = s[up];
  1176.     goto stf;
  1177. stj:
  1178.     up--;
  1179. stk:
  1180.     if (up == uv[p]) {
  1181.     lp = lv[p] - 1;
  1182. stl:
  1183.     if ((up - lp) < 2)
  1184.       goto stq;
  1185.     lp++;
  1186.     x = compare(s[lp],y,k,r,how);
  1187.     if (x < 0)
  1188.       goto stl;
  1189.     s[up] = s[lp];
  1190. stn:
  1191.     if ((up - lp) < 2)
  1192.       goto stp;
  1193.     up--;
  1194.     x = compare(s[up],y,k,r,how);
  1195.     if (x >= 0)
  1196.       goto stn;
  1197.     s[lp] = s[up];
  1198.     goto stl;
  1199. stp:
  1200.     up--;
  1201. stq:
  1202.     s[up] = y;
  1203.     if (s2)
  1204.       s2[up] = y2;
  1205.         if (up == lv[p]) {
  1206.         p--;
  1207.         goto stb;
  1208.     }
  1209.     uv[p] = up - 1;
  1210.     goto stc;
  1211.     }
  1212.     s[up] = y;
  1213.     if (s2)
  1214.       s2[up] = y2;
  1215.     if ((up - lv[p]) < (uv[p] - up)) {
  1216.     lv[p+1] = lv[p];
  1217.     uv[p+1] = up - 1;
  1218.     lv[p] = up + 1;
  1219.     } else {
  1220.     lv[p+1] = up + 1;
  1221.     uv[p+1] = uv[p];
  1222.     uv[p] = up - 1;
  1223.     }
  1224.     p++;
  1225.     goto stc;
  1226. }
  1227.  
  1228. #else  /* !USE_QSORT */
  1229.  
  1230. /* S H _ S O R T  --  Shell sort -- sorts string array s in place. */
  1231.  
  1232. /*
  1233.   Highly defensive and relatively quick.
  1234.   Uses shell sort algorithm.
  1235.  
  1236.   Args:
  1237.    s = pointer to array of strings.
  1238.    p = pointer to a second array to sort in parallel s, or NULL for none.
  1239.    n = number of elements in s.
  1240.    k = position of key.
  1241.    r = ascending lexical order if zero, reverse lexical order if nonzero.
  1242.    c = 0 for case independence, 1 for case matters, 2 for numeric.
  1243.  
  1244.   If k is past the right end of a string, the string is considered empty
  1245.   for comparison purposes.
  1246.  
  1247.   Hint:
  1248.    To sort a piece of an array, call with s pointing to the first element
  1249.    and n the number of elements to sort.
  1250.  
  1251.   Return value:
  1252.    None.  Always succeeds, unless any of s[0]..s[n-1] are bad pointers,
  1253.    in which case memory violations are possible, but C offers no defense
  1254.    against this, so no way to gracefully return an error code.
  1255. */
  1256. VOID
  1257. sh_sort(s,p,n,k,r,c) char **s, **p; int n, k, r, c; {
  1258.     int m, i, j, x;
  1259.     char *t, *t1, *t2, *u = NULL;
  1260. #ifdef CKFLOAT
  1261.     CKFLOAT f1, f2;
  1262. #else
  1263.     long n1, n2;
  1264. #endif /* CKFLOAT */
  1265.  
  1266.     if (!s) return;            /* Nothing to sort? */
  1267.     if (n < 2) return;            /* Not enough elements to sort? */
  1268.     if (k < 0) k = 0;            /* Key */
  1269.  
  1270.     m = n;                /* Initial group size is whole array */
  1271.     while (1) {
  1272.     m = m / 2;            /* Divide group size in half */
  1273.     if (m < 1)            /* Small as can be, so done */
  1274.       break;
  1275.     for (j = 0; j < n-m; j++) {    /* Sort each group */
  1276.         t = t2 = s[j+m];        /* Compare this one... */
  1277.         if (!t)            /* But if it's NULL */
  1278.           t2 = "";            /* make it the empty string */
  1279.         if (p)            /* Handle parallel array, if any */
  1280.           u = p[j+m];
  1281.         if (k > 0 && *t2) {
  1282.         if ((int)strlen(t2) < k) /* If key too big */
  1283.           t2 = "";        /* make key the empty string */
  1284.         else            /* Key is in string */
  1285.           t2 = t + k;        /* so point to key position */
  1286.         }
  1287.         for (i = j; i >= 0; i -= m) { /* Loop thru comparands s[i..]*/
  1288.         t1 = s[i];
  1289.         if (!t1)        /* Same deal */
  1290.           t1 = "";
  1291.         if (k > 0 && *t1) {
  1292.             if ((int)strlen(t1) < k)
  1293.               t1 = "";
  1294.             else
  1295.               t1 = s[i]+k;
  1296.         }
  1297.         if (c == 2) {        /* Numeric comparison */
  1298.             x = 0;
  1299. #ifdef CKFLOAT
  1300.             f2 = 0.0;
  1301.             f1 = 0.0;
  1302.             if (isfloat(t1,1)) {
  1303.             f1 = floatval;
  1304.             if (isfloat(t2,1))
  1305.               f2 = floatval;
  1306.             else
  1307.               f1 = 0.0;
  1308.             }
  1309.             if (f2 < f1)
  1310.               x = 1;
  1311.             else
  1312.               x = -1;
  1313. #else
  1314.             n2 = 0L;
  1315.             n1 = 0L;
  1316.             if (rdigits(t1)) {
  1317.             n1 = atol(t1);
  1318.             if (rdigits(t2))
  1319.               n2 = atol(t2);
  1320.             else
  1321.               n1 = 0L;
  1322.             }
  1323.             if (n2 < n1)
  1324.               x = 1;
  1325.             else
  1326.               x = -1;
  1327. #endif /* CKFLOAT */
  1328.         } else {
  1329.             x = ckstrcmp(t1,t2,-1,c); /* Compare */
  1330.         }
  1331.         if (r == 0 && x < 0)
  1332.           break;
  1333.         if (r != 0 && x > 0)
  1334.           break;
  1335.         s[i+m] = s[i];
  1336.         if (p) p[i+m] = p[i];
  1337.         }
  1338.         s[i+m] = t;
  1339.         if (p) p[i+m] = u;
  1340.     }
  1341.     }
  1342. }
  1343. #endif /* COMMENT */
  1344.  
  1345. /*  F I L E S E L E C T  --  Select this file for sending  */
  1346.  
  1347. int
  1348. fileselect(f,sa,sb,sna,snb,minsiz,maxsiz,nbu,nxlist,xlist)
  1349.  char *f,*sa,*sb,*sna,*snb; long minsiz,maxsiz; int nbu,nxlist; char ** xlist;
  1350. /* fileselect */ {
  1351.     char *fdate;
  1352.     int n;
  1353.     long z;
  1354.  
  1355.     if (!sa) sa = "";
  1356.     if (!sb) sb = "";
  1357.     if (!sna) sna = "";
  1358.     if (!snb) snb = "";
  1359.  
  1360.     debug(F110,"fileselect",f,0);
  1361.     if (*sa || *sb || *sna || *snb) {
  1362.     fdate = zfcdat(f);        /* Date/time of this file */
  1363.     if (!fdate) fdate = "";
  1364.     n = strlen(fdate);
  1365.     debug(F111,"fileselect fdate",fdate,n);
  1366.     if (n != 17)            /* Failed to get it */
  1367.       return(1);
  1368.     /* /AFTER: */
  1369.     if (sa[0] && (strcmp(fdate,(char *)sa) <= 0)) {
  1370.         debug(F110,"fileselect sa",sa,0);
  1371.         /* tlog(F110,"Skipping (too old)",f,0); */
  1372.         return(0);
  1373.     }
  1374.     /* /BEFORE: */
  1375.     if (sb[0] && (strcmp(fdate,(char *)sb) >= 0)) {
  1376.         debug(F110,"fileselect sb",sb,0);
  1377.         /* tlog(F110,"Skipping (too new)",f,0); */
  1378.         return(0);
  1379.     }
  1380.     /* /NOT-AFTER: */
  1381.     if (sna[0] && (strcmp(fdate,(char *)sna) > 0)) {
  1382.         debug(F110,"fileselect sna",sna,0);
  1383.         /* tlog(F110,"Skipping (too new)",f,0); */
  1384.         return(0);
  1385.     }
  1386.     /* /NOT-BEFORE: */
  1387.     if (snb[0] && (strcmp(fdate,(char *)snb) < 0)) {
  1388.         debug(F110,"fileselect snb",snb,0);
  1389.         /* tlog(F110,"Skipping (too old)",f,0); */
  1390.         return(0);
  1391.     }
  1392.     }
  1393.     if (minsiz > -1L || maxsiz > -1L) { /* Smaller or larger */
  1394.     z = zchki(f);            /* Get size */
  1395.     debug(F101,"fileselect filesize","",z);
  1396.     if (z < 0)
  1397.       return(1);
  1398.     if ((minsiz > -1L) && (z >= minsiz)) {
  1399.         debug(F111,"fileselect minsiz skipping",f,minsiz);
  1400.         /* tlog(F111,"Skipping (too big)",f,z); */
  1401.         return(0);
  1402.     }
  1403.     if ((maxsiz > -1L) && (z <= maxsiz)) {
  1404.         debug(F111,"fileselect maxsiz skipping",f,maxsiz);
  1405.         /* tlog(F110,"Skipping (too small)",f,0); */
  1406.         return(0);
  1407.     }
  1408.     }
  1409.     if (nbu) {                /* Skipping backup files? */
  1410.     if (ckmatch(
  1411. #ifdef CKREGEX
  1412.             "*.~[0-9]*~"    /* Not perfect but close enough. */
  1413. #else
  1414.             "*.~*~"        /* Less close. */
  1415. #endif /* CKREGEX */
  1416.             ,f,filecase,2+1)) {
  1417.         debug(F110,"fileselect skipping backup",f,0);
  1418.         return(0);
  1419.     }
  1420.     }
  1421.     for (n = 0; xlist && n < nxlist; n++) {
  1422.     if (!xlist[n]) {
  1423.         debug(F101,"fileselect xlist empty",0,n);
  1424.         break;
  1425.     }
  1426.     if (ckmatch(xlist[n],f,filecase,2+1)) {
  1427.         debug(F111,"fileselect xlist",xlist[n],n);
  1428.         debug(F110,"fileselect skipping",f,0);
  1429.         return(0);
  1430.     }
  1431.     }
  1432.     debug(F110,"fileselect selecting",f,0);
  1433.     return(1);
  1434. }
  1435.  
  1436. /* C K R A D I X  --  Radix converter */
  1437. /*
  1438.    Call with:
  1439.      s:   a number in string format.
  1440.      in:  int, specifying the radix of s, 2-36.
  1441.      out: int, specifying the radix to convert to, 2-36.
  1442.    Returns:
  1443.      NULL on error (illegal radix, illegal number, etc.).
  1444.      "-1" on overflow (number too big for unsigned long).
  1445.      Otherwise: Pointer to result.
  1446. */
  1447. #define RXRESULT 127
  1448. static char rxdigits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  1449. static char rxresult[RXRESULT+1];
  1450.  
  1451. char *
  1452. ckradix(s,in,out) char * s; int in, out; {
  1453.     char c, *r = rxresult;
  1454.     int d, minus = 0;
  1455.     unsigned long zz = 0L;
  1456.     long z;
  1457.     if (in < 2 || in > 36)        /* Verify legal input radix */
  1458.       return(NULL);
  1459.     if (out < 2 || out > 36)        /* and output radix. */
  1460.       return(NULL);
  1461.     if (*s == '+') {            /* Get sign if any */
  1462.     s++;
  1463.     } else if (*s == '-') {
  1464.     minus++;
  1465.     s++;
  1466.     }
  1467.     while (*s == SP || *s == '0')    /* Trim leading blanks or 0's */
  1468.       s++;
  1469. /*
  1470.   For detecting overflow, we use a signed copy of the unsigned long
  1471.   accumulator.  If it goes negative, we know we'll overflow NEXT time
  1472.   through the loop.
  1473. */
  1474.     for (; *s;  s++) {            /* Convert from input radix to */
  1475.     c = *s;                /* unsigned long */
  1476.     if (islower(c)) c = toupper(c);
  1477.     if (c >= '0' && c <= '9')
  1478.       d = c - '0';
  1479.     else if (c >= 'A' && c <= 'Z')
  1480.       d = c - 'A' + 10;
  1481.     else
  1482.       return(NULL);
  1483.     zz = zz * in + d;
  1484.     if (z < 0L)            /* Clever(?) overflow detector */
  1485.       return("-1");
  1486.         z = zz;
  1487.     }
  1488.     if (!zz) return("0");
  1489.     r = &rxresult[RXRESULT];        /* Convert from unsigned long */
  1490.     *r-- = NUL;                /* to output radix. */
  1491.     while (zz > 0 && r > rxresult) {
  1492.     d = zz % out;
  1493.     *r-- = rxdigits[d];
  1494.     zz = zz / out;
  1495.     }
  1496.     if (minus) *r-- = '-';        /* Replace original sign */
  1497.     return((char *)(r+1));
  1498. }
  1499.  
  1500. #ifndef NOB64
  1501. /* Base-64 conversion routines */
  1502.  
  1503. static char b64[] = {            /* Encoding vector */
  1504. #ifdef pdp11
  1505.   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  1506. #else
  1507.   'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S',
  1508.   'T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l',
  1509.   'm','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4',
  1510.   '5','6','7','8','9','+','/','=','\0'
  1511. #endif /* pdp11 */
  1512. };
  1513. static int b64tbl[] = {            /* Decoding vector */
  1514.     -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  1515.     -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  1516.     -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  1517.     52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1,
  1518.     -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
  1519.     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  1520.     -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  1521.     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
  1522.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1523.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1524.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1525.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1526.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1527.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1528.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1529.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  1530. };
  1531.  
  1532. /*
  1533.    B 8 T O B 6 4  --  Converts 8-bit data to Base64 encoding.
  1534.  
  1535.    Call with:
  1536.      s   = Pointer to 8-bit data;
  1537.      n   = Number of source bytes to encode (SEE NOTE).
  1538.            If it's a null-terminated string, you can use -1 here.
  1539.      out = Address of output buffer.
  1540.      len = Length of output buffer (should > 4/3 longer than input).
  1541.  
  1542.    Returns:
  1543.      >= 0 if OK, number of bytes placed in output buffer,
  1544.           with the subsequent byte set to NUL if space permits.
  1545.      -1 on error (output buffer space exhausted).
  1546.  
  1547.    NOTE:
  1548.      If this function is to be called repeatedly, e.g. to encode a data
  1549.      stream a chunk at a time, the source length must be a multiple of 3
  1550.      in all calls but the final one to avoid the generation of extraneous
  1551.      pad characters that would throw the decoder out of sync.  When encoding
  1552.      only a single string, this is not a consideration.  No internal state
  1553.      is kept, so there is no reset function.
  1554. */
  1555. int
  1556. b8tob64(s,n,out,len) char * s,* out; int n, len; {
  1557.     int b3, b4, i, x = 0;
  1558.     unsigned int t;
  1559.  
  1560.     if (n < 0) n = strlen(s);
  1561.  
  1562.     for (i = 0; i < n; i += 3,x += 4) { /* Loop through source bytes */
  1563.     b3 = b4 = 0;
  1564.     t = (unsigned)((unsigned)((unsigned)s[i] & 0xff) << 8);
  1565.     if (n - 1 > i) {        /* Do we have another after this? */
  1566.             t |= (unsigned)(s[i+1] & 0xff); /* Yes, OR it in */
  1567.             b3 = 1;            /* And remember */
  1568.         }
  1569.         t <<= 8;            /* Move over */
  1570.         if (n - 2 > i) {        /* Another one after that? */
  1571.             t |= (unsigned)(s[i+2] & 0xff); /* Yes, OR it in */
  1572.             b4 = 1;            /* and remember */
  1573.         }
  1574.     if (x + 4 > len)        /* Check output space */
  1575.       return(-1);
  1576.     out[x+3] = b64[b4 ? (t & 0x3f) : 64]; /* 64 = code for '=' */
  1577.         t >>= 6;
  1578.         out[x+2] = b64[b3 ? (t & 0x3f) : 64];
  1579.         t >>= 6;
  1580.         out[x+1] = b64[t & 0x3f];
  1581.         t >>= 6;
  1582.         out[x]   = b64[t & 0x3f];
  1583.     }
  1584.     if (x < len) out[x] = NUL;        /* Null-terminate the string */
  1585.     return(x);
  1586. }
  1587.  
  1588.  
  1589. /*
  1590.    B 6 4 T O B 8  --  Converts Base64 string to 8-bit data.
  1591.  
  1592.    Call with:
  1593.      s   = pointer to Base64 string (whitespace ignored).
  1594.      n   = length of string, or -1 if null terminated, or 0 to reset.
  1595.      out = address of output buffer.
  1596.      len = length of output buffer.
  1597.  
  1598.    Returns:
  1599.      >= 0 if OK, number of bytes placed in output buffer,
  1600.           with the subsequent byte set to NUL if space permits.
  1601.      <  0 on error:
  1602.        -1 = output buffer too small for input.
  1603.        -2 = input contains illegal characters.
  1604.        -3 = internal coding error.
  1605.  
  1606.    NOTE:
  1607.      Can be called repeatedly to decode a Base64 stream, one chunk at a
  1608.      time.  However, if it is to be called for multiple streams in
  1609.      succession, its internal state must be reset at the beginning of
  1610.      the new stream.
  1611. */
  1612. int
  1613. b64tob8(s,n,out,len) char * s,* out; int len; {    /* Decode */
  1614.     static int bits = 0;
  1615.     static unsigned int r = 0;
  1616.     int i, k = 0, x, t;
  1617.     unsigned char c;
  1618.  
  1619.     if (n == 0) {            /* Reset state */
  1620.     bits = 0;
  1621.     r = 0;
  1622.     return(0);
  1623.     }
  1624.     x = (n < 0) ? strlen(s) : n;    /* Source length */
  1625.  
  1626.     n = ((x + 3) / 4) * 3;        /* Compute destination length */
  1627.     if (x > 0 && s[x-1] == '=') n--;    /* Account for padding */
  1628.     if (x > 1 && s[x-2] == '=') n--;
  1629.     if (n > len)            /* Destination not big enough */
  1630.       return(-1);            /* Fail */
  1631.  
  1632.     for (i = 0; i < x; i++) {        /* Loop thru source */
  1633.     c = (unsigned)s[i];        /* Next char */
  1634.         t = b64tbl[c];            /* Code for this char */
  1635.     if (t == -2) {            /* Whitespace or Ctrl */
  1636.         n--;            /* Ignore */
  1637.         continue;
  1638.     } else if (t == -1) {        /* Illegal code */
  1639.         return(-2);            /* Fail. */
  1640.     } else if (t > 63 || t < 0)    /* Illegal value */
  1641.       return(-3);            /* fail. */
  1642.     bits += 6;            /* Count bits */
  1643.     r <<= 6;            /* Make space */
  1644.     r |= (unsigned) t;        /* OR in new code */
  1645.     if (bits >= 8) {        /* Have a byte yet? */
  1646.         bits -= 8;            /* Output it */
  1647.         c = (unsigned) ((r >> bits) & 0xff);
  1648.         out[k++] = c;
  1649.     }
  1650.     }
  1651.     if (k < len) out[k] = NUL;        /* Null-terminate in case it's */
  1652.     return(k);                /* a text string */
  1653. }
  1654. #endif /* NOB64 */
  1655.  
  1656. /* C H K N U M  --  See if argument string is an integer  */
  1657.  
  1658. /* Returns 1 if OK, zero if not OK */
  1659. /* If OK, string should be acceptable to atoi() */
  1660. /* Allows leading space, sign */
  1661.  
  1662. int
  1663. chknum(s) char *s; {            /* Check Numeric String */
  1664.     int x = 0;                /* Flag for past leading space */
  1665.     int y = 0;                /* Flag for digit seen */
  1666.     char c;
  1667.     debug(F110,"chknum",s,0);
  1668.     while (c = *s++) {            /* For each character in the string */
  1669.     switch (c) {
  1670.       case SP:            /* Allow leading spaces */
  1671.       case HT:
  1672.         if (x == 0) continue;
  1673.         else return(0);
  1674.       case '+':            /* Allow leading sign */
  1675.       case '-':
  1676.         if (x == 0) x = 1;
  1677.         else return(0);
  1678.         break;
  1679.       default:            /* After that, only decimal digits */
  1680.         if (c >= '0' && c <= '9') {
  1681.         x = y = 1;
  1682.         continue;
  1683.         } else return(0);
  1684.     }
  1685.     }
  1686.     return(y);
  1687. }
  1688.  
  1689.  
  1690. /*  R D I G I T S  -- Verify that all characters in arg ARE DIGITS  */
  1691.  
  1692. /*  Returns 1 if so, 0 if not or if string is empty */
  1693.  
  1694. int
  1695. rdigits(s) char *s; {
  1696.     if (!s) return(0);
  1697.     do {
  1698.         if (!isdigit(*s)) return(0);
  1699.         s++;
  1700.     } while (*s);
  1701.     return(1);
  1702. }
  1703.  
  1704. /*  P A R N A M  --  Return parity name */
  1705.  
  1706. char *
  1707. #ifdef CK_ANSIC
  1708. parnam(char c)
  1709. #else
  1710. parnam(c) char c;
  1711. #endif /* CK_ANSIC */
  1712. /* parnam */ {
  1713.     switch (c) {
  1714.     case 'e': return("even");
  1715.     case 'o': return("odd");
  1716.     case 'm': return("mark");
  1717.     case 's': return("space");
  1718.     case 0:   return("none");
  1719.     default:  return("invalid");
  1720.     }
  1721. }
  1722.  
  1723. char *                    /* Convert seconds to hh:mm:ss */
  1724. #ifdef CK_ANSIC
  1725. hhmmss(long x)
  1726. #else
  1727. hhmmss(x) long x;
  1728. #endif /* CK_ANSIC */
  1729. /* hhmmss(x) */ {
  1730.     static char buf[10];
  1731.     long s, h, m;
  1732.     h = x / 3600L;            /* Hours */
  1733.     x = x % 3600L;
  1734.     m = x / 60L;            /* Minutes */
  1735.     s = x % 60L;            /* Seconds */
  1736.     if (x > -1L)
  1737.       sprintf(buf,"%02ld:%02ld:%02ld",h,m,s);
  1738.     else
  1739.       buf[0] = NUL;
  1740.     return((char *)buf);
  1741. }
  1742.  
  1743. /* L S E T  --  Set s into p, right padding to length n with char c; */
  1744. /*
  1745.    s is a NUL-terminated string.
  1746.    If length(s) > n, only n bytes are moved.
  1747.    The result is NOT NUL terminated unless c == NUL and length(s) < n.
  1748.    The intended of this routine is for filling in fixed-length record fields.
  1749. */
  1750. VOID
  1751. lset(p,s,n,c) char *s; char *p; int n; int c; {
  1752.     int x;
  1753. #ifndef USE_MEMCPY
  1754.     int i;
  1755. #endif /* USE_MEMCPY */
  1756.     if (!s) s = "";
  1757.     x = strlen(s);
  1758.     if (x > n) x = n;
  1759. #ifdef USE_MEMCPY
  1760.     memcpy(p,s,x);
  1761.     if (n > x)
  1762.       memset(p+x,c,n-x);
  1763. #else
  1764.     for (i = 0; i < x; i++)
  1765.       *p++ = *s++;
  1766.     for (; i < n; i++)
  1767.       *p++ = c;
  1768. #endif /* USE_MEMCPY */
  1769. }
  1770.  
  1771. /* R S E T  --  Right-adjust s in p, left padding to length n with char c */
  1772.  
  1773. VOID
  1774. rset(p,s,n,c) char *s; char *p; int n; int c; {
  1775.     int x;
  1776. #ifndef USE_MEMCPY
  1777.     int i;
  1778. #endif /* USE_MEMCPY */
  1779.     if (!s) s = "";
  1780.     x = strlen(s);
  1781.     if (x > n) x = n;
  1782. #ifdef USE_MEMCPY
  1783.     memset(p,c,n-x);
  1784.     memcpy(p+n-x,s,x);
  1785. #else
  1786.     for (i = 0; i < (n - x); i++)
  1787.       *p++ = c;
  1788.     for (; i < n; i++)
  1789.       *p++ = *s++;
  1790. #endif /* USE_MEMCPY */
  1791. }
  1792.  
  1793. /*  U L O N G T O H E X  --  Unsigned long to hex  */
  1794.  
  1795. /*
  1796.   Converts unsigned long arg to hex and returns string pointer to
  1797.   rightmost n hex digits left padded with 0's.  Allows for longs
  1798.   up to 64 bits.  Returns pointer to result.
  1799. */
  1800. char *
  1801. ulongtohex(z,n) unsigned long z; int n; {
  1802.     static char hexbuf[17];
  1803.     int i = 16, x, k = 0;
  1804.     hexbuf[16] = '\0';
  1805.     if (n > 16) n = 16;
  1806.     k = 2 * (sizeof(long));
  1807.     for (i = 0; i < n; i++) {
  1808.     if (i > k || z == 0) {
  1809.         hexbuf[15-i] = '0';
  1810.     } else {
  1811.         x = z & 0x0f;
  1812.         z = z >> 4;
  1813.         hexbuf[15-i] = x + ((x < 10) ? '0' : 0x37);
  1814.     }
  1815.     }
  1816.     return((char *)(&hexbuf[16-i]));
  1817. }
  1818.  
  1819. /*  H E X T O U L O N G  --  Hex string to unsigned long  */
  1820.  
  1821. /*
  1822.   Converts n chars from s from hex to unsigned long.
  1823.   Returns:
  1824.    0L or positive, good result (0L is returned if arg is NULL or empty).
  1825.   -1L on error: non-hex arg or overflow.
  1826. */
  1827. long
  1828. hextoulong(s,n) char *s; int n; {
  1829.     char buf[64];
  1830.     unsigned long result = 0L;
  1831.     int d, count = 0, i;
  1832.     int flag = 0;
  1833.     if (!s) s = "";
  1834.     if (!*s) {
  1835.     return(0L);
  1836.     }
  1837.     if (n < 1)
  1838.       return(0L);
  1839.     if (n > 63) n = 63;
  1840.     strncpy(buf,s,n);
  1841.     buf[n] = '\0';
  1842.     s = buf;
  1843.     while (*s) {
  1844.     d = *s++;
  1845.     if ((d == '0' || d == ' ')) {
  1846.         if (!flag)
  1847.           continue;
  1848.     } else {
  1849.         flag = 1;
  1850.     }
  1851.     if (islower(d))
  1852.       d = toupper(d);
  1853.     if (d >= '0' && d <= '9') {
  1854.         d -= 0x30;
  1855.     } else if (d >= 'A' && d <= 'F') {
  1856.         d -= 0x37;
  1857.     } else {
  1858.         return(-1L);
  1859.     }
  1860.     if (++count > (sizeof(long) * 2))
  1861.       return(-1L);
  1862.     result = (result << 4) | (d & 0x0f);
  1863.     }
  1864.     return(result);
  1865. }
  1866.  
  1867. /* End of ckclib.c */
  1868.