4

strerror() function returns a short error description, given error number as argument. For example, if the argument is ENOTDIR, it will return "Not a directory", if the argument is EBUSY, it will return "Device or resource busy", etc.

But, is there a function that returns "ENOTDIR" for argument equals ENOTDIR, "EBUSY" for argument EBUSY, etc.?

I just want to avoid writing a huge switch statement for this purpose.

VividD
  • 10,456
  • 6
  • 64
  • 111
  • 1
    [errno](http://www.cplusplus.com/reference/cerrno/errno/) returns last error number. It's not exactly what you asked for, but you can always make an enum and convert it to strings later, [there are a couple ways to do that](http://stackoverflow.com/questions/5093460/how-to-convert-an-enum-type-variable-to-a-string). – Jezor Sep 14 '16 at 00:14
  • This sounds like a bad idea. Functions such as strerror() perror() are used specifically to provide helpful error messages. Why would you opt to print the enumerated name rather than the associated error message? – David Sep 14 '16 at 00:16
  • 1
    @David I am writing a tool used by programmers, and it is desireable to print "EFAULT", "EBUSY", "ENOTSOCK", etc., instead of some text. The programmers say it is more useful for them. I don't argue about that with programmers, especially if I am a programmer. ;) – VividD Sep 14 '16 at 00:20
  • Why a switch statement? I'd use an array of strings for this. – Ignacio Vazquez-Abrams Sep 14 '16 at 00:27
  • The values of `errno` are implementation defined, and only about three macros (`EDOM`, `ERANGE`, `EILSEQ`) are actually defined in the standard. Values like `ENOTDIR`, `EBUSY` are not standard. Which basically means, if you don't want the results from `strerror()` or `perror()`, you need to roll your "huge switch statement" (where, presumably, your definition of "huge" is something like "greater than 10 cases") – Peter Sep 14 '16 at 00:29
  • EAGAIN and EWOULDBLOCK may have the same value making a unique string impossible without additional context. All the others have distinct values so a switch statement would work. @IgnacioVazquez-Abrams, are the defines guaranteed to be valid array indices? – Robert Prévost Sep 14 '16 at 00:30
  • @RobertPrévost: I don't know if they're *guaranteed*, but I'd think that they would be as consecutive as reasonably possible. – Ignacio Vazquez-Abrams Sep 14 '16 at 00:32
  • 1
    @Peter How huge? On Linux there are about 130-140 defined errno values. – David Sep 14 '16 at 00:59

2 Answers2

3

No- there is no standard or commonly used nonstandard function that provides this functionality.

One approach would be to write a huge switch statement, but this might not be the best approach for you to take. Most values of errno are not specified by any standard, so their values may or may not be consistent across different operating systems or even different versions of the same operating system.

Plus it would be a pain in the rear end.

A more elegant approach, if some runtime overhead is acceptable, would be to write a function that looks up these errors codes when they occur, rather than hard-coding the values into a big table. GNU/Linux systems have a list of all possible errno values at:

/usr/include/asm-generic/errno-base.h
/usr/include/asm-generic/errno.h

These files provide a #define of each errno value along with their value and a short description in an adjacent comment. It'd be pretty trivial to search these files line-by-line and print out the matching error code. Even if not, these files would be the things to start with in your quest to write a huge switch statement.

Beware that the kernel might negate these values when they're passed to userspace.

David
  • 1,624
  • 11
  • 13
2

As David points out above, the problem is there is no standard function that can provide the desired functionality. So thinking that this would be a neat problem to try to write a script for, I wrote a little something to automatically generate the switch function (if it should come to be necessary) and posted the code here. Seems to work alright on OS X, otherwise mileage may vary. A script such as this could be added to the build process to make sure that the values were defined correctly.

Robert Prévost
  • 1,667
  • 16
  • 22