shithub: riscv

Download patch

ref: e0278f69176765b408be4f29e6f3e5d39b928601
parent: e9cd41467e5598d5c61bd04bf52d96ccd75472ca
author: Ori Bernstein <[email protected]>
date: Tue Sep 1 15:03:17 EDT 2020

seconds: tolerate trailing whitespace in dates

This allows handling dates with leading and trailing whitespace,
including newlines.

--- a/sys/src/cmd/seconds.c
+++ b/sys/src/cmd/seconds.c
@@ -1,5 +1,6 @@
 #include <u.h>
 #include <libc.h>
+#include <ctype.h>
 
 char *knownfmt[] = {
 	/* asctime */
@@ -57,6 +58,16 @@
 	nil,
 };
 
+static int
+nojunk(char *p)
+{
+	while(isspace(*p))
+		p++;
+	if(*p == '\0')
+		return 1;
+	werrstr("trailing junk");
+	return 0;
+}
 
 static void
 usage(void)
@@ -89,24 +100,21 @@
 		sysfatal("bad local time: %r");
 	for(i = 0; i < argc; i++){
 		if(fmt != nil){
-			if(tmparse(&tm, fmt, argv[i], tz, &ep) != nil && *ep == 0)
+			if(tmparse(&tm, fmt, argv[i], tz, &ep) && nojunk(ep))
 				goto Found;
 		}else{
 			for(f = knownfmt; *f != nil; f++)
-				if(tmparse(&tm, *f, argv[i], tz, &ep) != nil && *ep == 0)
+				if(tmparse(&tm, *f, argv[i], tz, &ep) != nil && nojunk(ep))
 					goto Found;
 			for(df = datefmt; *df; df++)
 			for(tf = timefmt; *tf; tf++)
 			for(zf = zonefmt; *zf; zf++){
 				snprint(buf, sizeof(buf), "%s%s%s", *df, *tf, *zf);
-				if(tmparse(&tm, buf, argv[i], tz, &ep) != nil && *ep == 0)
+				if(tmparse(&tm, buf, argv[i], tz, &ep) != nil && nojunk(ep))
 					goto Found;
 			}
 		}
-		if(*ep == 0)
-			sysfatal("tmparse: %r");
-		else
-			sysfatal("tmparse: trailing junk");
+		sysfatal("tmparse: %r");
 Found:
 		print("%lld\n", tmnorm(&tm));
 	}