addr.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
  2. * This file is part of the Linux-8086 C library and is distributed
  3. * under the GNU Library General Public License.
  4. */
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <netinet/in.h>
  8. #ifdef L_inet_aton
  9. int
  10. inet_aton(cp, inp)
  11. const char *cp;
  12. struct in_addr *inp;
  13. {
  14. unsigned long addr;
  15. int value;
  16. int part;
  17. if (!inp)
  18. return 0;
  19. addr = 0;
  20. for (part=1;part<=4;part++) {
  21. if (!isdigit(*cp))
  22. return 0;
  23. value = 0;
  24. while (isdigit(*cp)) {
  25. value *= 10;
  26. value += *cp++ - '0';
  27. if (value > 255)
  28. return 0;
  29. }
  30. if (*cp++ != ((part == 4) ? '\0' : '.'))
  31. return 0;
  32. addr <<= 8;
  33. addr |= value;
  34. }
  35. inp->s_addr = htonl(addr);
  36. return 1;
  37. }
  38. #endif
  39. #ifdef L_inet_addr
  40. unsigned long
  41. inet_addr(cp)
  42. const char *cp;
  43. {
  44. struct in_addr a;
  45. if (!inet_aton(cp, &a))
  46. return -1;
  47. else
  48. return a.s_addr;
  49. }
  50. #endif
  51. #ifdef L_inet_ntoa
  52. extern char * itoa(int);
  53. char *
  54. inet_ntoa(in)
  55. struct in_addr in;
  56. {
  57. static char buf[18];
  58. unsigned long addr = ntohl(in.s_addr);
  59. strcpy(buf, itoa((addr >> 24) & 0xff));
  60. strcat(buf, ".");
  61. strcat(buf, itoa((addr >> 16) & 0xff));
  62. strcat(buf, ".");
  63. strcat(buf, itoa((addr >> 8) & 0xff));
  64. strcat(buf, ".");
  65. strcat(buf, itoa(addr & 0xff));
  66. return buf;
  67. }
  68. #endif