[PATCH] Add ARGNUM and ARGNUMF(base)

From: dsp <dsp_AT_2f30.org>
Date: Mon, 11 Nov 2013 19:53:01 +0000

This is useful to support the obsolete syntax -NUM for tools like
head, tail and fold.

The existing tools bomb out on estrtol() in the same way
for both -n NUM and -NUM.
---
 arg.h  | 15 ++++++++++++++-
 fold.c |  4 +++-
 head.c |  4 +++-
 tail.c |  3 +++
 4 files changed, 23 insertions(+), 3 deletions(-)
diff --git a/arg.h b/arg.h
index 2009b70..becf91b 100644
--- a/arg.h
+++ b/arg.h
_AT_@ -29,11 +29,25 @@ extern char *argv0;
 					_argc = argv[0][0];\
 					switch (_argc)
 
+/* Handles obsolete -NUM syntax */
+#define ARGNUM				case '0':\
+					case '1':\
+					case '2':\
+					case '3':\
+					case '4':\
+					case '5':\
+					case '6':\
+					case '7':\
+					case '8':\
+					case '9'
+
 #define ARGEND			}\
 			}
 
 #define ARGC()		_argc
 
+#define ARGNUMF(base)	(estrtol(argv[0], (base)))
+
 #define EARGF(x)	((argv[0][1] == '\0' && argv[1] == NULL)?\
 				((x), abort(), (char *)0) :\
 				(brk = 1, (argv[0][1] != '\0')?\
_AT_@ -47,4 +61,3 @@ extern char *argv0;
 					(argc--, argv++, argv[0])))
 
 #endif
-
diff --git a/fold.c b/fold.c
index bbbb28a..d327c62 100644
--- a/fold.c
+++ b/fold.c
_AT_@ -35,6 +35,9 @@ main(int argc, char *argv[])
 	case 'w':
 		width = estrtol(EARGF(usage()), 0);
 		break;
+	ARGNUM:
+		width = ARGNUMF(0);
+		break;
 	default:
 		usage();
 	} ARGEND;
_AT_@ -102,4 +105,3 @@ foldline(const char *str, long width)
 			putchar('\n');
 	} while(str[i = n] && str[i] != '\n');
 }
-
diff --git a/head.c b/head.c
index 0cf8680..8011151 100644
--- a/head.c
+++ b/head.c
_AT_@ -23,6 +23,9 @@ main(int argc, char *argv[])
 	case 'n':
 		n = estrtol(EARGF(usage()), 0);
 		break;
+	ARGNUM:
+		n = ARGNUMF(0);
+		break;
 	default:
 		usage();
 	} ARGEND;
_AT_@ -53,4 +56,3 @@ head(FILE *fp, const char *str, long n)
 	if(ferror(fp))
 		eprintf("%s: read error:", str);
 }
-
diff --git a/tail.c b/tail.c
index da222b6..9c241db 100644
--- a/tail.c
+++ b/tail.c
_AT_@ -30,6 +30,9 @@ main(int argc, char *argv[])
 		if(lines[0] == '+')
 			tail = dropinit;
 		break;
+	ARGNUM:
+		n = ARGNUMF(0);
+		break;
 	default:
 		usage();
 	} ARGEND;
-- 
1.8.3.4
--G4iJoqBmSsgzjUCe--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Mon Nov 11 2013 - 21:24:07 CET