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);
74 * Set a file descriptor to non-blocking mode.
76 * \param fd The file descriptor.
80 __must_check int mark_fd_nonblocking(int fd)
82 int flags = fcntl(fd, F_GETFL);
84 return -ERRNO_TO_DSS_ERROR(errno);
85 flags = fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK);
87 return -ERRNO_TO_DSS_ERROR(errno);
92 * dss' wrapper for select(2).
94 * It calls select(2) (with no exceptfds) and starts over if select() was
95 * interrupted by a signal.
97 * \param n The highest-numbered descriptor in any of the two sets, plus 1.
98 * \param readfds fds that should be checked for readability.
99 * \param writefds fds that should be checked for writablility.
100 * \param timeout_tv upper bound on the amount of time elapsed before select()
103 * \return The return value of the underlying select() call on success, the
104 * negative system error code on errors.
106 * All arguments are passed verbatim to select(2).
107 * \sa select(2) select_tut(2).
109 int dss_select(int n, fd_set *readfds, fd_set *writefds,
110 struct timeval *timeout_tv)
114 ret = select(n, readfds, writefds, NULL, timeout_tv);
116 } while (ret < 0 && err == EINTR);
118 return -ERRNO_TO_DSS_ERROR(errno);