views:

242

answers:

6

In the book "The C Programming Language" it says:

"Many of the functions in the library set status indicators when error or end of file occur. These indicators may be set and tested explicitly. In addition, the integer expression errno (declared in <errno.h>) may contain an error number that gives further information about the most recent error."

Where can I see a list of these functions?

+4  A: 

You should assume that any function can set errno, not just those in the standard library. A list would therefore be a little pointless.

anon
I disagree, though life would certainly be easier your way! I'm currently struggling with some embedded libraries that return messages like "An error occurred: errno = 0" So as the OP intimates, if you'll pardon the irresistible pun on your name, it'd be helpful to be able to filter out those functions that don't set errno to helpful values.
Adam Liss
@Adam What pun would that be? I assume I'm missing something obvious and rude?
anon
The question wasn't about any function -- it was about those in the standard library. The standard documents the functions in the library that modify errno. What's pointless is treating functions guaranteed not to modify errno as if they might.
Dingo
@Dingo Did you read Michael Burr's answer?
anon
@Neil: A Butterworth Filter is a classic electronic circuit that's taught to EE undergrads as part of their mathematical hazing. Sorry to disappoint if you expected more rudeness. :-)@Dingo: Agree that these functions are documented and (we hope) follow standards. My point was that I've never come across a Canonical List of Functions that Set errno to Useful Values.
Adam Liss
@Adam Yeah, I knew about the Butterworth filter - I was hoping for something more (or indeed at all) rude :-(
anon
A: 

A proper question might be what are the values errno can get and what each of them means. You can see them listed in intro(2).

A: 

You can use your favorite editor and the "Find in files..." to search for files that contain the errno keyword.

Nick D
And bold fonts take up more space on disk?
Rob Wells
I don't understand you. Anyway, I meant to search library files (*.c, *.cpp).
Nick D
+1  A: 

Nearly all posix library functions can set errno if an error occurs, that is when the function returns -1. An exception are threading functions because setting one global error variable from multiple threads would be very dangerous. They return 0 on success, the errorcode otherwise (This code is compatible with errno so you can use the strerror and perror functions on it).

codymanix
Is there really a POSIX implementation that has threads but doesn't have a thread-local errno?
bk1e
`errno` is specified to be thread-specific. -1 for misinformation on this matter.
R..
I never said that errno isn't thread specific. The threading functions do not set errno anyway.
codymanix
+5  A: 

The standard says this about errno:

The value of errno is zero at program startup, but is never set to zero by any library function. The value of errno may be set to nonzero by a library function call whether or not there is an error, provided the use of errno is not documented in the description of the function in this International Standard.

Which says to me that any library function can screw around with errno in any way it likes except:

  • it can't set errno to 0
  • it can't do what it likes if the standard explicitly says otherwise

Note that the standard suggests the following in a footnote:

Thus, a program that uses errno for error checking should set it to zero before a library function call, then inspect it before a subsequent library function call. Of course, a library function can save the value of errno on entry and then set it to zero, as long as the original value is restored if errno's value is still zero just before the return.

As noted in other answers, it's common for functions that are not in the standard to set errno as well.

Michael Burr
+1  A: 

@Adam, as Neil said, any function can be compiled against errno.h and set errno. It is by definition, impossible to list all utilities that have been compiled in this way to use the core errno functionality.

That said, there are several ways that errors may be reported back to a user. Using errno is just one.

Rob Wells