2 * Copyright (C) 2006-2010 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
12 #include <sys/types.h>
17 #include "gcc-compat.h"
23 * Call a function for each subdirectory of the current working directory.
25 * \param dirname The directory to traverse.
26 * \param func The function to call for each subdirectory.
27 * \param private_data Pointer to an arbitrary data structure.
29 * For each top-level directory under \a dirname, the supplied function \a func is
30 * called. The full path of the subdirectory and the \a private_data pointer
31 * are passed to \a func.
33 * \return This function returns immediately if \a func returned a negative
34 * value. In this case \a func must set error_txt and this negative value is
35 * returned to the caller. Otherwise the function returns when all
36 * subdirectories have been passed to \a func.
39 int for_each_subdir(int (*func)(const char *, void *), void *private_data)
43 DIR *dir = opendir(".");
46 return -ERRNO_TO_DSS_ERROR(errno);
47 while ((entry = readdir(dir))) {
51 if (!strcmp(entry->d_name, "."))
53 if (!strcmp(entry->d_name, ".."))
55 ret = lstat(entry->d_name, &s) == -1;
57 ret = -ERRNO_TO_DSS_ERROR(errno);
63 ret = func(entry->d_name, private_data);
73 * Wrapper for chdir(2).
75 * \param path The specified directory.
79 int dss_chdir(const char *path)
83 return -ERRNO_TO_DSS_ERROR(errno);
87 * Set a file descriptor to non-blocking mode.
89 * \param fd The file descriptor.
93 __must_check int mark_fd_nonblocking(int fd)
95 int flags = fcntl(fd, F_GETFL);
97 return -ERRNO_TO_DSS_ERROR(errno);
98 flags = fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK);
100 return -ERRNO_TO_DSS_ERROR(errno);
105 * dss' wrapper for select(2).
107 * It calls select(2) (with no exceptfds) and starts over if select() was
108 * interrupted by a signal.
110 * \param n The highest-numbered descriptor in any of the two sets, plus 1.
111 * \param readfds fds that should be checked for readability.
112 * \param writefds fds that should be checked for writablility.
113 * \param timeout_tv upper bound on the amount of time elapsed before select()
116 * \return The return value of the underlying select() call on success, the
117 * negative system error code on errors.
119 * All arguments are passed verbatim to select(2).
120 * \sa select(2) select_tut(2).
122 int dss_select(int n, fd_set *readfds, fd_set *writefds,
123 struct timeval *timeout_tv)
127 ret = select(n, readfds, writefds, NULL, timeout_tv);
129 } while (ret < 0 && err == EINTR);
131 return -ERRNO_TO_DSS_ERROR(errno);