11 #include "gcc-compat.h"
16 * Call a function for each subdirectory of the current working directory.
18 * \param dirname The directory to traverse.
19 * \param func The function to call for each subdirecrtory.
20 * \param private_data Pointer to an arbitrary data structure.
22 * For each top-level directory under \a dirname, the supplied function \a func is
23 * called. The full path of the subdirectory and the \a private_data pointer
24 * are passed to \a func.
26 * \return This function returns immediately if \a func returned a negative
27 * value. In this case \a func must set error_txt and this negative value is
28 * returned to the caller. Otherwise the function returns when all
29 * subdirectories have been passed to \a func.
32 int for_each_subdir(int (*func)(const char *, void *), void *private_data)
36 DIR *dir = opendir(".");
39 ret = -ERRNO_TO_DSS_ERROR(errno);
40 make_err_msg("opendir(\".\") failed");
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);
54 make_err_msg("lstat(\"%s\") failed", entry->d_name);
60 ret = func(entry->d_name, private_data);
70 * Wrapper for chdir(2).
72 * \param path The specified directory.
76 int dss_chdir(const char *path)
78 int ret = chdir(path);
82 ret = -ERRNO_TO_DSS_ERROR(errno);
83 make_err_msg("chdir to %s failed", path);
88 * Set a file descriptor to non-blocking mode.
90 * \param fd The file descriptor.
94 __must_check int mark_fd_nonblocking(int fd)
96 int flags = fcntl(fd, F_GETFL);
98 return -ERRNO_TO_DSS_ERROR(errno);
99 flags = fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK);
101 return -ERRNO_TO_DSS_ERROR(errno);
106 * dss' wrapper for select(2).
108 * It calls select(2) (with no exceptfds) and starts over if select() was
109 * interrupted by a signal.
111 * \param n The highest-numbered descriptor in any of the two sets, plus 1.
112 * \param readfds fds that should be checked for readability.
113 * \param writefds fds that should be checked for writablility.
114 * \param timeout_tv upper bound on the amount of time elapsed before select()
117 * \return The return value of the underlying select() call on success, the
118 * negative system error code on errors.
120 * All arguments are passed verbatim to select(2).
121 * \sa select(2) select_tut(2).
123 int dss_select(int n, fd_set *readfds, fd_set *writefds,
124 struct timeval *timeout_tv)
128 ret = select(n, readfds, writefds, NULL, timeout_tv);
130 } while (ret < 0 && err == EINTR);
132 return -ERRNO_TO_DSS_ERROR(errno);