forked from fifteenhex/smolutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdf.c
More file actions
154 lines (127 loc) · 2.81 KB
/
Copy pathdf.c
File metadata and controls
154 lines (127 loc) · 2.81 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-License-Identifier: GPL-3.0-or-later
#include "config.h"
#define VERBOSE
#include "common.h"
#define PROC_MOUNTS "/proc/mounts"
static char linebuf[1024];
struct mount {
char *dev;
char *mountpoint;
char *type;
char *opts;
char *dump;
char *pass;
};
static int parse_mount(char *line, struct mount *mount)
{
char *dev;
char *mountpoint;
char *type;
char *opts;
char *dump;
char *pass;
dev = line;
/* Mount point should be after the dev */
mountpoint = strchr(line, ' ');
if (!mountpoint) {
verbose("Didn't find mountpoint in line\n");
return -EINVAL;
}
*mountpoint++ = '\0';
/* type should be after the mount point */
type = strchr(mountpoint, ' ');
if (!type) {
verbose("Didn't find type in line\n");
return -EINVAL;
}
*type++ = '\0';
/* options should be after the type */
opts = strchr(type, ' ');
if (!opts) {
verbose("Didn't find opts in line\n");
return -EINVAL;
}
*opts++ = '\0';
/* dump should be after the options */
dump = strchr(opts, ' ');
if (!dump) {
verbose("Didn't find dump in line\n");
return -EINVAL;
}
*dump++ = '\0';
/* pass should be after dump */
pass = strchr(dump, ' ');
if (!pass) {
verbose("Didn't find pass in line\n");
return -EINVAL;
}
*pass++ = '\0';
mount->dev = dev;
mount->mountpoint = mountpoint;
mount->type = type;
mount->opts = opts;
mount->dump = dump;
mount->pass = pass;
return 0;
}
static int process_line(char *line)
{
long long total = 0;
long long avail = 0;
long long used = 0;
struct mount mount;
struct statfs buf;
int usepercent;
int ret;
ret = parse_mount(line, &mount);
if (ret)
return ret;
ret = statfs(mount.mountpoint, &buf);
if (ret) {
verbose("stafs(%s, ..) failed: %d\n", mount.mountpoint, errno);
return -1;
}
if (buf.f_bsize) {
total = ((long long)buf.f_blocks * buf.f_bsize) / 1024;
avail = ((long long)buf.f_bavail * buf.f_bsize) / 1024;
used = ((long long)(buf.f_blocks - buf.f_bfree) * buf.f_bsize) / 1024;
usepercent = total ? (int)(used * 100 / total) : 0;
}
printf("%-20s %12lld %12lld %12lld %4d%% %s\n",
mount.type, total, used, avail, usepercent, mount.mountpoint);
return 0;
}
int main (int argc, char **argv, char **envp)
{
unsigned int pos = 0;
int __cleanup_fd fd;
int ret;
fd = open(PROC_MOUNTS, O_RDONLY);
if (fd < 0) {
verbose("Failed to open %s\n", PROC_MOUNTS);
return 1;
}
printf("%-20s %12s %12s %12s %5s %s\n",
"Filesystem", "1K-blocks", "Used", "Available", "Use%", "Mounted on");
while (true) {
ret = read(fd, &linebuf[pos], 1);
if (ret == 0)
break;
if (ret != 1) {
verbose("Failed to read from input\n");
return 1;
}
if (linebuf[pos] == '\n') {
linebuf[pos] = '\0';
ret = process_line(linebuf);
if (ret) {
verbose("Failed to process input line\n");
return 1;
}
pos = 0;
}
else
pos++;
}
return 0;
}