2 * Copyright (C) 2006-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
11 #include <sys/types.h>
17 #include "gcc-compat.h"
22 * Call a function for each subdirectory of the current working directory.
24 * \param dirname The directory to traverse.
25 * \param func The function to call for each subdirecrtory.
26 * \param private_data Pointer to an arbitrary data structure.
28 * For each top-level directory under \a dirname, the supplied function \a func is
29 * called. The full path of the subdirectory and the \a private_data pointer
30 * are passed to \a func.
32 * \return This function returns immediately if \a func returned a negative
33 * value. In this case \a func must set error_txt and this negative value is
34 * returned to the caller. Otherwise the function returns when all
35 * subdirectories have been passed to \a func.
38 int for_each_subdir(int (*func)(const char *, void *), void *private_data)
42 DIR *dir = opendir(".");
45 return -ERRNO_TO_DSS_ERROR(errno);
46 while ((entry = readdir(dir))) {
50 if (!strcmp(entry->d_name, "."))
52 if (!strcmp(entry->d_name, ".."))
54 ret = lstat(entry->d_name, &s) == -1;
56 ret = -ERRNO_TO_DSS_ERROR(errno);
62 ret = func(entry->d_name, private_data);
72 * Wrapper for chdir(2).
74 * \param path The specified directory.
78 int dss_chdir(const char *path)
82 return -ERRNO_TO_DSS_ERROR(errno);
86 * Set a file descriptor to non-blocking mode.
88 * \param fd The file descriptor.
92 __must_check int mark_fd_nonblocking(int fd)
94 int flags = fcntl(fd, F_GETFL);
96 return -ERRNO_TO_DSS_ERROR(errno);
97 flags = fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK);
99 return -ERRNO_TO_DSS_ERROR(errno);
104 * dss' wrapper for select(2).
106 * It calls select(2) (with no exceptfds) and starts over if select() was
107 * interrupted by a signal.
109 * \param n The highest-numbered descriptor in any of the two sets, plus 1.
110 * \param readfds fds that should be checked for readability.
111 * \param writefds fds that should be checked for writablility.
112 * \param timeout_tv upper bound on the amount of time elapsed before select()
115 * \return The return value of the underlying select() call on success, the
116 * negative system error code on errors.
118 * All arguments are passed verbatim to select(2).
119 * \sa select(2) select_tut(2).
121 int dss_select(int n, fd_set *readfds, fd_set *writefds,
122 struct timeval *timeout_tv)
126 ret = select(n, readfds, writefds, NULL, timeout_tv);
128 } while (ret < 0 && err == EINTR);
130 return -ERRNO_TO_DSS_ERROR(errno);