Reporting on errno Values

When an error occurs, it is simple for the program to test for a specific case and act upon it. The problem becomes more complex when all you want to do is report the error to the user. Users do not like to memorize error codes, so a method must exist to translate an errno code into a readable message.

Meaningful error messages can be reported by a UNIX program to the user, in the following ways:

  • Use the perror(3) function to generate a message from the errno value and report it to stderr.

  • Use the provided sys_errlist[] array of messages (on FreeBSD this is described by the man page strerror(3)).

  • Use the strerror(3) function to return a message for the error code provided in the function argument.

Using the perror(3)

Get Advanced UNIX Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.