Mercurial > hg > toybox
comparison toys/posix/df.c @ 681:44904c7212e7
Follow symlinks to get actual device name, getmountlist() reverses order for us now, detect stdout to full device.
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 26 Oct 2012 21:15:31 -0500 |
parents | 7e846e281e38 |
children | 598263aee2b9 |
comparison
equal
deleted
inserted
replaced
680:ff2cf02d9703 | 681:44904c7212e7 |
---|---|
49 static void show_mt(struct mtab_list *mt) | 49 static void show_mt(struct mtab_list *mt) |
50 { | 50 { |
51 int len; | 51 int len; |
52 long size, used, avail, percent; | 52 long size, used, avail, percent; |
53 uint64_t block; | 53 uint64_t block; |
54 char *device; | |
54 | 55 |
55 // Return if it wasn't found (should never happen, but with /etc/mtab...) | 56 // Return if it wasn't found (should never happen, but with /etc/mtab...) |
56 if (!mt) return; | 57 if (!mt) return; |
57 | 58 |
58 // If we have -t, skip other filesystem types | 59 // If we have -t, skip other filesystem types |
77 avail = (long)((block | 78 avail = (long)((block |
78 * (getuid() ? mt->statvfs.f_bavail : mt->statvfs.f_bfree)) | 79 * (getuid() ? mt->statvfs.f_bavail : mt->statvfs.f_bfree)) |
79 / TT.units); | 80 / TT.units); |
80 percent = size ? 100-(long)((100*(uint64_t)avail)/size) : 0; | 81 percent = size ? 100-(long)((100*(uint64_t)avail)/size) : 0; |
81 | 82 |
83 device = *mt->device == '/' ? realpath(mt->device, NULL) : NULL; | |
84 if (!device) device = mt->device; | |
85 | |
82 // Figure out appropriate spacing | 86 // Figure out appropriate spacing |
83 len = 25 - strlen(mt->device); | 87 len = 25 - strlen(device); |
84 if (len < 1) len = 1; | 88 if (len < 1) len = 1; |
85 if (CFG_DF_PEDANTIC && (toys.optflags & FLAG_P)) { | 89 if (CFG_DF_PEDANTIC && (toys.optflags & FLAG_P)) { |
86 printf("%s %ld %ld %ld %ld%% %s\n", mt->device, size, used, avail, | 90 xprintf("%s %ld %ld %ld %ld%% %s\n", device, size, used, avail, |
87 percent, mt->dir); | 91 percent, mt->dir); |
88 } else { | 92 } else { |
89 printf("%s% *ld % 10ld % 9ld % 3ld%% %s\n",mt->device, len, | 93 xprintf("%s% *ld % 10ld % 9ld % 3ld%% %s\n", device, len, |
90 size, used, avail, percent, mt->dir); | 94 size, used, avail, percent, mt->dir); |
91 } | 95 } |
96 | |
97 if (device != mt->device) free(device); | |
92 } | 98 } |
93 | 99 |
94 void df_main(void) | 100 void df_main(void) |
95 { | 101 { |
96 struct mtab_list *mt, *mt2, *mtlist; | 102 struct mtab_list *mt, *mt2, *mtlist; |
121 } | 127 } |
122 | 128 |
123 // Find and display this filesystem. Use _last_ hit in case of | 129 // Find and display this filesystem. Use _last_ hit in case of |
124 // -- bind mounts. | 130 // -- bind mounts. |
125 mt2 = NULL; | 131 mt2 = NULL; |
126 for (mt = mtlist; mt; mt = mt->next) | 132 for (mt = mtlist; mt; mt = mt->next) { |
127 if (st.st_dev == mt->stat.st_dev) mt2 = mt; | 133 if (st.st_dev == mt->stat.st_dev) { |
134 mt2 = mt; | |
135 break; | |
136 } | |
137 } | |
128 show_mt(mt2); | 138 show_mt(mt2); |
129 } | 139 } |
130 } else { | 140 } else { |
131 // Get and loop through mount list. | 141 // Get and loop through mount list. |
132 | 142 |