diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/code-from-errno.c | 27 | ||||
-rw-r--r-- | src/gpg-error.h.in | 16 | ||||
-rw-r--r-- | src/strerror.c | 6 |
3 files changed, 44 insertions, 5 deletions
diff --git a/src/code-from-errno.c b/src/code-from-errno.c index d431ef1..96fcf20 100644 --- a/src/code-from-errno.c +++ b/src/code-from-errno.c @@ -22,6 +22,8 @@ #include <config.h> #endif +#include <errno.h> + #include <gpg-error.h> #include "code-from-errno.h" @@ -32,11 +34,34 @@ gpg_err_code_t gpg_err_code_from_errno (int err) { - int idx = errno_to_idx (err); + int idx; if (!err) return GPG_ERR_NO_ERROR; + idx = errno_to_idx (err); + + if (idx < 0) + return GPG_ERR_UNKNOWN_ERRNO; + + return GPG_ERR_SYSTEM_ERROR | err_code_from_index[idx]; +} + + +/* Retrieve the error code directly from the ERRNO variable. This + returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped + (report this) and GPG_ERR_MISSING_ERRNO if ERRNO has the value 0. */ +gpg_err_code_t +gpg_err_code_from_syserror (void) +{ + int err = errno; + int idx; + + if (!err) + return GPG_ERR_MISSING_ERRNO; + + idx = errno_to_idx (err); + if (idx < 0) return GPG_ERR_UNKNOWN_ERRNO; diff --git a/src/gpg-error.h.in b/src/gpg-error.h.in index 7c372ce..46325c5 100644 --- a/src/gpg-error.h.in +++ b/src/gpg-error.h.in @@ -212,7 +212,7 @@ const char *gpg_strsource (gpg_error_t err); /* Retrieve the error code for the system error ERR. This returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report - this). */ + this). */ gpg_err_code_t gpg_err_code_from_errno (int err); @@ -220,6 +220,14 @@ gpg_err_code_t gpg_err_code_from_errno (int err); if CODE is not a system error code. */ int gpg_err_code_to_errno (gpg_err_code_t code); + +/* Retrieve the error code directly from the ERRNO variable. This + returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped + (report this) and GPG_ERR_MISSING_ERRNO if ERRNO has the value 0. */ +gpg_err_code_t gpg_err_code_from_syserror (void); + + + /* Self-documenting convenience functions. */ @@ -236,6 +244,12 @@ gpg_error_from_errno (int err) return gpg_error (gpg_err_code_from_errno (err)); } +static GPG_ERR_INLINE gpg_error_t +gpg_error_from_syserror (void) +{ + return gpg_error (gpg_err_code_from_syserror ()); +} + #ifdef __cplusplus } #endif diff --git a/src/strerror.c b/src/strerror.c index 410c4ab..dc56040 100644 --- a/src/strerror.c +++ b/src/strerror.c @@ -61,7 +61,7 @@ gpg_strerror (gpg_error_t err) to a resource shortage, set *STR to NULL and return 1. If this call fails because the error number is not valid, don't set *STR and return 0. */ -int +static int system_strerror_r (int no, char *buf, size_t buflen) { char *errstr; @@ -89,7 +89,7 @@ system_strerror_r (int no, char *buf, size_t buflen) #else /* STRERROR_R_CHAR_P */ /* Now the POSIX version. */ -int +static int system_strerror_r (int no, char *buf, size_t buflen) { return strerror_r (no, buf, buflen); @@ -102,7 +102,7 @@ system_strerror_r (int no, char *buf, size_t buflen) version. Maybe we are even lucky and the system's strerror() is already thread-safe. */ -int +static int system_strerror_r (int no, char *buf, size_t buflen) { char *errstr = strerror (no); |