1 /* SPDX-License-Identifier: GPL-2.0 */
13 #include "gcc-compat.h"
19 * Call a function for each subdirectory of the current working directory.
21 * \param dirname The directory to traverse.
22 * \param func The function to call for each subdirectory.
23 * \param private_data Pointer to an arbitrary data structure.
25 * For each top-level directory under \a dirname, the supplied function \a func is
26 * called. The full path of the subdirectory and the \a private_data pointer
27 * are passed to \a func.
29 * \return This function returns immediately if \a func returned a negative
30 * value. In this case \a func must set error_txt and this negative value is
31 * returned to the caller. Otherwise the function returns when all
32 * subdirectories have been passed to \a func.
35 int for_each_subdir(int (*func)(const char *, void *), void *private_data)
39 DIR *dir = opendir(".");
42 return -ERRNO_TO_DSS_ERROR(errno);
43 while ((entry = readdir(dir))) {
47 if (!strcmp(entry->d_name, "."))
49 if (!strcmp(entry->d_name, ".."))
51 ret = lstat(entry->d_name, &s) == -1;
53 ret = -ERRNO_TO_DSS_ERROR(errno);
59 ret = func(entry->d_name, private_data);
70 * Set a file descriptor to non-blocking mode.
72 * \param fd The file descriptor.
76 __must_check int mark_fd_nonblocking(int fd)
78 int flags = fcntl(fd, F_GETFL);
80 return -ERRNO_TO_DSS_ERROR(errno);
81 flags = fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK);
83 return -ERRNO_TO_DSS_ERROR(errno);
88 * dss' wrapper for select(2).
90 * It calls select(2) (with no exceptfds) and starts over if select() was
91 * interrupted by a signal.
93 * \param n The highest-numbered descriptor in any of the two sets, plus 1.
94 * \param readfds fds that should be checked for readability.
95 * \param writefds fds that should be checked for writablility.
96 * \param timeout_tv upper bound on the amount of time elapsed before select()
99 * \return The return value of the underlying select() call on success, the
100 * negative system error code on errors.
102 * All arguments are passed verbatim to select(2).
103 * \sa select(2) select_tut(2).
105 int dss_select(int n, fd_set *readfds, fd_set *writefds,
106 struct timeval *timeout_tv)
110 ret = select(n, readfds, writefds, NULL, timeout_tv);
112 } while (ret < 0 && err == EINTR);
114 return -ERRNO_TO_DSS_ERROR(errno);