#define _XOPEN_SOURCE #include #include #include #include #include #define TITLE u8"david’s journal" #define ENTRY_DIR "entries" static char **entries; static int num_entries; static void load_entries(char *dir, size_t *max_fn_size) { DIR *d = opendir(dir); if (!d) exit(1); int size = 1024; entries = malloc(size * sizeof(*entries)); struct dirent *de; while ((de = readdir(d))) { if (de->d_name[0] == '.') continue; if (num_entries == size) entries = realloc(entries, (size *= 2) * sizeof(*entries)); size_t fn_size = strlen(de->d_name) + 1; if (fn_size > *max_fn_size) *max_fn_size = fn_size; char *entry = malloc(fn_size); memcpy(entry, de->d_name, fn_size); entries[num_entries++] = entry; } closedir(d); } static void print_entry(char *path, char *ts) { FILE *f = fopen(path, "r"); if (!f) return; struct tm tm; strptime(ts, "%s", &tm); printf( "

%d/%02d/%02d ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); 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); puts("

"); } static int entry_cmp(const void *a, const void *b) { return atoi(*(const char **)b) - atoi(*(const char **)a); } int main() { size_t max_fn_size = 0; load_entries(ENTRY_DIR, &max_fn_size); qsort(entries, num_entries, sizeof(entries[0]), entry_cmp); puts( "\n" "\n" "\n" "\n" "\n" "\n" ""TITLE"\n" "\n" "\n" "

"TITLE"

"); char *path = malloc(sizeof(ENTRY_DIR) + max_fn_size); memcpy(path, ENTRY_DIR"/", sizeof(ENTRY_DIR)); for (int i = 0; i < num_entries; ++i) { strcpy(path + sizeof(ENTRY_DIR), entries[i]); print_entry(path, entries[i]); free(entries[i]); } free(entries); free(path); puts( "\n" ""); return 0; }