commit 9aa405b8d95764154ce381375a5e4bd14cb25c22 Author: Aaron Gutierrez Date: Wed Nov 7 21:14:29 2018 -0800 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f4dce18 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# binary files +annotate + +# object files +*.o + +# temp stuff +*.swp +*.swo +.DS_STORE diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e9602b0 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +CFLAGS=--std=c99 -O2 -Wall -Wextra -pedantic + +annotate: annotate.o + +clean: + rm -f annotate *.o + +.PHONY: clean diff --git a/annotate.c b/annotate.c new file mode 100644 index 0000000..ec06730 --- /dev/null +++ b/annotate.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include + +#define BUF_SIZE 1024 +#define PRINT_BUF_SIZE 1280 + +void print_now() { + time_t now; + time(&now); + + struct tm *time_parts = localtime(&now); + + printf("[%d-%02d-%02d %02d:%02d:%02d] ", + time_parts->tm_year + 1900, + time_parts->tm_mon + 1, + time_parts->tm_mday, + time_parts->tm_hour, + time_parts->tm_min, + time_parts->tm_sec); +} + +/** + * Program that reads from STDIN and writes to STDOUT with each line annotated + * with the current date and time + */ +int main() { + char buffer[BUF_SIZE]; + + + // when true, print the time on the next iteration + // allows us to avoid printing after the last newline + bool output_time = true; + + ssize_t count_read; + while ((count_read = read(STDIN_FILENO, buffer, BUF_SIZE))) { + for (ssize_t i=0; i < count_read; i++) { + if (output_time && !feof(stdin)) { + print_now(); + output_time = false; + } + + if (buffer[i] == '\n') { + printf("\n"); + fflush(stdout); + output_time = true; + } else { + fputc(buffer[i], stdout); + } + } + } + + if (count_read < 0) { + perror("Error reading from stdin"); + return 1; + } + + fflush(stdout); + + return 0; +}