misc.c
Go to the documentation of this file.00001
00002
00003
00004
00005 FILE_LICENCE ( GPL2_OR_LATER );
00006
00007 #include <stdlib.h>
00008 #include <ctype.h>
00009 #include <byteswap.h>
00010 #include <gpxe/in.h>
00011 #include <gpxe/timer.h>
00012
00013
00014
00015
00016 int inet_aton ( const char *cp, struct in_addr *inp ) {
00017 const char *p = cp;
00018 const char *digits_start;
00019 unsigned long ip = 0;
00020 unsigned long val;
00021 int j;
00022 for(j = 0; j <= 3; j++) {
00023 digits_start = p;
00024 val = strtoul(p, ( char ** ) &p, 10);
00025 if ((p == digits_start) || (val > 255)) return 0;
00026 if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
00027 ip = (ip << 8) | val;
00028 }
00029 if ( *p == '\0' ) {
00030 inp->s_addr = htonl(ip);
00031 return 1;
00032 }
00033 return 0;
00034 }
00035
00036 unsigned long strtoul ( const char *p, char **endp, int base ) {
00037 unsigned long ret = 0;
00038 unsigned int charval;
00039
00040 while ( isspace ( *p ) )
00041 p++;
00042
00043 if ( base == 0 ) {
00044 base = 10;
00045 if ( *p == '0' ) {
00046 p++;
00047 base = 8;
00048 if ( ( *p | 0x20 ) == 'x' ) {
00049 p++;
00050 base = 16;
00051 }
00052 }
00053 }
00054
00055 while ( 1 ) {
00056 charval = *p;
00057 if ( charval >= 'a' ) {
00058 charval = ( charval - 'a' + 10 );
00059 } else if ( charval >= 'A' ) {
00060 charval = ( charval - 'A' + 10 );
00061 } else if ( charval <= '9' ) {
00062 charval = ( charval - '0' );
00063 }
00064 if ( charval >= ( unsigned int ) base )
00065 break;
00066 ret = ( ( ret * base ) + charval );
00067 p++;
00068 }
00069
00070 if ( endp )
00071 *endp = ( char * ) p;
00072
00073 return ( ret );
00074 }
00075
00076
00077
00078
00079
00080