aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto E. Vargas Caballero <k0ga@shike2.com>2012-10-28 06:27:42 +0100
committerRoberto E. Vargas Caballero <k0ga@shike2.com>2012-10-28 06:27:42 +0100
commit5de1468554c98ed8a40ac3d1e5d5c7db2eb1480b (patch)
tree0964a1ab765ee2a03ca68558d8aae9c2dd82740e
parentab40392d08babcc02653a5a33352132ea9f1988f (diff)
downloadst-patched-5de1468554c98ed8a40ac3d1e5d5c7db2eb1480b.tar.bz2
st-patched-5de1468554c98ed8a40ac3d1e5d5c7db2eb1480b.tar.xz
st-patched-5de1468554c98ed8a40ac3d1e5d5c7db2eb1480b.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.c39
1 files changed, 30 insertions, 9 deletions
diff --git a/st.c b/st.c
index f967d2c..e19cefd 100644
--- a/st.c
+++ b/st.c
@@ -340,6 +340,7 @@ static int utf8encode(long *, char *);
340static int utf8size(char *); 340static int utf8size(char *);
341static int isfullutf8(char *, int); 341static int isfullutf8(char *, int);
342 342
343static ssize_t xwrite(int, char *, size_t);
343static void *xmalloc(size_t); 344static void *xmalloc(size_t);
344static void *xrealloc(void *, size_t); 345static void *xrealloc(void *, size_t);
345static void *xcalloc(size_t nmemb, size_t size); 346static void *xcalloc(size_t nmemb, size_t size);
@@ -379,6 +380,21 @@ static char *opt_embed = NULL;
379static char *opt_class = NULL; 380static char *opt_class = NULL;
380static char *opt_font = NULL; 381static char *opt_font = NULL;
381 382
383
384ssize_t
385xwrite(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
382void * 398void *
383xmalloc(size_t len) { 399xmalloc(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