S
Stephan Beal
Hello, all!
If this question seems off topic, please feel free to ignore it.
i'm busy porting some code which currently uses the POSIX file API
(e.g. fopen(), fclose(), fwrite(), etc.) to use the lower-level APIs
based on file descriptors. The original reason for porting is so that
i can add locking via fcntl() (and the literature suggests against
mixing the low-level and (FILE*) APIs), but tests have also shown the
port to provide a very substantial performance increase for my
particular use case.
i've run into a bit of a problem, though...
i can't seem to find lower-level equivalents for functions like feof
(), ferror(), and fclearerr(). Do such a things exist for the lower-
level API (i.e. requiring a file descriptor instead of a (FILE*)? If
not, is there a way to simulate them? For example, here's my first
attempt at simulating feof(), but i honestly have no clue if this
approach is valid using the lower-level API (seems reasonable/naive
enough, though):
bool iseof( int fileno )
{
off_t p1 = lseek( fileno, 0L, SEEK_CUR );
off_t p2 = lseek( fileno, 0L, SEEK_END );
bool rc = (p1 >= p2);
lseek( fileno, p1, SEEK_SET );
return rc;
}
The routines i'm looking for replacements for (or looking to simulate)
are:
- feof()
- ferror()
- clearerr()
all the other operations of the i/o device interface/API i'm working
inside of can be implemented directly using the lower-level operations
(e.g. read(), write(), ftruncate(), fdatasync()/fsync(), lseek()).
Any pointers in the right (or *a* right) direction would be much
appreciated.
If this question seems off topic, please feel free to ignore it.
i'm busy porting some code which currently uses the POSIX file API
(e.g. fopen(), fclose(), fwrite(), etc.) to use the lower-level APIs
based on file descriptors. The original reason for porting is so that
i can add locking via fcntl() (and the literature suggests against
mixing the low-level and (FILE*) APIs), but tests have also shown the
port to provide a very substantial performance increase for my
particular use case.
i've run into a bit of a problem, though...
i can't seem to find lower-level equivalents for functions like feof
(), ferror(), and fclearerr(). Do such a things exist for the lower-
level API (i.e. requiring a file descriptor instead of a (FILE*)? If
not, is there a way to simulate them? For example, here's my first
attempt at simulating feof(), but i honestly have no clue if this
approach is valid using the lower-level API (seems reasonable/naive
enough, though):
bool iseof( int fileno )
{
off_t p1 = lseek( fileno, 0L, SEEK_CUR );
off_t p2 = lseek( fileno, 0L, SEEK_END );
bool rc = (p1 >= p2);
lseek( fileno, p1, SEEK_SET );
return rc;
}
The routines i'm looking for replacements for (or looking to simulate)
are:
- feof()
- ferror()
- clearerr()
all the other operations of the i/o device interface/API i'm working
inside of can be implemented directly using the lower-level operations
(e.g. read(), write(), ftruncate(), fdatasync()/fsync(), lseek()).
Any pointers in the right (or *a* right) direction would be much
appreciated.