Repository navigation
Expand file tree
/
Copy pathdfc.c
More file actions
708 lines (619 loc) · 13.3 KB
/
Copy pathdfc.c
File metadata and controls
708 lines (619 loc) · 13.3 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/*
* 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, bflag, gflag, hflag, iflag, kflag, mflag, nflag, sflag, tflag, vflag, wflag;
int Kflag, Mflag, Gflag;
int
main(int argc, char *argv[])
{
struct list queue;
int ch;
while ((ch = getopt(argc, argv, "abghiGkKmMnstvw")) != -1) {
switch (ch) {
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'g':
gflag = 1;
Gflag = 0;
break;
case 'G':
gflag = 0;
Gflag = 1;
break;
case 'h':
hflag = 1;
break;
case 'i':
iflag = 1;
break;
case 'k':
kflag = 1;
Kflag = 0;
break;
case 'K':
kflag = 0;
Kflag = 1;
break;
case 'm':
mflag = 1;
Mflag = 0;
break;
case 'M':
mflag = 0;
Mflag = 1;
break;
case 'n':
nflag = 1;
break;
case 's':
sflag = 1;
break;
case 't':
tflag = 1;
break;
case 'v':
vflag = 1;
break;
case 'w':
wflag = 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);
/* fetch information from getmntent and statvfs */
fetch_info(&queue);
/* 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"
" -b size in Bytes\n"
" -g size in Gio\n"
" -G size in Go\n"
" -h print this message\n"
" -i info about inodes\n"
" -k size in Kio\n"
" -K size in Ko\n"
" -m size in Mio\n"
" -M size in Mo\n"
" -n do not print header\n"
" -s sum the total usage\n"
" -t hide filesystem type\n"
" -v print program version\n"
" -w use a wider bar\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.opts = "none";
fmi.bsize = 0;
fmi.frsize = 0;
fmi.blocks = 0;
fmi.bfree = 0;
fmi.bavail = 0;
fmi.files = 0;
fmi.ffree = 0;
fmi.favail = 0;
fmi.next = NULL;
return fmi;
/* NOTREACHED */
}
/*
* fetch information from getmntent and statvfs and store it into the queue
* @lst: queue in which to store information
*/
void
fetch_info(struct list *lst)
{
FILE *mtab;
struct mntent *entbuf;
struct statvfs vfsbuf;
struct fsmntinfo *fmi;
/* init fsmntinfo */
if ((fmi = malloc(sizeof(struct fsmntinfo))) == NULL) {
(void)fputs("Error while allocating memory to fmi", stderr);
exit(EXIT_FAILURE);
/* NOTREACHED */
}
*fmi = fmi_init();
/* open mtab file */
if ((mtab = fopen("/etc/mtab", "r")) == NULL) {
perror("Error while opening mtab file ");
exit(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 ");
exit(EXIT_FAILURE);
/* NOTREACHED */
} else {
/* infos from getmntent */
if ((fmi->fsname = strdup(trk(entbuf->mnt_fsname)))
== NULL) {
fmi->fsname = "unknown";
}
if ((fmi->dir = strdup(trk(entbuf->mnt_dir))) == NULL) {
fmi->dir = "unknown";
}
if ((fmi->type = strdup(trk(entbuf->mnt_type))) == NULL) {
fmi->type = "unknown";
}
if ((fmi->opts = strdup(trk(entbuf->mnt_opts))) == NULL) {
fmi->opts = "none";
}
/* infos from statvfs */
fmi->bsize = vfsbuf.f_bsize;
fmi->frsize = vfsbuf.f_frsize;
fmi->blocks = vfsbuf.f_blocks;
fmi->bfree = vfsbuf.f_bfree;
fmi->bavail = vfsbuf.f_bavail;
fmi->files = vfsbuf.f_files;
fmi->ffree = vfsbuf.f_ffree;
fmi->favail = vfsbuf.f_favail;
/* pointer to the next element */
fmi->next = NULL;
/* enqueue the element into the queue */
enqueue(lst, *fmi);
/* skip fuse-daemon */
if (strcmp(fmi->fsname, "gvfs-fuse-daemon") == 0)
continue;
/* adjust longest for the queue */
if (aflag) {
/* is it the longest fsname? */
lst->fsmaxlen = imax((int)strlen(fmi->fsname),
lst->fsmaxlen);
/* is it the longest dir */
lst->dirmaxlen = imax((int)strlen(fmi->dir),
lst->dirmaxlen);
/* is it the longest type? */
lst->typemaxlen = imax((int)strlen(fmi->type),
lst->typemaxlen);
} else {
/* we do not care about stuff not from /dev/ */
if (strncmp(fmi->fsname, "/dev/", 5) == 0) {
/* is it the longest fsname? */
lst->fsmaxlen = imax(
(int)strlen(fmi->fsname),
lst->fsmaxlen);
/* is it the longest dir */
lst->dirmaxlen = imax(
(int)strlen(fmi->dir),
lst->dirmaxlen);
/* is it the longest type? */
lst->typemaxlen = imax(
(int)strlen(fmi->type),
lst->typemaxlen);
}
}
}
}
/* we need to close the mtab file now */
if (fclose(mtab) == EOF)
perror("Could not close mtab file ");
}
/*
* Actually displays infos in nice manner
* @lst: queue containing all required informations
*/
void
disp(struct list *lst)
{
struct fsmntinfo *p = NULL;
int i, j, n;
int barinc = 5;
double perctused, size, avail, used;
double stot, atot, utot;
stot = atot = utot = n = 0;
/* legend on top */
if (!nflag)
disp_header(lst);
if (lst->fsmaxlen < 11)
lst->fsmaxlen = 11;
p = lst->head;
while (p != NULL) {
/* we do not care about gvfs-fuse-daemon and the others */
if (strcmp(p->fsname, "gvfs-fuse-daemon") == 0 ||
strcmp(p->fsname, "proc") == 0 ||
strcmp(p->fsname, "sys") == 0 ||
strcmp(p->fsname, "devpts") == 0) {
p = p->next;
continue;
/* NOTREACHED */
}
if (!aflag) {
/* skip some stuff we do not care about */
if (strncmp(p->fsname, "/dev/", 5) != 0) {
p = p->next;
continue;
/* NOTREACHED */
}
}
/* filesystem */
(void)printf("%s", p->fsname);
for (i = (int)strlen(p->fsname); i < lst->fsmaxlen + 1; i++)
(void)printf(" ");
/* type */
if (!tflag) {
(void)printf("%s", p->type);
for (i = (int)strlen(p->type); i < lst->typemaxlen + 1; i++)
(void)printf(" ");
}
size = (double)p->blocks *(double)p->frsize;
avail = (double)p->bavail * (double)p->frsize;
used = size - avail;
/* calculate the % used */
if (size == 0)
perctused = 100.0;
else
perctused = (used / size) * 100.0;
if (sflag) {
stot += size;
atot += avail;
utot += used;
}
/* option to display a wider bar */
if (wflag) {
barinc = 2;
}
/* used (*) */
(void)printf("[");
for (i = 0; i < perctused; i += barinc)
(void)printf("*");
for (j = i; j < 100; j += barinc)
(void)printf("-");
/* %used */
(void)printf("] %3.f%%", perctused);
/* format to requested format (k,m,g) */
if (bflag || kflag || Kflag || mflag || Mflag || gflag || Gflag) {
size = cvrt(size);
avail = cvrt(avail);
}
/* avail and total */
if (kflag || Kflag) {
(void)printf("%10.fK", avail);
(void)printf("%10.fK", size);
} else if (mflag || Mflag) {
(void)printf("%9.1fM", avail);
(void)printf("%9.1fM", size);
} else if (gflag || Gflag) {
(void)printf("%9.1fG", avail);
(void)printf("%9.1fG", size);
} else if (bflag) {
(void)printf("%15.fB", avail);
(void)printf("%15.fB", size);
} else { /* human readable */
humanize(avail);
humanize(size);
}
/* info about inodes */
if (iflag) {
(void)printf("%10ld", p->files);
(void)printf("%10ld", p->favail);
}
/* mounted on */
(void)printf(" %s\n", p->dir);
p = p->next;
}
if (sflag)
disp_sum(lst, stot, atot, utot);
}
/*
* Display header
* @lst: queue containing the informations
*/
void
disp_header(struct list *lst)
{
int i;
int barinc = 5;
(void)printf("FILESYSTEM ");
for (i = 11; i < lst->fsmaxlen; i++)
(void)printf(" ");
if (!tflag) {
(void)printf("TYPE ");
if (lst->typemaxlen > 5)
for (i = 5; i < lst->typemaxlen + 1; i++)
(void)printf(" ");
else
lst->typemaxlen = 5;
}
/* option to display a wider bar */
if (wflag) {
barinc = 35;
}
(void)printf("(*) USED");
for (i = 0; i < (barinc + 1); i++)
(void)printf(" ");
(void)printf("FREE (-) ");
(void)printf("%%USED");
if (kflag || Kflag)
(void)printf(" ");
else if (bflag)
(void)printf(" ");
else
(void)printf(" ");
(void)printf("AVAILABLE");
if (kflag || Kflag)
(void)printf(" ");
else if (mflag || Mflag)
(void)printf(" ");
else if (bflag)
(void)printf(" ");
else
(void)printf(" ");
(void)printf("TOTAL");
if (iflag) {
(void)printf(" #INODES");
(void)printf(" AV.INODES");
}
(void)puts(" MOUNTED ON");
}
/*
* display the sum (useful when -s option is used
* @lst: queue containing the informations
* @stot: size total
* @utot:
*/
void
disp_sum(struct list *lst, double stot, double atot, double utot)
{
int i,j;
int barinc = 5;
double ptot = 0;
if (stot == 0)
ptot = 100.0;
else
ptot = (utot / stot) * 100.0;
(void)printf("SUM:");
j = lst->fsmaxlen + 1;
if (!tflag)
j += lst->typemaxlen + 1;
for (i = 4; i < j; i++)
(void)printf(" ");
/* option to display a wider bar */
if (wflag)
barinc = 2;
(void)printf("[");
for (i = 0; i < ptot ; i += barinc)
(void)printf("*");
for (j = i; j < 100; j += barinc)
(void)printf("-");
(void)printf("] %3.f%%", ptot);
if (bflag || kflag || Kflag || mflag || Mflag || gflag || Gflag) {
stot = cvrt(stot);
atot = cvrt(atot);
}
/* free and total */
if (kflag || Kflag) {
(void)printf("%10.fK", atot);
(void)printf("%10.fK", stot);
} else if (mflag || Mflag) {
(void)printf("%9.1fM", atot);
(void)printf("%9.1fM", stot);
} else if (gflag || Gflag) {
(void)printf("%9.1fG", atot);
(void)printf("%9.1fG", stot);
} else if (bflag) {
(void)printf("%15.fB", atot);
(void)printf("%15.fB", stot);
} else {/* human readable */
humanize(atot);
humanize(stot);
}
(void)printf("\n");
}
/*
* Converts the argument to the correct format (K,M,G)
* @n: number to convert
*/
double
cvrt(double n)
{
if (kf
89F9
lag)
n /= 1024.0;
else if (mflag)
/* 1024^2 */
n /= 1048576.0;
else if (gflag)
/* 1024^3 */
n /= 1073741824.0;
else if (Kflag)
n /= 1000.0;
else if (Mflag)
n /= 1000000.0;
else if (Gflag)
n /= 1000000000.0;
return n;
/* NOTREACHED */
}
/*
* convert to human readable format and print it
* @n: number to convert and print
*/
double
humanize(double n)
{
int i = 0;
while ((n >= 1000) && (i < 3)) {
n /= 1024.0;
i++;
}
switch (i) {
case 0: /* bytes */
(void)printf("%15.fB", n);
break;
case 1: /* Kio */
(void)printf("%10.fK", n);
break;
case 2: /* Mio */
(void)printf("%9.1fM", n);
break;
case 3: /* Gio */
(void)printf("%9.1fG", n);
break;
default:/* else ??? */
(void)fprintf(stderr, "Error while humanizing %f\n", n);
}
}
/*
* Return the longest of the two parameters
*/
int
imax(int a, int b)
{
return (a > b ? a : b);
/* NOTREACHED */
}
/*
* Truncate str to the third occurrence of /
* Example: str: /dev/disk/by-uuid/3c301e8b-560c-4914-b50d-8a49e713003c
* It then returns: /dev/disk/by-uuid
* Returns unmodified str otherwise
* @str: char* to truncate
*/
char *
trk(char *str)
{
char *p = str;
char trunc[strlen(str)];
int i = 0;
int len;
/* if no occurrence of /, return the unmodified str */
if ((p = strchr(str, '/')) == NULL)
return str;
/* NOTREACHED */
while ((i < 3) && (p != NULL)) {
/* in case there is only in / */
if ((p = strchr(p + 1, '/')) == NULL)
return str;
/* NOTREACHED */
i++;
}
/* p contains the part of str we want to truncate from str */
len = strlen(str) - strlen(p);
(void)strncpy(trunc, str, len);
trunc[len] = '\0';
str = strdup(trunc);
return str;
/* NOTREACHED */
}