From 78dbcb3481a881bc7bb70df2976ffeb447541597 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Tue, 7 May 2024 20:07:53 -0400 Subject: Add pipe/redirect support Signed-off-by: Ian Moffett --- src/mex.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src/mex.c') diff --git a/src/mex.c b/src/mex.c index 8741e30..9514bb4 100644 --- a/src/mex.c +++ b/src/mex.c @@ -90,13 +90,28 @@ int main(int argc, char **argv) { FILE *fp; + int is_tty; - if (argc < 2 || argc > 2) { + is_tty = isatty(STDIN_FILENO); + + /* + * If this is a TTY and we don't have enough args + * give back an error. + */ + if ((argc < 2 || argc > 2) && is_tty) { printf("Usage: %s \n", argv[0]); return 1; } - fp = fopen(argv[1], "rb"); + /* + * If this is a TTY, open the filename passed + * to us, otherwise just use stdin. + */ + if (is_tty) { + fp = fopen(argv[1], "rb"); + } else { + fp = stdin; + } if (fp == NULL) { perror("fopen"); @@ -104,7 +119,8 @@ main(int argc, char **argv) } dump_file(fp); - fclose(fp); + if (fp != stdin) + fclose(fp); return 0; } -- cgit v1.2.3