From a88200d28236506be5e38c31872fe8f97a7877ff Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 10 Sep 2018 01:53:50 -0700 Subject: initial import --- journal.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 journal.c diff --git a/journal.c b/journal.c new file mode 100644 index 0000000..7507444 --- /dev/null +++ b/journal.c @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include + +#define ENTRY_DIR "entries" +#define MAX_ENTRIES 1024 + +static char *entries[MAX_ENTRIES]; +static int num_entries; + +static void load_entries() { + DIR *d = opendir(ENTRY_DIR); + if (!d) exit(1); + + struct dirent *de; + while ((de = readdir(d))) { + if (de->d_name[0] == '.') continue; + if (num_entries == MAX_ENTRIES) exit(1); + + size_t entry_size = strlen(de->d_name) + 1; + char *entry = malloc(entry_size); + memcpy(entry, de->d_name, entry_size); + entries[num_entries++] = entry; + } + + closedir(d); +} + +static void print_entry(char *entry) { + size_t len = strlen(entry) + 1; + char *path = malloc(sizeof(ENTRY_DIR) + len); + memcpy(path, ENTRY_DIR"/", sizeof(ENTRY_DIR)); + memcpy(path + sizeof(ENTRY_DIR), entry, len); + FILE *f = fopen(path, "r"); + if (!f) return; + + char date[11]; + long time = atol(entry); + strftime(date, sizeof(date), "%Y/%m/%d", localtime(&time)); + printf("

%s ", date); + + for (int p = getc(f), c = getc(f); p != EOF; p = c, c = getc(f)) { + if (p != '\n') + putchar(p); + else if (c != '\n' && c != EOF) + printf("

"); + } + + fclose(f); + + printf("

\n"); +} + +static int entry_cmp(const void *a, const void *b) { + return atoi(*(const char **)b) - atoi(*(const char **)a); +} + +int main() { + load_entries(); + qsort(entries, num_entries, sizeof(char *), entry_cmp); + + printf( + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "david's journal\n" + "\n" + "\n" + "
\n" + "

david's journal

\n"); + + for (int i = 0; i < num_entries; ++i) { + print_entry(entries[i]); + free(entries[i]); + } + + printf( + "
\n" + "\n" + "\n"); + + return 0; +} -- cgit v1.2.3-70-g09d2