[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / core / misc.c
blobc19591bb9f5e3563cd989664161b0afa9c163d26
1 /**************************************************************************
2 MISC Support Routines
3 **************************************************************************/
5 FILE_LICENCE ( GPL2_OR_LATER );
7 #include <stdlib.h>
8 #include <ctype.h>
9 #include <byteswap.h>
10 #include <gpxe/in.h>
11 #include <gpxe/timer.h>
13 /**************************************************************************
14 INET_ATON - Convert an ascii x.x.x.x to binary form
15 **************************************************************************/
16 int inet_aton ( const char *cp, struct in_addr *inp ) {
17 const char *p = cp;
18 const char *digits_start;
19 unsigned long ip = 0;
20 unsigned long val;
21 int j;
22 for(j = 0; j <= 3; j++) {
23 digits_start = p;
24 val = strtoul(p, ( char ** ) &p, 10);
25 if ((p == digits_start) || (val > 255)) return 0;
26 if ( ( j < 3 ) && ( *(p++) != '.' ) ) return 0;
27 ip = (ip << 8) | val;
29 if ( *p == '\0' ) {
30 inp->s_addr = htonl(ip);
31 return 1;
33 return 0;
36 unsigned long strtoul ( const char *p, char **endp, int base ) {
37 unsigned long ret = 0;
38 unsigned int charval;
40 while ( isspace ( *p ) )
41 p++;
43 if ( base == 0 ) {
44 base = 10;
45 if ( *p == '0' ) {
46 p++;
47 base = 8;
48 if ( ( *p | 0x20 ) == 'x' ) {
49 p++;
50 base = 16;
55 while ( 1 ) {
56 charval = *p;
57 if ( charval >= 'a' ) {
58 charval = ( charval - 'a' + 10 );
59 } else if ( charval >= 'A' ) {
60 charval = ( charval - 'A' + 10 );
61 } else if ( charval <= '9' ) {
62 charval = ( charval - '0' );
64 if ( charval >= ( unsigned int ) base )
65 break;
66 ret = ( ( ret * base ) + charval );
67 p++;
70 if ( endp )
71 *endp = ( char * ) p;
73 return ( ret );
77 * Local variables:
78 * c-basic-offset: 8
79 * End: