#include #include #include #include #include #define TITLE "david's journal" #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 c = getc(f), nl = 0; c != EOF; c = getc(f)) { if (c == '\n') { ++nl; continue; } if (nl > 0) { printf(nl == 1 ? "
" : "

"); nl = 0; } putchar(c); } 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" ""TITLE"\n" "\n" "\n" "

"TITLE"

\n"); for (int i = 0; i < num_entries; ++i) { print_entry(entries[i]); free(entries[i]); } printf( "\n" "\n"); return 0; }