diff --git a/zet.dpi.c b/zet.dpi.c index c09ba4f..d0f7cc3 100644 --- a/zet.dpi.c +++ b/zet.dpi.c @@ -94,7 +94,7 @@ static int count_lines_in(const char *str) { static int count_lines(int fd) { int count = 1; - do { + if (fd >= 0) do { unsigned char buf[4096]; size_t len = sizeof(buf)-1; int rc = read_some(fd, buf, &len); @@ -189,7 +189,7 @@ static int write_topbar(int fd, enum page page, const char *id, const char *titl static int passthrough_note_html(int fd, int note_fd) { int rc; - do { + if (note_fd >= 0) do { unsigned char buf[4096]; size_t len = sizeof(buf)-1; rc = read_some(note_fd, buf, &len); @@ -219,6 +219,7 @@ static int dpi_respond_err(int fd, const char *fmt, ...) { } static char *zet_get_title_fd(int fd, char *buf, size_t len) { + if (fd < 0) return NULL; int rc = read_some(fd, (unsigned char *)buf, &len); if (rc < 0) return NULL; buf[len] = '\0'; @@ -745,7 +746,7 @@ md_rndr_autolink(struct buf *ob, const struct buf *link, enum mkd_autolink type, } static int dpi_serve_zet(int fd, char *path) { - int rc; + int rc = 0; char *hash, *query; parse_req_uri(path, &hash, &query); @@ -761,21 +762,20 @@ static int dpi_serve_zet(int fd, char *path) { if (sz < 0 || (size_t)sz >= sizeof(path_buf)) return dpi_respond_err(fd, "read_file"); int note_fd = open(path_buf, O_RDONLY); - if (note_fd < 0 && errno == ENOENT) return dpi_serve_not_found(fd); - if (note_fd < 0) return dpi_respond_err(fd, "open"); + if (note_fd < 0 && errno != ENOENT) return dpi_respond_err(fd, "open"); int rows = count_lines(note_fd); if (rows < 0) return dpi_respond_err(fd, "count_lines"); static const int max_rows = 57, min_rows = 8; if (rows > max_rows) rows = max_rows; else if (rows < min_rows) rows = min_rows; - rc = lseek(note_fd, 0, SEEK_SET); + if (note_fd >= 0) rc = lseek(note_fd, 0, SEEK_SET); if (rc < 0) return dpi_respond_err(fd, "lseek"); char buf[128]; char *title = zet_get_title_fd(note_fd, buf, sizeof(buf)); if (title == NULL) title = id; - rc = lseek(note_fd, 0, SEEK_SET); + if (note_fd >= 0) rc = lseek(note_fd, 0, SEEK_SET); if (rc < 0) return dpi_respond_err(fd, "lseek"); rc = dpi_send_header(fd, "text/html"); @@ -803,13 +803,13 @@ static int dpi_serve_zet(int fd, char *path) { "" ); if (rc < 0) { warn("dpi_send_header"); close(fd); return 0; } - rc = lseek(note_fd, 0, SEEK_SET); + if (note_fd >= 0) rc = lseek(note_fd, 0, SEEK_SET); if (rc < 0) { rc = dprintf(fd, "\nError: %s", strerror(errno)); if (rc < 0) warnx("dprintf"); close(fd); } - char *text = read_full(note_fd); + char *text = note_fd >= 0 ? read_full(note_fd) : NULL; if (text == NULL) { rc = dprintf(fd, "\nError: %s", strerror(errno)); if (rc < 0) warnx("dprintf");