diff --git a/README.md b/README.md index fe81fd0..370cae1 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ $ echo "Hello world" | tcat ``` ## Usage -The following is the ouput from the `ping` command: +The following is the ouput from the `ping` command: ``` $ ping -c 3 github.com PING github.com (192.30.252.131): 56 data bytes @@ -38,7 +38,24 @@ $ ping -c 3 github.com | tcat 2014-02-09T20:58:44+0000 round-trip min/avg/max/stddev = 106.783/110.892/116.658/4.198 ms ``` -Time Cat prepends each line of input with a timestamp followed by a single tab character. This will be the first whitespace character on the live. This means that it is very easy to remove the timestamps again. You can use `cut -f 2`, for example. +The format that the time is printed in can be overiden by the user in one of two ways. You can specify the format using the `-f` or `--format` command-line options. You can also set the format by setting the `TCAT_FORMAT` environmental variable. The format should be specified using the `strftime(3)` syntax. If the format is overridden using the command-line options, this will take precence over the environment variable. + +Here are three examples showing the default usage, format passed as a command-line option, and through the environment variable. + +``` +$ echo default | tcat +2014-03-08T13:15:43+0000 default + +$ echo command-line | tcat --format '%H:%M:%S' +13:17:03 command-line + +$ echo environment | TCAT_FORMAT='%Y/%m/%d' tcat +2014/03/08 environment +``` + +## Advanced Usage +Time Cat prepends each line of input with a timestamp followed by a single tab character. This will be the first whitespace character on the line. This means that it is very easy to remove the timestamps again. You can use `cut -f 2`, for example. + ``` $ echo foo foo @@ -49,7 +66,7 @@ foo ``` ## Building -Time Cat should build easily on any Unix. It has been tested on Ubuntu and OSX using gcc and clang. +Time Cat should build easily on any Unix. It has been tested on Ubuntu and OSX using gcc and clang. To build Time Cat just navigate to where you have cloned the respository and run `make`. diff --git a/tcat.c b/tcat.c index fe742ed..c57e8cb 100644 --- a/tcat.c +++ b/tcat.c @@ -10,9 +10,10 @@ enum { version_patch = 2 }; -static const char program_name[] = "tcat"; -static const char env_flag[] = "TCAT_FORMAT"; -static const char *format = "%FT%T%z\t"; +static const char program_name[] = "tcat"; +static const char env_flag[] = "TCAT_FORMAT"; +static const char default_format[] = "%FT%T%z"; +static const char* format = default_format; static void io_error(FILE* file) { if (feof(file)) { @@ -36,6 +37,9 @@ static void print_time() { if(len != fwrite(buffer, 1, len, stdout)){ io_error(stdout); } + if (fputc('\t', stdout) == EOF) { + io_error(stdout); + } } static void version(FILE* output) { @@ -52,9 +56,10 @@ static inline void usage(FILE* output) { "Available Options:\n" "\t-v, --version\tprint %s version\n" "\t-h, --help\tprint this help\n" + "\t-f, --format\tset time format string in strftime syntax (default: \"%s\")\n" "\n" "Help can be found at https://github.com/marcomorain/tcat\n", - program_name); + program_name, default_format); } static int match(const char* x, const char* a, const char* b) { @@ -65,6 +70,11 @@ int main(int argc, char** argv) { int bad_usage = 0; + char *format_env = getenv(env_flag); + if (format_env) { + format = format_env; + } + for (int i = 1; i < argc; i++) { if (match(argv[i], "-v", "--version")) { version(stdout); @@ -76,6 +86,15 @@ int main(int argc, char** argv) { return EXIT_SUCCESS; } + if (match(argv[i], "-f", "--format")) { + if (++i >= argc) { + fprintf(stderr, "Error: no format string given provided for %s option\n", argv[i-1]); + return EXIT_FAILURE; + } + format = argv[i]; + continue; + } + // Show all bad options bad_usage = 1; fprintf(stderr, "Invalid option:\t%s\n", argv[i]); @@ -91,11 +110,6 @@ int main(int argc, char** argv) { fprintf(stderr, "Warning: input is from TTY\n"); } - char *format_env = getenv(env_flag); - if (format_env) { - format = format_env; - } - int last = '\n'; for(int c = fgetc(stdin); c != EOF; c = fgetc(stdin)) { if (last == '\n') {