commit fee669cbaa9c842d18ed53838ea84b05335e40bc
parent bdbc1fb6cd9b3396216dce195cb939aeecb0a29e
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sat, 25 Oct 2014 10:22:27 +0000
use ssize_t for read() and write(), style
Diffstat:
M | sob.c | | | 151 | +++++++++++++++++++++++++++++++++++++++---------------------------------------- |
1 file changed, 75 insertions(+), 76 deletions(-)
diff --git a/sob.c b/sob.c
@@ -30,70 +30,70 @@ struct line {
size_t collen; /* total length (in columns) */
};
-static void cb_pipe_insert(const char *, size_t, size_t);
-static void cb_pipe_replaceword(const char *, size_t, size_t);
-
-static void line_clear(void);
-static void line_copywordcursor(char *, size_t);
-static void line_cursor_begin(void);
-static void line_cursor_end(void);
-static void line_cursor_move(size_t);
-static void line_cursor_next(void);
-static void line_cursor_prev(void);
-static void line_cursor_wordprev(void);
-static void line_cursor_wordnext(void);
-static void line_delcharprev(void);
-static void line_delcharnext(void);
-static void line_deltoend(void);
-static void line_delwordprev(void);
-static void line_delwordcursor(void);
-static void line_draw(void);
-static void line_exit(void);
-static void line_getwordpos(size_t, size_t, size_t *, size_t *, size_t *,
- size_t *);
-static void line_getwordposprev(size_t, size_t, size_t *, size_t *);
-static void line_getwordposnext(size_t, size_t, size_t *, size_t *);
-static void line_inserttext(const char *);
-static void line_newline(void);
-static void line_out(void);
-static void line_prompt(void);
-static int line_promptlen(void);
-static int line_pipeto(char **);
-static int line_wordpipeto(char **);
-
-static int pipe_read(int, int, char *,
+static void cb_pipe_insert(const char *, size_t, size_t);
+static void cb_pipe_replaceword(const char *, size_t, size_t);
+
+static void line_clear(void);
+static void line_copywordcursor(char *, size_t);
+static void line_cursor_begin(void);
+static void line_cursor_end(void);
+static void line_cursor_move(size_t);
+static void line_cursor_next(void);
+static void line_cursor_prev(void);
+static void line_cursor_wordprev(void);
+static void line_cursor_wordnext(void);
+static void line_delcharprev(void);
+static void line_delcharnext(void);
+static void line_deltoend(void);
+static void line_delwordprev(void);
+static void line_delwordcursor(void);
+static void line_draw(void);
+static void line_exit(void);
+static void line_getwordpos(size_t, size_t, size_t *, size_t *, size_t *,
+ size_t *);
+static void line_getwordposprev(size_t, size_t, size_t *, size_t *);
+static void line_getwordposnext(size_t, size_t, size_t *, size_t *);
+static void line_inserttext(const char *);
+static void line_newline(void);
+static void line_out(void);
+static void line_prompt(void);
+static int line_promptlen(void);
+static int line_pipeto(char **);
+static int line_wordpipeto(char **);
+
+static int pipe_read(int, int, char *,
+ void (*)(const char *, size_t, size_t));
+static int pipe_cmd(char *[], char *,
void (*)(const char *, size_t, size_t));
-static int pipe_cmd(char *[], char *,
- void (*)(const char *, size_t, size_t));
-
-static void clear(void);
-static void gettermsize(void);
-static void handleinput(const unsigned char *, size_t);
-static void initialinput(void);
-static void resize(void);
-static int run(void);
-static void setup(void);
-static void sighandler(int);
-static void usage(void);
-
-static int readfd(int, char *, size_t);
-static int writefd(int, char *, size_t);
-
-static size_t colw(const char *, size_t);
-static int nonspace(int c);
-static size_t utf8len(const char *);
-static size_t utfprevn(const char *, size_t, size_t);
-static size_t utfnextn(const char *, size_t, size_t);
-static void utfuntilchar(size_t *, size_t *, int (*)(int), int);
-
-static struct termios ttystate, ttysave;
-
-static struct line line;
-static size_t dirtylen; /* dirty length (in columns) */
-static int cols, rows;
-static int isrunning = 1;
-static FILE * outfp = NULL;
-static FILE * lineoutfp = NULL;
+
+static void clear(void);
+static void gettermsize(void);
+static void handleinput(const unsigned char *, size_t);
+static void initialinput(void);
+static void resize(void);
+static int run(void);
+static void setup(void);
+static void sighandler(int);
+static void usage(void);
+
+static ssize_t readfd(int, char *, size_t);
+static ssize_t writefd(int, char *, size_t);
+
+static size_t colw(const char *, size_t);
+static int nonspace(int c);
+static size_t utf8len(const char *);
+static size_t utfprevn(const char *, size_t, size_t);
+static size_t utfnextn(const char *, size_t, size_t);
+static void utfuntilchar(size_t *, size_t *, int (*)(int), int);
+
+static struct termios ttystate, ttysave;
+
+static struct line line;
+static size_t dirtylen; /* dirty length (in columns) */
+static int cols, rows;
+static int isrunning = 1;
+static FILE * outfp = NULL;
+static FILE * lineoutfp = NULL;
#include "config.h"
@@ -548,10 +548,9 @@ line_copywordcursor(char *buf, size_t bufsiz)
buf[len] = '\0';
}
-static int
+static ssize_t
readfd(int fd, char *buf, size_t len) {
- size_t i = 0;
- ssize_t r;
+ ssize_t r, i = 0;
while(len > 0) {
if((r = read(fd, &buf[i], len)) == -1) {
@@ -563,16 +562,15 @@ readfd(int fd, char *buf, size_t len) {
} else if(r == 0) {
return i;
}
- i += (size_t)r;
- len -= (size_t)r;
+ i += r;
+ len -= r;
}
return i;
}
-static int
+static ssize_t
writefd(int fd, char *buf, size_t len) {
- size_t i = 0;
- ssize_t w;
+ ssize_t w, i = 0;
while(len > 0) {
if((w = write(fd, &buf[i], len)) == -1) {
@@ -584,8 +582,8 @@ writefd(int fd, char *buf, size_t len) {
} else if(w == 0) {
return i;
}
- i += (size_t)w;
- len -= (size_t)w;
+ i += w;
+ len -= w;
}
return i;
}
@@ -598,7 +596,8 @@ pipe_read(int fd_in, int fd_out, char *writestr,
struct timeval tv;
fd_set fdr, fdw;
size_t total = 0;
- int r, w, maxfd, status = -1, haswritten = 0;
+ ssize_t r;
+ int maxfd, status = -1, haswritten = 0;
for(;;) {
FD_ZERO(&fdr);
@@ -630,13 +629,13 @@ pipe_read(int fd_in, int fd_out, char *writestr,
}
if(f) {
buf[r] = '\0';
- total += r;
+ total += (size_t)r;
f(buf, r, total);
}
}
}
if(FD_ISSET(fd_out, &fdw)) {
- if((w = writefd(fd_out, writestr, strlen(writestr))) == -1)
+ if(writefd(fd_out, writestr, strlen(writestr)) == -1)
goto fini;
close(fd_out); /* sends EOF */
fd_out = -1;