Debian Codegraph @

examples/diex.c

raw
1 /* this code is in the public domain */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdbool.h>
5 #include <math.h>
6
7 #include <di.h>
8
9 enum {
10 HAVE_SPACE = 0,
11 NO_SPACE = 1,
12 };
13
14 int
15 check_space (const char *fn, double space_wanted) {
16 void *di_data;
17 int targc;
18 const char *targv [10];
19 int exitflag;
20 const di_pub_disk_info_t *pub;
21 int rval = NO_SPACE;
22 int count;
23
24 targc = 1;
25 targv [0] = fn;
26 targv [1] = NULL;
27
28 di_data = di_initialize ();
29
30 exitflag = di_process_options (di_data, targc, targv, 0);
31 if (exitflag != DI_EXIT_NORM) {
32 di_cleanup (di_data);
33 exit (exitflag);
34 }
35
36 exitflag = di_get_all_disk_info (di_data);
37 if (exitflag != DI_EXIT_NORM) {
38 di_cleanup (di_data);
39 exit (exitflag);
40 }
41
42 count = di_iterate_init (di_data, DI_ITER_PRINTABLE);
43 while ((pub = di_iterate (di_data)) != NULL) {
44 double dval;
45
46 /* compare terabytes available */
47 dval = di_get_scaled (di_data, pub->index, DI_SCALE_TERA,
48 DI_SPACE_AVAIL, DI_VALUE_NONE, DI_VALUE_NONE);
49 if (dval >= space_wanted) {
50 rval = HAVE_SPACE;
51 }
52 }
53 di_cleanup (di_data);
54
55 return rval;
56 }
57
58 int
59 main (int argc, char *argv []) {
60 const char *fn = NULL;
61 double spwant = 0.2; /* terabytes */
62 bool rval;
63
64 if (argc > 1) {
65 fn = argv [1];
66 }
67 if (fn == NULL) {
68 fprintf (stderr, "Usage: %s <path> [<space-wanted>]\n", argv [0]);
69 exit (1);
70 }
71
72 if (argc > 2) {
73 spwant = atof (argv [2]);
74 }
75
76 rval = check_space (fn, spwant);
77 if (rval == NO_SPACE) {
78 fprintf (stdout, "Not enough disk space\n");
79 }
80 if (rval == HAVE_SPACE) {
81 fprintf (stdout, "Enough disk space\n");
82 }
83
84 return rval;
85 }
86