You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

misc.c 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**************************************************************************
  2. MISC Support Routines
  3. **************************************************************************/
  4. FILE_LICENCE ( GPL2_OR_LATER );
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <byteswap.h>
  8. #include <ipxe/in.h>
  9. #include <ipxe/timer.h>
  10. /**************************************************************************
  11. INET_ATON - Convert an ascii x.x.x.x to binary form
  12. **************************************************************************/
  13. int inet_aton ( const char *cp, struct in_addr *inp ) {
  14. const char *p = cp;
  15. const char *digits_start;
  16. unsigned long ip = 0;
  17. unsigned long val;
  18. int j;
  19. for(j = 0; j <= 3; j++) {
  20. digits_start = p;
  21. val = strtoul(p, ( char ** ) &p, 10);
  22. if ((p == digits_start) || (val > 255)) return 0;
  23. if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
  24. ip = (ip << 8) | val;
  25. }
  26. if ( *p == '\0' ) {
  27. inp->s_addr = htonl(ip);
  28. return 1;
  29. }
  30. return 0;
  31. }
  32. unsigned long strtoul ( const char *p, char **endp, int base ) {
  33. unsigned long ret = 0;
  34. int negative = 0;
  35. unsigned int charval;
  36. while ( isspace ( *p ) )
  37. p++;
  38. if ( *p == '-' ) {
  39. negative = 1;
  40. p++;
  41. }
  42. base = strtoul_base ( &p, base );
  43. while ( 1 ) {
  44. charval = strtoul_charval ( *p );
  45. if ( charval >= ( unsigned int ) base )
  46. break;
  47. ret = ( ( ret * base ) + charval );
  48. p++;
  49. }
  50. if ( negative )
  51. ret = -ret;
  52. if ( endp )
  53. *endp = ( char * ) p;
  54. return ( ret );
  55. }
  56. /*
  57. * Local variables:
  58. * c-basic-offset: 8
  59. * End:
  60. */