forked from nkabir/reprepro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filecntl.c
89 lines (76 loc) · 1.7 KB
/
filecntl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* written 2007 by Bernhard R. Link
* This file is in the public domain.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <config.h>
#include <limits.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include "filecntl.h"
#ifndef HAVE_CLOSEFROM
void closefrom(int lowfd) {
long maxopen;
int fd;
# ifdef F_CLOSEM
if (fcntl(lowfd, F_CLOSEM, NULL) == 0)
return;
# endif
maxopen = sysconf(_SC_OPEN_MAX);
if (maxopen > INT_MAX)
maxopen = INT_MAX;
if (maxopen < 0)
maxopen = 1024;
for (fd = lowfd ; fd <= maxopen ; fd++)
(void)close(fd);
}
#endif
void markcloseonexec(int fd) {
long l;
l = fcntl(fd, F_GETFD, 0);
if (l >= 0) {
(void)fcntl(fd, F_SETFD, l|FD_CLOEXEC);
}
}
int deletefile(const char *fullfilename) {
int ret, e;
ret = unlink(fullfilename);
if (ret != 0) {
e = errno;
fprintf(stderr, "error %d unlinking %s: %s\n",
e, fullfilename, strerror(e));
return (e != 0)?e:EINVAL;
}
return 0;
}
bool isregularfile(const char *fullfilename) {
struct stat s;
int i;
assert(fullfilename != NULL);
i = stat(fullfilename, &s);
return i == 0 && S_ISREG(s.st_mode);
}
bool isdirectory(const char *fullfilename) {
struct stat s;
int i;
assert(fullfilename != NULL);
i = stat(fullfilename, &s);
return i == 0 && S_ISDIR(s.st_mode);
}
bool isanyfile(const char *fullfilename) {
struct stat s;
int i;
assert(fullfilename != NULL);
i = lstat(fullfilename, &s);
return i == 0;
}