]> git.ipfire.org Git - thirdparty/util-linux.git/blob - include/fileutils.h
libmount: fix comment referring to passno field
[thirdparty/util-linux.git] / include / fileutils.h
1 #ifndef UTIL_LINUX_FILEUTILS
2 #define UTIL_LINUX_FILEUTILS
3
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <dirent.h>
8 #include <sys/stat.h>
9
10 #include "c.h"
11
12 extern int mkstemp_cloexec(char *template);
13
14 extern int xmkstemp(char **tmpname, const char *dir, const char *prefix);
15
16 static inline FILE *xfmkstemp(char **tmpname, const char *dir, const char *prefix)
17 {
18 int fd;
19 FILE *ret;
20
21 fd = xmkstemp(tmpname, dir, prefix);
22 if (fd == -1)
23 return NULL;
24
25 if (!(ret = fdopen(fd, "w+" UL_CLOEXECSTR))) {
26 close(fd);
27 return NULL;
28 }
29 return ret;
30 }
31
32 #ifdef HAVE_OPENAT
33 static inline FILE *fopen_at(int dir, const char *filename,
34 int flags, const char *mode)
35 {
36 int fd = openat(dir, filename, flags);
37 if (fd < 0)
38 return NULL;
39
40 return fdopen(fd, mode);
41 }
42 #endif
43
44 static inline int is_same_inode(const int fd, const struct stat *st)
45 {
46 struct stat f;
47
48 if (fstat(fd, &f) < 0)
49 return 0;
50 else if (f.st_dev != st->st_dev || f.st_ino != st->st_ino)
51 return 0;
52 return 1;
53 }
54
55 extern int dup_fd_cloexec(int oldfd, int lowfd);
56 extern int get_fd_tabsize(void);
57
58 extern int mkdir_p(const char *path, mode_t mode);
59 extern char *stripoff_last_component(char *path);
60
61 /* This is readdir()-like function, but skips "." and ".." directory entries */
62 static inline struct dirent *xreaddir(DIR *dp)
63 {
64 struct dirent *d;
65
66 while ((d = readdir(dp))) {
67 if (!strcmp(d->d_name, ".") ||
68 !strcmp(d->d_name, ".."))
69 continue;
70 break;
71 }
72 return d;
73 }
74
75 #endif /* UTIL_LINUX_FILEUTILS */