1 /* df.c - report free disk space.
2  *
3  * Copyright 2006 Rob Landley <rob@landley.net>
4  *
5  * See http://opengroup.org/onlinepubs/9699919799/utilities/df.html
6 
7 USE_DF(NEWTOY(df, "HPkht*a[-HPkh]", TOYFLAG_SBIN))
8 
9 config DF
10   bool "df"
11   default y
12   help
13     usage: df [-HPkh] [-t type] [FILESYSTEM ...]
14 
15     The "disk free" command shows total/used/available disk space for
16     each filesystem listed on the command line, or all currently mounted
17     filesystems.
18 
19     -P	The SUSv3 "Pedantic" option
20     -k	Sets units back to 1024 bytes (the default without -P)
21     -h	Human readable output (K=1024)
22     -H	Human readable output (k=1000)
23     -t type	Display only filesystems of this type.
24 
25     Pedantic provides a slightly less useful output format dictated by Posix,
26     and sets the units to 512 bytes instead of the default 1024 bytes.
27 */
28 
29 #define FOR_df
30 #include "toys.h"
31 
GLOBALS(struct arg_list * fstype;long units;int column_widths[5];int header_shown;)32 GLOBALS(
33   struct arg_list *fstype;
34 
35   long units;
36   int column_widths[5];
37   int header_shown;
38 )
39 
40 static void measure_column(int col, const char *s)
41 {
42   size_t len = strlen(s);
43 
44   if (TT.column_widths[col] < len) TT.column_widths[col] = len;
45 }
46 
measure_numeric_column(int col,long long n)47 static void measure_numeric_column(int col, long long n)
48 {
49   snprintf(toybuf, sizeof(toybuf), "%lld", n);
50   return measure_column(col, toybuf);
51 }
52 
show_header()53 static void show_header()
54 {
55   TT.header_shown = 1;
56 
57   // The filesystem column is always at least this wide.
58   if (TT.column_widths[0] < 14) TT.column_widths[0] = 14;
59 
60   if (toys.optflags & (FLAG_H|FLAG_h)) {
61     xprintf("%-*s Size  Used Avail Use%% Mounted on\n",
62             TT.column_widths[0], "Filesystem");
63   } else {
64     const char *blocks_label = TT.units == 512 ? "512-blocks" : "1K-blocks";
65     const char *use_label = toys.optflags & FLAG_P ? "Capacity" : "Use%";
66 
67     measure_column(1, blocks_label);
68     measure_column(2, "Used");
69     measure_column(3, "Available");
70     measure_column(4, use_label);
71     xprintf("%-*s %*s %*s %*s %*s Mounted on\n",
72             TT.column_widths[0], "Filesystem",
73             TT.column_widths[1], blocks_label,
74             TT.column_widths[2], "Used",
75             TT.column_widths[3], "Available",
76             TT.column_widths[4], use_label);
77 
78     // For the "Use%" column, the trailing % should be inside the column.
79     TT.column_widths[4]--;
80   }
81 }
82 
show_mt(struct mtab_list * mt,int measuring)83 static void show_mt(struct mtab_list *mt, int measuring)
84 {
85   long long size, used, avail, percent, block;
86   char *device;
87 
88   // Return if it wasn't found (should never happen, but with /etc/mtab...)
89   if (!mt) return;
90 
91   // If we have -t, skip other filesystem types
92   if (TT.fstype) {
93     struct arg_list *al;
94 
95     for (al = TT.fstype; al; al = al->next)
96       if (!strcmp(mt->type, al->arg)) break;
97 
98     if (!al) return;
99   }
100 
101   // If we don't have -a, skip synthetic filesystems
102   if (!(toys.optflags & FLAG_a) && !mt->statvfs.f_blocks) return;
103 
104   // Figure out how much total/used/free space this filesystem has,
105   // forcing 64-bit math because filesystems are big now.
106   block = mt->statvfs.f_bsize ? mt->statvfs.f_bsize : 1;
107   size = (block * mt->statvfs.f_blocks) / TT.units;
108   used = (block * (mt->statvfs.f_blocks-mt->statvfs.f_bfree)) / TT.units;
109   avail = (block*(getuid()?mt->statvfs.f_bavail:mt->statvfs.f_bfree))/TT.units;
110   if (!(used+avail)) percent = 0;
111   else {
112     percent = (used*100)/(used+avail);
113     if (used*100 != percent*(used+avail)) percent++;
114   }
115 
116   device = *mt->device == '/' ? realpath(mt->device, NULL) : NULL;
117   if (!device) device = mt->device;
118 
119   if (measuring) {
120     measure_column(0, device);
121     measure_numeric_column(1, size);
122     measure_numeric_column(2, used);
123     measure_numeric_column(3, avail);
124   } else {
125     if (!TT.header_shown) show_header();
126 
127     if (toys.optflags & (FLAG_H|FLAG_h)) {
128       char *size_str = toybuf, *used_str = toybuf+64, *avail_str = toybuf+128;
129       int hr_flags = (toys.optflags & FLAG_H) ? HR_1000 : 0;
130 
131       human_readable(size_str, size, hr_flags);
132       human_readable(used_str, used, hr_flags);
133       human_readable(avail_str, avail, hr_flags);
134       xprintf("%-*s %4s  %4s  %4s % 3lld%% %s\n",
135         TT.column_widths[0], device,
136         size_str, used_str, avail_str, percent, mt->dir);
137     } else xprintf("%-*s %*lld %*lld %*lld %*lld%% %s\n",
138         TT.column_widths[0], device,
139         TT.column_widths[1], size,
140         TT.column_widths[2], used,
141         TT.column_widths[3], avail,
142         TT.column_widths[4], percent,
143         mt->dir);
144   }
145 
146   if (device != mt->device) free(device);
147 }
148 
df_main(void)149 void df_main(void)
150 {
151   struct mtab_list *mt, *mtstart, *mtend;
152   int measuring;
153 
154   if (toys.optflags & (FLAG_H|FLAG_h)) {
155     TT.units = 1;
156   } else {
157     // Units are 512 bytes if you select "pedantic" without "kilobytes".
158     TT.units = toys.optflags & FLAG_P ? 512 : 1024;
159   }
160 
161   if (!(mtstart = xgetmountlist(0))) return;
162   mtend = dlist_terminate(mtstart);
163 
164   // If we have a list of filesystems on the command line, loop through them.
165   if (*toys.optargs) {
166     // Measure the names then output the table.
167     for (measuring = 1; measuring >= 0; --measuring) {
168       char **next;
169 
170       for (next = toys.optargs; *next; next++) {
171         struct stat st;
172 
173         // Stat it (complain if we can't).
174         if (stat(*next, &st)) {
175           perror_msg("'%s'", *next);
176           continue;
177         }
178 
179         // Find and display this filesystem.  Use _last_ hit in case of
180         // overmounts (which is first hit in the reversed list).
181         for (mt = mtend; mt; mt = mt->prev) {
182           if (st.st_dev == mt->stat.st_dev
183               || (st.st_rdev && (st.st_rdev == mt->stat.st_dev)))
184           {
185             show_mt(mt, measuring);
186             break;
187           }
188         }
189       }
190     }
191   } else {
192     // Loop through mount list to filter out overmounts.
193     for (mt = mtend; mt; mt = mt->prev) {
194       struct mtab_list *mt2, *mt3;
195 
196       // 0:0 is LANANA null device
197       if (!mt->stat.st_dev) continue;
198 
199       // Filter out overmounts.
200       mt3 = mt;
201       for (mt2 = mt->prev; mt2; mt2 = mt2->prev) {
202         if (mt->stat.st_dev == mt2->stat.st_dev) {
203           // For --bind mounts, show earliest mount
204           if (!strcmp(mt->device, mt2->device)) {
205             if (!toys.optflags & FLAG_a) mt3->stat.st_dev = 0;
206             mt3 = mt2;
207           } else mt2->stat.st_dev = 0;
208         }
209       }
210     }
211 
212     // Measure the names then output the table.
213     for (measuring = 1; measuring >= 0; --measuring) {
214       // Cosmetic: show filesystems in creation order.
215       for (mt = mtstart; mt; mt = mt->next) {
216         if (mt->stat.st_dev) show_mt(mt, measuring);
217       }
218     }
219   }
220 
221   if (CFG_TOYBOX_FREE) llist_traverse(mtstart, free);
222 }
223