Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement strerror function #719

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/cc/std/include/errno_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* MIT License
*
* Copyright (c) 2022-2025 ArthurPV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef _CC_STD_ERRNO_MAP_H
#define _CC_STD_ERRNO_MAP_H

char *
__get_errno(int errnum);

#endif /* _CC_STD_ERRNO_MAP_H */
161 changes: 161 additions & 0 deletions lib/cc/std/include/errno_map/linux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* MIT License
*
* Copyright (c) 2022-2025 ArthurPV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef _CC_STD_ERRNO_MAP_LINUX_H
#define _CC_STD_ERRNO_MAP_LINUX_H

E(0x00, "No error")
E(EPERM, "Operation not permitted")
E(ENOENT, "No such file or directory")
E(ESRCH, "No such process")
E(EINTR, "Interrupted system call")
E(EIO, "Input/output error")
E(ENXIO, "No such device or address")
E(E2BIG, "Argument list too long")
E(ENOEXEC, "Exec format error")
E(EBADF, "Bad file descriptor")
E(ECHILD, "No child processes")
E(EAGAIN, "Resource temporarily unavailable")
E(ENOMEM, "Cannot allocate memory")
E(EACCES, "Permission denied")
E(EFAULT, "Bad address")
E(ENOTBLK, "Block device required")
E(EBUSY, "Device or resource busy")
E(EEXIST, "File exists")
E(EXDEV, "Invalid cross-device link")
E(ENODEV, "No such device")
E(ENOTDIR, "Not a directory")
E(EISDIR, "Is a directory")
E(EINVAL, "Invalid argument")
E(ENFILE, "Too many open files in system")
E(EMFILE, "Too many open files")
E(ENOTTY, "Inappropriate ioctl for device")
E(ETXTBSY, "Text file busy")
E(EFBIG, "File too large")
E(ENOSPC, "No space left on device")
E(ESPIPE, "Illegal seek")
E(EROFS, "Read-only file system")
E(EMLINK, "Too many links")
E(EPIPE, "Broken pipe")
E(EDOM, "Numerical argument out of domain")
E(ERANGE, "Numerical result out of range")
E(EDEADLK, "Resource deadlock avoided")
E(ENAMETOOLONG, "File name too long")
E(ENOLCK, "No locks available")
E(ENOSYS, "Function not implemented")
E(ENOTEMPTY, "Directory not empty")
E(ELOOP, "Too many levels of symbolic links")
E(ENOMSG, "No message of desired type")
E(EIDRM, "Identifier removed")
E(ECHRNG, "Channel number out of range")
E(EL2NSYNC, "Level 2 not synchronized")
E(EL3HLT, "Level 3 halted")
E(EL3RST, "Level 3 reset")
E(ELNRNG, "Link number out of range")
E(EUNATCH, "Protocol driver not attached")
E(ENOCSI, "No CSI structure available")
E(EL2HLT, "Level 2 halted")
E(EBADE, "Invalid exchange")
E(EBADR, "Invalid request descriptor")
E(EXFULL, "Exchange full")
E(ENOANO, "No anode")
E(EBADRQC, "Invalid request code")
E(EBADSLT, "Invalid slot")
E(EBFONT, "Bad font file format")
E(ENOSTR, "Device not a stream")
E(ENODATA, "No data available")
E(ETIME, "Timer expired")
E(ENOSR, "Out of streams resources")
E(ENONET, "Machine is not on the network")
E(ENOPKG, "Package not installed")
E(EREMOTE, "Object is remote")
E(ENOLINK, "Link has been severed")
E(EADV, "Advertise error")
E(ESRMNT, "Srmount error")
E(ECOMM, "Communication error on send")
E(EPROTO, "Protocol error")
E(EMULTIHOP, "Multihop attempted")
E(EDOTDOT, "RFS specific error")
E(EBADMSG, "Bad message")
E(EOVERFLOW, "Value too large for defined data type")
E(ENOTUNIQ, "Name not unique on network")
E(EBADFD, "File descriptor in bad state")
E(EREMCHG, "Remote address changed")
E(ELIBACC, "Can not access a needed shared library")
E(ELIBBAD, "Accessing a corrupted shared library")
E(ELIBSCN, ".lib section in a.out corrupted")
E(ELIBMAX, "Attempting to link in too many shared libraries")
E(ELIBEXEC, "Cannot exec a shared library directly")
E(EILSEQ, "Invalid or incomplete multibyte or wide character")
E(ERESTART, "Interrupted system call should be restarted")
E(ESTRPIPE, "Streams pipe error")
E(EUSERS, "Too many users")
E(ENOTSOCK, "Socket operation on non-socket")
E(EDESTADDRREQ, "Destination address required")
E(EMSGSIZE, "Message too long")
E(EPROTOTYPE, "Protocol wrong type for socket")
E(ENOPROTOOPT, "Protocol not available")
E(EPROTONOSUPPORT, "Protocol not supported")
E(ESOCKTNOSUPPORT, "Socket type not supported")
E(EOPNOTSUPP, "Operation not supported")
E(EPFNOSUPPORT, "Protocol family not supported")
E(EAFNOSUPPORT, "Address family not supported by protocol")
E(EADDRINUSE, "Address already in use")
E(EADDRNOTAVAIL, "Cannot assign requested address")
E(ENETDOWN, "Network is down")
E(ENETUNREACH, "Network is unreachable")
E(ENETRESET, "Network dropped connection on reset")
E(ECONNABORTED, "Software caused connection abort")
E(ECONNRESET, "Connection reset by peer")
E(ENOBUFS, "No buffer space available")
E(EISCONN, "Transport endpoint is already connected")
E(ENOTCONN, "Transport endpoint is not connected")
E(ESHUTDOWN, "Cannot send after transport endpoint shutdown")
E(ETOOMANYREFS, "Too many references: cannot splice")
E(ETIMEDOUT, "Connection timed out")
E(ECONNREFUSED, "Connection refused")
E(EHOSTDOWN, "Host is down")
E(EHOSTUNREACH, "No route to host")
E(EALREADY, "Operation already in progress")
E(EINPROGRESS, "Operation now in progress")
E(ESTALE, "Stale file handle")
E(EUCLEAN, "Structure needs cleaning")
E(ENOTNAM, "Not a XENIX named type file")
E(ENAVAIL, "No XENIX semaphores available")
E(EISNAM, "Is a named type file")
E(EREMOTEIO, "Remote I/O error")
E(EDQUOT, "Disk quota exceeded")
E(ENOMEDIUM, "No medium found")
E(EMEDIUMTYPE, "Wrong medium type")
E(ECANCELED, "Operation canceled")
E(ENOKEY, "Required key not available")
E(EKEYEXPIRED, "Key has expired")
E(EKEYREVOKED, "Key has been revoked")
E(EKEYREJECTED, "Key was rejected by service")
E(EOWNERDEAD, "Owner died")
E(ENOTRECOVERABLE, "State not recoverable")
E(ERFKILL, "Operation not possible due to RF-kill")
E(EHWPOISON, "Memory page has hardware error")

#endif /* _CC_STD_ERRNO_MAP_LINUX_H */
1 change: 1 addition & 0 deletions lib/cc/std/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
if(LILY_COMPILE_LIB_CC_STD)
set(LIB_CC_STD_SRC
${CMAKE_SOURCE_DIR}/lib/cc/std/src/errno.c
${CMAKE_SOURCE_DIR}/lib/cc/std/src/errno_map.c
${CMAKE_SOURCE_DIR}/lib/cc/std/src/string/strerror.c
${CMAKE_SOURCE_DIR}/lib/cc/std/src/sys/exit.c
${CMAKE_SOURCE_DIR}/lib/cc/std/src/sys/write.c)
Expand Down
49 changes: 49 additions & 0 deletions lib/cc/std/src/errno_map.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* MIT License
*
* Copyright (c) 2022-2025 ArthurPV
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <errno.h>
#include <errno_map.h>
#include <platform.h>
#include <stddef.h>

static char *__errno_map[] = {
#define E(id, s) [id] = s,
#if defined(_CC_STD_LINUX)
#include <errno_map/linux.h>
#else
#error
#endif
#undef E
};
static size_t __errno_map_length = sizeof(__errno_map) / sizeof(*__errno_map);

char *
__get_errno(int errnum)
{
if (errnum >= 0 && errnum < __errno_map_length) {
return __errno_map[errnum];
}

return NULL;
}
10 changes: 9 additions & 1 deletion lib/cc/std/src/string/strerror.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@
* SOFTWARE.
*/

#include <errno_map.h>

char *
strerror(int errnum)
{
return "";
char *res = __get_errno(errnum);

if (res) {
return res;
}

return "Unknown error";
}
Loading