vsta

Check-in [f2a0dfc9bb]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Support for %n format.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:f2a0dfc9bb8374579f20da9c4fb00942d82b1dfa36c1905d2c2126fe546ecbb6
User & Date: vandys 2001-12-21 14:41:25
Context
2001-12-21
14:50
Fix warnings. Add missing file. check-in: 2e2f499e93 user: vandys tags: master, trunk
14:41
Support for %n format. check-in: f2a0dfc9bb user: vandys tags: master, trunk
14:41
More CVS fallout. check-in: a022ec8942 user: vandys tags: master, trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to vsta/src/lib/scanf.c.

    48     48   	SPC,0,0,0,0,0,0,0,
    49     49   	0,0,0,0,0,0,0,0,
    50     50   	0,0,0,0,0,0,0,0,
    51     51   	0,0,0,0,0,0,0,0,
    52     52   };
    53     53   
    54     54   static int
    55         -instr(char *ptr, int type, int len, FILE *fp, int *eofptr)
           55  +instr(char *ptr, int type, int len, FILE *fp, int *eofptr,
           56  +	int *nreadp)
    56     57   {
    57     58   	int ch;
    58     59   	char *optr;
    59     60   	int ignstp;
    60     61   
    61     62   	*eofptr = 0;
    62     63   	optr = ptr;
................................................................................
    63     64   	if (type=='c' && len==30000) {
    64     65   		len = 1;
    65     66   	}
    66     67   	ignstp = 0;
    67     68   	if (type == 's') {
    68     69   		ignstp = SPC;
    69     70   	}
    70         -	while ((ch = getc(fp)) != EOF && _sctab[ch] & ignstp) {
    71         -		;
           71  +	while ((ch = getc(fp)) != EOF) {
           72  +		*nreadp += 1;
           73  +		if (!(_sctab[ch] & ignstp)) {
           74  +			break;
           75  +		}
    72     76   	}
    73     77   	ignstp = SPC;
    74     78   	if (type=='c')
    75     79   		ignstp = 0;
    76     80   	else if (type=='[')
    77     81   		ignstp = STP;
    78     82   	while (ch != EOF && (_sctab[ch]&ignstp)==0) {
    79     83   		if (ptr) {
    80     84   			*ptr++ = ch;
    81     85   		}
    82     86   		if (--len <= 0) {
    83     87   			break;
    84     88   		}
    85         -		ch = getc(fp);
           89  +		if ((ch = getc(fp)) != EOF) {
           90  +			*nreadp += 1;
           91  +		}
    86     92   	}
    87     93   	if (ch != EOF) {
    88     94   		if (len > 0) {
    89     95   			ungetc(ch, fp);
           96  +			*nreadp -= 1;
    90     97   		}
    91     98   		*eofptr = 0;
    92     99   	} else {
    93    100   		*eofptr = 1;
    94    101   	}
    95    102   	if (ptr && ptr!=optr) {
    96    103   		if (type != 'c') {
................................................................................
    98    105   		}
    99    106   		return(1);
   100    107   	}
   101    108   	return(0);
   102    109   }
   103    110   
   104    111   static int
   105         -innum(int **ptr, int type, int len, int size, FILE *fp, int *eofptr)
          112  +innum(int **ptr, int type, int len, int size, FILE *fp,
          113  +	int *eofptr, int *nreadp)
   106    114   {
   107    115   	char *np;
   108    116   	char numbuf[64];
   109    117   	int c, base;
   110    118   	int expseen, scale, negflg, c1, ndigit;
   111    119   	long lcval;
   112    120   
   113    121   	if (type=='c' || type=='s' || type=='[') {
   114    122   		return(instr(ptr ? *(char **)ptr :
   115         -			(char *)NULL, type, len, fp, eofptr));
          123  +			(char *)NULL, type, len, fp, eofptr, nreadp));
   116    124   	}
   117    125   	lcval = 0;
   118    126   	ndigit = 0;
   119    127   	scale = INT;
   120    128   	if (type=='e'||type=='f') {
   121    129   		scale = FLOAT;
   122    130   	}
................................................................................
   125    133   		base = 8;
   126    134   	} else if (type=='x') {
   127    135   		base = 16;
   128    136   	}
   129    137   	np = numbuf;
   130    138   	expseen = 0;
   131    139   	negflg = 0;
   132         -	while ((c = getc(fp)) == ' ' || c == '\t' || c == '\n');
          140  +	while ((c = getc(fp)) != EOF) {
          141  +		*nreadp += 1;
          142  +		if ((c != ' ') && (c != '\t') && (c != '\n')) {
          143  +			break;
          144  +		}
          145  +	}
   133    146   	if (c=='-') {
   134    147   		negflg++;
   135    148   		*np++ = c;
   136         -		c = getc(fp);
          149  +		if ((c = getc(fp)) != EOF) {
          150  +			*nreadp += 1;
          151  +		}
   137    152   		len--;
   138    153   	} else if (c=='+') {
   139    154   		len--;
   140         -		c = getc(fp);
          155  +		if ((c = getc(fp)) != EOF) {
          156  +			*nreadp += 1;
          157  +		}
   141    158   	}
   142         -	for ( ; --len>=0; *np++ = c, c = getc(fp)) {
          159  +	for ( ; --len>=0; *np++ = c, c = getc(fp), ++*nreadp) {
   143    160   		if (isdigit(c)
   144    161   		 || base==16 && ('a'<=c && c<='f' || 'A'<=c && c<='F')) {
   145    162   			ndigit++;
   146    163   			if (base == 8) {
   147    164   				lcval <<=3;
   148    165   			} else if (base==10) {
   149    166   				lcval = ((lcval<<2) + lcval)<<1;
................................................................................
   169    186   			continue;
   170    187   		} else if ((c=='e'||c=='E') && expseen==0) {
   171    188   			if (base != 10 || scale == INT || ndigit == 0) {
   172    189   				break;
   173    190   			}
   174    191   			expseen++;
   175    192   			*np++ = c;
   176         -			c = getc(fp);
          193  +			if ((c = getc(fp)) != EOF) {
          194  +				*nreadp += 1;
          195  +			}
   177    196   			if (c!='+'&&c!='-'&&('0'>c||c>'9')) {
   178    197   				break;
   179    198   			}
   180    199   		} else {
   181    200   			break;
   182    201   		}
   183    202   	}
   184    203   	if (negflg) {
   185    204   		lcval = -lcval;
   186    205   	}
   187    206   	if (c != EOF) {
   188    207   		ungetc(c, fp);
          208  +		*nreadp -= 1;
   189    209   		*eofptr = 0;
   190    210   	} else {
   191    211   		*eofptr = 1;
   192    212   	}
   193    213    	if (ptr == NULL || np == numbuf || (negflg && np == numbuf+1) ) {
   194    214   		return(0);
   195    215   	}
................................................................................
   258    278   	}
   259    279   	return((char *)s);
   260    280   }
   261    281   
   262    282   int
   263    283   _doscan(FILE *fp, const char *fmt, void *argp2)
   264    284   {
   265         -	int ch;
   266         -	int nmatch, len, ch1;
   267         -	int **ptr, fileended, size;
          285  +	int ch, nmatch, len, ch1, **ptr, fileended, size, nread = 0;
   268    286   	void **argp = argp2;
   269    287   
   270    288   	nmatch = 0;
   271    289   	fileended = 0;
   272    290   	for (;;) switch (ch = *fmt++) {
   273    291   	case '\0': 
   274    292   		return (nmatch);
................................................................................
   298    316   		} else if (ch=='[') {
   299    317   			fmt = getccl((unsigned char *)fmt);
   300    318   		}
   301    319   		if (isupper(ch)) {
   302    320   			ch = tolower(ch);
   303    321   			size = LONG;
   304    322   		}
   305         -		if (ch == '\0')
          323  +		if (ch == '\0') {
   306    324   			return(-1);
   307         -		if (innum(ptr, ch, len, size, fp, &fileended) && ptr) {
          325  +		}
          326  +		if (ch == 'n') {
          327  +			if (ptr) {
          328  +				**ptr = nread;
          329  +				nmatch += 1;
          330  +			}
          331  +			break;
          332  +		}
          333  +		if (innum(ptr, ch, len, size, fp, &fileended, &nread) &&
          334  +				ptr) {
   308    335   			nmatch++;
   309    336   		}
   310    337   		if (fileended) {
   311    338   			return(nmatch? nmatch: -1);
   312    339   		}
   313    340   		break;
   314    341   
   315    342   	case ' ':
   316    343   	case '\n':
   317    344   	case '\t': 
   318         -		while ((ch1 = getc(fp))==' ' || ch1=='\t' || ch1=='\n')
   319         -			;
   320         -		if (ch1 != EOF)
          345  +		while ((ch1 = getc(fp)) != EOF) {
          346  +			nread += 1;
          347  +			if ((ch1 != ' ') && (ch1 != '\t') &&
          348  +					(ch1 != '\n')) {
          349  +				break;
          350  +			}
          351  +		}
          352  +		if (ch1 != EOF) {
   321    353   			ungetc(ch1, fp);
          354  +			nread -= 1;
          355  +		}
   322    356   		break;
   323    357   
   324    358   	default: 
   325    359   	def:
   326    360   		ch1 = getc(fp);
          361  +		nread += 1;
   327    362   		if (ch1 != ch) {
   328    363   			if (ch1==EOF)
   329    364   				return(-1);
   330    365   			ungetc(ch1, fp);
   331    366   			return(nmatch);
   332    367   		}
   333    368   	}