0

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); } 
3
  • Show some minimal reproducible example please. Commented Dec 4, 2018 at 19:48
  • 1
    Why copy errno values to what looks like an enum or another set of macros representing integral values? Commented Dec 4, 2018 at 19:50
  • 1
    @GovindParmar I can think of 2 useful reason: grouping of errors as done above with EACCES, EPERM into NO_ACCESS_PERMISSION and allowing a desired sequent index or bit mask of error identifiers. Commented Dec 4, 2018 at 20:04

2 Answers 2

3

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 ... 
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

"you forgot to #include <errno.h>" - You are absolutely right!!! I've added it and now compiler is quiet. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.