diff options
author | pgen <p.gen.progs@gmail.com> | 2021-01-23 22:42:13 +0100 |
---|---|---|
committer | pgen <p.gen.progs@gmail.com> | 2021-01-23 22:42:13 +0100 |
commit | 229be48b14c66775ebebd16f2816be879c0e9252 (patch) | |
tree | b8c086be27b38bd35196e94899e130a27dd6de26 | |
parent | f6637bea0c1cb3b773aa1e00b89dee548e2ed2cd (diff) |
Improve the function that gets the cursor position
Remove the now useless xread utility function and try to do it faster.
-rw-r--r-- | smenu.c | 34 | ||||
-rw-r--r-- | utils.c | 28 | ||||
-rw-r--r-- | utils.h | 3 |
3 files changed, 21 insertions, 44 deletions
@@ -1415,14 +1415,19 @@ int get_cursor_position(int * const r, int * const c) { char buf[32]; - size_t i = 0; - int count = 64; - int v; - int rc = 1; + char * s; + + int count = 64; + int v; + int rc = 1; + + int ask; /* Number of asked characters. */ + int got; /* Number of characters obtained. */ + + int buf_size = sizeof(buf); /* Report cursor location. */ /* """"""""""""""""""""""" */ - errno = 0; while ((v = write(STDOUT_FILENO, "\x1b[6n", 4)) == -1 && count) { if (errno == EINTR) @@ -1443,17 +1448,20 @@ read: /* Read the response: ESC [ rows ; cols R. */ /* """"""""""""""""""""""""""""""""""""""" */ - while (i < sizeof(buf) - 1) - { - if (xread(STDIN_FILENO, buf + i, 1) != 1) - break; + *(s = buf) = 0; - i++; + do + { + ask = buf_size - 1 - (s - buf); + got = read(STDIN_FILENO, s, ask); - if (buf[i - 1] == 'R') + if (got < 0 && errno == EINTR) + got = 0; + else if (got == 0) break; - } - buf[i] = '\0'; + + s += got; + } while (strchr(buf, 'R') == 0); /* Parse it. */ /* """"""""" */ @@ -326,31 +326,3 @@ is_integer(const char * const s) strtol(s, &end, 10); return (*end == '\0'); } - -/* ========= */ -/* Safe read */ -/* ========= */ -ssize_t -xread(int fd, void * buf, size_t n) -{ - size_t rem = n; - ssize_t r; - char * ptr = buf; - - while (rem > 0) - { - if ((r = read(fd, ptr, rem)) < 0) - { - if (errno == EINTR) - r = 0; - else - return -1; - } - else if (r == 0) - break; /* EOF */ - - rem -= r; - ptr += r; - } - return (n - rem); -} @@ -68,7 +68,4 @@ xwcscasecmp(const wchar_t * w1s, const wchar_t * w2s); int is_integer(const char * const s); -ssize_t -xread(int fd, void * buf, size_t n); - #endif |