]> git.ipfire.org Git - thirdparty/util-linux.git/blob - include/fileutils.h
Merge branch 'travis-checkusage' of https://github.com/rudimeier/util-linux
[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 <sys/stat.h>
8
9 #include "c.h"
10
11 extern int mkstemp_cloexec(char *template);
12
13 extern int xmkstemp(char **tmpname, const char *dir, const char *prefix);
14
15 static inline FILE *xfmkstemp(char **tmpname, const char *dir, const char *prefix)
16 {
17 int fd;
18 FILE *ret;
19
20 fd = xmkstemp(tmpname, dir, prefix);
21 if (fd == -1)
22 return NULL;
23
24 if (!(ret = fdopen(fd, "w+" UL_CLOEXECSTR))) {
25 close(fd);
26 return NULL;
27 }
28 return ret;
29 }
30
31 #ifdef HAVE_OPENAT
32 static inline FILE *fopen_at(int dir, const char *filename,
33 int flags, const char *mode)
34 {
35 int fd = openat(dir, filename, flags);
36 if (fd < 0)
37 return NULL;
38
39 return fdopen(fd, mode);
40 }
41 #endif
42
43 static inline int is_same_inode(const int fd, const struct stat *st)
44 {
45 struct stat f;
46
47 if (fstat(fd, &f) < 0)
48 return 0;
49 else if (f.st_dev != st->st_dev || f.st_ino != st->st_ino)
50 return 0;
51 return 1;
52 }
53
54 extern int dup_fd_cloexec(int oldfd, int lowfd);
55 extern int get_fd_tabsize(void);
56
57 extern int mkdir_p(const char *path, mode_t mode);
58 extern char *stripoff_last_component(char *path);
59
60 #endif /* UTIL_LINUX_FILEUTILS */