summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-08-06 08:12:55 -0400
committerJakob Kaivo <jkk@ung.org>2019-08-06 08:12:55 -0400
commit41ae16c72828912c8f7710f9b0b961e754805d46 (patch)
treeee8c90131b38ea53e743042ff08ce6517f9f0701
parent8324cbe227d932ca76f878bbd943d7b552571b31 (diff)
handle obsolete syntax
-rw-r--r--head.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/head.c b/head.c
index 58ee02a..1069ab0 100644
--- a/head.c
+++ b/head.c
@@ -23,7 +23,9 @@
*/
#define _POSIX_C_SOURCE 2
+#include <ctype.h>
#include <errno.h>
+#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@@ -56,13 +58,40 @@ static int head(const char *path, int nlines)
return 0;
}
+static void fixobsolete(int argc, char *argv[])
+{
+ for (int i = 1; i < argc; i++) {
+ if (argv[i][0] != '-') {
+ return;
+ }
+
+ if (!strcmp(argv[i], "--")) {
+ return;
+ }
+
+ if (isdigit(argv[i][1])) {
+ fprintf(stderr, "head: '-#' is obsolete; use '-n #'\n");
+ char *opt = malloc(strlen(argv[i]) + 2);
+ if (opt == NULL) {
+ perror("strings");
+ exit(1);
+ }
+ sprintf(opt, "-n %s", argv[i] + 1);
+ argv[i] = opt;
+ }
+ }
+}
+
int main(int argc, char **argv)
{
+ setlocale(LC_ALL, "");
+
int lines = 10;
int showname = 0;
char *end;
int c;
+ fixobsolete(argc, argv);
while ((c = getopt(argc, argv, "n:")) != -1) {
switch (c) {
case 'n':