home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff225.lzh / AmigaTCP / src / netuser.c < prev    next >
C/C++ Source or Header  |  1989-06-24  |  1KB  |  75 lines

  1. /* Miscellaneous format conversion subroutines */
  2.  
  3. #include "machdep.h"
  4. #include "netuser.h"
  5. int net_error;
  6.  
  7. /* Convert Internet address in dotted-decimal format (44.0.0.1) to binary */
  8. int32
  9. aton(s)
  10. char *s;
  11. {
  12.     int32 n;
  13.     int atoi(),i;
  14.     char *index();
  15.  
  16.     n = 0;
  17.     for(i=24;i>=0;i -= 8){
  18.         n |= (int32)atoi(s) << i;
  19.         if((s = index(s,'.')) == NULLCHAR)
  20.             break;
  21.         s++;
  22.     }
  23.     return n;
  24. }
  25. /* Convert an internet address (in host byte order) to a dotted decimal ascii
  26.  * string, e.g., 255.255.255.255\0
  27.  */
  28. char *
  29. inet_ntoa(a)
  30. int32 a;
  31. {
  32.     static char buf[16];
  33.  
  34.     sprintf(buf,"%u.%u.%u.%u",
  35.         hibyte(hiword(a)),
  36.         lobyte(hiword(a)),
  37.         hibyte(loword(a)),
  38.         lobyte(loword(a)) );
  39.     return buf;
  40. }
  41. /* Convert a socket (address + port) to an ascii string of the form
  42.  * aaa.aaa.aaa.aaa:ppppp
  43.  */
  44. char *
  45. psocket(s)
  46. struct socket *s;
  47. {
  48.     static char buf[30];
  49.  
  50.     sprintf(buf,"%s:%u",inet_ntoa(s->address),s->port);
  51.     return buf;
  52. }
  53. /* Convert hex-ascii string to long integer */
  54. long
  55. htol(s)
  56. char *s;
  57. {
  58.     long ret;
  59.     char c;
  60.  
  61.     ret = 0;
  62.     while((c = *s++) != '\0'){
  63.         c &= 0x7f;
  64.         if(c >= '0' && c <= '9')
  65.             ret = ret*16 + (c - '0');
  66.         else if(c >= 'a' && c <= 'f')
  67.             ret = ret*16 + (10 + c - 'a');
  68.         else if(c >= 'A' && c <= 'F')
  69.             ret = ret*16 + (10 + c - 'A');
  70.         else
  71.             break;
  72.     }
  73.     return ret;
  74. }
  75.