int isdigit ( int c /* character to test */ ) { return __isdigit(c); }
static int valid_digit(char c, char base) { if (base == 16) { return __isxdigit(c); } else { return (__isdigit(c) && (c < '0' + base)); } }
unsigned long inet_addr(char *inetString) { unsigned long val, base, n; char c; unsigned long parts[MAX_PARTS]; unsigned long *pp; /* If null inet string */ if (inetString == NULL) { errnoSet(S_inetLib_ILLEGAL_INTERNET_ADDRESS); return ERROR; } /* End if null inet string */ /* Initialize locals */ pp = parts; again: val = 0; base = 10; /* If zero prefix */ if (*inetString == '0') { base = 8; inetString++; /* If hexadecimal prefix */ if ( (*inetString == 'x') || (*inetString == 'X') ) { base = 16; inetString++; } /* End if hexadecimal prefix */ } /* End if zero prefix */ /* While not string terminator */ while ( (c = *inetString) ) { /* If digit */ if ( __isdigit((int) c) ) { val = (val * base) + (c - '0'); inetString++; continue; } /* End if digit */ /* If hexadecimal digit */ if ( __isxdigit((int) c) ) { val = (val << 4) + (c + 10 - (__islower((int) c) ? 'a' : 'A')); inetString++; continue; } /* End if hexadecimal digit */ /* If here break loop */ break; } /* End while not string terminator */ /* If dot found */ if (*inetString == '.') { /* If string to large or value to large */ if ( (pp >= parts + (MAX_PARTS - 1)) || (val > 0xff) ) { errnoSet(S_inetLib_ILLEGAL_INTERNET_ADDRESS); return ERROR; } /* End if string to large or value to large */ /* Store value */ *pp++ = val; /* Advance */ inetString++; /* Find next value */ goto again; } /* End if dot found */ /* If trailing character */ if ( (*inetString) && !(__isspace((int) *inetString)) ) { errnoSet(S_inetLib_ILLEGAL_INTERNET_ADDRESS); return ERROR; } /* End if trailing character */ /* Store value */ *pp++ = val; n = pp - parts; /* Select parts number */ switch (n) { /* 32 bit address */ case 1: val = parts[0]; break; /* 8.24 bits address */ case 2: /* If value to large */ if (val > 0xffffff) { errnoSet(S_inetLib_ILLEGAL_INTERNET_ADDRESS); return ERROR; } /* End if value to large */ /* Store value */ val = (parts[0] << 24) | parts[1]; break; /* 8.8.16 bits address */ case 3: /* If value to large */ if (val > 0xffff) { errnoSet(S_inetLib_ILLEGAL_INTERNET_ADDRESS); return ERROR; } /* End if value to large */ /* Store value */ val = (parts[0] << 24) | (parts[1] << 16) | parts[2]; break; /* 8.8.8.8 bits */ case 4: /* If value to large */ if (val > 0xff) { errnoSet(S_inetLib_ILLEGAL_INTERNET_ADDRESS); return ERROR; } /* End if value to large */ /* Store value */ val = (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]; break; default: errnoSet(S_inetLib_ILLEGAL_INTERNET_ADDRESS); return ERROR; } /* End select part number */ return htonl(val); }
int vfscanf(FILE *fp, const char *format, va_list ap) { #ifdef INCLUDE_LONG_LONG #define STRTO_L_(s,e,b,u) _strto_ll(s,e,b,u) #define MAX_DIGITS 64 #define UV_TYPE unsigned long long #define V_TYPE long long #else #define STRTO_L_(s,e,b,u) _strto_l(s,e,b,u) #define MAX_DIGITS 32 #define UV_TYPE unsigned long #define V_TYPE long #endif #ifdef INCLUDE_FLOAT long double ld; #endif UV_TYPE uv; struct scan_cookie sc; unsigned const char *fmt; const char *p; unsigned char *b; void *vp; int cc, i, cnt; signed char lval; unsigned char store, usflag, base, invert, r0, r1; unsigned char buf[MAX_DIGITS+2]; unsigned char scanset[UCHAR_MAX + 1]; init_scan_cookie(&sc,fp); fmt = (unsigned const char *) format; cnt = 0; while (*fmt) { store = 1; lval = 0; sc.width = INT_MAX; if (*fmt == '%') { /* Conversion specification. */ ++fmt; if (*fmt == '*') { /* Suppress assignment. */ store = 0; ++fmt; } for (i = 0 ; __isdigit(*fmt) ; sc.width = i) { i = (i * 10) + (*fmt++ - '0'); /* Get specified width. */ } for (i = 0 ; i < sizeof(qual_s) ; i++) { /* Optional qualifier. */ if (qual_s[i] == *fmt) { ++fmt; lval += qsz[i]; if ((i < 2) && (qual_s[i] == *fmt)) { /* Double h or l. */ ++fmt; lval += qsz[i]; } break; } } for (p = spec_s ; *p ; p++) { /* Process format specifier. */ if (*fmt != *p) continue; if (p-spec_s < 1) { /* % - match a '%'*/ goto matchchar; } if (p-spec_s < 2) { /* n - store number of chars read */ *(va_arg(ap, int *)) = sc.nread; scan_getc_nw(&sc); goto nextfmt; } if (p-spec_s > 3) { /* skip white space if not c or [ */ while (__isspace(scan_getc_nw(&sc))) {} scan_ungetc(&sc); } if (p-spec_s < 5) { /* [,c,s - string conversions */ invert = 0; if (*p == 'c') { invert = 1; if (sc.width == INT_MAX) { sc.width = 1; } } for (i=0 ; i<= UCHAR_MAX ; i++) { scanset[i] = ((*p == 's') ? (__isspace(i) == 0) : 0); } if (*p == '[') { /* need to build a scanset */ if (*++fmt == '^') { invert = 1; ++fmt; } if (*fmt == ']') { scanset[(int)']'] = 1; ++fmt; } r0 = 0; while (*fmt && *fmt !=']') { /* build scanset */ if ((*fmt == '-') && r0 && (fmt[1] != ']')) { /* range */ ++fmt; if (*fmt < r0) { r1 = r0; r0 = *fmt; } else { r1 = *fmt; } for (i=r0 ; i<= r1 ; i++) { scanset[i] = 1; } r0 = 0; } else { r0 = *fmt; scanset[r0] = 1; } ++fmt; } if (!*fmt) { /* format string exhausted! */ goto done; } } /* ok -- back to common work */ if (sc.width <= 0) { goto done; } if (store) { b = va_arg(ap, unsigned char *); } else { b = buf; } cc = scan_getc(&sc); if (cc <= 0) { scan_ungetc(&sc); goto done; /* return EOF if cnt == 0 */ } i = 0; while ((cc>0) && (scanset[cc] != invert)) { i = 1; /* yes, we stored something */ *b = cc; b += store; cc = scan_getc(&sc); } if (i==0) { scan_ungetc(&sc); goto done; /* return cnt */ } if (*p != 'c') { /* nul-terminate the stored string */ *b = 0; } cnt += store; goto nextfmt; } if (p-spec_s < 12) { /* o,u,p,x,X,i,d - (un)signed integer */ if (*p == 'p') { /* assume pointer same size as int or long. */ lval = (sizeof(char *) == sizeof(long)); } usflag = ((p-spec_s) < 10); /* (1)0 if (un)signed */ base = radix[(int)(p-spec_s) - 5]; b = buf; if (sc.width <= 0) { goto done; } cc = scan_getc(&sc); if ((cc == '+') || (cc == '-')) { /* Handle leading sign.*/ *b++ = cc; cc = scan_getc(&sc); } if (cc == '0') { /* Possibly set base and handle prefix. */ if ((base == 0) || (base == 16)) { cc = scan_getc(&sc); if ((cc == 'x') || (cc == 'X')) { /* We're committed to base 16 now. */ base = 16; cc = scan_getc(&sc); } else { /* oops... back up */ scan_ungetc(&sc); cc = '0'; if (base == 0) { base = 8; } } } } if (base == 0) { /* Default to base 10 */ base = 10; } /* At this point, we're ready to start reading digits. */ if (cc == '0') { *b++ = cc; /* Store first leading 0 */ do { /* but ignore others. */ cc = scan_getc(&sc); } while (cc == '0'); } while (valid_digit(cc,base)) { /* Now for nonzero digits.*/ if (b - buf < MAX_DIGITS) { *b++ = cc; } cc = scan_getc(&sc); } *b = 0; /* null-terminate */ if ((b == buf) || (*--b == '+') || (*b == '-')) { scan_ungetc(&sc); goto done; /* No digits! */ } if (store) { if (*buf == '-') { usflag = 0; } uv = STRTO_L_(buf, NULL, base, usflag); vp = va_arg(ap, void *); switch (lval) { case 2: /* If no long long, treat as long . */ #ifdef INCLUDE_LONG_LONG *((unsigned long long *)vp) = uv; break; #endif case 1: #if ULONG_MAX == UINT_MAX case 0: /* int and long int are the same */ #endif #ifdef INCLUDE_LONG_LONG if (usflag) { if (uv > ULONG_MAX) { uv = ULONG_MAX; } } else if (((V_TYPE)uv) > LONG_MAX) { uv = LONG_MAX; } else if (((V_TYPE)uv) < LONG_MIN) { uv = (UV_TYPE) LONG_MIN; } #endif *((unsigned long *)vp) = (unsigned long)uv; break; #if ULONG_MAX != UINT_MAX case 0: /* int and long int are different */ if (usflag) { if (uv > UINT_MAX) { uv = UINT_MAX; } } else if (((V_TYPE)uv) > INT_MAX) { uv = INT_MAX; } else if (((V_TYPE)uv) < INT_MIN) { uv = (UV_TYPE) INT_MIN; } *((unsigned int *)vp) = (unsigned int)uv; break; #endif case -1: if (usflag) { if (uv > USHRT_MAX) { uv = USHRT_MAX; } } else if (((V_TYPE)uv) > SHRT_MAX) { uv = SHRT_MAX; } else if (((V_TYPE)uv) < SHRT_MIN) { uv = (UV_TYPE) SHRT_MIN; } *((unsigned short *)vp) = (unsigned short)uv; break; case -2: if (usflag) { if (uv > UCHAR_MAX) { uv = UCHAR_MAX; } } else if (((V_TYPE)uv) > CHAR_MAX) { uv = CHAR_MAX; } else if (((V_TYPE)uv) < CHAR_MIN) { uv = (UV_TYPE) CHAR_MIN; } *((unsigned char *)vp) = (unsigned char) uv; break; default: assert(0); } ++cnt; } goto nextfmt; } #ifdef INCLUDE_FLOAT else { /* floating point */ if (sc.width <= 0) {
static void _dopr( char **sbuffer, char **buffer, size_t *maxlen, size_t *retlen, int *truncated, const char *format, va_list args) { char ch; LLONG value; char *strvalue; int min; int max; int state; int flags; int cflags; size_t currlen; state = DP_S_DEFAULT; flags = currlen = cflags = min = 0; max = -1; ch = *format++; while (state != DP_S_DONE) { if (ch == '\0' || (buffer == NULL && currlen >= *maxlen)) state = DP_S_DONE; switch (state) { case DP_S_DEFAULT: if (ch == '%') state = DP_S_FLAGS; else doapr_outch(sbuffer,buffer, &currlen, maxlen, ch); ch = *format++; break; case DP_S_FLAGS: switch (ch) { case '-': flags |= DP_F_MINUS; ch = *format++; break; case '+': flags |= DP_F_PLUS; ch = *format++; break; case ' ': flags |= DP_F_SPACE; ch = *format++; break; case '#': flags |= DP_F_NUM; ch = *format++; break; case '0': flags |= DP_F_ZERO; ch = *format++; break; default: state = DP_S_MIN; break; } break; case DP_S_MIN: if (__isdigit((unsigned char)ch)){ min = 10 * min + char_to_int(ch); ch = *format++; } else if (ch == '*') { min = va_arg(args, int); ch = *format++; state = DP_S_DOT; } else state = DP_S_DOT; break; case DP_S_DOT: if (ch == '.') { state = DP_S_MAX; ch = *format++; } else state = DP_S_MOD; break; case DP_S_MAX: if (__isdigit((unsigned char)ch)) { if (max < 0) max = 0; max = 10 * max + char_to_int(ch); ch = *format++; } else if (ch == '*') { max = va_arg(args, int); ch = *format++; state = DP_S_MOD; } else
inline bool doisdigit(char c) { return __isdigit(c); }