-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat.c
78 lines (56 loc) · 1.37 KB
/
stat.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
#include <assert.h>
#include <limits.h>
#include <unistd.h>
#include "stat.h"
void xfinfo(const struct stat *const st,
time_t *const mtime,
long long *const size) {
assert(st != NULL);
assert(mtime != NULL);
assert(size != NULL);
assert(st->st_mtim.tv_sec >= 0);
assert(st->st_size >= 0);
*mtime = st->st_mtim.tv_sec;
*size = st->st_size;
assert(*mtime >= 0);
assert(*size >= 0);
}
bool xfstat(const char *const path,
time_t *const mtime,
long long *const size) {
assert(path != NULL);
assert(mtime != NULL);
assert(size != NULL);
struct stat st;
if (stat(path, &st) == -1)
return (false);
xfinfo(&st, mtime, size);
return (true);
}
bool xlstatlen(const char *const path,
size_t *const tlen) {
assert(path != NULL);
assert(tlen != NULL);
struct stat st;
if (lstat(path, &st) == -1)
return (false);
assert(st.st_size >= 0);
assert(st.st_size <= UINT_MAX);
*tlen = (size_t) st.st_size;
return (true);
}
bool xlstat(const char *const path,
char *const tg,
const size_t tlen) {
assert(path != NULL);
assert(tg != NULL);
assert(tlen > 0);
ssize_t llen = readlink(path, tg, tlen);
if (llen == -1)
return (false);
assert(llen >= 0);
if ((size_t) llen != tlen)
return (false);
*(tg + tlen) = '\0';
return (true);
}