Ann
Ann

Reputation: 27

C wrapper to errno

I've written the function to wrap an errno, but I have compile error: error: ‘EACCES’ undeclared (first use in this function) case EACCES: What I'm doing wrong? How can I wrap the errno with switch-case? status_t defined as enum of relevant errors.

static status_t GetErrorStatus (int errno_value)
    {
        status_t err_status = COMMON_ERROR;

        switch (errno_value) 
        {
            case EACCES: 
                err_status = NO_ACCESS_PERMISSION;
                break;
            case EPERM:
                err_status = NO_ACCESS_PERMISSION;
                break;
            case EIDRM:
                err_status = SEMAPHORE_REMOVED;
                break;
            case ENOENT:
                err_status = FILE_DOESNT_EXIST;
                break;
            case EEXIST:
                err_status = SEMAPHORE_ALREADY_EXISTS;
                break;

            default: err_status = COMMON_ERROR;
        }
        return (err_status);
    }

Upvotes: 0

Views: 766

Answers (2)

chux
chux

Reputation: 153468

How can I wrap the errno with switch-case?

Not all platforms support the various errors. C specifies only 3: EDOM EILSEQ ERANGE in <errno.h> and, importantly, they are macros. I'd expect additional platform specific errors to also be so testable.

    switch (errno_value) 
    {
        #ifdef EACCES
        case EACCES: 
            err_status = NO_ACCESS_PERMISSION;
            break;
        #endif
        #ifdef EPERM
        case EPERM:
            err_status = NO_ACCESS_PERMISSION;
            break;
        #endif
        ...

Upvotes: 3

Show a complete code.

My guess is that you forgot to #include <errno.h> or that on your particular system EACCESS is not defined.

On Linux, read errno(3). EACCESS is mentioned as POSIX, so on some non-POSIX systems it might not be defined.

The C11 standard n1570 mentions errno in its §7.5 and EACCESS is not listed there. If it exists, it should be a macro, so you might wrap some appropriate part of your code with #ifdef EACCESS ... #endif

Upvotes: 1

Related Questions