return &_PDCLIB_errno;
}
-/* TODO: Doing this via a static array is not the way to do it. */
-char const * _PDCLIB_errno_texts[] = {
- "",
- "ERANGE (Range error)",
- "EDOM (Domain error)",
- "EIO (I/O error)",
- "EUNKNOWN (Unknown error)",
- "EINVAL (Invalid parameter value)",
- "ERETRY (I/O retries exceeded)"
-};
-
#endif
#ifdef TEST
( stream->status & ( _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_ERRORFLAG | _PDCLIB_WIDESTREAM | _PDCLIB_EOFFLAG ) ) ||
! ( stream->status & ( _PDCLIB_FREAD | _PDCLIB_FRW ) ) )
{
- _PDCLIB_errno = _PDCLIB_EIO;
+ /* Function called on illegal (e.g. output) stream.
+ See comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
+ */
+ _PDCLIB_errno = _PDCLIB_ERROR;
stream->status |= _PDCLIB_ERRORFLAG;
return EOF;
}
( stream->status & ( _PDCLIB_FREAD | _PDCLIB_ERRORFLAG | _PDCLIB_WIDESTREAM | _PDCLIB_EOFFLAG ) ) ||
! ( stream->status & ( _PDCLIB_FWRITE | _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) )
{
- _PDCLIB_errno = _PDCLIB_EIO;
+ /* Function called on illegal (e.g. input) stream.
+ See the comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
+ */
+ _PDCLIB_errno = _PDCLIB_ERROR;
stream->status |= _PDCLIB_ERRORFLAG;
return EOF;
}
previous = current;
current = current->next;
}
- _PDCLIB_errno = _PDCLIB_EIO;
+ /* See the comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
+ */
+ _PDCLIB_errno = _PDCLIB_ERROR;
return -1;
}
#ifndef REGTEST
#include <errno.h>
+#include <locale.h>
/* TODO: Doing this via a static array is not the way to do it. */
void perror( const char * s )
{
fprintf( stderr, "%s: ", s );
}
- fprintf( stderr, "%s\n", _PDCLIB_errno_texts[ errno ] );
+ if ( errno >= _PDCLIB_ERRNO_MAX )
+ {
+ fprintf( stderr, "Unknown error\n" );
+ }
+ else
+ {
+ fprintf( stderr, "%s\n", _PDCLIB_lconv._PDCLIB_errno_texts[errno] );
+ }
return;
}
#ifndef REGTEST
+#include <locale.h>
+
/* TODO: Doing this via a static array is not the way to do it. */
char * strerror( int errnum )
{
- return (char *)_PDCLIB_errno_texts[ errnum ];
+ if ( errnum == 0 || errnum >= _PDCLIB_ERRNO_MAX )
+ {
+ return (char *)"Unknown error";
+ }
+ else
+ {
+ return _PDCLIB_lconv._PDCLIB_errno_texts[errnum];
+ }
}
#endif
struct lconv
{
struct _PDCLIB_ctype_t * ctype; /* internal <ctype.h> information */
+ char * _PDCLIB_errno_texts[_PDCLIB_ERRNO_MAX]; /* strerror() / perror() */
char * decimal_point; /* decimal point character */
char * thousands_sep; /* character for seperating groups of digits */
char * grouping; /* string indicating the size of digit groups */
/* errno */
/* -------------------------------------------------------------------------- */
+/* If PDCLib would call its error number "errno" directly, there would be no way
+ to catch its value from underlying system calls that also use it (i.e., POSIX
+ operating systems). That is why we use an internal name, providing a means to
+ access it through <errno.h>.
+*/
extern int _PDCLIB_errno;
-int * _PDCLIB_errno_func( void );
-/* ERANGE and EDOM are specified by the standard. */
-#define _PDCLIB_ERANGE 1
-#define _PDCLIB_EDOM 2
-/* Used in the example implementation for any kind of I/O error. */
-#define _PDCLIB_EIO 3
-/* Used in the example implementation for "unknown error". */
-#define _PDCLIB_EUNKNOWN 4
-/* Used in the example implementation for "invalid parameter value". */
-#define _PDCLIB_EINVAL 5
-/* Used in the example implementation for "I/O retries exceeded". */
-#define _PDCLIB_ERETRY 6
-/* One larger than the largest used errno */
-#define _PDCLIB_EMAX 7
-
-/* TODO: Doing this via a static array is not the way to do it. */
-char const * _PDCLIB_errno_texts[ _PDCLIB_EMAX ];
+/* A mechanism for delayed evaluation. (Not sure if this is really necessary, so
+ no detailed documentation on the "why".)
+*/
+int * _PDCLIB_errno_func( void );
/* -------------------------------------------------------------------------- */
/* <ctype.h> lookup tables */
/* Reading error */
switch ( errno )
{
+ /* See comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
+ */
case EBADF:
case EFAULT:
case EINTR:
case EINVAL:
case EIO:
- _PDCLIB_errno = _PDCLIB_EIO;
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
default:
- _PDCLIB_errno = _PDCLIB_EUNKNOWN;
+ /* This should probably be something like EUNKNOWN. */
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
}
stream->status |= _PDCLIB_ERRORFLAG;
/* Write error */
switch ( errno )
{
+ /* See <_PDCLIB_config.h>. There should be differenciated errno
+ handling here, possibly even a 1:1 mapping; but that is up
+ to the individual platform.
+ */
case EBADF:
case EFAULT:
case EFBIG:
case EIO:
case ENOSPC:
case EPIPE:
- _PDCLIB_errno = _PDCLIB_EIO;
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
default:
- _PDCLIB_errno = _PDCLIB_EUNKNOWN;
+ /* This should be something like EUNKNOWN. */
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
}
stream->status |= _PDCLIB_ERRORFLAG;
return 0;
}
}
- _PDCLIB_errno = _PDCLIB_ERETRY;
+ /* Number of retries exceeded. You probably want a different errno value
+ here.
+ */
+ _PDCLIB_errno = _PDCLIB_ERROR;
stream->status |= _PDCLIB_ERRORFLAG;
/* Move unwritten remains to begin of buffer. */
stream->bufidx -= written;
{
switch ( errno )
{
+ /* See the comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
+ */
case EACCES:
case EFAULT:
case EINTR:
case EOVERFLOW:
case EROFS:
case ETXTBSY:
- _PDCLIB_errno = _PDCLIB_EIO;
+ _PDCLIB_errno = _PDCLIB_ERROR;
+ break;
default:
- _PDCLIB_errno = _PDCLIB_EUNKNOWN;
+ /* This should be something like EUNKNOWN. */
+ _PDCLIB_errno = _PDCLIB_ERROR;
+ break;
}
}
return rc;
{
switch ( errno )
{
+ /* See the comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
+ */
case EACCES:
case EFAULT:
case EIO:
case ENOTDIR:
case EPERM:
case EROFS:
- _PDCLIB_errno = _PDCLIB_EIO;
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
default:
- _PDCLIB_errno = _PDCLIB_EUNKNOWN;
+ /* This should be something like EUNKNOWN. */
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
}
return -1;
{
switch ( errno )
{
+ /* See the comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
+ */
case EACCES:
case EEXIST:
case EFAULT:
case EPERM:
case EROFS:
case EXDEV:
- _PDCLIB_errno = _PDCLIB_EIO;
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
default:
- _PDCLIB_errno = _PDCLIB_EUNKNOWN;
+ /* This should be something like EUNKNOWN. */
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
}
return EOF;
/* EMPTY - OK */
break;
default:
- _PDCLIB_errno = _PDCLIB_EINVAL;
+ /* See comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
+ */
+ _PDCLIB_errno = _PDCLIB_ERROR;
return EOF;
break;
}
{
case EBADF:
case EFAULT:
- _PDCLIB_errno = _PDCLIB_EIO;
+ /* See comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
+ */
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
default:
- _PDCLIB_errno = _PDCLIB_EUNKNOWN;
+ /* This should probably be something like EUNKNOWN. */
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
}
return EOF;
struct lconv _PDCLIB_lconv = {
/* _PDCLIB_ctype */ _ctype + 1,
+ /* _PDCLIB_errno_texts */
+ {
+ /* no error */ (char *)"",
+ /* ERANGE */ (char *)"ERANGE (Range error)",
+ /* EDOM */ (char *)"EDOM (Domain error)"
+ },
/* decimal_point */ (char *)".",
/* thousands_sep */ (char *)"",
/* grouping */ (char *)"",
{
switch ( errno )
{
- /* These are the values possible on a Linux machine. Adapt the
- values and their mapping to PDCLib errno values at will. (This
- is an example implementation, so we keep it very simple.)
+ /* See the comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
*/
case EACCES:
case EFAULT:
case ENOTDIR:
case EPERM:
case EROFS:
- _PDCLIB_errno = _PDCLIB_EIO;
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
default:
- _PDCLIB_errno = _PDCLIB_EUNKNOWN;
+ /* This should be something like EUNKNOWN. */
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
}
}
/* to nothing. (This is to avoid warnings with the exit functions under GCC.) */
#define _PDCLIB_NORETURN __attribute__(( noreturn ))
+/* The maximum value that errno can be set to. This is used to set the size */
+/* of the array in struct lconv (<locale.h>) holding error messages for the */
+/* strerror() and perror() functions. (If you change this value because you */
+/* are using additional errno values, you *HAVE* to provide appropriate error */
+/* messages for *ALL* locales.) */
+/* Default is 2 (0, ERANGE, EDOM). */
+#define _PDCLIB_ERRNO_MAX 3
+
/* -------------------------------------------------------------------------- */
/* Integers */
/* -------------------------------------------------------------------------- */
this capability dependent on implementation-defined behaviour (not good).
*/
#define _PDCLIB_UNGETCBUFSIZE 1
+
+/* errno -------------------------------------------------------------------- */
+
+/* These are the values that _PDCLIB_errno can be set to by the library.
+
+ By keeping PDCLib's errno in the _PDCLIB_* namespace, the library is capable
+ to "translate" between errno values used by the hosting operating system and
+ those used and passed out by the library.
+
+ Example: In the example platform, the remove() function uses the unlink()
+ system call as backend. Linux sets its errno to EISDIR if you try to unlink()
+ a directory, but POSIX demands EPERM. Within the remove() function, you can
+ catch the 'errno == EISDIR', and set '_PDCLIB_errno = _PDCLIB_EPERM'. Anyone
+ using PDCLib's <errno.h> will "see" EPERM instead of EISDIR (the _PDCLIB_*
+ prefix removed by <errno.h> mechanics).
+
+ If you do not want that kind of translation, you might want to "match" the
+ values used by PDCLib with those used by the host OS, as to avoid confusion.
+
+ The standard only defines three distinct errno values: ERANGE, EDOM, and
+ EILSEQ. The standard leaves it up to "the implementation" whether there are
+ any more beyond those three. There is some controversy as to whether errno is
+ such a good idea at all, so you might want to come up with a different error
+ reporting facility for your platform. Since errno values beyond the three
+ defined by the standard are not portable anyway (unless you look at POSIX),
+ having your own error reporting facility would not hurt anybody either.
+*/
+#define _PDCLIB_ERANGE 1
+#define _PDCLIB_EDOM 2
+#define _PDCLIB_EILSEQ 3
+
+/* The following is not strictly "configuration", but there is no better place
+ to explain it than here.
+
+ PDCLib strives to be as generic as possible, so by default it does NOT define
+ any values beyond the three standard ones above, even where it would have
+ been prudent and convenient to do so. Any errno "caught" from the host OS,
+ and some internal error conditions as well, are all lumped together into the
+ value of '_PDCLIB_ERROR'.
+
+ '_PDCLIB_ERROR' is STRICLY meant as a PLACEHOLDER only.
+
+ You should NEVER ship an adaption of PDCLib still using that particular
+ value. You should NEVER write code that *tests* for that value. Indeed it is
+ not even conforming, since errno values should be defined as beginning with
+ an uppercase 'E', and there is no mechanics in <errno.h> to unmask that
+ particular value (for exactly that reason).
+
+ The idea is that you scan the source of PDCLib for occurrences of this macro
+ and replace _PDCLIB_ERROR with whatever additional errno value you came up
+ with for your platform.
+
+ If you cannot find it within you to do that, tell your clients to check for
+ an errno value larger than zero. That, at least, would be standard compliant
+ (and fully portable).
+*/
+#define _PDCLIB_ERROR 4
+
/* Reading error */
switch ( errno )
{
+ /* See comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
+ */
case EBADF:
case EFAULT:
case EINTR:
case EINVAL:
case EIO:
- _PDCLIB_errno = _PDCLIB_EIO;
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
default:
- _PDCLIB_errno = _PDCLIB_EUNKNOWN;
+ /* This should probably be something like EUNKNOWN. */
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
}
stream->status |= _PDCLIB_ERRORFLAG;
/* Write error */
switch ( errno )
{
+ /* See <_PDCLIB_config.h>. There should be differenciated errno
+ handling here, possibly even a 1:1 mapping; but that is up
+ to the individual platform.
+ */
case EBADF:
case EFAULT:
case EFBIG:
case EIO:
case ENOSPC:
case EPIPE:
- _PDCLIB_errno = _PDCLIB_EIO;
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
default:
- _PDCLIB_errno = _PDCLIB_EUNKNOWN;
+ /* This should be something like EUNKNOWN. */
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
}
stream->status |= _PDCLIB_ERRORFLAG;
return 0;
}
}
- _PDCLIB_errno = _PDCLIB_ERETRY;
+ /* Number of retries exceeded. You probably want a different errno value
+ here.
+ */
+ _PDCLIB_errno = _PDCLIB_ERROR;
stream->status |= _PDCLIB_ERRORFLAG;
/* Move unwritten remains to begin of buffer. */
stream->bufidx -= written;
{
switch ( errno )
{
+ /* See the comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
+ */
case EACCES:
case EFAULT:
case EINTR:
case EOVERFLOW:
case EROFS:
case ETXTBSY:
- _PDCLIB_errno = _PDCLIB_EIO;
+ _PDCLIB_errno = _PDCLIB_ERROR;
+ break;
default:
- _PDCLIB_errno = _PDCLIB_EUNKNOWN;
+ /* This should be something like EUNKNOWN. */
+ _PDCLIB_errno = _PDCLIB_ERROR;
+ break;
}
}
return rc;
{
switch ( errno )
{
+ /* See the comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
+ */
case EACCES:
case EFAULT:
case EIO:
case ENOTDIR:
case EPERM:
case EROFS:
- _PDCLIB_errno = _PDCLIB_EIO;
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
default:
- _PDCLIB_errno = _PDCLIB_EUNKNOWN;
+ /* This should be something like EUNKNOWN. */
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
}
return -1;
{
switch ( errno )
{
+ /* See the comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
+ */
case EACCES:
case EEXIST:
case EFAULT:
case EPERM:
case EROFS:
case EXDEV:
- _PDCLIB_errno = _PDCLIB_EIO;
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
default:
- _PDCLIB_errno = _PDCLIB_EUNKNOWN;
+ /* This should be something like EUNKNOWN. */
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
}
return EOF;
/* EMPTY - OK */
break;
default:
- _PDCLIB_errno = _PDCLIB_EINVAL;
+ _PDCLIB_errno = _PDCLIB_ERROR;
return EOF;
break;
}
}
switch ( errno )
{
+ /* See comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
+ */
case EBADF:
case EFAULT:
- _PDCLIB_errno = _PDCLIB_EIO;
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
default:
- _PDCLIB_errno = _PDCLIB_EUNKNOWN;
+ /* This should probably be something like EUNKNOWN. */
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
}
return EOF;
{
switch ( errno )
{
- /* These are the values possible on a Linux machine. Adapt the
- values and their mapping to PDCLib errno values at will. (This
- is an example implementation, so we keep it very simple.)
+ /* See the comments on implementation-defined errno values in
+ <_PDCLIB_config.h>.
*/
case EACCES:
case EFAULT:
case ENOTDIR:
case EPERM:
case EROFS:
- _PDCLIB_errno = _PDCLIB_EIO;
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
default:
- _PDCLIB_errno = _PDCLIB_EUNKNOWN;
+ /* This should be something like EUNKNOWN. */
+ _PDCLIB_errno = _PDCLIB_ERROR;
break;
}
}