// fileutil.h // PUBLIC DOMAIN // http:///www.purposeful.co.uk/software/yabaplib /* I, Tom Vajzovic, am the author of this software and its documentation. I permanently abandon all intellectual property rights in them, including copyright, trademarks, design rights, database right, patents, and the right to be identified as the author. I am fairly certain that the software does what the documentation says it does, but I do not guarantee that it does, or that it does what you think it should. I do not guarantee that it will not have undesirable side effects. You are free to use, modify and distribute this software as you please, but you do so at your own risk. If you do not pass on this warning then you may be responsible for any problems encountered by those who obtain the software through you. */ #ifndef FILEUTIL_H_INCLUDED #define FILEUTIL_H_INCLUDED #include #include #ifdef __cplusplus extern "C" { #endif /****************************************************************************/ /* */ /* Close a file descriptor without changing errno on success or failure. */ /* */ /****************************************************************************/ void close_preserving_errno(int fd); /****************************************************************************/ /* */ /* Seek to an absolute offset in a file, return zero on success. */ /* */ /****************************************************************************/ int lseek_set(int fd, off_t offset); /****************************************************************************/ /* */ /* These functions return a single datum from stat(2) */ /* */ /****************************************************************************/ off_t fd_filesize (int fd); size_t fd_blocksize (int fd); /****************************************************************************/ /* */ /* These functions set a single flag using fcntl(2) */ /* */ /****************************************************************************/ int fd_blocking (int fd); int fd_nonblocking (int fd); int fd_cloexec (int fd); /****************************************************************************/ /* */ /* copy_fd makes the file previously available on descriptor old_fd also */ /* available on the descriptor new_fd. */ /* */ /* move_fd makes the file previously available on descriptor old_fd only */ /* available on the descriptor new_fd. */ /* */ /* These functions return zero on success, non-zero on error. */ /* */ /* if old_fd equals new_fd then copy_fd fails with errno set to EINVAL. */ /* */ /* if old_fd equals new_fd then move_fd does nothing and returns success. */ /* */ /* if move_fd fails, old_fd is definitely closed, no new file descriptor */ /* is opened, and any file descriptor previously on new_fd may be closed. */ /* */ /****************************************************************************/ int copy_fd (int old_fd, int new_fd); int move_fd (int old_fd, int new_fd); /****************************************************************************/ /* */ /* null_input opens /dev/null for reading on STDIN_FILENO */ /* */ /* null_output opens /dev/null for writing on STDOUT_FILENO */ /* */ /****************************************************************************/ int null_input (void); int null_output (void); /******************************************************************************/ /* */ /* These functions take the same arguments as read() or write(). They call */ /* read() or write() repeatedly until it returns zero (end of file) or less */ /* than zero (error) or until the full number of bytes have been transferred. */ /* */ /* If read() or write() fail with errno set to EINTR they repeat the call. */ /* */ /* If read() or write() fail with EAGAIN or EWOULDBLOCK and the descriptor is */ /* non-blocking then they set it to blocking and try again. If they do this */ /* and are not going to close the file descriptor then they restore the flags */ /* before returning. */ /* */ /* On any other error they keep errno set to the first error encountered and */ /* return failure. */ /* */ /* read_blocking() and write_blocking() return the total number of bytes */ /* transferred or negative on failure, the same as read() or write(). */ /* */ /* read_all() and write_all() return zero if the full number of bytes was */ /* transferred or non-zero with errno set to EAGAIN otherwise. */ /* */ /* read_all_or_close() and write_all_or_close() are the same as read_all() */ /* and write_all() except that they close the file descriptor on failure. */ /* */ /* read_blocking_close(), write_blocking_close(), read_all_close() and */ /* write_all_close() are the same as read_blocking(), write_blocking(), */ /* read_all() and write_all() respectively, except that they always close */ /* the file descriptor. */ /* */ /* If write_blocking_close() or write_all_close() call write() successfully */ /* for the full amount of data but then close() fails, then they return */ /* failure with errno kept at what close() set it to. */ /* */ /* read_blocking_close() and read_all_close() do not check the return value */ /* of close(). */ /* */ /******************************************************************************/ ssize_t read_blocking (int fd, void *buf, size_t count); ssize_t write_blocking (int fd, const void *buf, size_t count); ssize_t read_blocking_close (int fd, void *buf, size_t count); ssize_t write_blocking_close (int fd, const void *buf, size_t count); int read_all (int fd, void *buf, size_t count); int write_all (int fd, const void *buf, size_t count); int read_all_or_close (int fd, void *buf, size_t count); int write_all_or_close (int fd, const void *buf, size_t count); int read_all_close (int fd, void *buf, size_t count); int write_all_close (int fd, const void *buf, size_t count); #ifdef __cplusplus } #endif #endif /* FILEUTIL_H_INCLUDED */