Implemented perror() and strerror()

This commit is contained in:
2022-05-17 21:45:37 +01:00
parent 7b5edf6e8d
commit 912ee45cd3
5 changed files with 300 additions and 124 deletions

View File

@@ -58,6 +58,8 @@ extern int vfprintf(FILE *fp, const char *restrict format, va_list arg);
extern int vsprintf(char *buf, const char *restrict format, va_list arg); extern int vsprintf(char *buf, const char *restrict format, va_list arg);
extern int vsnprintf(char *buf, size_t sz, const char *restrict format, va_list arg); extern int vsnprintf(char *buf, size_t sz, const char *restrict format, va_list arg);
extern void perror(const char *s);
#if defined(__cplusplus) #if defined(__cplusplus)
} }
#endif #endif

View File

@@ -30,6 +30,8 @@ extern char *strdup(const char *s);
extern char *strtok(char *s, const char *delim); extern char *strtok(char *s, const char *delim);
extern char *strtok_r(char *s, const char *delim, char **sp); extern char *strtok_r(char *s, const char *delim, char **sp);
extern char *strerror(int errnum);
#if defined(__cplusplus) #if defined(__cplusplus)
} /* extern "C" */ } /* extern "C" */
#endif #endif

View File

@@ -0,0 +1,13 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
void perror(const char *s)
{
if (s) {
fprintf(stderr, "%s: %s\n", s, strerror(errno));
} else {
fputs(strerror(errno), stderr);
fputc('\n', stderr);
}
}

View File

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

View File

@@ -1,129 +1,136 @@
#ifndef SYS_HORIZON_SYS__ERRNO_H_ #ifndef SYS_HORIZON_SYS__ERRNO_H_
#define SYS_HORIZON_SYS__ERRNO_H_ #define SYS_HORIZON_SYS__ERRNO_H_
#define EPERM 1 /* Not super-user */ #define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */ #define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */ #define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */ #define EINTR 4 /* Interrupted system call */
#define EIO 5 /* I/O error */ #define EIO 5 /* Input/output error */
#define ENXIO 6 /* No such device or address */ #define ENXIO 6 /* No such device or address */
#define E2BIG 7 /* Arg list too long */ #define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */ #define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file number */ #define EBADF 9 /* Bad file descriptor */
#define ECHILD 10 /* No children */ #define ECHILD 10 /* No child processes */
#define EAGAIN 11 /* No more processes */ #define EAGAIN 11 /* Resource temporarily unavailable */
#define ENOMEM 12 /* Not enough core */ #define ENOMEM 12 /* Cannot allocate memory */
#define EACCES 13 /* Permission denied */ #define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */ #define EFAULT 14 /* Bad address */
#define ENOTBLK 15 /* Block device required */ #define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Mount device busy */ #define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */ #define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */ #define EXDEV 18 /* Invalid cross-device link */
#define ENODEV 19 /* No such device */ #define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */ #define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */ #define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */ #define EINVAL 22 /* Invalid argument */
#define ENFILE 23 /* Too many open files in system */ #define ENFILE 23 /* Too many open files in system */
#define EMFILE 24 /* Too many open files */ #define EMFILE 24 /* Too many open files */
#define ENOTTY 25 /* Not a typewriter */ #define ENOTTY 25 /* Inappropriate ioctl for device */
#define ETXTBSY 26 /* Text file busy */ #define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */ #define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */ #define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */ #define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read only file system */ #define EROFS 30 /* Read-only file system */
#define EMLINK 31 /* Too many links */ #define EMLINK 31 /* Too many links */
#define EPIPE 32 /* Broken pipe */ #define EPIPE 32 /* Broken pipe */
#define EDOM 33 /* Math arg out of domain of func */ #define EDOM 33 /* Numerical argument out of domain */
#define ERANGE 34 /* Math result not representable */ #define ERANGE 34 /* Numerical result out of range */
#define ENOMSG 35 /* No message of desired type */ #define EDEADLK 35 /* Resource deadlock avoided */
#define EIDRM 36 /* Identifier removed */ #define ENAMETOOLONG 36 /* File name too long */
#define ECHRNG 37 /* Channel number out of range */ #define ENOLCK 37 /* No locks available */
#define EL2NSYNC 38 /* Level 2 not synchronized */ #define ENOSYS 38 /* Function not implemented */
#define EL3HLT 39 /* Level 3 halted */ #define ENOTEMPTY 39 /* Directory not empty */
#define EL3RST 40 /* Level 3 reset */ #define ELOOP 40 /* Too many levels of symbolic links */
#define ELNRNG 41 /* Link number out of range */ #define ENOMSG 41 /* No message of desired type */
#define EUNATCH 42 /* Protocol driver not attached */ #define EIDRM 42 /* Identifier removed */
#define ENOCSI 43 /* No CSI structure available */ #define ECHRNG 43 /* Channel number out of range */
#define EL2HLT 44 /* Level 2 halted */ #define EL2NSYNC 44 /* Level 2 not synchronized */
#define EDEADLK 45 /* Deadlock condition */ #define EL3HLT 45 /* Level 3 halted */
#define ENOLCK 46 /* No record locks available */ #define EL3RST 46 /* Level 3 reset */
#define EBADE 50 /* Invalid exchange */ #define ELNRNG 47 /* Link number out of range */
#define EBADR 51 /* Invalid request descriptor */ #define EUNATCH 48 /* Protocol driver not attached */
#define EXFULL 52 /* Exchange full */ #define ENOCSI 49 /* No CSI structure available */
#define ENOANO 53 /* No anode */ #define EL2HLT 50 /* Level 2 halted */
#define EBADRQC 54 /* Invalid request code */ #define EBADE 51 /* Invalid exchange */
#define EBADSLT 55 /* Invalid slot */ #define EBADR 52 /* Invalid request descriptor */
#define EDEADLOCK 56 /* File locking deadlock error */ #define EXFULL 53 /* Exchange full */
#define EBFONT 57 /* Bad font file fmt */ #define ENOANO 54 /* No anode */
#define ENOSTR 60 /* Device not a stream */ #define EBADRQC 55 /* Invalid request code */
#define ENODATA 61 /* No data (for no delay io) */ #define EBADSLT 56 /* Invalid slot */
#define ETIME 62 /* Timer expired */ #define EBFONT 57 /* Bad font file format */
#define ENOSR 63 /* Out of streams resources */ #define ENOSTR 58 /* Device not a stream */
#define ENONET 64 /* Machine is not on the network */ #define ENODATA 59 /* No data available */
#define ENOPKG 65 /* Package not installed */ #define ETIME 60 /* Timer expired */
#define EREMOTE 66 /* The object is remote */ #define ENOSR 61 /* Out of streams resources */
#define ENOLINK 67 /* The link has been severed */ #define ENONET 62 /* Machine is not on the network */
#define EADV 68 /* Advertise error */ #define ENOPKG 63 /* Package not installed */
#define ESRMNT 69 /* Srmount error */ #define EREMOTE 64 /* Object is remote */
#define ECOMM 70 /* Communication error on send */ #define ENOLINK 65 /* Link has been severed */
#define EPROTO 71 /* Protocol error */ #define EADV 66 /* Advertise error */
#define EMULTIHOP 74 /* Multihop attempted */ #define ESRMNT 67 /* Srmount error */
#define ELBIN 75 /* Inode is remote (not really error) */ #define ECOMM 68 /* Communication error on send */
#define EDOTDOT 76 /* Cross mount point (not really error) */ #define EPROTO 69 /* Protocol error */
#define EBADMSG 77 /* Trying to read unreadable message */ #define EMULTIHOP 70 /* Multihop attempted */
#define EFTYPE 79 /* Inappropriate file type or format */ #define EDOTDOT 71 /* RFS specific error */
#define ENOTUNIQ 80 /* Given log. name not unique */ #define EBADMSG 72 /* Bad message */
#define EBADFD 81 /* f.d. invalid for this operation */ #define EOVERFLOW 73 /* Value too large for defined data type */
#define EREMCHG 82 /* Remote address changed */ #define ENOTUNIQ 74 /* Name not unique on network */
#define ELIBACC 83 /* Can't access a needed shared lib */ #define EBADFD 75 /* File descriptor in bad state */
#define ELIBBAD 84 /* Accessing a corrupted shared lib */ #define EREMCHG 76 /* Remote address changed */
#define ELIBSCN 85 /* .lib section in a.out corrupted */ #define ELIBACC 77 /* Can not access a needed shared library */
#define ELIBMAX 86 /* Attempting to link in too many libs */ #define ELIBBAD 78 /* Accessing a corrupted shared library */
#define ELIBEXEC 87 /* Attempting to exec a shared library */ #define ELIBSCN 79 /* .lib section in a.out corrupted */
#define ENOSYS 88 /* Function not implemented */ #define ELIBMAX 80 /* Attempting to link in too many shared libraries */
#define ENMFILE 89 /* No more files */ #define ELIBEXEC 81 /* Cannot exec a shared library directly */
#define ENOTEMPTY 90 /* Directory not empty */ #define EILSEQ 82 /* Invalid or incomplete multibyte or wide character */
#define ENAMETOOLONG 91 /* File or path name too long */ #define ERESTART 83 /* Interrupted system call should be restarted */
#define ELOOP 92 /* Too many symbolic links */ #define ESTRPIPE 84 /* Streams pipe error */
#define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */ #define EUSERS 85 /* Too many users */
#define EPFNOSUPPORT 96 /* Protocol family not supported */ #define ENOTSOCK 86 /* Socket operation on non-socket */
#define ECONNRESET 104 /* Connection reset by peer */ #define EDESTADDRREQ 87 /* Destination address required */
#define ENOBUFS 105 /* No buffer space available */ #define EMSGSIZE 88 /* Message too long */
#define EAFNOSUPPORT 106 /* Address family not supported by protocol family */ #define EPROTOTYPE 89 /* Protocol wrong type for socket */
#define EPROTOTYPE 107 /* Protocol wrong type for socket */ #define ENOPROTOOPT 90 /* Protocol not available */
#define ENOTSOCK 108 /* Socket operation on non-socket */ #define EPROTONOSUPPORT 91 /* Protocol not supported */
#define ENOPROTOOPT 109 /* Protocol not available */ #define ESOCKTNOSUPPORT 92 /* Socket type not supported */
#define ESHUTDOWN 110 /* Can't send after socket shutdown */ #define ENOTSUP 93 /* Operation not supported */
#define ECONNREFUSED 111 /* Connection refused */ #define EPFNOSUPPORT 94 /* Protocol family not supported */
#define EADDRINUSE 112 /* Address already in use */ #define EAFNOSUPPORT 95 /* Address family not supported by protocol */
#define ECONNABORTED 113 /* Connection aborted */ #define EADDRINUSE 96 /* Address already in use */
#define ENETUNREACH 114 /* Network is unreachable */ #define EADDRNOTAVAIL 97 /* Cannot assign requested address */
#define ENETDOWN 115 /* Network interface is not configured */ #define ENETDOWN 98 /* Network is down */
#define ETIMEDOUT 116 /* Connection timed out */ #define ENETUNREACH 99 /* Network is unreachable */
#define EHOSTDOWN 117 /* Host is down */ #define ENETRESET 100 /* Network dropped connection on reset */
#define EHOSTUNREACH 118 /* Host is unreachable */ #define ECONNABORTED 101 /* Software caused connection abort */
#define EINPROGRESS 119 /* Connection already in progress */ #define ECONNRESET 102 /* Connection reset by peer */
#define EALREADY 120 /* Socket already connected */ #define ENOBUFS 103 /* No buffer space available */
#define EDESTADDRREQ 121 /* Destination address required */ #define EISCONN 104 /* Transport endpoint is already connected */
#define EMSGSIZE 122 /* Message too long */ #define ENOTCONN 105 /* Transport endpoint is not connected */
#define EPROTONOSUPPORT 123 /* Unknown protocol */ #define ESHUTDOWN 106 /* Cannot send after transport endpoint shutdown */
#define ESOCKTNOSUPPORT 124 /* Socket type not supported */ #define ETOOMANYREFS 107 /* Too many references: cannot splice */
#define EADDRNOTAVAIL 125 /* Address not available */ #define ETIMEDOUT 108 /* Connection timed out */
#define ENETRESET 126 #define ECONNREFUSED 109 /* Connection refused */
#define EISCONN 127 /* Socket is already connected */ #define EHOSTDOWN 110 /* Host is down */
#define ENOTCONN 128 /* Socket is not connected */ #define EHOSTUNREACH 111 /* No route to host */
#define ETOOMANYREFS 129 #define EALREADY 112 /* Operation already in progress */
#define EPROCLIM 130 #define EINPROGRESS 113 /* Operation now in progress */
#define EUSERS 131 #define ESTALE 114 /* Stale file handle */
#define EDQUOT 132 #define EUCLEAN 115 /* Structure needs cleaning */
#define ESTALE 133 #define ENOTNAM 116 /* Not a XENIX named type file */
#define ENOTSUP 134 /* Not supported */ #define ENAVAIL 117 /* No XENIX semaphores available */
#define ENOMEDIUM 135 /* No medium (in tape drive) */ #define EISNAM 118 /* Is a named type file */
#define ENOSHARE 136 /* No such host or network path */ #define EREMOTEIO 119 /* Remote I/O error */
#define ECASECLASH 137 /* Filename exists with different case */ #define EDQUOT 120 /* Disk quota exceeded */
#define EILSEQ 138 #define ENOMEDIUM 121 /* No medium found */
#define EOVERFLOW 139 /* Value too large for defined data type */ #define EMEDIUMTYPE 122 /* Wrong medium type */
#define EWOULDBLOCK EAGAIN /* Operation would block */ #define ECANCELED 123 /* Operation canceled */
#define __ELASTERROR 2000 /* Users can add values starting here */ #define ENOKEY 124 /* Required key not available */
#define EKEYEXPIRED 125 /* Key has expired */
#define EKEYREVOKED 126 /* Key has been revoked */
#define EKEYREJECTED 127 /* Key was rejected by service */
#define EOWNERDEAD 128 /* Owner died */
#define ENOTRECOVERABLE 129 /* State not recoverable */
#define ERFKILL 130 /* Operation not possible due to RF-kill */
#define EHWPOISON 131 /* Memory page has hardware error */
#endif #endif