Qsmtp  0.37
diropen.h
Go to the documentation of this file.
1 
4 #ifndef DIROPEN_H
5 #define DIROPEN_H
6 
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <sys/stat.h>
10 
11 #if O_DIRECTORY == 0
12 #include <errno.h>
13 #include <sys/stat.h>
14 #endif /* O_DIRECTORY == 0 */
15 
32 static inline int __attribute__ ((nonnull (2)))
33 get_dirfd(int base, const char *dirname)
34 {
35  int fd;
36 
37 #ifdef O_PATH /* recent Linux */
38  fd = openat(base, dirname, O_PATH | O_DIRECTORY | O_CLOEXEC);
39 #elif defined(O_SEARCH)
40  fd = openat(base, dirname, O_SEARCH | O_DIRECTORY | O_CLOEXEC);
41 #else /* O_SEARCH */
42  fd = openat(base, dirname, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
43 #endif /* O_PATH */
44 
45 #if O_DIRECTORY == 0
46  {
47  struct stat st;
48  /* in case O_DIRECTORY is not supported make sure that this really is
49  * a directory */
50  if (fstat(fd, &st) != 0) {
51  int e = errno;
52  close(fd);
53  errno = e;
54  return -1;
55  }
56 
57  if (!S_ISDIR(st.st_mode)) {
58  close(fd);
59  errno = ENOTDIR;
60  return -1;
61  }
62  }
63 #endif /* O_DIRECTORY */
64 
65  return fd;
66 }
67 
68 #endif /* DIROPEN_H */
get_dirfd
static int get_dirfd(int base, const char *dirname)
get a file descriptor for the given directory
Definition: diropen.h:33