/
df.c
150 lines (126 loc) · 2.85 KB
/
df.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
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
/* df - disk free block printout Author: Andy Tanenbaum */
#include "../h/const.h"
#include "../h/type.h"
#include "../fs/const.h"
#include "../fs/type.h"
#include "../fs/super.h"
#include "stat.h"
main(argc, argv)
int argc;
char *argv[];
{
register int i;
if (argc <= 1) {
std_err("Usage: df special ...\n");
exit(1);
}
sync(); /* have to make sure disk is up-to-date */
for (i = 1; i < argc; i++) df(argv[i]);
exit(0);
}
df(name)
char *name;
{
register int fd;
int i_count, z_count, totblocks, busyblocks, i;
char buf[BLOCK_SIZE], *s0;
struct super_block super, *sp;
struct stat statbuf;
extern char *itoa();
if ( (fd = open(name,0)) < 0) {
perror(name);
return;
}
/* Is it a block special file? */
if (fstat(fd, &statbuf) < 0) {
stderr2(name, ": Cannot stat\n");
return;
}
if ( (statbuf.st_mode & S_IFMT) != S_IFBLK) {
stderr2(name, ": not a block special file\n");
return;
}
lseek(fd, (long)BLOCK_SIZE, 0); /* skip boot block */
if (read(fd, &super, SUPER_SIZE) != SUPER_SIZE) {
stderr2(name, ": Can't read super block\n");
close(fd);
return;
}
lseek(fd, (long) BLOCK_SIZE * 2L, 0); /* skip rest of super block */
sp = &super;
if (sp->s_magic != SUPER_MAGIC) {
stderr2(name, ": Not a valid file system\n");
close(fd);
return;
}
i_count = bit_count(sp->s_imap_blocks, sp->s_ninodes+1, fd);
if (i_count < 0) {
stderr2(name, ": can't find bit maps\n");
close(fd);
return;
}
z_count = bit_count(sp->s_zmap_blocks, sp->s_nzones, fd);
if (z_count < 0) {
stderr2(name, ": can't find bit maps\n");
close(fd);
return;
}
totblocks = sp->s_nzones << sp->s_log_zone_size;
busyblocks = z_count << sp->s_log_zone_size;
/* Print results. */
prints("%s ",name);
s0 = name;
while (*s0) s0++;
i = 12 - (s0 - name);
while (i--) prints(" ");
prints("i-nodes: ");
num3(i_count - 1);
prints(" used ");
num3(sp->s_ninodes + 1 - i_count);
prints(" free blocks: ");
num3(busyblocks);
prints(" used ");
num3(totblocks - busyblocks);
prints(" free\n");
close(fd);
}
bit_count(blocks, bits, fd)
int blocks;
int bits;
int fd;
{
register int i, b;
int busy, count, w;
int *wptr, *wlim;
int buf[BLOCK_SIZE/sizeof(int)];
/* Loop on blocks, reading one at a time and counting bits. */
busy = 0;
count = 0;
for (i = 0; i < blocks; i++) {
if (read(fd, buf, BLOCK_SIZE) != BLOCK_SIZE) return(-1);
wptr = &buf[0];
wlim = &buf[BLOCK_SIZE/sizeof(int)];
/* Loop on the words of a block */
while (wptr != wlim) {
w = *wptr++;
/* Loop on the bits of a word. */
for (b = 0; b < 8*sizeof(int); b++) {
if ( (w>>b) & 1) busy++;
if (++count == bits) return(busy);
}
}
}
}
stderr2(s1, s2)
char *s1, *s2;
{
std_err(s1);
std_err(s2);
}
num3(n)
int n;
{
if (n < 10) prints(" %s", itoa(n));
else if (n < 100) prints(" %s", itoa(n));
else prints("%s", itoa(n));
}