From f408ba192c3444060f6a4a86c56358cc3989af87 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 12:04:34 +0100 Subject: [PATCH] Sys: errorText, what an errno means On Linux a table of glibc's texts, carried only by a program that calls it; over C, strerror. Library growth that breaks no program, met in rwalk's Rwfiles, which named four errnos by hand. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 748ea34..0600265 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -583,23 +583,6 @@ Met in rrem's parser, documenting `(?>x*)`. -## The text of an errno - -`Sys` returns minus an errno, and a tool that tells the user what -failed must turn it into words: `Rwfiles` maps four by hand, and says -"error 13" for the rest. - -The proposal, in `Sys`: - -``` -proc errorText*(err: INT): []U8; (* what errno err (or minus it) means, as strerror says; "unknown error" otherwise *) -``` - -a fixed table of Linux's names, so it needs no C. It breaks no -program. - -Met in rwalk's `Rwfiles`. - ## Setting an environment variable `Sys.env` reads a variable; nothing sets one. A test can't give the diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 71d688f..a57ad42 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -11,7 +11,7 @@ | Module | What | |---|---| | `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; `trace`, the calls under way; what the compiler's checks call when they fail | -| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `close`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `envAt`, `workDir`, `setWorkDir`; programs, `spawn` and `exec`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others | +| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `close`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `envAt`, `workDir`, `setWorkDir`; programs, `spawn` and `exec`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; what an errno means, `errorText`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | @@ -249,6 +249,8 @@ proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *) proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *) proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *) + +proc errorText*(err: INT): []U8; (* what errno err, or minus it, means, as C's strerror says *) proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *) proc envAt*(i: INT): ([]U8, BOOL); (* the environment's ith NAME=value, from 0; false past the last *) diff --git a/lib/C.raz b/lib/C.raz index 3da5977..2f1ba1b 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -55,6 +55,7 @@ extern proc clock_nanosleep*(clock, flags: I32; t, left: ^U8): I32; extern proc getrandom*(buf: ^U8; n: UINT; flags: U32): INT; extern proc getenv*(name: ^U8): ^U8; +extern proc strerror*(err: I32): ^U8; extern proc getcwd*(buf: ^U8; n: UINT): ^U8; extern proc chdir*(path: ^U8): I32; extern proc stat*(path, st: ^U8): I32; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 380e7e4..d056514 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -1003,6 +1003,154 @@ return n, k; end; +const + (* what each errno means, as glibc's strerror says it; 41 and 58 + are none *) + errorTexts: [134][]U8 = [ + "Success", (* 0 *) + "Operation not permitted", (* 1, EPERM *) + "No such file or directory", (* 2, ENOENT *) + "No such process", (* 3, ESRCH *) + "Interrupted system call", (* 4, EINTR *) + "Input/output error", (* 5, EIO *) + "No such device or address", (* 6, ENXIO *) + "Argument list too long", (* 7, E2BIG *) + "Exec format error", (* 8, ENOEXEC *) + "Bad file descriptor", (* 9, EBADF *) + "No child processes", (* 10, ECHILD *) + "Resource temporarily unavailable", (* 11, EAGAIN *) + "Cannot allocate memory", (* 12, ENOMEM *) + "Permission denied", (* 13, EACCES *) + "Bad address", (* 14, EFAULT *) + "Block device required", (* 15, ENOTBLK *) + "Device or resource busy", (* 16, EBUSY *) + "File exists", (* 17, EEXIST *) + "Invalid cross-device link", (* 18, EXDEV *) + "No such device", (* 19, ENODEV *) + "Not a directory", (* 20, ENOTDIR *) + "Is a directory", (* 21, EISDIR *) + "Invalid argument", (* 22, EINVAL *) + "Too many open files in system", (* 23, ENFILE *) + "Too many open files", (* 24, EMFILE *) + "Inappropriate ioctl for device", (* 25, ENOTTY *) + "Text file busy", (* 26, ETXTBSY *) + "File too large", (* 27, EFBIG *) + "No space left on device", (* 28, ENOSPC *) + "Illegal seek", (* 29, ESPIPE *) + "Read-only file system", (* 30, EROFS *) + "Too many links", (* 31, EMLINK *) + "Broken pipe", (* 32, EPIPE *) + "Numerical argument out of domain", (* 33, EDOM *) + "Numerical result out of range", (* 34, ERANGE *) + "Resource deadlock avoided", (* 35, EDEADLOCK *) + "File name too long", (* 36, ENAMETOOLONG *) + "No locks available", (* 37, ENOLCK *) + "Function not implemented", (* 38, ENOSYS *) + "Directory not empty", (* 39, ENOTEMPTY *) + "Too many levels of symbolic links", (* 40, ELOOP *) + "unknown error", (* 41 *) + "No message of desired type", (* 42, ENOMSG *) + "Identifier removed", (* 43, EIDRM *) + "Channel number out of range", (* 44, ECHRNG *) + "Level 2 not synchronized", (* 45, EL2NSYNC *) + "Level 3 halted", (* 46, EL3HLT *) + "Level 3 reset", (* 47, EL3RST *) + "Link number out of range", (* 48, ELNRNG *) + "Protocol driver not attached", (* 49, EUNATCH *) + "No CSI structure available", (* 50, ENOCSI *) + "Level 2 halted", (* 51, EL2HLT *) + "Invalid exchange", (* 52, EBADE *) + "Invalid request descriptor", (* 53, EBADR *) + "Exchange full", (* 54, EXFULL *) + "No anode", (* 55, ENOANO *) + "Invalid request code", (* 56, EBADRQC *) + "Invalid slot", (* 57, EBADSLT *) + "unknown error", (* 58 *) + "Bad font file format", (* 59, EBFONT *) + "Device not a stream", (* 60, ENOSTR *) + "No data available", (* 61, ENODATA *) + "Timer expired", (* 62, ETIME *) + "Out of streams resources", (* 63, ENOSR *) + "Machine is not on the network", (* 64, ENONET *) + "Package not installed", (* 65, ENOPKG *) + "Object is remote", (* 66, EREMOTE *) + "Link has been severed", (* 67, ENOLINK *) + "Advertise error", (* 68, EADV *) + "Srmount error", (* 69, ESRMNT *) + "Communication error on send", (* 70, ECOMM *) + "Protocol error", (* 71, EPROTO *) + "Multihop attempted", (* 72, EMULTIHOP *) + "RFS specific error", (* 73, EDOTDOT *) + "Bad message", (* 74, EBADMSG *) + "Value too large for defined data type", (* 75, EOVERFLOW *) + "Name not unique on network", (* 76, ENOTUNIQ *) + "File descriptor in bad state", (* 77, EBADFD *) + "Remote address changed", (* 78, EREMCHG *) + "Can not access a needed shared library", (* 79, ELIBACC *) + "Accessing a corrupted shared library", (* 80, ELIBBAD *) + ".lib section in a.out corrupted", (* 81, ELIBSCN *) + "Attempting to link in too many shared libraries", (* 82, ELIBMAX *) + "Cannot exec a shared library directly", (* 83, ELIBEXEC *) + "Invalid or incomplete multibyte or wide character", (* 84, EILSEQ *) + "Interrupted system call should be restarted", (* 85, ERESTART *) + "Streams pipe error", (* 86, ESTRPIPE *) + "Too many users", (* 87, EUSERS *) + "Socket operation on non-socket", (* 88, ENOTSOCK *) + "Destination address required", (* 89, EDESTADDRREQ *) + "Message too long", (* 90, EMSGSIZE *) + "Protocol wrong type for socket", (* 91, EPROTOTYPE *) + "Protocol not available", (* 92, ENOPROTOOPT *) + "Protocol not supported", (* 93, EPROTONOSUPPORT *) + "Socket type not supported", (* 94, ESOCKTNOSUPPORT *) + "Operation not supported", (* 95, ENOTSUP *) + "Protocol family not supported", (* 96, EPFNOSUPPORT *) + "Address family not supported by protocol", (* 97, EAFNOSUPPORT *) + "Address already in use", (* 98, EADDRINUSE *) + "Cannot assign requested address", (* 99, EADDRNOTAVAIL *) + "Network is down", (* 100, ENETDOWN *) + "Network is unreachable", (* 101, ENETUNREACH *) + "Network dropped connection on reset", (* 102, ENETRESET *) + "Software caused connection abort", (* 103, ECONNABORTED *) + "Connection reset by peer", (* 104, ECONNRESET *) + "No buffer space available", (* 105, ENOBUFS *) + "Transport endpoint is already connected", (* 106, EISCONN *) + "Transport endpoint is not connected", (* 107, ENOTCONN *) + "Cannot send after transport endpoint shutdown", (* 108, ESHUTDOWN *) + "Too many references: cannot splice", (* 109, ETOOMANYREFS *) + "Connection timed out", (* 110, ETIMEDOUT *) + "Connection refused", (* 111, ECONNREFUSED *) + "Host is down", (* 112, EHOSTDOWN *) + "No route to host", (* 113, EHOSTUNREACH *) + "Operation already in progress", (* 114, EALREADY *) + "Operation now in progress", (* 115, EINPROGRESS *) + "Stale file handle", (* 116, ESTALE *) + "Structure needs cleaning", (* 117, EUCLEAN *) + "Not a XENIX named type file", (* 118, ENOTNAM *) + "No XENIX semaphores available", (* 119, ENAVAIL *) + "Is a named type file", (* 120, EISNAM *) + "Remote I/O error", (* 121, EREMOTEIO *) + "Disk quota exceeded", (* 122, EDQUOT *) + "No medium found", (* 123, ENOMEDIUM *) + "Wrong medium type", (* 124, EMEDIUMTYPE *) + "Operation canceled", (* 125, ECANCELED *) + "Required key not available", (* 126, ENOKEY *) + "Key has expired", (* 127, EKEYEXPIRED *) + "Key has been revoked", (* 128, EKEYREVOKED *) + "Key was rejected by service", (* 129, EKEYREJECTED *) + "Owner died", (* 130, EOWNERDEAD *) + "State not recoverable", (* 131, ENOTRECOVERABLE *) + "Operation not possible due to RF-kill", (* 132, ERFKILL *) + "Memory page has hardware error" (* 133, EHWPOISON *)]; + +(* What errno err, or minus it, means, as C's strerror says it; + "unknown error" for a number that is no errno. *) +proc errorText*(err: INT): []U8; +begin + if (err < 0) and (err > -LEN(errorTexts)) then err := -err; end; + if (err < 0) or (err >= LEN(errorTexts)) then return "unknown error"; end; + return errorTexts[err]; +end; + (* The value of the environment variable name; "" if it isn't set. The environment follows the arguments and their nil. *) proc env*(name: []U8): []U8; @@ -1894,6 +2042,8 @@ if (n # 5) or (buf[0..5] # "hello") or (read(fd, buf) # 0) then exit(2); end; close(fd); if openRead("/no/such/file") >= 0 then exit(2); end; + if (errorText(-2) # "No such file or directory") or (errorText(13) # "Permission denied") + or (errorText(41) # "unknown error") or (errorText(134) # "unknown error") then exit(2); end; if isTerminal(-1) then exit(2); end; p := mmap(100); if (p = nil) or (p[99] # 0) then exit(2); end; diff --git a/lib/Sys.raz b/lib/Sys.raz index fef5641..b0860b5 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -1005,6 +1005,20 @@ return n, k; end; +(* What errno err, or minus it, means, as the C library's strerror + says it. *) +proc errorText*(err: INT): []U8; +var s: ^U8; n: INT; +begin + if err < 0 then err := -err; end; + if (err < 0) or (err > 7FFFFFFFH) then return "unknown error"; end; (* the smallest INT, or past an int *) + s := C.strerror(I32(err)); + if s = nil then return "unknown error"; end; + n := 0; + while s[n] # 0 do INC(n); end; + return s[0..n]; +end; + (* The value of the environment variable name; "" if it isn't set. *) proc env*(name: []U8): []U8; var a: [4096]U8; s: ^U8; n: INT; @@ -1914,6 +1928,7 @@ if (n # 5) or (buf[0..5] # "hello") or (read(fd, buf) # 0) then exit(2); end; close(fd); if openRead("/no/such/file") >= 0 then exit(2); end; + if (errorText(-2) # "No such file or directory") or (errorText(13) # "Permission denied") then exit(2); end; if isTerminal(-1) then exit(2); end; p := mmap(100); if (p = nil) or (p[99] # 0) then exit(2); end;