Mercurial > hg > toybox
annotate toys/df.c @ 9:8f8a8ac59c14 0.0.1
Closer support for "pedantic" option for SUSv3. (Ok, it's %ld instead of the
%d the spec says, but same output. And you can't actually select it until I
get menuconfig in. But hey...)
author | landley@driftwood |
---|---|
date | Mon, 30 Oct 2006 11:18:30 -0500 |
parents | fc9c0503d5e2 |
children | eb46bb5626cb |
rev | line source |
---|---|
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
1 /* vi: set sw=4 ts=4: */ |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
2 /* |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
3 * df.c - report free disk space. |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
4 * |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
5 * Implemented roughly according to SUSv3: |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
6 * http://www.opengroup.org/onlinepubs/009695399/utilities/df.html |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
7 * |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
8 * usage: df [-k] [-P|-t] [file...] |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
9 */ |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
10 |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
diff
changeset
|
11 #include "toys.h" |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
diff
changeset
|
12 |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
13 static void show_mt(struct mtab_list *mt) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
14 { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
15 int len; |
9
8f8a8ac59c14
Closer support for "pedantic" option for SUSv3. (Ok, it's %ld instead of the
landley@driftwood
parents:
7
diff
changeset
|
16 long size, used, avail, percent; |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
17 uint64_t block; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
18 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
19 // Return if it wasn't found (should never happen, but with /etc/mtab...) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
20 if (!mt) return; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
21 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
22 // If we have -t, skip other filesystem types |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
23 if (toy.df.fstype) { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
24 struct string_list *sl; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
25 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
26 for (sl = toy.df.fstype; sl; sl = sl->next) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
27 if (!strcmp(mt->type, sl->str)) break; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
28 if (!sl) return; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
29 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
30 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
31 // If we don't have -a, skip synthetic filesystems |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
32 if (!(toys.optflags & 1) && !mt->statvfs.f_blocks) return; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
33 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
34 // Figure out how much total/used/free space this filesystem has, |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
35 // forcing 64-bit math because filesystems are big now. |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
36 block = mt->statvfs.f_bsize ? : 1; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
37 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
38 size = (long)((block * mt->statvfs.f_blocks) / toy.df.units); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
39 used = (long)((block * (mt->statvfs.f_blocks-mt->statvfs.f_bfree)) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
40 / toy.df.units); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
41 avail = (long)((block |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
42 * (getuid() ? mt->statvfs.f_bavail : mt->statvfs.f_bfree)) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
43 / toy.df.units); |
9
8f8a8ac59c14
Closer support for "pedantic" option for SUSv3. (Ok, it's %ld instead of the
landley@driftwood
parents:
7
diff
changeset
|
44 percent = 100-(long)((100*(uint64_t)avail)/size); |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
45 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
46 // Figure out appropriate spacing |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
47 len = 25 - strlen(mt->device); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
48 if (len < 1) len = 1; |
9
8f8a8ac59c14
Closer support for "pedantic" option for SUSv3. (Ok, it's %ld instead of the
landley@driftwood
parents:
7
diff
changeset
|
49 if (CFG_DF_PEDANTIC && (toys.optflags & 8)) { |
8f8a8ac59c14
Closer support for "pedantic" option for SUSv3. (Ok, it's %ld instead of the
landley@driftwood
parents:
7
diff
changeset
|
50 printf("%s %ld %ld %ld %ld%% %s\n", mt->device, size, used, avail, |
8f8a8ac59c14
Closer support for "pedantic" option for SUSv3. (Ok, it's %ld instead of the
landley@driftwood
parents:
7
diff
changeset
|
51 percent, mt->dir); |
8f8a8ac59c14
Closer support for "pedantic" option for SUSv3. (Ok, it's %ld instead of the
landley@driftwood
parents:
7
diff
changeset
|
52 } else { |
8f8a8ac59c14
Closer support for "pedantic" option for SUSv3. (Ok, it's %ld instead of the
landley@driftwood
parents:
7
diff
changeset
|
53 printf("%s% *ld % 10ld % 9ld % 3ld%% %s\n",mt->device, len, |
8f8a8ac59c14
Closer support for "pedantic" option for SUSv3. (Ok, it's %ld instead of the
landley@driftwood
parents:
7
diff
changeset
|
54 size, used, avail, percent, mt->dir); |
8f8a8ac59c14
Closer support for "pedantic" option for SUSv3. (Ok, it's %ld instead of the
landley@driftwood
parents:
7
diff
changeset
|
55 } |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
56 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
57 |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
diff
changeset
|
58 int df_main(void) |
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
diff
changeset
|
59 { |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
60 struct mtab_list *mt, *mt2, *mtlist; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
61 char **argv; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
62 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
63 // get_optflags("Pkt:a",&(toy.df.fstype)); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
64 argv = NULL; |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
65 |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
66 // Handle -P and -k |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
67 toy.df.units = 1024; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
68 if (CFG_DF_PEDANTIC && (toys.optflags & 8)) { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
69 // Units are 512 bytes if you select "pedantic" without "kilobytes". |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
70 if ((toys.optflags&3) == 1) toy.df.units = 512; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
71 printf("Filesystem %ld-blocks Used Available Capacity Mounted on\n", |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
72 toy.df.units); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
73 } else puts("Filesystem\t1K-blocks\tUsed Available Use% Mounted on"); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
74 |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
75 mtlist = getmountlist(1); |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
76 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
77 // If we have a list of filesystems on the command line, loop through them. |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
78 if (argv) { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
79 char *next; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
80 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
81 for(next = *argv; *next; next++) { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
82 struct stat st; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
83 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
84 // Stat it (complain if we can't). |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
85 if(!stat(next, &st)) { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
86 perror_msg("`%s'", next); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
87 toys.exitval = 1; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
88 continue; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
89 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
90 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
91 // Find and display this filesystem. Use _last_ hit in case of |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
92 // -- bind mounts. |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
93 mt2 = NULL; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
94 for (mt = mtlist; mt; mt = mt->next) |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
95 if (st.st_dev == mt->stat.st_dev) mt2 = mt; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
96 show_mt(mt2); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
97 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
98 } else { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
99 // Get and loop through mount list. |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
100 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
101 for (mt = mtlist; mt; mt = mt->next) { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
102 struct mtab_list *mt2, *mt3; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
103 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
104 if (!mt->stat.st_dev) continue; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
105 |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
106 // Filter out overmounts. |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
107 mt3 = mt; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
108 for (mt2 = mt->next; mt2; mt2 = mt2->next) { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
109 if (mt->stat.st_dev == mt2->stat.st_dev) { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
110 // For --bind mounts, take last match |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
111 if (!strcmp(mt->device, mt2->device)) mt3 = mt2; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
112 // Filter out overmounts |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
113 mt2->stat.st_dev = 0; |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
114 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
115 } |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
116 show_mt(mt3); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
117 } |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
118 } |
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
119 |
7
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
120 if (CFG_TOYS_FREE) { |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
121 llist_free(mtlist, NULL); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
122 free(argv); |
fc9c0503d5e2
Implement df. Add -Wall to build and fix up warnings. Add copyright notices.
landley@driftwood
parents:
2
diff
changeset
|
123 } |
2
67b517913e56
Infrastructure, first drop of toy shell, and a bit of work on df.
landley@driftwood
parents:
1
diff
changeset
|
124 return 0; |
1
59d58fab67c6
Next snapshot. Tries to grab something out of lib in order to build, I have
landley@driftwood
parents:
diff
changeset
|
125 } |