]> git.ipfire.org Git - thirdparty/util-linux.git/blame - include/fileutils.h
kill: add missing ifdefs
[thirdparty/util-linux.git] / include / fileutils.h
CommitLineData
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
12extern int mkstemp_cloexec(char *template);
13
bde91c85 14extern int xmkstemp(char **tmpname, const char *dir, const char *prefix);
6b79eb38 15
bde91c85 16static 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
33static 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
44static 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 55extern int dup_fd_cloexec(int oldfd, int lowfd);
be92327e
KZ
56extern int get_fd_tabsize(void);
57
934530c7 58extern int mkdir_p(const char *path, mode_t mode);
d4eaabc8 59extern char *stripoff_last_component(char *path);
934530c7 60
7761bd3b
KZ
61/* This is readdir()-like function, but skips "." and ".." directory entries */
62static 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
488f65fc
KZ
75extern void close_all_fds(const int exclude[], size_t exsz);
76
be92327e 77#endif /* UTIL_LINUX_FILEUTILS */