|
|
1.1 ! root 1: #include <ctype.h> ! 2: ! 3: #define ERROR 0x10 /* largest input base */ ! 4: ! 5: /* ! 6: * atod() converts the string 'num' to a double and returns its value. ! 7: * If there is a non-digit in the string, or if there is an overflow, ! 8: * then atod() exits with an appropriate error message. ! 9: * atod() accepts leading zero for octal and leading 0x for hexidecimal; ! 10: * in the latter case, 'a'-'f' and 'A'-'F' are accepted as digits. ! 11: */ ! 12: double ! 13: atod(num) ! 14: char *num; ! 15: { ! 16: register char *str; ! 17: register int i; ! 18: double res = 0, ! 19: base = 10; ! 20: ! 21: str = num; ! 22: i = *str++; ! 23: if (i == '0') ! 24: if ((i = *str++) == 'x') { ! 25: i = *str++; ! 26: base = 0x10; ! 27: } else ! 28: base = 010; ! 29: for (; i != '\0'; i = *str++) { ! 30: i = todigit(i); ! 31: if (i >= base) ! 32: die("bad number '%s'", num); ! 33: res = res * base + i; ! 34: if (res+1 == res) ! 35: die("number too big '%s'", num); ! 36: } ! 37: return (res); ! 38: } ! 39: ! 40: ! 41: /* ! 42: * todigit() converts character 'ch' to an integer equivalent, ! 43: * assuming that 'ch' is a digit or 'a'-'f' or 'A'-'F'. ! 44: * If this is not true, then it returns ERROR. ! 45: */ ! 46: todigit(ch) ! 47: register int ch; ! 48: { ! 49: if (!isascii(ch)) ! 50: return (ERROR); ! 51: if (isdigit(ch)) ! 52: return (ch - '0' + 0); ! 53: if (isupper(ch)) ! 54: ch = tolower(ch); ! 55: if ('a' <= ch && ch <= 'f') ! 56: return (ch - 'a' + 0xA); ! 57: return (ERROR); ! 58: } ! 59: ! 60:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.