// fileutil.c // 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. */ #include #include #include #include #include #include #include "fileutil.h" /****************************************************************************/ /* */ /* Close a file descriptor without changing errno on success or failure. */ /* */ /****************************************************************************/ void close_preserving_errno(int fd) { int save_errno = errno; (void)close(fd); errno = save_errno; } /****************************************************************************/ /* */ /* Seek to an absolute offset in a file, return zero on success. */ /* */ /****************************************************************************/ int lseek_set(int fd, off_t offset) { if (offset < 0) { errno = EINVAL; return -1; } off_t sought = lseek(fd, offset, SEEK_SET); if (sought < 0) { return -1; } if (sought != offset) { errno = ENXIO; return -1; } return 0; } /****************************************************************************/ /* */ /* These functions return a single datum from stat(2) */ /* */ /****************************************************************************/ off_t fd_filesize(int fd) { struct stat stat_buf; if (fstat(fd, &stat_buf) != 0) { return -1; } return stat_buf.st_size; } size_t fd_blocksize(int fd) { struct stat stat_buf; if (fstat(fd, &stat_buf) != 0) { return 0; } return stat_buf.st_blksize; } /****************************************************************************/ /* */ /* These functions set a single flag using fcntl(2) */ /* */ /****************************************************************************/ int fd_nonblocking(int fd) { int flags = fcntl(fd, F_GETFL); if (flags < 0) { return -1; } if ((flags & O_NONBLOCK) == O_NONBLOCK) { return 0; } return fcntl(fd, F_SETFL, (flags | O_NONBLOCK)); } int fd_blocking(int fd) { int flags = fcntl(fd, F_GETFL); if (flags < 0) { return -1; } if ((flags & O_NONBLOCK) == 0) { return 0; } return fcntl(fd, F_SETFL, (flags & ~O_NONBLOCK)); } int fd_cloexec(int fd) { int flags = fcntl(fd, F_GETFD); if (flags < 0) { return -1; } if ((flags & FD_CLOEXEC) == FD_CLOEXEC) { return 0; } return fcntl(fd, F_SETFD, (flags | FD_CLOEXEC)); } /****************************************************************************/ /* */ /* 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) { if (new_fd == old_fd) { errno = EINVAL; return -1; } int result = dup2(old_fd, new_fd); if (result < 0) { return -1; } if (result != new_fd) { (void)close(result); errno = ENOSYS; return -1; } return 0; } int move_fd(int old_fd, int new_fd) { if (new_fd == old_fd) { return 0; } if (copy_fd(old_fd, new_fd)) { close_preserving_errno(old_fd); return -1; } (void)close(old_fd); return 0; } /****************************************************************************/ /* */ /* null_input opens /dev/null for reading on STDIN_FILENO */ /* */ /* null_output opens /dev/null for writing on STDOUT_FILENO */ /* */ /****************************************************************************/ int null_input(void) { (void)close(STDIN_FILENO); int fd = open("/dev/null", O_RDONLY); if (fd < 0) { return -1; } return move_fd(fd, STDIN_FILENO); // probably a no-op } int null_output(void) { (void)close(STDOUT_FILENO); int fd = open("/dev/null", O_WRONLY); if (fd < 0) { return -1; } return move_fd(fd, STDOUT_FILENO); // probably a no-op } /******************************************************************************/ /* */ /* 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 according 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() would. */ /* */ /* 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_request) {{{ size_t count_done = 0; int flags = -1; while (count_done < count_request) { ssize_t result = read(fd, &((char*)buf)[count_done], (count_request - count_done)); if (result < 0) { int save_errno = errno; if (save_errno == EINTR) { continue; } if ((flags == -1) && ((save_errno == EAGAIN) || (save_errno == EWOULDBLOCK))) { flags = fcntl(fd, F_GETFL); if (flags < 0) { return -1; } if ((flags & O_NONBLOCK) == 0) { errno = save_errno; return -1; } if (fcntl(fd, F_SETFL, (flags & ~O_NONBLOCK))) { return -1; } continue; } if (flags != -1) { (void)fcntl(fd, F_SETFL, flags); } errno = save_errno; return -1; } if (result == 0) { break; } count_done += (size_t)result; } if (flags != -1) { if (fcntl(fd, F_SETFL, flags)) { return -1; } } return count_done; }}} ssize_t read_blocking_close(int fd, void *buf, size_t count_request) {{{ size_t count_done = 0; int flags = -1; while (count_done < count_request) { ssize_t result = read(fd, &((char*)buf)[count_done], (count_request - count_done)); if (result < 0) { int save_errno = errno; if (save_errno == EINTR) { continue; } if ((flags == -1) && ((save_errno == EAGAIN) || (save_errno == EWOULDBLOCK))) { flags = fcntl(fd, F_GETFL); if (flags < 0) { close_preserving_errno(fd); return -1; } if ((flags & O_NONBLOCK) == 0) { (void)close(fd); errno = save_errno; return -1; } if (fcntl(fd, F_SETFL, (flags & ~O_NONBLOCK))) { close_preserving_errno(fd); return -1; } continue; } (void)close(fd); errno = save_errno; return -1; } if (result == 0) { break; } count_done += (size_t)result; } // we have the complete data, don't discard it if a problem occurs after we got it (void)close(fd); return count_done; }}} int read_all(int fd, void *buf, size_t count_request) {{{ ssize_t result = read_blocking(fd, buf, count_request); if (result < 0) { return -1; } if ((size_t)result != count_request) { errno = EAGAIN; return -1; } return 0; }}} int read_all_or_close(int fd, void *buf, size_t count_request) {{{ ssize_t result = read_blocking(fd, buf, count_request); if (result < 0) { close_preserving_errno(fd); return -1; } if ((size_t)result != count_request) { if (close(fd) == 0) { // if close returns non-zero we have a "real" error, // if not then set EAGAIN to indicate unexpected EOF errno = EAGAIN; } return -1; } return 0; }}} int read_all_close(int fd, void *buf, size_t count_request) {{{ ssize_t result = read_blocking_close(fd, buf, count_request); if (result < 0) { return -1; } if ((size_t)result != count_request) { errno = EAGAIN; return -1; } return 0; }}} ssize_t write_blocking(int fd, const void *buf, size_t count_request) {{{ size_t count_done = 0; int flags = -1; while (count_done < count_request) { ssize_t result = write(fd, &((const char*)buf)[count_done], (count_request - count_done)); if (result < 0) { int save_errno = errno; if (save_errno == EINTR) { continue; } if ((flags == -1) && ((save_errno == EAGAIN) || (save_errno == EWOULDBLOCK))) { flags = fcntl(fd, F_GETFL); if (flags < 0) { return -1; } if ((flags & O_NONBLOCK) == 0) { errno = save_errno; return -1; } if (fcntl(fd, F_SETFL, (flags & ~O_NONBLOCK))) { return -1; } continue; } if (flags != -1) { (void)fcntl(fd, F_SETFL, flags); } errno = save_errno; return -1; } if (result == 0) { break; } count_done += (size_t)result; } if (flags != -1) { if (fcntl(fd, F_SETFL, flags)) { return -1; } } return count_done; }}} ssize_t write_blocking_close(int fd, const void *buf, size_t count_request) {{{ size_t count_done = 0; int flags = -1; while (count_done < count_request) { ssize_t result = write(fd, &((const char*)buf)[count_done], (count_request - count_done)); if (result < 0) { int save_errno = errno; if (save_errno == EINTR) { continue; } if ((flags == -1) && ((save_errno == EAGAIN) || (save_errno == EWOULDBLOCK))) { flags = fcntl(fd, F_GETFL); if (flags < 0) { close_preserving_errno(fd); return -1; } if ((flags & O_NONBLOCK) == 0) { (void)close(fd); errno = save_errno; return -1; } if (fcntl(fd, F_SETFL, (flags & ~O_NONBLOCK))) { close_preserving_errno(fd); return -1; } continue; } (void)close(fd); errno = save_errno; return -1; } if (result == 0) { break; } count_done += (size_t)result; } if (close(fd)) { return -1; } return count_done; }}} int write_all(int fd, const void *buf, size_t count_request) {{{ ssize_t result = write_blocking(fd, buf, count_request); if (result < 0) { return -1; } if ((size_t)result != count_request) { errno = EAGAIN; return -1; } return 0; }}} int write_all_or_close(int fd, const void *buf, size_t count_request) {{{ ssize_t result = write_blocking(fd, buf, count_request); if (result < 0) { close_preserving_errno(fd); return -1; } if ((size_t)result != count_request) { if (close(fd) == 0) { // if close returns non-zero we have a "real" error, // if not then set EAGAIN to indicate unexpected EOF errno = EAGAIN; } return -1; } return 0; }}} int write_all_close(int fd, const void *buf, size_t count_request) {{{ ssize_t result = write_blocking_close(fd, buf, count_request); if (result < 0) { return -1; } if ((size_t)result != count_request) { errno = EAGAIN; return -1; } return 0; }}}