9os

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit f82f4e89c15cfb1b8f019d6710ba40dce50609bf
parent 9c8d68280c9f248f7f1772a709a0910740e1fca8
Author: Roberto E. Vargas Caballero <roberto.vargas@midokura.com>
Date:   Tue, 15 Nov 2022 20:15:45 +0100

os9: Improve getline() error handling

Previous version didn't differentiate between end of file
and some other error. Also, the interface didn't allow to
detect truncations.

Diffstat:
Msrc/libk/getline.c | 15+++++++--------
Msrc/os9/sys.c | 21+++++++++++++++------
2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/src/libk/getline.c b/src/libk/getline.c @@ -5,18 +5,17 @@ int getline(Chan *c, char *line, int size) { - int n; + int n, r; char ch; - for (n = 0; n < size-1; ++n) { - if (devread(c, &ch, 1) < 0) + for (ch = n = 0; n < size-1 && ch != '\n'; ++n) { + if ((r = devread(c, &ch, 1)) < 0) return -1; + if (r == 0) + break; line[n] = ch; - if (ch == '\n') { - line[n+1] = '\0'; - return n; - } } + line[n] = '\0'; - return -1; + return n; } diff --git a/src/os9/sys.c b/src/os9/sys.c @@ -63,6 +63,7 @@ mountline(char *line) static void iconf(void) { + int n; Chan *c; char line[80], *s, *p; @@ -71,7 +72,11 @@ iconf(void) return; } - while (getline(c, line, sizeof(line)) > 0) { + while ((n = getline(c, line, sizeof(line))) > 0) { + if (line[n-1] != '\n') { + kprint("iconf: truncated configuration line\n"); + continue; + } for (p = line; isspace(*p); ++p) ; s = p; @@ -80,15 +85,19 @@ iconf(void) while (isspace(*p)) *p++ = '\0'; - if (strcmp(s, "write") == 0) + if (strcmp(s, "write") == 0) { writeline(p); - else if (strcmp(s, "bind") == 0) + } else if (strcmp(s, "bind") == 0) { bindline(p); - else if (strcmp(s, "mount") == 0) + } else if (strcmp(s, "mount") == 0) { mountline(p); - else - panic("iconf:verb"); + } else { + kprint("iconf: incorrect verb '%s'\n", s); + continue; + } } + if (n < 0) + kprint("iconf: error reading configuration:%r\n"); devclose(c); }