addr.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. int inet_aton(const char *cp, struct in_addr *inp);
  9. #ifdef L_inet_aton
  10. int 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 inet_addr(cp)
  41. const char *cp;
  42. {
  43. struct in_addr a;
  44. if (!inet_aton(cp, &a))
  45. return -1;
  46. else
  47. return a.s_addr;
  48. }
  49. #endif
  50. #ifdef L_inet_ntoa
  51. extern char *itoa(int);
  52. char *inet_ntoa(in)
  53. struct in_addr in;
  54. {
  55. static char buf[18];
  56. unsigned long addr = ntohl(in.s_addr);
  57. strcpy(buf, itoa((addr >> 24) & 0xff));
  58. strcat(buf, ".");
  59. strcat(buf, itoa((addr >> 16) & 0xff));
  60. strcat(buf, ".");
  61. strcat(buf, itoa((addr >> 8) & 0xff));
  62. strcat(buf, ".");
  63. strcat(buf, itoa(addr & 0xff));
  64. return buf;
  65. }
  66. #endif