home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / MISC.C < prev    next >
C/C++ Source or Header  |  1994-08-26  |  5KB  |  240 lines

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