-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiskMeter.c
192 lines (159 loc) · 4.59 KB
/
DiskMeter.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
htop - DiskMeter.c
(C) 2004-2011 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "DiskMeter.h"
#include "CRT.h"
#include <unistd.h>
#include <sys/statvfs.h>
#include <stdint.h>
/*{
#include "Meter.h"
}*/
int DiskMeter_attributes[] = {
DISK_BLOCK,DISK_INODE
};
static void Disk_getListPart(int id,char* part,int* count){
char line[1024];
char dev[100] ;
char mountpoint[100] ;
int i;
sprintf( part ,"%s", "Error" );
FILE* fd = fopen( "/proc/mounts","r");
if (fd){
i=0;
while ( fgets(line,sizeof(line),fd) != NULL )
{
sscanf(line,"%s%s",dev,mountpoint);
if ( dev[0] == '/' )
{
if( i == id){
sprintf( part ,"%s", mountpoint );
}
i++;
}
}
fclose(fd);
*count = i;
}
}
static void DiskMeter_display(Object* cast, RichString* out) {
char buffer[50];
Meter* this = (Meter*)cast;
RichString_append(out, CRT_colors[METER_TEXT], " : ");
sprintf(buffer, "%5.1f%% ", this->values[0]);
RichString_append(out, CRT_colors[METER_TEXT], "bl:");
RichString_append(out, CRT_colors[DISK_BLOCK], buffer);
sprintf(buffer, "%5.1f%% ", this->values[1]);
RichString_append(out, CRT_colors[METER_TEXT], "in:");
RichString_append(out, CRT_colors[DISK_INODE], buffer);
}
static void DiskMeter_setValues(Meter* this, char* buffer, int size) {
struct statvfs info;
uint64_t percent;
uint64_t percent_inode;
int id ;
char part[100];
int count;
id = this->param;
Disk_getListPart(id,&part,&count);
statvfs (part,&info);
uintmax_t u100 = (info.f_blocks - info.f_bfree) * 100;
uintmax_t nonroot_total =info.f_blocks ;
percent = u100 / nonroot_total + (u100 % nonroot_total != 0);
this->values[0]= percent;
u100 = (info.f_files - info.f_ffree) * 100;
nonroot_total =info.f_files ;
percent_inode = u100 / nonroot_total + (u100 % nonroot_total != 0);
this->values[1]= percent_inode;
snprintf(buffer,size,"%d%%",percent);
}
static void DiskMeter_init(Meter* this) {
char part[100] ;
int osef;
Disk_getListPart(this->param,&part,&osef);
Meter_setCaption(this,part);
}
static void SingleColDisksMeter_draw(Meter* this, int x, int y, int w) {
Meter** meters = (Meter**) this->drawData;
int count;
char part[100] ;
Disk_getListPart(0,&part,&count);
for (int i = 0; i < count ; i++) {
meters[i]->draw(meters[i], x, y, w);
y += meters[i]->h;
}
}
static void AllDisksMeter_init(Meter* this) {
int count ;
char part[100] ;
Disk_getListPart(0,&part,&count);
if (!this->drawData)
this->drawData = calloc(sizeof(Meter*), count-1);
Meter** meters = (Meter**) this->drawData;
for (int i = 0; i < count ; i++) {
if (!meters[i])
meters[i] = Meter_new(this->pl, i, (MeterClass*) Class(DiskMeter));
Meter_init(meters[i]);
}
if (this->mode == 0)
this->mode = TEXT_METERMODE;
int h = Meter_modes[this->mode]->h;
this->h = h * count ;
}
static void AllDisksMeter_done(Meter* this) {
Meter** meters = (Meter**) this->drawData;
int count;
char part[100] ;
Disk_getListPart(0,&part,&count);
for (int i = 0; i < count ; i++)
Meter_delete((Object*)meters[i]);
}
static void AllDisksMeter_updateMode(Meter* this, int mode) {
Meter** meters = (Meter**) this->drawData;
this->mode = mode;
int h = Meter_modes[mode]->h;
int count;
char part[100] ;
Disk_getListPart(0,&part,&count);
for (int i = 0; i < count; i++) {
Meter_setMode(meters[i], mode);
}
this->h = h * count ;
}
MeterClass DiskMeter_class = {
.super = {
.extends = Class(Meter),
.delete = Meter_delete,
.display = DiskMeter_display
},
.setValues = DiskMeter_setValues,
.defaultMode = TEXT_METERMODE,
.total = 100.0,
.items = 1,
.attributes = DiskMeter_attributes,
.name = "Disk",
.uiName = "Disk",
.caption = "Disk : ",
.init = DiskMeter_init
};
MeterClass AllDisksMeter_class = {
.super = {
.extends = Class(Meter),
.delete = Meter_delete,
.display = DiskMeter_display
},
.defaultMode = CUSTOM_METERMODE,
.items = 1,
.total = 100.0,
.attributes = DiskMeter_attributes,
.name = "AllDisk",
.uiName = "AllDisk",
.caption = "Disks",
.draw = SingleColDisksMeter_draw,
.init = AllDisksMeter_init,
.updateMode = AllDisksMeter_updateMode,
.done = AllDisksMeter_done
};