Mercurial > hg > toybox
comparison toys/posix/df.c @ 694:786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
The actual code should be the same afterward, this is just cosmetic refactoring.
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 13 Nov 2012 17:14:08 -0600 |
parents | c29e69a0e85e |
children | 6cc69be43c42 |
comparison
equal
deleted
inserted
replaced
693:4a5a250e0633 | 694:786841fdb1e0 |
---|---|
1 /* vi: set sw=4 ts=4: | 1 /* df.c - report free disk space. |
2 * | |
3 * df.c - report free disk space. | |
4 * | 2 * |
5 * Copyright 2006 Rob Landley <rob@landley.net> | 3 * Copyright 2006 Rob Landley <rob@landley.net> |
6 * | 4 * |
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/df.html | 5 * See http://opengroup.org/onlinepubs/9699919799/utilities/df.html |
8 | 6 |
9 USE_DF(NEWTOY(df, "Pkt*a", TOYFLAG_USR|TOYFLAG_SBIN)) | 7 USE_DF(NEWTOY(df, "Pkt*a", TOYFLAG_USR|TOYFLAG_SBIN)) |
10 | 8 |
11 config DF | 9 config DF |
12 bool "df (disk free)" | 10 bool "df (disk free)" |
13 default y | 11 default y |
14 help | 12 help |
15 usage: df [-t type] [FILESYSTEM ...] | 13 usage: df [-t type] [FILESYSTEM ...] |
16 | 14 |
17 The "disk free" command, df shows total/used/available disk space for | 15 The "disk free" command, df shows total/used/available disk space for |
18 each filesystem listed on the command line, or all currently mounted | 16 each filesystem listed on the command line, or all currently mounted |
19 filesystems. | 17 filesystems. |
20 | 18 |
21 -t type | 19 -t type Display only filesystems of this type. |
22 Display only filesystems of this type. | |
23 | 20 |
24 config DF_PEDANTIC | 21 config DF_PEDANTIC |
25 bool "options -P and -k" | 22 bool "options -P and -k" |
26 default y | 23 default y |
27 depends on DF | 24 depends on DF |
28 help | 25 help |
29 usage: df [-Pk] | 26 usage: df [-Pk] |
30 | 27 |
31 -P The SUSv3 "Pedantic" option | 28 -P The SUSv3 "Pedantic" option |
32 | 29 |
33 Provides a slightly less useful output format dictated by | 30 Provides a slightly less useful output format dictated by |
34 the Single Unix Specification version 3, and sets the | 31 the Single Unix Specification version 3, and sets the |
35 units to 512 bytes instead of the default 1024 bytes. | 32 units to 512 bytes instead of the default 1024 bytes. |
36 | 33 |
37 -k Sets units back to 1024 bytes (the default without -P) | 34 -k Sets units back to 1024 bytes (the default without -P) |
38 */ | 35 */ |
39 | 36 |
40 #define FOR_df | 37 #define FOR_df |
41 #include "toys.h" | 38 #include "toys.h" |
42 | 39 |
43 GLOBALS( | 40 GLOBALS( |
44 struct arg_list *fstype; | 41 struct arg_list *fstype; |
45 | 42 |
46 long units; | 43 long units; |
47 ) | 44 ) |
48 | 45 |
49 static void show_mt(struct mtab_list *mt) | 46 static void show_mt(struct mtab_list *mt) |
50 { | 47 { |
51 int len; | 48 int len; |
52 long long size, used, avail, percent, block; | 49 long long size, used, avail, percent, block; |
53 char *device; | 50 char *device; |
54 | 51 |
55 // Return if it wasn't found (should never happen, but with /etc/mtab...) | 52 // Return if it wasn't found (should never happen, but with /etc/mtab...) |
56 if (!mt) return; | 53 if (!mt) return; |
57 | 54 |
58 // If we have -t, skip other filesystem types | 55 // If we have -t, skip other filesystem types |
59 if (TT.fstype) { | 56 if (TT.fstype) { |
60 struct arg_list *al; | 57 struct arg_list *al; |
61 | 58 |
62 for (al = TT.fstype; al; al = al->next) { | 59 for (al = TT.fstype; al; al = al->next) |
63 if (!strcmp(mt->type, al->arg)) break; | 60 if (!strcmp(mt->type, al->arg)) break; |
64 } | |
65 if (!al) return; | |
66 } | |
67 | 61 |
68 // If we don't have -a, skip synthetic filesystems | 62 if (!al) return; |
69 if (!(toys.optflags & FLAG_a) && !mt->statvfs.f_blocks) return; | 63 } |
70 | 64 |
71 // Figure out how much total/used/free space this filesystem has, | 65 // If we don't have -a, skip synthetic filesystems |
72 // forcing 64-bit math because filesystems are big now. | 66 if (!(toys.optflags & FLAG_a) && !mt->statvfs.f_blocks) return; |
73 block = mt->statvfs.f_bsize ? mt->statvfs.f_bsize : 1; | |
74 size = (block * mt->statvfs.f_blocks) / TT.units; | |
75 used = (block * (mt->statvfs.f_blocks-mt->statvfs.f_bfree)) / TT.units; | |
76 avail = (block * (getuid() ? mt->statvfs.f_bavail : mt->statvfs.f_bfree)) | |
77 / TT.units; | |
78 if (!(used+avail)) percent = 0; | |
79 else { | |
80 percent = (used*100)/(used+avail); | |
81 if (used*100 != percent*(used+avail)) percent++; | |
82 } | |
83 | 67 |
84 device = *mt->device == '/' ? realpath(mt->device, NULL) : NULL; | 68 // Figure out how much total/used/free space this filesystem has, |
85 if (!device) device = mt->device; | 69 // forcing 64-bit math because filesystems are big now. |
70 block = mt->statvfs.f_bsize ? mt->statvfs.f_bsize : 1; | |
71 size = (block * mt->statvfs.f_blocks) / TT.units; | |
72 used = (block * (mt->statvfs.f_blocks-mt->statvfs.f_bfree)) / TT.units; | |
73 avail = (block * (getuid() ? mt->statvfs.f_bavail : mt->statvfs.f_bfree)) | |
74 / TT.units; | |
75 if (!(used+avail)) percent = 0; | |
76 else { | |
77 percent = (used*100)/(used+avail); | |
78 if (used*100 != percent*(used+avail)) percent++; | |
79 } | |
86 | 80 |
87 // Figure out appropriate spacing | 81 device = *mt->device == '/' ? realpath(mt->device, NULL) : NULL; |
88 len = 25 - strlen(device); | 82 if (!device) device = mt->device; |
89 if (len < 1) len = 1; | |
90 if (CFG_DF_PEDANTIC && (toys.optflags & FLAG_P)) { | |
91 xprintf("%s %lld %lld %lld %lld%% %s\n", device, size, used, avail, | |
92 percent, mt->dir); | |
93 } else { | |
94 xprintf("%s% *lld % 10lld % 9lld % 3lld%% %s\n", device, len, | |
95 size, used, avail, percent, mt->dir); | |
96 } | |
97 | 83 |
98 if (device != mt->device) free(device); | 84 // Figure out appropriate spacing |
85 len = 25 - strlen(device); | |
86 if (len < 1) len = 1; | |
87 if (CFG_DF_PEDANTIC && (toys.optflags & FLAG_P)) { | |
88 xprintf("%s %lld %lld %lld %lld%% %s\n", device, size, used, avail, | |
89 percent, mt->dir); | |
90 } else { | |
91 xprintf("%s% *lld % 10lld % 9lld % 3lld%% %s\n", device, len, | |
92 size, used, avail, percent, mt->dir); | |
93 } | |
94 | |
95 if (device != mt->device) free(device); | |
99 } | 96 } |
100 | 97 |
101 void df_main(void) | 98 void df_main(void) |
102 { | 99 { |
103 struct mtab_list *mt, *mt2, *mtlist; | 100 struct mtab_list *mt, *mt2, *mtlist; |
104 | 101 |
105 // Handle -P and -k | 102 // Handle -P and -k |
106 TT.units = 1024; | 103 TT.units = 1024; |
107 if (CFG_DF_PEDANTIC && (toys.optflags & FLAG_P)) { | 104 if (CFG_DF_PEDANTIC && (toys.optflags & FLAG_P)) { |
108 // Units are 512 bytes if you select "pedantic" without "kilobytes". | 105 // Units are 512 bytes if you select "pedantic" without "kilobytes". |
109 if ((toys.optflags&(FLAG_P|FLAG_k)) == FLAG_P) TT.units = 512; | 106 if ((toys.optflags&(FLAG_P|FLAG_k)) == FLAG_P) TT.units = 512; |
110 printf("Filesystem %ld-blocks Used Available Capacity Mounted on\n", | 107 printf("Filesystem %ld-blocks Used Available Capacity Mounted on\n", |
111 TT.units); | 108 TT.units); |
112 } else puts("Filesystem\t1K-blocks\tUsed Available Use% Mounted on"); | 109 } else puts("Filesystem\t1K-blocks\tUsed Available Use% Mounted on"); |
113 | 110 |
114 mtlist = getmountlist(1); | 111 mtlist = getmountlist(1); |
115 | 112 |
116 // If we have a list of filesystems on the command line, loop through them. | 113 // If we have a list of filesystems on the command line, loop through them. |
117 if (*toys.optargs) { | 114 if (*toys.optargs) { |
118 char **next; | 115 char **next; |
119 | 116 |
120 for(next = toys.optargs; *next; next++) { | 117 for(next = toys.optargs; *next; next++) { |
121 struct stat st; | 118 struct stat st; |
122 | 119 |
123 // Stat it (complain if we can't). | 120 // Stat it (complain if we can't). |
124 if(stat(*next, &st)) { | 121 if(stat(*next, &st)) { |
125 perror_msg("`%s'", *next); | 122 perror_msg("`%s'", *next); |
126 toys.exitval = 1; | 123 toys.exitval = 1; |
127 continue; | 124 continue; |
128 } | 125 } |
129 | 126 |
130 // Find and display this filesystem. Use _last_ hit in case of | 127 // Find and display this filesystem. Use _last_ hit in case of |
131 // -- bind mounts. | 128 // -- bind mounts. |
132 mt2 = NULL; | 129 mt2 = NULL; |
133 for (mt = mtlist; mt; mt = mt->next) { | 130 for (mt = mtlist; mt; mt = mt->next) { |
134 if (st.st_dev == mt->stat.st_dev) { | 131 if (st.st_dev == mt->stat.st_dev) { |
135 mt2 = mt; | 132 mt2 = mt; |
136 break; | 133 break; |
137 } | 134 } |
138 } | 135 } |
139 show_mt(mt2); | 136 show_mt(mt2); |
140 } | 137 } |
141 } else { | 138 } else { |
142 // Get and loop through mount list. | 139 // Get and loop through mount list. |
143 | 140 |
144 for (mt = mtlist; mt; mt = mt->next) { | 141 for (mt = mtlist; mt; mt = mt->next) { |
145 struct mtab_list *mt2, *mt3; | 142 struct mtab_list *mt2, *mt3; |
146 | 143 |
147 if (!mt->stat.st_dev) continue; | 144 if (!mt->stat.st_dev) continue; |
148 | 145 |
149 // Filter out overmounts. | 146 // Filter out overmounts. |
150 mt3 = mt; | 147 mt3 = mt; |
151 for (mt2 = mt->next; mt2; mt2 = mt2->next) { | 148 for (mt2 = mt->next; mt2; mt2 = mt2->next) { |
152 if (mt->stat.st_dev == mt2->stat.st_dev) { | 149 if (mt->stat.st_dev == mt2->stat.st_dev) { |
153 // For --bind mounts, take last match | 150 // For --bind mounts, take last match |
154 if (!strcmp(mt->device, mt2->device)) mt3 = mt2; | 151 if (!strcmp(mt->device, mt2->device)) mt3 = mt2; |
155 // Filter out overmounts | 152 // Filter out overmounts |
156 mt2->stat.st_dev = 0; | 153 mt2->stat.st_dev = 0; |
157 } | 154 } |
158 } | 155 } |
159 show_mt(mt3); | 156 show_mt(mt3); |
160 } | 157 } |
161 } | 158 } |
162 | 159 |
163 if (CFG_TOYBOX_FREE) llist_traverse(mtlist, free); | 160 if (CFG_TOYBOX_FREE) llist_traverse(mtlist, free); |
164 } | 161 } |