home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / MISC.C < prev    next >
C/C++ Source or Header  |  1991-03-08  |  4KB  |  232 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.  
  56. /* Copy a string to a malloc'ed buffer. Turbo C has this one in its
  57.  * library, but it doesn't call mallocw() and can therefore return NULL.
  58.  * NOS uses of strdup() generally don't check for NULL, so they need this one.
  59.  */
  60. char *
  61. strdup(s)
  62. const char *s;
  63. {
  64.     register char *out;
  65.     register int len;
  66.  
  67.     if(s == NULLCHAR)
  68.         return NULLCHAR;
  69.     len = strlen(s);
  70.     out = mallocw(len+1);
  71.     /* This is probably a tad faster than strcpy, since we know the len */
  72.     memcpy(out,s,len);
  73.     out[len] = '\0';
  74.     return out;
  75. }
  76. /* Routines not needed for Turbo 2.0, but available for older libraries */
  77. #ifdef    AZTEC
  78.  
  79. /* Case-insensitive string comparison */
  80. strnicmp(a,b,n)
  81. register char *a,*b;
  82. register int n;
  83. {
  84.     char a1,b1;
  85.  
  86.     while(n-- != 0 && (a1 = *a++) != '\0' && (b1 = *b++) != '\0'){
  87.         if(a1 == b1)
  88.             continue;    /* No need to convert */
  89.         a1 = tolower(a1);
  90.         b1 = tolower(b1);
  91.         if(a1 == b1)
  92.             continue;    /* NOW they match! */
  93.         if(a1 > b1)
  94.             return 1;
  95.         if(a1 < b1)
  96.             return -1;
  97.     }
  98.     return 0;
  99. }
  100.  
  101. char *
  102. strtok(s1,s2)
  103. char *s1;    /* Source string (first call) or NULL */
  104. #ifdef    __STDC__    /* Ugly kludge for aztec's declaration */
  105. const char *s2;    /* Delimiter string */
  106. #else
  107. char *s2;    /* Delimiter string */
  108. #endif
  109. {
  110.     static int isdelim();
  111.     static char *next;
  112.     register char *cp;
  113.     char *tmp;
  114.  
  115.     if(s2 == NULLCHAR)
  116.         return NULLCHAR;    /* Must give delimiter string */
  117.  
  118.     if(s1 != NULLCHAR)
  119.         next = s1;        /* First call */
  120.  
  121.     if(next == NULLCHAR)
  122.         return NULLCHAR;    /* No more */
  123.  
  124.     /* Find beginning of this token */
  125.     for(cp = next;*cp != '\0' && isdelim(*cp,s2);cp++)
  126.         ;
  127.  
  128.     if(*cp == '\0')
  129.         return NULLCHAR;    /* Trailing delimiters, no token */
  130.  
  131.     /* Save the beginning of this token, and find its end */
  132.     tmp = cp;
  133.     next = NULLCHAR;    /* In case we don't find another delim */
  134.     for(;*cp != '\0';cp++){
  135.         if(isdelim(*cp,s2)){
  136.             *cp = '\0';
  137.             next = cp + 1;    /* Next call will begin here */
  138.             break;
  139.         }
  140.     }
  141.     return tmp;
  142. }
  143. static int
  144. isdelim(c,delim)
  145. char c;
  146. register char *delim;
  147. {
  148.     char d;
  149.  
  150.     while((d = *delim++) != '\0'){
  151.         if(c == d)
  152.             return 1;
  153.     }
  154.     return 0;
  155. }
  156. #endif    /* AZTEC */
  157.  
  158.  
  159.  
  160. /* Host-network conversion routines, replaced on the x86 with
  161.  * assembler code in pcgen.asm
  162.  */
  163. #ifndef    MSDOS
  164. /* Put a long in host order into a char array in network order */
  165. char *
  166. put32(cp,x)
  167. register char *cp;
  168. int32 x;
  169. {
  170.     *cp++ = x >> 24;
  171.     *cp++ = x >> 16;
  172.     *cp++ = x >> 8;
  173.     *cp++ = x;
  174.     return cp;
  175. }
  176. /* Put a short in host order into a char array in network order */
  177. char *
  178. put16(cp,x)
  179. register char *cp;
  180. int16 x;
  181. {
  182.     *cp++ = x >> 8;
  183.     *cp++ = x;
  184.  
  185.     return cp;
  186. }
  187. int16
  188. get16(cp)
  189. register char *cp;
  190. {
  191.     register int16 x;
  192.  
  193.     x = uchar(*cp++);
  194.     x <<= 8;
  195.     x |= uchar(*cp);
  196.     return x;
  197. }
  198. /* Machine-independent, alignment insensitive network-to-host long conversion */
  199. int32
  200. get32(cp)
  201. register char *cp;
  202. {
  203.     int32 rval;
  204.  
  205.     rval = uchar(*cp++);
  206.     rval <<= 8;
  207.     rval |= uchar(*cp++);
  208.     rval <<= 8;
  209.     rval |= uchar(*cp++);
  210.     rval <<= 8;
  211.     rval |= uchar(*cp);
  212.  
  213.     return rval;
  214. }
  215. /* Compute int(log2(x)) */
  216. int
  217. log2(x)
  218. register int16 x;
  219. {
  220.     register int n = 16;
  221.     for(;n != 0;n--){
  222.         if(x & 0x8000)
  223.             break;
  224.         x <<= 1;
  225.     }
  226.     n--;
  227.     return n;
  228. }
  229.  
  230. #endif
  231.  
  232.