Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm writing a program for Systems Programming in Unix, and one of the requirements is to process all possible error returns from system calls.

So, rather than having a function tailored to each system call, I'd like to have one function take care of that responsibility. So, are all error number returns unique? If not, what areas of overlap exist?

share|improve this question

3 Answers 3

There are two aspects: the ways system calls signal that an error occurred, and the way what error occurred is reported.

Most system calls signal that an error occurred by returning -1, but this is not completely universal (for example, some system calls are always successful, e.g. getpid).

If you know an error occurred, the error code is always in errno¹. There are standard values defined in errno.h, and every unix variants adds a few of its own. Error codes are known by constants whose name begins with E; the numeric values vary from OS to OS. These error codes are standard (e.g. EACCESS always means “permission denied”, EIO always means “input/output error”, …), but what precisely each error message means depends on the system call.

The standard functions strerror and perror provide error messages that you can display to a user.

¹ Note that if no error occurred during the last system call or C library function call, errno may contain garbage.

share|improve this answer

The only overlaps I'm aware of are synonyms in areas that historically have differed between AT&T and BSD-derived Unixes. For example, AT&T Unix's EAGAIN means the same thing as BSD's EWOULDBLOCK, so they have the same value on systems that define both.

share|improve this answer

No, there should be no overlaps in errno.h on a given system. Check your errno.h (most likely somewhere under /usr/include) for defines starting with E, as in ENOENT and such, and make a switch() statement handling each case. Then you can call your own function for all system call errors.

(Sounds a lot like you're implementing perror(3).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.