diff options
| author | Roberto E. Vargas Caballero <k0ga@shike2.com> | 2012-10-28 06:27:42 +0100 |
|---|---|---|
| committer | Roberto E. Vargas Caballero <k0ga@shike2.com> | 2012-10-28 06:27:42 +0100 |
| commit | ee3fbeb6c8c354cf4db226a5b1583c531ea37af4 (patch) | |
| tree | 0964a1ab765ee2a03ca68558d8aae9c2dd82740e | |
| parent | 2e38ab7afdc56e3853751918f1b7705362bea01c (diff) | |
| download | st-ee3fbeb6c8c354cf4db226a5b1583c531ea37af4.tar.gz st-ee3fbeb6c8c354cf4db226a5b1583c531ea37af4.zip | |
Add error control to iofile
write can write less bytes than we request, so it is necessary check the
return value, in case of error print a message and don't continnue writing
in the file.
---
st.c | 39 ++++++++++++++++++++++++++++++---------
1 file changed, 30 insertions(+), 9 deletions(-)
| -rw-r--r-- | st.c | 39 |
1 files changed, 30 insertions, 9 deletions
| @@ -340,6 +340,7 @@ static int utf8encode(long *, char *); | |||
| 340 | static int utf8size(char *); | 340 | static int utf8size(char *); |
| 341 | static int isfullutf8(char *, int); | 341 | static int isfullutf8(char *, int); |
| 342 | 342 | ||
| 343 | static ssize_t xwrite(int, char *, size_t); | ||
| 343 | static void *xmalloc(size_t); | 344 | static void *xmalloc(size_t); |
| 344 | static void *xrealloc(void *, size_t); | 345 | static void *xrealloc(void *, size_t); |
| 345 | static void *xcalloc(size_t nmemb, size_t size); | 346 | static void *xcalloc(size_t nmemb, size_t size); |
| @@ -379,6 +380,21 @@ static char *opt_embed = NULL; | |||
| 379 | static char *opt_class = NULL; | 380 | static char *opt_class = NULL; |
| 380 | static char *opt_font = NULL; | 381 | static char *opt_font = NULL; |
| 381 | 382 | ||
| 383 | |||
| 384 | ssize_t | ||
| 385 | xwrite(int fd, char *s, size_t len) { | ||
| 386 | size_t aux = len; | ||
| 387 | |||
| 388 | while(len > 0) { | ||
| 389 | ssize_t r = write(fd, s, len); | ||
| 390 | if(r < 0) | ||
| 391 | return r; | ||
| 392 | len -= r; | ||
| 393 | s += r; | ||
| 394 | } | ||
| 395 | return aux; | ||
| 396 | } | ||
| 397 | |||
| 382 | void * | 398 | void * |
| 383 | xmalloc(size_t len) { | 399 | xmalloc(size_t len) { |
| 384 | void *p = malloc(len); | 400 | void *p = malloc(len); |
| @@ -926,13 +942,12 @@ ttynew(void) { | |||
| 926 | cmdfd = m; | 942 | cmdfd = m; |
| 927 | signal(SIGCHLD, sigchld); | 943 | signal(SIGCHLD, sigchld); |
| 928 | if(opt_io) { | 944 | if(opt_io) { |
| 929 | if(!strcmp(opt_io, "-")) { | 945 | iofd = (!strcmp(opt_io, "-")) ? |
| 930 | iofd = STDOUT_FILENO; | 946 | STDOUT_FILENO : |
| 931 | } else { | 947 | open(opt_io, O_WRONLY | O_CREAT, 0666); |
| 932 | if((iofd = open(opt_io, O_WRONLY | O_CREAT, 0666)) < 0) { | 948 | if(iofd < 0) { |
| 933 | fprintf(stderr, "Error opening %s:%s\n", | 949 | fprintf(stderr, "Error opening %s:%s\n", |
| 934 | opt_io, strerror(errno)); | 950 | opt_io, strerror(errno)); |
| 935 | } | ||
| 936 | } | 951 | } |
| 937 | } | 952 | } |
| 938 | } | 953 | } |
| @@ -1793,8 +1808,14 @@ tputc(char *c, int len) { | |||
| 1793 | uchar ascii = *c; | 1808 | uchar ascii = *c; |
| 1794 | bool control = ascii < '\x20' || ascii == 0177; | 1809 | bool control = ascii < '\x20' || ascii == 0177; |
| 1795 | 1810 | ||
| 1796 | if(iofd != -1) | 1811 | if(iofd != -1) { |
| 1797 | write(iofd, c, len); | 1812 | if (xwrite(iofd, c, len) < 0) { |
| 1813 | fprintf(stderr, "Error writting in %s:%s\n", | ||
| 1814 | opt_io, strerror(errno)); | ||
| 1815 | close(iofd); | ||
| 1816 | iofd = -1; | ||
| 1817 | } | ||
| 1818 | } | ||
| 1798 | /* | 1819 | /* |
| 1799 | * STR sequences must be checked before of anything | 1820 | * STR sequences must be checked before of anything |
| 1800 | * because it can use some control codes as part of the sequence | 1821 | * because it can use some control codes as part of the sequence |
