diff options
author | Ian Moffett <industrial.reformer@gmail.com> | 2024-05-07 20:07:53 -0400 |
---|---|---|
committer | Ian Moffett <industrial.reformer@gmail.com> | 2024-05-07 20:07:53 -0400 |
commit | 78dbcb3481a881bc7bb70df2976ffeb447541597 (patch) | |
tree | f1d418a2cc8ad9174a499dd374be9a500c3a5d40 | |
parent | ccb20831680dedbbbfc4d8fe2d669224d82e9a9e (diff) |
Add pipe/redirect support
Signed-off-by: Ian Moffett <industrial.reformer@gmail.com>
-rw-r--r-- | src/mex.c | 22 |
1 files changed, 19 insertions, 3 deletions
@@ -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 <filename>\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; } |