3 * Like cat, with datestamps
14 #include <mLib/mdwopt.h>
15 #include <mLib/quis.h>
16 #include <mLib/report.h>
18 static const char *fmt = "%Y-%m-%d %H:%M:%S %Z: ";
19 static struct tm *(*cvt)(const time_t *) = localtime;
21 static void version(void) { pquis(stdout, "$ " VERSION); }
22 static void usage(FILE *fp)
23 { pquis(fp, "Usage: $ [-z] [-f FORMAT] [FILE...]"); }
25 static void help(void)
27 version(); putchar('\n');
30 Copy the FILEs (or standard input) to standard output, prefixing each line\n\
33 -h, --help Show this help text.\n\
34 -v, --version Show the program's version number.\n\
35 -u, --usage Show a brief usage message.\n\
37 -f, --format=FORMAT Write datestamps using the strftime(3) FORMAT.\n\
38 -z, --utc, --zulu Use UTC rather than local time for datestamps.\n\
42 static void cat(FILE *in)
51 if ((ch = getc(in)) == EOF)
56 strftime(buf, sizeof(buf), fmt, tm);
57 fwrite(buf, 1, strlen(buf), stdout);
66 int main(int argc, char *argv[])
74 setvbuf(stdin, 0, _IOLBF, 0);
77 static const struct option opt[] = {
78 { "help", 0, 0, 'h' },
79 { "version", 0, 0, 'v' },
80 { "usage", 0, 0, 'u' },
81 { "format", OPTF_ARGREQ, 0, 'f' },
83 { "zulu", 0, 0, 'z' },
87 if ((i = mdwopt(argc, argv, "hvuf:z", opt, 0, 0, 0)) < 0)
116 if (optind == argc) {
117 if (isatty(STDIN_FILENO))
118 die(EXIT_FAILURE, "no arguments, and stdin is a terminal");
120 } else for (i = optind; i < argc; i++) {
121 if (strcmp(argv[i], "-") == 0)
123 else if ((fp = fopen(argv[i], "r")) == 0) {
124 moan("failed to open `%s': %s", argv[i], strerror(errno));
132 if (ferror(stdout) || fflush(stdout) || fclose(stdout)) {
133 moan("error writing output: %s", strerror(errno));
137 return ((f & F_BOGUS) ? EXIT_FAILURE : 0);