From 96369d6f94358ad060eca95b92b0d2b0b80d44c6 Mon Sep 17 00:00:00 2001 From: Harshit-Dhanwalkar Date: Sun, 21 Jun 2026 19:15:08 +0530 Subject: [PATCH] Add nl_srterror --- C/lib/nl_errno.c | 15 +++++++++------ C/lib/nl_errno.h | 2 ++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/C/lib/nl_errno.c b/C/lib/nl_errno.c index 099f2b9..2e12a73 100644 --- a/C/lib/nl_errno.c +++ b/C/lib/nl_errno.c @@ -1,5 +1,6 @@ #include "nl_errno.h" #include "nl_syscall.h" + #include int errno = 0; @@ -24,14 +25,16 @@ static const struct { {28, "No space left on device"}, {32, "Broken pipe"}, {110, "Connection timed out"}, {0, (const char *)0}}; -void nl_perror(const char *msg) { - const char *estr = "Unknown error"; +const char *nl_strerror(int e) { for (int i = 0; _errtab[i].msg; i++) { - if (_errtab[i].code == errno) { - estr = _errtab[i].msg; - break; - } + if (_errtab[i].code == e) + return _errtab[i].msg; } + return "Unknown error"; +} + +void nl_perror(const char *msg) { + const char *estr = nl_strerror(errno); if (msg && msg[0]) { _ewrite(msg, _slen(msg)); _ewrite(": ", 2); diff --git a/C/lib/nl_errno.h b/C/lib/nl_errno.h index aa16e7f..7f0068a 100644 --- a/C/lib/nl_errno.h +++ b/C/lib/nl_errno.h @@ -5,8 +5,10 @@ extern int errno; +const char *nl_strerror(int err); void nl_perror(const char *msg); #define perror(msg) nl_perror(msg) +#define strerror(e) nl_strerror(e) #endif