diff --git a/zet.dpi.c b/zet.dpi.c index 86a1bc7..fd139b5 100644 --- a/zet.dpi.c +++ b/zet.dpi.c @@ -121,7 +121,7 @@ static int write_html(int fd, const char *buf, size_t len) { return 0; } -static int write_topbar(int fd, enum page page, const char *str) { +static int write_topbar(int fd, enum page page, const char *id, const char *title) { int rc = write_buf(fd, "
"); @@ -180,24 +183,29 @@ static int dpi_respond_err(int fd, const char *fmt, ...) { return rc; } -static char *zet_get_title(const char *id, char *buf, size_t len) { - char path_buf[_POSIX_PATH_MAX]; - ssize_t sz = snprintf(path_buf, sizeof(path_buf), "%s/%s", zet_dir, id); - if (sz < 0 || (size_t)sz >= sizeof(path_buf)) return NULL; - int fd = open(path_buf, O_RDONLY); - if (fd < 0) return NULL; +static char *zet_get_title_fd(int fd, char *buf, size_t len) { int rc = read_some(fd, (unsigned char *)buf, &len); - if (rc < 0) { close(fd); return NULL; } + if (rc < 0) return NULL; buf[len] = '\0'; char *end = strchr(buf, '\n'); if (end != NULL) *end = '\0'; - close(fd); while (*buf == '#') buf++; while (*buf == ' ') buf++; - if (!*buf) { + return buf; +} + +static char *zet_get_title(const char *id, char *buf, size_t len) { + char path_buf[_POSIX_PATH_MAX]; + ssize_t sz = snprintf(path_buf, sizeof(path_buf), "%s/%s", zet_dir, id); + if (sz < 0 || (size_t)sz >= sizeof(path_buf)) return NULL; + int fd = open(path_buf, O_RDONLY); + if (fd < 0) return NULL; + buf = zet_get_title_fd(fd, buf, len); + if (buf != NULL && *buf == '\0') { strncpy(buf, "§", sigil_size); strncpy(buf + sigil_size, id, len); } + close(fd); return buf; } @@ -325,7 +333,7 @@ static int dpi_serve_zet_index(int fd) { "" "" ""); - rc |= write_topbar(fd, PAGE_ALL, NULL); + rc |= write_topbar(fd, PAGE_ALL, NULL, NULL); rc |= write_buf(fd, "