views:

119

answers:

2

Is it possible that win32's _open() return valid FD that is negative ?

In other words, is comparison

 if( (fd=_open(...)) < 0) error...;

as safe as form

 if( (fd=_open(...)) == -1) error...;

?

I am asking because all msdn examples
are in form if(fd == -1 ), and never form if( fd < 0).

+1  A: 

No. Negative values are all errors. In fact, if you look at read(), it asserts the fd is between 0 and 31.

jeffamaphone
+1  A: 

Windows file descriptors are borrowed from MS-DOS 2.0 file handles, which were loosely based on Unix file descriptors, all of which are indices into the process's table of file control blocks. So a valid file handle must be non-negative.

Loadmaster
The HANDLE returned by CreateFile is an index into the process's table of kernel objects. The FDs returned by the VCs _open are indexes into a table maintained by the msv c-runtime.
Chris Becke
True, in MSC there is an extra level of indirection, i.e., a file descriptor is an index into a (process-specific) library table containing system file handles.
Loadmaster