addr.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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
  11. inet_aton(cp, inp)
  12. const char *cp;
  13. struct in_addr *inp;
  14. {
  15. unsigned long addr;
  16. int value;
  17. int part;
  18. if (!inp)
  19. return 0;
  20. addr = 0;
  21. for (part=1;part<=4;part++) {
  22. if (!isdigit(*cp))
  23. return 0;
  24. value = 0;
  25. while (isdigit(*cp)) {
  26. value *= 10;
  27. value += *cp++ - '0';
  28. if (value > 255)
  29. return 0;
  30. }
  31. if (*cp++ != ((part == 4) ? '\0' : '.'))
  32. return 0;
  33. addr <<= 8;
  34. addr |= value;
  35. }
  36. inp->s_addr = htonl(addr);
  37. return 1;
  38. }
  39. #endif
  40. #ifdef L_inet_addr
  41. unsigned long
  42. inet_addr(cp)
  43. const char *cp;
  44. {
  45. struct in_addr a;
  46. if (!inet_aton(cp, &a))
  47. return -1;
  48. else
  49. return a.s_addr;
  50. }
  51. #endif
  52. #ifdef L_inet_ntoa
  53. extern char * itoa(int);
  54. char *
  55. inet_ntoa(in)
  56. struct in_addr in;
  57. {
  58. static char buf[18];
  59. unsigned long addr = ntohl(in.s_addr);
  60. strcpy(buf, itoa((addr >> 24) & 0xff));
  61. strcat(buf, ".");
  62. strcat(buf, itoa((addr >> 16) & 0xff));
  63. strcat(buf, ".");
  64. strcat(buf, itoa((addr >> 8) & 0xff));
  65. strcat(buf, ".");
  66. strcat(buf, itoa(addr & 0xff));
  67. return buf;
  68. }
  69. #endif