diff --git a/spool/spool.c b/spool/spool.c index 0887f74..9c6d205 100644 --- a/spool/spool.c +++ b/spool/spool.c @@ -22,36 +22,11 @@ #define BUF_SIZE 8192 -static void usage(const char *name) +static void spool_file(FILE *file) { - fprintf(stderr, "usage: %s [file]\n", name); - exit(1); -} - - -int main(int argc, const char **argv) -{ - FILE *file; char buf[BUF_SIZE+1]; size_t n; - char *port; - switch (argc) { - case 1: - file = stdin; - break; - case 2: - file = fopen(argv[1], "r"); - if (!file) { - perror(argv[1]); - exit(1); - } - break; - default: - usage(*argv); - } - port = getenv("PORT"); - serial_open(port ? port : DEFAULT_PORT); while (1) { n = fread(buf, 1, BUF_SIZE, file); if (!n) @@ -59,6 +34,51 @@ int main(int argc, const char **argv) buf[n] = 0; serial_printf("%s", buf); } +} + + +static void usage(const char *name) +{ + fprintf(stderr, "usage: %s [file ...]\n", name); + exit(1); +} + + +int main(int argc, const char **argv) +{ + FILE **files; + char *port; + int n_files, i; + + /* + * Open all files before starting to spool, so that we don't risk + * failing in the middle of the job. + */ + n_files = argc == 1 ? 1 : argc-1; + files = malloc(sizeof(FILE *)); + if (!files) { + perror("malloc"); + exit(1); + } + + if (argc == 1) { + *files = stdin; + } else { + if (*argv[1] == '-') + usage(*argv); + for (i = 0; i != n_files; i++) { + files[i] = fopen(argv[i+1], "r"); + if (!files[i]) { + perror(argv[i+1]); + exit(1); + } + } + } + + port = getenv("PORT"); + serial_open(port ? port : DEFAULT_PORT); + for (i = 0; i != n_files; i++) + spool_file(files[i]); sleep(3600); serial_close(); return 0;