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);