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 2.0KB

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