From 41ae16c72828912c8f7710f9b0b961e754805d46 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Tue, 6 Aug 2019 08:12:55 -0400 Subject: handle obsolete syntax --- head.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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 #include +#include #include #include #include @@ -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': -- cgit v1.2.1