Repository navigation
Expand file tree
/
Copy pathdfc.c
More file actions
447 lines (387 loc) · 8.35 KB
/
Copy pathdfc.c
File metadata and controls
447 lines (387 loc) · 8.35 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* dfc.c
*
* (C) 2012 - Hahling Robin <robin.hahling@gw-computing.net>
*
* Displays free disk space in an elegant manner.
*/
#define _POSIX_C_SOURCE 2
#define _BSD_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <mntent.h>
#include <string.h>
#include <sys/param.h>
#include <sys/statvfs.h>
#include "dfc.h"
/* set flags for options */
int aflag, hflag, gflag, kflag, mflag, vflag;
int
main(int argc, char *argv[])
{
FILE *mtab;
struct mntent *entbuf;
struct statvfs vfsbuf;
struct fsmntinfo *fmi;
struct list queue;
int ch;
while ((ch = getopt(argc, argv, "ahgkmv")) != -1) {
switch (ch) {
case 'a':
aflag = 1;
break;
case 'h':
hflag = 1;
break;
case 'g':
gflag = 1;
break;
case 'k':
kflag = 1;
break;
case 'm':
mflag = 1;
break;
case 'v':
vflag = 1;
break;
case '?':
default:
usage(EXIT_FAILURE);
/* NOTREACHED */
}
}
if (hflag)
usage(EXIT_SUCCESS);
/* NOTREACHED */
if (vflag) {
(void)printf("dfc %s\n", VERSION);
return EXIT_SUCCESS;
/* NOTREACHED */
}
/* initializes the queue */
init_queue(&queue);
/* init fsmntinfo */
if ((fmi = malloc(sizeof(struct fsmntinfo))) == NULL) {
(void)fp
8000
uts("Error while allocating memory to fmi", stderr);
return EXIT_FAILURE;
/* NOTREACHED */
}
*fmi = fmi_init();
/* open mtab file */
if ((mtab = fopen("/etc/mtab", "r")) == NULL) {
perror("Error while opening mtab file ");
return EXIT_FAILURE;
/* NOTREACHED */
}
/* loop to get infos from all the mounted fs */
while ((entbuf = getmntent(mtab)) != NULL) {
/* get infos from statvfs */
if (statvfs(entbuf->mnt_dir, &vfsbuf) == -1) {
(void)fprintf(stderr, "Error using statvfs on %s\n",
entbuf->mnt_dir);
perror("with this error code ");
return EXIT_FAILURE;
/* NOTREACHED */
} else {
/* infos from getmntent */
if ((fmi->fsname = strdup(entbuf->mnt_fsname)) == NULL) {
fmi->fsname = "unknown";
}
if ((fmi->dir = strdup(entbuf->mnt_dir)) == NULL) {
fmi->dir = "unknown";
}
if ((fmi->type = strdup(entbuf->mnt_type)) == NULL) {
fmi->type = "unknown";
}
/* infos from statvfs */
fmi->bsize = vfsbuf.f_frsize;
fmi->blocks = vfsbuf.f_blocks;
fmi->bfree = vfsbuf.f_bavail;
/* pointer to the next element */
fmi->next = NULL;
/* enqueue the element into the queue */
enqueue(&queue, *fmi);
/* skip fuse-daemon */
if (strcmp(fmi->fsname, "gvfs-fuse-daemon") == 0)
continue;
/* adjust longest for the queue */
if (aflag) {
/* is it the longest type? */
queue.fsmaxlen = imax((int)strlen(fmi->type),
queue.typemaxlen);
/* is it the longest dir */
queue.dirmaxlen = imax((int)strlen(fmi->dir),
queue.dirmaxlen);
/* is it the longest fsname? */
queue.typemaxlen = imax((int)strlen(fmi->fsname),
queue.fsmaxlen);
} else {
/* we do not care about stuff not from /dev/ */
if (strncmp(fmi->fsname, "/dev/", 5) == 0) {
/* is it the longest type? */
queue.fsmaxlen = imax(
(int)strlen(fmi->type),
queue.typemaxlen);
/* is it the longest dir */
queue.dirmaxlen = imax(
(int)strlen(fmi->dir),
queue.dirmaxlen);
/* is it the longest fsname? */
queue.typemaxlen = imax(
(int)strlen(fmi->fsname),
queue.fsmaxlen);
}
}
}
}
/* we need to close the mtab file now */
if (fclose(mtab) == EOF)
perror("Could not close mtab file ");
/* actually displays the infos we have gotten */
disp(queue);
return EXIT_SUCCESS;
/* NOTREACHED */
}
/*
* Display usage.
* param: status --> status code (EXIT_SUCCESS, EXIT_FAILURE, ...)
*/
static void
usage(int status)
{
if (status != 0)
(void)fputs("Try dfc -h for more informations\n", stderr);
else
(void)fputs("Usage: dfc [OPTIONS(S)]\n"
"Available options:\n"
" -a print all fs from mtab\n"
" -h print this message\n"
" -g size in Gio\n"
" -k size in Kio\n"
" -m size in Mio\n"
" -v print program version\n",
stdout);
exit(status);
/* NOTREACHED */
}
/*
* Initializes a queue structure
* @lst: queue pointer
*/
void
init_queue(struct list *lst)
{
lst->head = NULL;
lst->tail = NULL;
lst->fsmaxlen = -1;
lst->dirmaxlen = -1;
lst->typemaxlen = -1;
}
/*
* Checks if a queue is empty
* @lst: queue
* Returns:
* --> 1 (true) if the queue is empty
* --> 0 if not
*/
int
is_empty(struct list lst)
{
if (lst.head == NULL)
return 1;
/* NOTREACHED */
else
return 0;
/* NOTREACHED */
}
/*
* Enqueues an element into a queue
* @lst: queue pointer
* @elt: element
* Returns:
* --> -1 on error
* --> 0 on sucess
*/
int
enqueue(struct list *lst, struct fsmntinfo fmi)
{
struct fsmntinfo *new_fmi = malloc(sizeof(struct fsmntinfo));
if (new_fmi == NULL) {
(void)fputs("Error while allocating memory to fmi", stderr);
return -1;
/* NOTREACHED */
}
/* initialize the new element to be inserted in the queue */
*new_fmi = fmi;
if (is_empty((*lst)))
lst->head = new_fmi;
else
lst->tail->next = new_fmi;
lst->tail = new_fmi;
return 0;
/* NOTREACHED */
}
/*
* Inits an fsmntinfo to some defaults values
* Returns:
* --> fsmntinfo that has been initialized
*/
struct fsmntinfo
fmi_init(void)
{
struct fsmntinfo fmi;
fmi.fsname = "unknown";
fmi.dir = "unknown";
fmi.type = "unknown";
fmi.bsize = 0;
fmi.blocks = 0;
fmi.bfree = 0;
fmi.next = NULL;
return fmi;
/* NOTREACHED */
}
/*
* Actually displays infos in nice manner
* @lst: queue containing all required informations
*/
void
disp(struct list lst)
{
struct fsmntinfo *p = NULL;
int i, j;
double perctused, size, free, used;
/* legend on top */
disp_header(&lst);
p = lst.head;
while (p != NULL) {
/* we do not care about gvfs-fuse-daemon */
if (strcmp(p->fsname, "gvfs-fuse-daemon") == 0){
p = p->next;
continue;
}
if (!aflag) {
/* skip some stuff we do not care about */
if (strncmp(p->fsname, "/dev/", 5) != 0) {
p = p->next;
continue;
}
}
/* filesystem */
(void)printf("%s", p->fsname);
for (i = (int)strlen(p->fsname); i < lst.fsmaxlen; i++)
(void)printf(" ");
/* type */
(void)printf("%s", p->type);
for (i = (int)strlen(p->type); i < lst.typemaxlen; i++)
(void)printf(" ");
size = (double)p->blocks *(double)p->bsize;
free = (double)p->bfree * (double)p->bsize;
/* calculate the % used */
used = size - free;
if (size == 0)
perctused = 100.0;
else
perctused = (used / size) * 100.0;
/* used (*) */
(void)printf("[");
for (i = 0; i < perctused; i += 5)
(void)printf("*");
for (j = i; j < 100; j += 5)
(void)printf("-");
/* %used */
(void)printf("] %3.f%%", perctused);
/* format to requested format (k,m,g) */
size = cvrt(size);
free = cvrt(free);
/* free and total */
if (kflag) {
(void)printf("%10.fK", free);
(void)printf("%10.fK", size);
} else if (mflag) {
(void)printf("%9.fM", free);
(void)printf("%9.fM", size);
} else if (gflag) {
(void)printf("%9.1fG", free);
(void)printf("%7.1fG", size);
} else {
(void)printf("%15.fB", free);
(void)printf("%15.fB", size);
}
/* mounted on */
(void)printf(" %s\n", p->dir);
/* reinit the length */
p = p->next;
}
}
/*
* Display header
* @lst: queue containing the informations
*/
void
disp_header(struct list *lst)
{
int i;
(void)printf("FILESYSTEM ");
if (lst->fsmaxlen > 11)
for (i = 11; i < lst->fsmaxlen; i++)
(void)printf(" ");
else
lst->fsmaxlen = 11;
(void)printf("TYPE ");
if (lst->typemaxlen > 5)
for (i = 5; i < lst->typemaxlen; i++)
(void)printf(" ");
else
lst->typemaxlen = 5;
(void)printf("USED (*)");
for (i = 0; i < 6; i++)
(void)printf(" ");
(void)printf("FREE (-) ");
(void)printf("%%USED");
if (kflag)
(void)printf(" ");
else if (mflag || gflag)
(void)printf(" ");
else
(void)printf(" ");
(void)printf("AVAILABLE");
if (kflag)
(void)printf(" ");
else if (mflag)
(void)printf(" ");
else if (gflag)
(void)printf(" ");
else
(void)printf(" ");
(void)printf("TOTAL");
(void)puts(" MOUNTED ON");
}
/*
* Converts the argument to the correct format (K,M,G)
* @n: number to convert
*/
double
cvrt(double n)
{
if (kflag)
n /= 1024.0;
else if (mflag)
/* 1024^2 */
n /= 1048576.0;
else if (gflag)
/* 1024^3 */
n /= 1073741824.0;
return n;
/* NOTREACHED */
}
/*
* Return the longest of the two parameters
*/
int
imax(int a, int b)
{
return (a > b ? a : b);
}