aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Moffett <industrial.reformer@gmail.com>2024-05-07 20:07:53 -0400
committerIan Moffett <industrial.reformer@gmail.com>2024-05-07 20:07:53 -0400
commit78dbcb3481a881bc7bb70df2976ffeb447541597 (patch)
treef1d418a2cc8ad9174a499dd374be9a500c3a5d40
parentccb20831680dedbbbfc4d8fe2d669224d82e9a9e (diff)
Add pipe/redirect support
Signed-off-by: Ian Moffett <industrial.reformer@gmail.com>
-rw-r--r--src/mex.c22
1 files changed, 19 insertions, 3 deletions
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 <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;
}