ls

simple syscall based programming language from scratch
git clone git://git.kocotian.pl/ls.git
Log | Files | Refs | README | LICENSE

commit eb123ac750ccfbd670963eafa08411d6593d3db5
parent 1e09329efdf787e15e399e1cd25d9666d9ad45a6
Author: kocotian <kocotian@kocotian.pl>
Date:   Sun, 28 Feb 2021 13:15:04 +0100

fixed errwarn

Diffstat:
Mutil.c | 23++++++++++++++++++-----
Mutil.h | 1+
2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/util.c b/util.c @@ -61,22 +61,35 @@ nextline(int fd, char *buf, size_t size) return (ssize_t)count; } +ssize_t +strchlen(const char *s, char c) +{ + char *p; + if ((p = strchr(s, c)) == NULL) + return strlen(s); + return p - s; +} + void errwarn(const char *fmt, int iserror, Token token, ...) { - extern const char *filename, *line; - extern int fileline; + extern char *contents; + char *buf; va_list ap; fprintf(stderr, "\033[0;1m%s:%d:%d: \033[1;3%s: \033[0m", token.file, token.line, token.col, iserror ? "1merror" : "3mwarning"); - va_start(ap, iserror); + va_start(ap, token); vfprintf(stderr, fmt, ap); va_end(ap); - fprintf(stderr, "\n% 5d | %s%c", fileline, line, - line[strlen(line) - 1] != '\n' ? '\n' : 0); + buf = malloc(strchlen((contents + token.off) - (token.col - 1), '\n') + 1); + snprintf(buf, strchlen((contents + token.off) - (token.col - 1), '\n') + 1, + "%s", (contents + token.off) - (token.col - 1)); + fprintf(stderr, "\n% 5d | %s%c", token.line, buf, + contents[token.off + token.len] != '\n' ? '\n' : 0); + free(buf); if (iserror) exit(1); } diff --git a/util.h b/util.h @@ -10,4 +10,5 @@ void die(const char *fmt, ...); void *ecalloc(size_t nmemb, size_t size); ssize_t nextline(int fd, char *buf, size_t size); +ssize_t strchlen(const char *s, char c); void errwarn(const char *fmt, int iserror, Token token, ...);