1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00

Added sortix_strerror(3) which replaces strerror(3).

The string returned is now const - POSIX did not allow modifying the string
in any case, conforming applications should not break. If _SORTIX_SOURCE is
defined strerror(3) automatically redirects to sortix_strerror(3),
otherwise the application will receive the traditional function.
This commit is contained in:
Jonas 'Sortie' Termansen 2012-05-27 17:38:00 +02:00
parent 9905a2f2d6
commit 87b81080d5
2 changed files with 71 additions and 58 deletions

View file

@ -1,6 +1,6 @@
/****************************************************************************** /******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012.
This file is part of LibMaxsi. This file is part of LibMaxsi.
@ -20,8 +20,9 @@
error.cpp error.cpp
Error reporting functions and utilities. Error reporting functions and utilities.
******************************************************************************/ *******************************************************************************/
#define __SORTIX_STDLIB_REDIRECTS 0
#include <libmaxsi/platform.h> #include <libmaxsi/platform.h>
#include <libmaxsi/error.h> #include <libmaxsi/error.h>
#ifndef SORTIX_KERNEL #ifndef SORTIX_KERNEL
@ -29,64 +30,68 @@
#include <stdio.h> #include <stdio.h>
#endif #endif
namespace Maxsi namespace Maxsi {
{ namespace Error {
namespace Error
{ extern "C" { int errno = 0; }
extern "C" { int errno = 0; }
#ifndef SORTIX_KERNEL #ifndef SORTIX_KERNEL
DEFN_SYSCALL1(int, SysRegisterErrno, SYSCALL_REGISTER_ERRNO, int*); DEFN_SYSCALL1(int, SysRegisterErrno, SYSCALL_REGISTER_ERRNO, int*);
extern "C" void init_error_functions() extern "C" void init_error_functions()
{ {
errno = 0; errno = 0;
SysRegisterErrno(&errno); SysRegisterErrno(&errno);
} }
#endif #endif
extern "C" char* strerror(int code) extern "C" const char* sortix_strerror(int errnum)
{
switch ( errnum )
{ {
switch ( code ) case ENOTBLK: return "Block device required";
{ case ENODEV: return "No such device";
case ENOTBLK: return (char*) "Block device required"; case EWOULDBLOCK: return "Operation would block";
case ENODEV: return (char*) "No such device"; case EBADF: return "Bad file descriptor";
case EWOULDBLOCK: return (char*) "Operation would block"; case EOVERFLOW: return "Value too large to be stored in data type";
case EBADF: return (char*) "Bad file descriptor"; case ENOENT: return "No such file or directory";
case EOVERFLOW: return (char*) "Value too large to be stored in data type"; case ENOSPC: return "No space left on device";
case ENOENT: return (char*) "No such file or directory"; case EEXIST: return "File exists";
case ENOSPC: return (char*) "No space left on device"; case EROFS: return "Read-only file system";
case EEXIST: return (char*) "File exists"; case EINVAL: return "Invalid argument";
case EROFS: return (char*) "Read-only file system"; case ENOTDIR: return "Not a directory";
case EINVAL: return (char*) "Invalid argument"; case ENOMEM: return "Not enough memory";
case ENOTDIR: return (char*) "Not a directory"; case ERANGE: return "Result too large";
case ENOMEM: return (char*) "Not enough memory"; case EISDIR: return "Is a directory";
case ERANGE: return (char*) "Result too large"; case EPERM: return "Operation not permitted";
case EISDIR: return (char*) "Is a directory"; case EIO: return "Input/output error";
case EPERM: return (char*) "Operation not permitted"; case ENOEXEC: return "Exec format error";
case EIO: return (char*) "Input/output error"; case EACCES: return "Permission denied";
case ENOEXEC: return (char*) "Exec format error"; case ESRCH: return "No such process";
case EACCES: return (char*) "Permission denied"; case ENOTTY: return "Not a tty";
case ESRCH: return (char*) "No such process"; case ECHILD: return "No child processes";
case ENOTTY: return (char*) "Not a tty"; case ENOSYS: return "Function not implemented";
case ECHILD: return (char*) "No child processes"; case ENOTSUP: return "Operation not supported";
case ENOSYS: return (char*) "Function not implemented"; case EBLOCKING: return "Operation is blocking";
case ENOTSUP: return (char*) "Operation not supported"; case EINTR: return "Interrupted function call";
case EBLOCKING: return (char*) "Operation is blocking"; case ENOTEMPTY: return "Directory not empty";
case EINTR: return (char*) "Interrupted function call"; case EBUSY: return "Device or resource busy";
case ENOTEMPTY: return (char*) "Directory not empty"; case EPIPE: return "Broken pipe";
case EBUSY: return (char*) "Device or resource busy"; case EILSEQ: return "Illegal byte sequence";
case EPIPE: return (char*) "Broken pipe"; case ELAKE: return "Sit by a lake";
case EILSEQ: return (char*) "Illegal byte sequence"; case EMFILE: return "Too many open files";
case ELAKE: return (char*) "Sit by a lake"; case EAGAIN: return "Resource temporarily unavailable";
case EMFILE: return (char*) "Too many open files"; case EEOF: return "End of file";
case EAGAIN: return (char*) "Resource temporarily unavailable"; case EBOUND: return "Out of bounds";
case EEOF: return (char*) "End of file"; case EINIT: return "Not initialized";
case EBOUND: return (char*) "Out of bounds"; default: return "Unknown error condition";
case EINIT: return (char*) "Not initialized";
default: return (char*) "Unknown error condition";
}
}
} }
} }
extern "C" char* strerror(int errnum)
{
return (char*) sortix_strerror(errnum);
}
} // namespace Error
} // namespace Maxsi

View file

@ -1,6 +1,6 @@
/****************************************************************************** /******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012.
This file is part of LibMaxsi. This file is part of LibMaxsi.
@ -20,7 +20,7 @@
string.h string.h
String operations. String operations.
******************************************************************************/ *******************************************************************************/
#ifndef _STRING_H #ifndef _STRING_H
#define _STRING_H 1 #define _STRING_H 1
@ -46,7 +46,6 @@ int strcoll(const char*, const char*);
size_t strcspn(const char*, const char*); size_t strcspn(const char*, const char*);
char* strcpy(char* restrict, const char* restrict); char* strcpy(char* restrict, const char* restrict);
char* strdup(const char*); char* strdup(const char*);
char* strerror(int);
size_t strlen(const char*); size_t strlen(const char*);
char* strncat(char* restrict, const char* restrict, size_t); char* strncat(char* restrict, const char* restrict, size_t);
int strncmp(const char*, const char*, size_t); int strncmp(const char*, const char*, size_t);
@ -76,6 +75,15 @@ size_t strxfrm_l(char* restrict, const char* restrict, size_t, locale_t);
char* strchrnul(const char* str, int c); char* strchrnul(const char* str, int c);
#endif #endif
#if defined(_SORTIX_SOURCE)
const char* sortix_strerror(int errnum);
#endif
#if defined(_SOURCE_SOURCE) && __SORTIX_STDLIB_REDIRECTS
const char* strerror(int errnum) asm ("sortix_getenv");
#else
char* strerror(int errnum);
#endif
__END_DECLS __END_DECLS
#endif #endif