shithub: riscv

Download patch

ref: 73f38fc5460cb68662dd237022bda636ad734045
parent: 27fc79b04bee837f513f8ac92c3e50ae76c27abe
author: Ori Bernstein <ori@eigenstate.org>
date: Tue May 12 06:48:33 EDT 2020

[ape] add missing conversion flags for scanf

We're missing type flags for:

	hh: char
	ll: vlong
	z:  size_t
	t:  ptrdiff_t
	j:  intmax_t

The lack of '%lld' was causing us to fail when parsing
timezone files. This brings us in line with the specifiers
in the C99 standard, section 7.19.6.2p11

--- a/sys/include/ape/inttypes.h
+++ b/sys/include/ape/inttypes.h
@@ -27,4 +27,6 @@
 #define PRIu32 "u"
 #define PRIu64 "llu"
 
+extern intmax_t strtoimax(const char *, char **, int);
+
 #endif
--- a/sys/src/ape/lib/ap/gen/strtoll.c
+++ b/sys/src/ape/lib/ap/gen/strtoll.c
@@ -1,4 +1,5 @@
 #include <stdlib.h>
+#include <stdint.h>
 #include <limits.h>
 #include <errno.h>
 
@@ -100,4 +101,10 @@
 	if(neg)
 		return -n;
 	return n;
+}
+
+intmax_t
+strtoimax(char *nptr, char **endptr, int base)
+{
+	return strtoll(nptr, endptr, base);
 }
--- a/sys/src/ape/lib/ap/stdio/vfscanf.c
+++ b/sys/src/ape/lib/ap/stdio/vfscanf.c
@@ -96,7 +96,16 @@
 		}
 		else
 			width=-1;
-		type=*fmtp=='h' || *fmtp=='l' || *fmtp=='L'?*fmtp++:'n';
+		type = 'n';
+		if(*fmtp=='h' || *fmtp=='l' || *fmtp=='L' || *fmtp=='j' || *fmtp=='z' || *fmtp=='t')
+			type = *fmtp++;
+		if(type == 'l' && *fmtp == 'l'){
+			type = 'V';
+			fmtp++;
+		}else if(type == 'h' && *fmtp == 'h'){
+			type = 'H';
+			fmtp++;
+		}
 		if(!icvt[*fmtp]) goto NonSpecial;
 		if(!(*icvt[*fmtp])(f, &args, store, width, type))
 			return ncvt?ncvt:EOF;
@@ -137,7 +146,7 @@
 static int
 icvt_fixed(FILE *f, va_list *args,
 				int store, int width, int type, int unsgned, int base){
-	unsigned long int num=0;
+	unsigned long long num=0;
 	int sign=1, ndig=0, dig;
 	int c;
 	do
@@ -194,18 +203,28 @@
 		switch(unsgned){
 		case SIGNED:
 			switch(type){
-			case 'h': *va_arg(*args,  short *)=num*sign; break;
-			case 'n': *va_arg(*args,  int *)=num*sign; break;
+			case 'H': *va_arg(*args, char *)=num*sign; break;
+			case 'h': *va_arg(*args, short *)=num*sign; break;
+			case 'n': *va_arg(*args, int *)=num*sign; break;
 			case 'l':
-			case 'L': *va_arg(*args,  long *)=num*sign; break;
+			case 'L': *va_arg(*args, long *)=num*sign; break;
+			case 'j':
+			case 'V': *va_arg(*args, long long*)=num*sign; break;
+			case 'z': *va_arg(*args, ssize_t*)=num*sign; break;
+			case 't': *va_arg(*args, ptrdiff_t*)=num*sign; break;
 			}
 			break;
 		case UNSIGNED:
 			switch(type){
+			case 'H': *va_arg(*args, unsigned char *)=num*sign; break;
 			case 'h': *va_arg(*args, unsigned short *)=num*sign; break;
 			case 'n': *va_arg(*args, unsigned int *)=num*sign; break;
 			case 'l':
 			case 'L': *va_arg(*args, unsigned long *)=num*sign; break;
+			case 'j':
+			case 'V': *va_arg(*args, unsigned long long *)=num*sign; break;
+			case 'z': *va_arg(*args, size_t*)=num*sign; break;
+			case 't': *va_arg(*args, ptrdiff_t*)=num*sign; break;
 			}
 			break;
 		case POINTER: