+ if (!ferror(f))
+ return -E_FGETS;
+ if (errno != EINTR) {
+ PARA_ERROR_LOG("%s\n", strerror(errno));
+ return -E_FGETS;
+ }
+ clearerr(f);
+ goto again;
+}
+
+/**
+ * Paraslash's wrapper for mmap.
+ *
+ * \param length Number of bytes to mmap.
+ * \param prot Either PROT_NONE or the bitwise OR of one or more of
+ * PROT_EXEC PROT_READ PROT_WRITE.
+ * \param flags Exactly one of MAP_SHARED and MAP_PRIVATE.
+ * \param fd The file to mmap from.
+ * \param offset Mmap start.
+ * \param map Result pointer.
+ *
+ * \return Standard.
+ *
+ * \sa mmap(2).
+ */
+int para_mmap(size_t length, int prot, int flags, int fd, off_t offset,
+ void *map)
+{
+ void **m = map;
+
+ errno = EINVAL;
+ if (!length)
+ goto err;
+ *m = mmap(NULL, length, prot, flags, fd, offset);
+ if (*m != MAP_FAILED)
+ return 1;
+err:
+ *m = NULL;
+ return -ERRNO_TO_PARA_ERROR(errno);
+}
+
+/**
+ * Wrapper for the open(2) system call.
+ *
+ * \param path The filename.
+ * \param flags The usual open(2) flags.
+ * \param mode Specifies the permissions to use.
+ *
+ * The mode parameter must be specified when O_CREAT is in the flags, and is
+ * ignored otherwise.
+ *
+ * \return The file descriptor on success, negative on errors.
+ *
+ * \sa open(2).
+ */
+int para_open(const char *path, int flags, mode_t mode)
+{
+ int ret = open(path, flags, mode);
+
+ if (ret >= 0)
+ return ret;
+ return -ERRNO_TO_PARA_ERROR(errno);
+}
+
+/**
+ * Wrapper for chdir(2).
+ *
+ * \param path The specified directory.
+ *
+ * \return Standard.
+ */
+int para_chdir(const char *path)
+{
+ int ret = chdir(path);
+
+ if (ret >= 0)
+ return 1;
+ return -ERRNO_TO_PARA_ERROR(errno);
+}
+
+/**
+ * Save the cwd and open a given directory.
+ *
+ * \param dirname Path to the directory to open.
+ * \param dir Result pointer.
+ * \param cwd File descriptor of the current working directory.
+ *
+ * \return Standard.
+ *
+ * Opening the current directory (".") and calling fchdir() to return is
+ * usually faster and more reliable than saving cwd in some buffer and calling
+ * chdir() afterwards.
+ *
+ * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
+ * stored in \a cwd. If the function returns success, and \a cwd is not \p
+ * NULL, the caller must close this file descriptor (probably after calling
+ * fchdir(*cwd)).
+ *
+ * On errors, the function undos everything, so the caller needs neither close
+ * any files, nor change back to the original working directory.
+ *
+ * \sa getcwd(3).
+ *
+ */
+int para_opendir(const char *dirname, DIR **dir, int *cwd)
+{
+ int ret;
+
+ if (cwd) {
+ ret = para_open(".", O_RDONLY, 0);
+ if (ret < 0)
+ return ret;
+ *cwd = ret;
+ }
+ ret = para_chdir(dirname);
+ if (ret < 0)
+ goto close_cwd;
+ *dir = opendir(".");
+ if (*dir)
+ return 1;
+ ret = -ERRNO_TO_PARA_ERROR(errno);
+ /* Ignore return value of fchdir() and close(). We're busted anyway. */
+ if (cwd) {
+ int __a_unused ret2 = fchdir(*cwd); /* STFU, gcc */
+ }
+close_cwd:
+ if (cwd)
+ close(*cwd);
+ return ret;
+}
+
+/**
+ * A wrapper for fchdir().
+ *
+ * \param fd An open file descriptor.
+ *
+ * \return Standard.
+ */
+int para_fchdir(int fd)
+{
+ if (fchdir(fd) < 0)
+ return -ERRNO_TO_PARA_ERROR(errno);
+ return 1;
+}
+
+/**
+ * A wrapper for mkdir(2).
+ *
+ * \param path Name of the directory to create.
+ * \param mode The permissions to use.
+ *
+ * \return Standard.
+ */
+int para_mkdir(const char *path, mode_t mode)
+{
+ if (!mkdir(path, mode))
+ return 1;
+ return -ERRNO_TO_PARA_ERROR(errno);
+}
+
+/**
+ * Open a file and map it into memory.
+ *
+ * \param path Name of the regular file to map.
+ * \param open_mode Either \p O_RDONLY or \p O_RDWR.
+ * \param map On success, the mapping is returned here.
+ * \param size size of the mapping.
+ * \param fd_ptr The file descriptor of the mapping.
+ *
+ * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
+ * open call is closed after mmap(). Otherwise the file is kept open and the
+ * file descriptor is returned in \a fd_ptr.
+ *
+ * \return Standard.
+ *
+ * \sa para_open(), mmap(2).
+ */
+int mmap_full_file(const char *path, int open_mode, void **map,
+ size_t *size, int *fd_ptr)
+{
+ int fd, ret, mmap_prot, mmap_flags;
+ struct stat file_status;
+
+ if (open_mode == O_RDONLY) {
+ mmap_prot = PROT_READ;
+ mmap_flags = MAP_PRIVATE;
+ } else {
+ mmap_prot = PROT_READ | PROT_WRITE;
+ mmap_flags = MAP_SHARED;
+ }
+ ret = para_open(path, open_mode, 0);
+ if (ret < 0)
+ return ret;
+ fd = ret;
+ if (fstat(fd, &file_status) < 0) {
+ ret = -ERRNO_TO_PARA_ERROR(errno);
+ goto out;
+ }
+ *size = file_status.st_size;
+ ret = para_mmap(*size, mmap_prot, mmap_flags, fd, 0, map);
+out:
+ if (ret < 0 || !fd_ptr)
+ close(fd);
+ else
+ *fd_ptr = fd;
+ return ret;
+}
+
+/**
+ * A wrapper for munmap(2).
+ *
+ * \param start The start address of the memory mapping.
+ * \param length The size of the mapping.
+ *
+ * \return Positive on success, \p -E_MUNMAP on errors.
+ *
+ * \sa munmap(2), mmap_full_file().
+ */
+int para_munmap(void *start, size_t length)
+{
+ int err;
+ if (munmap(start, length) >= 0)
+ return 1;
+ err = errno;
+ PARA_ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
+ strerror(err));
+ return -ERRNO_TO_PARA_ERROR(err);
+}
+
+/**
+ * Check a file descriptor for writability.
+ *
+ * \param fd The file descriptor.
+ *
+ * \return positive if fd is ready for writing, zero if it isn't, negative if
+ * an error occurred.
+ */
+
+int write_ok(int fd)
+{
+ struct timeval tv = {0, 0};
+ fd_set wfds;
+ int ret;
+again:
+ FD_ZERO(&wfds);
+ FD_SET(fd, &wfds);
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ ret = select(fd + 1, NULL, &wfds, NULL, &tv);
+ if (ret < 0 && errno == EINTR)
+ goto again;
+ return ret;
+}
+
+/**
+ * Ensure that file descriptors 0, 1, and 2 are valid.
+ *
+ * Common approach that opens /dev/null until it gets a file descriptor greater
+ * than two.
+ *
+ * \sa okir's Black Hats Manual.
+ */
+void valid_fd_012(void)
+{
+ while (1) {
+ int fd = open("/dev/null", O_RDWR);
+ if (fd < 0)
+ exit(EXIT_FAILURE);
+ if (fd > 2) {
+ close(fd);
+ break;
+ }
+ }