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.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**************************************************************************
  2. MISC Support Routines
  3. **************************************************************************/
  4. #include <stdlib.h>
  5. #include <byteswap.h>
  6. #include <gpxe/in.h>
  7. #include <gpxe/timer.h>
  8. /**************************************************************************
  9. INET_ATON - Convert an ascii x.x.x.x to binary form
  10. **************************************************************************/
  11. int inet_aton ( const char *cp, struct in_addr *inp ) {
  12. const char *p = cp;
  13. const char *digits_start;
  14. unsigned long ip = 0;
  15. unsigned long val;
  16. int j;
  17. for(j = 0; j <= 3; j++) {
  18. digits_start = p;
  19. val = strtoul(p, ( char ** ) &p, 10);
  20. if ((p == digits_start) || (val > 255)) return 0;
  21. if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
  22. ip = (ip << 8) | val;
  23. }
  24. if ( *p == '\0' ) {
  25. inp->s_addr = htonl(ip);
  26. return 1;
  27. }
  28. return 0;
  29. }
  30. int isspace ( int c ) {
  31. switch ( c ) {
  32. case ' ':
  33. case '\f':
  34. case '\n':
  35. case '\r':
  36. case '\t':
  37. case '\v':
  38. return 1;
  39. default:
  40. return 0;
  41. }
  42. }
  43. unsigned long strtoul ( const char *p, char **endp, int base ) {
  44. unsigned long ret = 0;
  45. unsigned int charval;
  46. while ( isspace ( *p ) )
  47. p++;
  48. if ( base == 0 ) {
  49. base = 10;
  50. if ( *p == '0' ) {
  51. p++;
  52. base = 8;
  53. if ( ( *p | 0x20 ) == 'x' ) {
  54. p++;
  55. base = 16;
  56. }
  57. }
  58. }
  59. while ( 1 ) {
  60. charval = *p;
  61. if ( charval >= 'a' ) {
  62. charval = ( charval - 'a' + 10 );
  63. } else if ( charval >= 'A' ) {
  64. charval = ( charval - 'A' + 10 );
  65. } else if ( charval <= '9' ) {
  66. charval = ( charval - '0' );
  67. }
  68. if ( charval >= ( unsigned int ) base )
  69. break;
  70. ret = ( ( ret * base ) + charval );
  71. p++;
  72. }
  73. if ( endp )
  74. *endp = ( char * ) p;
  75. return ( ret );
  76. }
  77. /*
  78. * Local variables:
  79. * c-basic-offset: 8
  80. * End:
  81. */