]>
Commit | Line | Data |
---|---|---|
6b79eb38 SK |
1 | #ifndef UTIL_LINUX_FILEUTILS |
2 | #define UTIL_LINUX_FILEUTILS | |
3 | ||
372112e9 KZ |
4 | #include <stdio.h> |
5 | #include <fcntl.h> | |
6 | #include <unistd.h> | |
7761bd3b | 7 | #include <dirent.h> |
3f684db0 | 8 | #include <sys/stat.h> |
372112e9 KZ |
9 | |
10 | #include "c.h" | |
11 | ||
4d751c00 RM |
12 | extern int mkstemp_cloexec(char *template); |
13 | ||
bde91c85 | 14 | extern int xmkstemp(char **tmpname, const char *dir, const char *prefix); |
6b79eb38 | 15 | |
bde91c85 | 16 | static inline FILE *xfmkstemp(char **tmpname, const char *dir, const char *prefix) |
7961acce SK |
17 | { |
18 | int fd; | |
19 | FILE *ret; | |
f272b32c | 20 | |
bde91c85 | 21 | fd = xmkstemp(tmpname, dir, prefix); |
f272b32c | 22 | if (fd == -1) |
7961acce | 23 | return NULL; |
f272b32c | 24 | |
7e3729e7 | 25 | if (!(ret = fdopen(fd, "w+" UL_CLOEXECSTR))) { |
7961acce SK |
26 | close(fd); |
27 | return NULL; | |
28 | } | |
29 | return ret; | |
30 | } | |
be92327e | 31 | |
dc049516 | 32 | #ifdef HAVE_OPENAT |
1a048dc5 RM |
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 | } | |
dc049516 | 42 | #endif |
1a048dc5 | 43 | |
3f684db0 SK |
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 | ||
8e86d93d | 55 | extern int dup_fd_cloexec(int oldfd, int lowfd); |
be92327e KZ |
56 | extern int get_fd_tabsize(void); |
57 | ||
934530c7 | 58 | extern int mkdir_p(const char *path, mode_t mode); |
d4eaabc8 | 59 | extern char *stripoff_last_component(char *path); |
934530c7 | 60 | |
7761bd3b KZ |
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 | ||
be92327e | 75 | #endif /* UTIL_LINUX_FILEUTILS */ |