tex: report failures when reading or writing files - neatvi - [fork] simple vi-… | |
git clone git://src.adamsgaard.dk/neatvi | |
Log | |
Files | |
Refs | |
README | |
--- | |
commit 1c7721d44a7eca5625abb8848294646e9c786f6e | |
parent 849e2276726b60138737f4fbf260ed3e04f5965b | |
Author: Ali Gholami Rudi <[email protected]> | |
Date: Thu, 14 Apr 2016 01:13:16 +0430 | |
ex: report failures when reading or writing files | |
Suggested and tested by Christian Neukirchen <[email protected]>. | |
Diffstat: | |
M ex.c | 19 +++++++++++++++---- | |
M lbuf.c | 24 ++++++++++++++++++------ | |
M vi.h | 4 ++-- | |
3 files changed, 35 insertions(+), 12 deletions(-) | |
--- | |
diff --git a/ex.c b/ex.c | |
t@@ -379,11 +379,14 @@ static int ec_edit(char *ec) | |
bufs_switch(bufs_open(path)); | |
fd = open(ex_path(), O_RDONLY); | |
if (fd >= 0) { | |
- lbuf_rd(xb, fd, 0, lbuf_len(xb)); | |
+ int rd = lbuf_rd(xb, fd, 0, lbuf_len(xb)); | |
close(fd); | |
snprintf(msg, sizeof(msg), "\"%s\" %d lines [r]\n", | |
ex_path(), lbuf_len(xb)); | |
- ex_show(msg); | |
+ if (rd) | |
+ ex_show("read failed\n"); | |
+ else | |
+ ex_show(msg); | |
} | |
lbuf_saved(xb, path[0] != '\0'); | |
bufs[0].mtime = mtime(ex_path()); | |
t@@ -420,7 +423,11 @@ static int ec_read(char *ec) | |
ex_show("read failed\n"); | |
return 1; | |
} | |
- lbuf_rd(xb, fd, pos, pos); | |
+ if (lbuf_rd(xb, fd, pos, pos)) { | |
+ ex_show("read failed\n"); | |
+ close(fd); | |
+ return 1; | |
+ } | |
close(fd); | |
} | |
xrow = end + lbuf_len(xb) - n - 1; | |
t@@ -469,7 +476,11 @@ static int ec_write(char *ec) | |
ex_show("write failed\n"); | |
return 1; | |
} | |
- lbuf_wr(xb, fd, beg, end); | |
+ if (lbuf_wr(xb, fd, beg, end)) { | |
+ ex_show("write failed\n"); | |
+ close(fd); | |
+ return 1; | |
+ } | |
close(fd); | |
} | |
snprintf(msg, sizeof(msg), "\"%s\" %d lines [w]\n", | |
diff --git a/lbuf.c b/lbuf.c | |
t@@ -200,23 +200,35 @@ static void lbuf_opt(struct lbuf *lb, char *buf, int pos… | |
lbuf_savemark(lb, lo, i); | |
} | |
-void lbuf_rd(struct lbuf *lbuf, int fd, int beg, int end) | |
+int lbuf_rd(struct lbuf *lbuf, int fd, int beg, int end) | |
{ | |
char buf[1 << 10]; | |
struct sbuf *sb; | |
- int nr; | |
+ long nr; | |
sb = sbuf_make(); | |
while ((nr = read(fd, buf, sizeof(buf))) > 0) | |
sbuf_mem(sb, buf, nr); | |
- lbuf_edit(lbuf, sbuf_buf(sb), beg, end); | |
+ if (!nr) | |
+ lbuf_edit(lbuf, sbuf_buf(sb), beg, end); | |
sbuf_free(sb); | |
+ return nr != 0; | |
} | |
-void lbuf_wr(struct lbuf *lbuf, int fd, int beg, int end) | |
+int lbuf_wr(struct lbuf *lbuf, int fd, int beg, int end) | |
{ | |
int i; | |
- for (i = beg; i < end; i++) | |
- write(fd, lbuf->ln[i], strlen(lbuf->ln[i])); | |
+ for (i = beg; i < end; i++) { | |
+ char *ln = lbuf->ln[i]; | |
+ long nw = 0; | |
+ long nl = strlen(ln); | |
+ while (nw < nl) { | |
+ long nc = write(fd, ln + nw, nl - nw); | |
+ if (nc < 0) | |
+ return 1; | |
+ nw += nc; | |
+ } | |
+ } | |
+ return 0; | |
} | |
/* replace lines beg through end with buf */ | |
diff --git a/vi.h b/vi.h | |
t@@ -8,8 +8,8 @@ | |
/* line buffer, managing a number of lines */ | |
struct lbuf *lbuf_make(void); | |
void lbuf_free(struct lbuf *lbuf); | |
-void lbuf_rd(struct lbuf *lbuf, int fd, int beg, int end); | |
-void lbuf_wr(struct lbuf *lbuf, int fd, int beg, int end); | |
+int lbuf_rd(struct lbuf *lbuf, int fd, int beg, int end); | |
+int lbuf_wr(struct lbuf *lbuf, int fd, int beg, int end); | |
void lbuf_edit(struct lbuf *lbuf, char *s, int beg, int end); | |
char *lbuf_cp(struct lbuf *lbuf, int beg, int end); | |
char *lbuf_get(struct lbuf *lbuf, int pos); |