home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / MISC.C < prev    next >
C/C++ Source or Header  |  1992-05-02  |  5KB  |  253 lines

  1. /* Miscellaneous machine independent utilities
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "socket.h"
  7. #include "mbuf.h"
  8.  
  9. /* Select from an array of strings, or return ascii number if out of range */
  10. char *
  11. smsg(msgs,nmsgs,n)
  12. char *msgs[];
  13. unsigned nmsgs,n;
  14. {
  15.     static char buf[16];
  16.  
  17.     if(n < nmsgs && msgs[n] != NULLCHAR)
  18.         return msgs[n];
  19.     sprintf(buf,"%u",n);
  20.     return buf;
  21. }
  22.  
  23. /* Convert hex-ascii to integer */
  24. int
  25. htoi(s)
  26. char *s;
  27. {
  28.     int i = 0;
  29.     char c;
  30.  
  31.     while((c = *s++) != '\0'){
  32.         if(c == 'x')
  33.             continue;    /* allow 0x notation */
  34.         if('0' <= c && c <= '9')
  35.             i = (i * 16) + (c - '0');
  36.         else if('a' <= c && c <= 'f')
  37.             i = (i * 16) + (c - 'a' + 10);
  38.         else if('A' <= c && c <= 'F')
  39.             i = (i * 16) + (c - 'A' + 10);
  40.         else
  41.             break;
  42.     }
  43.     return i;
  44. }
  45. /* replace terminating end of line marker(s) with null */
  46. void
  47. rip(s)
  48. register char *s;
  49. {
  50.     register char *cp;
  51.  
  52.     if((cp = strchr(s,'\n')) != NULLCHAR)
  53.         *cp = '\0';
  54. }
  55. /* Count the occurrances of 'c' in a buffer */
  56. int
  57. memcnt(buf,c,size)
  58. char *buf;
  59. char c;
  60. int size;
  61. {
  62.     int cnt = 0;
  63.     char *icp;
  64.  
  65.     while(size != 0){
  66.         if((icp = memchr(buf,c,size)) == NULLCHAR)
  67.             break;    /* No more found */
  68.         /* Advance the start of the next search to right after
  69.          * this character
  70.          */
  71.         buf += (icp - buf + 1);
  72.         size -= (icp - buf + 1);
  73.         cnt++;
  74.     }
  75.     return cnt;
  76. }
  77. /* Copy a string to a malloc'ed buffer. Turbo C has this one in its
  78.  * library, but it doesn't call mallocw() and can therefore return NULL.
  79.  * NOS uses of strdup() generally don't check for NULL, so they need this one.
  80.  */
  81. char *
  82. strdup(s)
  83. const char *s;
  84. {
  85.     register char *out;
  86.     register int len;
  87.  
  88.     if(s == NULLCHAR)
  89.         return NULLCHAR;
  90.     len = strlen(s);
  91.     out = mallocw(len+1);
  92.     /* This is probably a tad faster than strcpy, since we know the len */
  93.     memcpy(out,s,len);
  94.     out[len] = '\0';
  95.     return out;
  96. }
  97. /* Routines not needed for Turbo 2.0, but available for older libraries */
  98. #ifdef    AZTEC
  99.  
  100. /* Case-insensitive string comparison */
  101. strnicmp(a,b,n)
  102. register char *a,*b;
  103. register int n;
  104. {
  105.     char a1,b1;
  106.  
  107.     while(n-- != 0 && (a1 = *a++) != '\0' && (b1 = *b++) != '\0'){
  108.         if(a1 == b1)
  109.             continue;    /* No need to convert */
  110.         a1 = tolower(a1);
  111.         b1 = tolower(b1);
  112.         if(a1 == b1)
  113.             continue;    /* NOW they match! */
  114.         if(a1 > b1)
  115.             return 1;
  116.         if(a1 < b1)
  117.             return -1;
  118.     }
  119.     return 0;
  120. }
  121.  
  122. char *
  123. strtok(s1,s2)
  124. char *s1;    /* Source string (first call) or NULL */
  125. #ifdef    __STDC__    /* Ugly kludge for aztec's declaration */
  126. const char *s2;    /* Delimiter string */
  127. #else
  128. char *s2;    /* Delimiter string */
  129. #endif
  130. {
  131.     static int isdelim();
  132.     static char *next;
  133.     register char *cp;
  134.     char *tmp;
  135.  
  136.     if(s2 == NULLCHAR)
  137.         return NULLCHAR;    /* Must give delimiter string */
  138.  
  139.     if(s1 != NULLCHAR)
  140.         next = s1;        /* First call */
  141.  
  142.     if(next == NULLCHAR)
  143.         return NULLCHAR;    /* No more */
  144.  
  145.     /* Find beginning of this token */
  146.     for(cp = next;*cp != '\0' && isdelim(*cp,s2);cp++)
  147.         ;
  148.  
  149.     if(*cp == '\0')
  150.         return NULLCHAR;    /* Trailing delimiters, no token */
  151.  
  152.     /* Save the beginning of this token, and find its end */
  153.     tmp = cp;
  154.     next = NULLCHAR;    /* In case we don't find another delim */
  155.     for(;*cp != '\0';cp++){
  156.         if(isdelim(*cp,s2)){
  157.             *cp = '\0';
  158.             next = cp + 1;    /* Next call will begin here */
  159.             break;
  160.         }
  161.     }
  162.     return tmp;
  163. }
  164. static int
  165. isdelim(c,delim)
  166. char c;
  167. register char *delim;
  168. {
  169.     char d;
  170.  
  171.     while((d = *delim++) != '\0'){
  172.         if(c == d)
  173.             return 1;
  174.     }
  175.     return 0;
  176. }
  177. #endif    /* AZTEC */
  178.  
  179.  
  180.  
  181. /* Host-network conversion routines, replaced on the x86 with
  182.  * assembler code in pcgen.asm
  183.  */
  184. #ifndef    MSDOS
  185. /* Put a long in host order into a char array in network order */
  186. char *
  187. put32(cp,x)
  188. register char *cp;
  189. int32 x;
  190. {
  191.     *cp++ = x >> 24;
  192.     *cp++ = x >> 16;
  193.     *cp++ = x >> 8;
  194.     *cp++ = x;
  195.     return cp;
  196. }
  197. /* Put a short in host order into a char array in network order */
  198. char *
  199. put16(cp,x)
  200. register char *cp;
  201. int16 x;
  202. {
  203.     *cp++ = x >> 8;
  204.     *cp++ = x;
  205.  
  206.     return cp;
  207. }
  208. int16
  209. get16(cp)
  210. register char *cp;
  211. {
  212.     register int16 x;
  213.  
  214.     x = uchar(*cp++);
  215.     x <<= 8;
  216.     x |= uchar(*cp);
  217.     return x;
  218. }
  219. /* Machine-independent, alignment insensitive network-to-host long conversion */
  220. int32
  221. get32(cp)
  222. register char *cp;
  223. {
  224.     int32 rval;
  225.  
  226.     rval = uchar(*cp++);
  227.     rval <<= 8;
  228.     rval |= uchar(*cp++);
  229.     rval <<= 8;
  230.     rval |= uchar(*cp++);
  231.     rval <<= 8;
  232.     rval |= uchar(*cp);
  233.  
  234.     return rval;
  235. }
  236. /* Compute int(log2(x)) */
  237. int
  238. log2(x)
  239. register int16 x;
  240. {
  241.     register int n = 16;
  242.     for(;n != 0;n--){
  243.         if(x & 0x8000)
  244.             break;
  245.         x <<= 1;
  246.     }
  247.     n--;
  248.     return n;
  249. }
  250.  
  251. #endif
  252.  
  253.