setting errno

Colin Peters colin@fu.is.saga-u.ac.jp
Sun Jan 31 23:52:00 GMT 1999


Tim Taylor <ttaylor@mitre.org> wrote:

>I'm porting a program that sets errno if an operation fails.  However, in
>the B20.1 errno is a macro that dereferences the pointer returned by a
>function #define errno (*__errno())
>
>What is the proper way to set errno in cygwin?
>
>Based on comments I've read in the headers, it appears that the following
>will work:
>
>_REENT->_errno = xxx
>
>Is this correct, or is there a more compatible or preferred way of doing
this?


In general setting errno as usual should work with one caveat. You *must*
include errno.h and not define errno as an extern int or some such (as I
have seen done in some programs). Then your code

errno = xxx;

Will be expanded to the preprocessor to

(*__errno()) = xxx;

Which is perfectly fine as long as the __errno function does what it should
do, that is, return a pointer to the 'real' shared errno variable.

Note: the reason you can't do it (in general) the way it is done in UNIX is
because the C library functions inside the DLL need to access errno, so
errno needs to be inside the DLL, and the way DLLs work you can't access
anything in them except through a pointer stored in an internal table. This
means that function calls to DLLs or access to variables in DLLs actually
ends up in code in the form (*_imp__function_name)(...); or
(*_imp__variable_name) = x; where _imp__xxx is a symbol defined in the
import library to be the place in the import table where the address of
symbol xxx from the DLL  will go. The reason it is a function call and not
extern int __declspec(dllimport) _errno; is that, until recently, GNU gcc
didn't understand __declspec and assumed all symbols in a DLL were functions
(as they were in 16-bit Windows).

But, as long as you include errno.h and don't do things like having
structure members or class members called 'errno' you should be able to use
errno as if it was a normal variable, and this is the most portable way to
do it.

Colin.

- Colin Peters - colin at fu.is.saga-u.ac.jp
- http://www.geocities.com/Tokyo/Towers/6162/index.html
- Go not to usenet for counsel, for it will say both
- 'yes' and 'no' and 'try another newsgroup'.


-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".



More information about the Cygwin mailing list