From 0d4ac83e644c13cdba4349c6a8b3d3ade5ff6ec7 Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Sat, 14 Nov 2015 16:19:36 +0100 Subject: Fill up argument list with implicit stdin --- arr.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/arr.c b/arr.c index 755c54e..1ab96cd 100644 --- a/arr.c +++ b/arr.c @@ -222,17 +222,23 @@ main(int argc, char *argv[]) { /* fmt(">>%{3 1*1:4}<<\n", a); */ /* fmt("%zz%%zz\n", a); */ - // default to stdin when no file arguments are given - if (argc == 2) { - argv[argc] = "-"; - argc++; + // number of implicit stdin arguments + int stdins = 0; + + if (argc < 2) { + fprintf(stderr, "Usage: %s FMT FILES...\n", argv[0]); + exit(1); } - FILE **files = calloc(argc, sizeof (FILE *)); - char **lines = calloc(argc, sizeof (char *)); + // default to stdin when no file arguments are given + if (argc == 2) + stdins++; + + FILE **files = calloc(argc+stdins, sizeof (FILE *)); + char **lines = calloc(argc+stdins, sizeof (char *)); int i; - for (i = 1; i < argc-1; i++) - if (strcmp(argv[i+1], "-") == 0) + for (i = 1; i < argc-1+stdins; i++) + if (i >= argc-1 || strcmp(argv[i+1], "-") == 0) files[i] = stdin; else files[i] = fopen(argv[i], "r"); // XXX error handling @@ -242,7 +248,7 @@ main(int argc, char *argv[]) { int delim = '\n'; while (1) { eof = 0; - for (i = 1; i < argc-1; i++) { + for (i = 1; i < argc-1+stdins; i++) { int read = getdelim(lines+i, &len, delim, files[i]); // XXX error handling if (lines[i][read-1] == delim) // strip delimiter -- cgit 1.4.1