termsize.c: Check return values of write() and read() calls
While error and short read or write conditions with so small buffers are hard to impossible to trigger, Checking the values is proper formally so was the warning from the compiler
This commit is contained in:
parent
3d37f6d7f9
commit
2372fc41a8
32
termsize.c
32
termsize.c
|
@ -15,9 +15,9 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
int fd;
|
int fd;
|
||||||
const char * cmd;
|
|
||||||
char buf[256];
|
char buf[256];
|
||||||
|
|
||||||
struct curpos
|
struct curpos
|
||||||
|
@ -26,20 +26,41 @@ struct curpos
|
||||||
int col;
|
int col;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void write_asciiz(const char * str)
|
||||||
|
{
|
||||||
|
ssize_t sret;
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
len = strlen(str);
|
||||||
|
loop:
|
||||||
|
sret = write(fd, str, len);
|
||||||
|
if (sret == -1) abort();
|
||||||
|
str += (size_t)sret;
|
||||||
|
len -= (size_t)sret;
|
||||||
|
if (len > 0) goto loop;
|
||||||
|
}
|
||||||
|
|
||||||
void get_cursor_position(struct curpos * pos)
|
void get_cursor_position(struct curpos * pos)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
char * p;
|
char * p;
|
||||||
|
ssize_t sret;
|
||||||
|
|
||||||
cmd = "\033[6n";
|
write_asciiz("\033[6n");
|
||||||
write(fd, cmd, strlen(cmd));
|
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
loop:
|
loop:
|
||||||
if (i == sizeof(buf)) abort();
|
if (i == sizeof(buf)) abort();
|
||||||
|
|
||||||
read(fd, buf + i, 1);
|
sret = read(fd, buf + i, 1);
|
||||||
|
if (sret == -1)
|
||||||
|
{
|
||||||
|
if (errno == EINTR) goto loop;
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
if (sret == 0) goto loop;
|
||||||
|
if (sret != 1) abort();
|
||||||
|
|
||||||
if (buf[i] == 'R')
|
if (buf[i] == 'R')
|
||||||
{
|
{
|
||||||
buf[i] = 0;
|
buf[i] = 0;
|
||||||
|
@ -61,7 +82,7 @@ loop:
|
||||||
void set_cursor_position(const struct curpos * pos)
|
void set_cursor_position(const struct curpos * pos)
|
||||||
{
|
{
|
||||||
sprintf(buf, "\033[%d;%dH", pos->row, pos->col);
|
sprintf(buf, "\033[%d;%dH", pos->row, pos->col);
|
||||||
write(fd, buf, strlen(buf));
|
write_asciiz(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_terminal_size(struct curpos * size)
|
void get_terminal_size(struct curpos * size)
|
||||||
|
@ -82,7 +103,6 @@ int main(void)
|
||||||
{
|
{
|
||||||
struct termios tty_save;
|
struct termios tty_save;
|
||||||
struct termios tty;
|
struct termios tty;
|
||||||
const char * cmd;
|
|
||||||
struct curpos size;
|
struct curpos size;
|
||||||
struct winsize winsize;
|
struct winsize winsize;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue