1 | /*
|
---|
2 | * Copyright (c) 2013 Manuele Conti
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup df
|
---|
30 | * @brief Df utility.
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <unistd.h>
|
---|
40 | #include <stdint.h>
|
---|
41 | #include <getopt.h>
|
---|
42 | #include <sys/statfs.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <adt/list.h>
|
---|
45 | #include <vfs/vfs.h>
|
---|
46 |
|
---|
47 | #define NAME "df"
|
---|
48 |
|
---|
49 | #define HEADER_TABLE "Filesystem %4u-blocks Used Available Used%% Mounted on"
|
---|
50 | #define HEADER_TABLE_HR "Filesystem Size Used Available Used%% Mounted on"
|
---|
51 |
|
---|
52 | #define PERCENTAGE(x, tot) ((unsigned long long) (100L * (x) / (tot)))
|
---|
53 | #define FSBK_TO_BK(x, fsbk, bk) \
|
---|
54 | (((fsbk) != 0 && (fsbk) < (bk)) ? \
|
---|
55 | (unsigned long long) ((x) / ((bk) / (fsbk))) : \
|
---|
56 | (unsigned long long) ((x) * ((fsbk) / (bk))))
|
---|
57 |
|
---|
58 | static unsigned int unit_size;
|
---|
59 | static unsigned int human_readable;
|
---|
60 |
|
---|
61 | static int size_to_human_readable(char buf[], uint64_t bytes);
|
---|
62 | static void print_header(void);
|
---|
63 | static void print_statfs(struct statfs *, char *, char *);
|
---|
64 | static void print_usage(void);
|
---|
65 |
|
---|
66 | int main(int argc, char *argv[])
|
---|
67 | {
|
---|
68 | int optres, errflg = 0;
|
---|
69 | struct statfs st;
|
---|
70 |
|
---|
71 | unit_size = 512;
|
---|
72 | human_readable = 0;
|
---|
73 |
|
---|
74 | /******************************************/
|
---|
75 | /* Parse command line options... */
|
---|
76 | /******************************************/
|
---|
77 | while ((optres = getopt(argc, argv, ":uhb:")) != -1) {
|
---|
78 | switch(optres) {
|
---|
79 | case 'u':
|
---|
80 | print_usage();
|
---|
81 | return 0;
|
---|
82 |
|
---|
83 | case 'h':
|
---|
84 | human_readable = 1;
|
---|
85 | break;
|
---|
86 |
|
---|
87 | case 'b':
|
---|
88 | str_uint32_t(optarg, NULL, 0, 0, &unit_size);
|
---|
89 | break;
|
---|
90 |
|
---|
91 | case ':':
|
---|
92 | fprintf(stderr, "Option -%c requires an operand\n", optopt);
|
---|
93 | errflg++;
|
---|
94 | break;
|
---|
95 |
|
---|
96 | case '?':
|
---|
97 | fprintf(stderr, "Unrecognized option: -%c\n", optopt);
|
---|
98 | errflg++;
|
---|
99 | break;
|
---|
100 |
|
---|
101 | default:
|
---|
102 | fprintf(stderr, "Unknown error while parsing command line options.");
|
---|
103 | errflg++;
|
---|
104 | break;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | if (optind > argc) {
|
---|
109 | fprintf(stderr, "Too many input parameter\n");
|
---|
110 | errflg++;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (errflg) {
|
---|
114 | print_usage();
|
---|
115 | return 1;
|
---|
116 | }
|
---|
117 |
|
---|
118 | LIST_INITIALIZE(mtab_list);
|
---|
119 | get_mtab_list(&mtab_list);
|
---|
120 | print_header();
|
---|
121 | list_foreach(mtab_list, cur) {
|
---|
122 | mtab_ent_t *mtab_ent = list_get_instance(cur, mtab_ent_t,
|
---|
123 | link);
|
---|
124 | statfs(mtab_ent->mp, &st);
|
---|
125 | print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp);
|
---|
126 | }
|
---|
127 | putchar('\n');
|
---|
128 | return 0;
|
---|
129 | }
|
---|
130 |
|
---|
131 | static int size_to_human_readable(char buf[], uint64_t bytes)
|
---|
132 | {
|
---|
133 | const char *units = "BkMGTPEZY";
|
---|
134 | int i = 0;
|
---|
135 | int limit;
|
---|
136 |
|
---|
137 | limit = str_length(units);
|
---|
138 | while (bytes >= 1024) {
|
---|
139 | if (i >= limit)
|
---|
140 | return -1;
|
---|
141 | bytes /= 1024;
|
---|
142 | i++;
|
---|
143 | }
|
---|
144 | snprintf(buf, 6, "%4llu%c", (unsigned long long)bytes, units[i]);
|
---|
145 |
|
---|
146 | return 0;
|
---|
147 | }
|
---|
148 |
|
---|
149 | static void print_header(void)
|
---|
150 | {
|
---|
151 | if (human_readable)
|
---|
152 | printf(HEADER_TABLE_HR);
|
---|
153 | else
|
---|
154 | printf(HEADER_TABLE, unit_size);
|
---|
155 | putchar('\n');
|
---|
156 | }
|
---|
157 |
|
---|
158 | static void print_statfs(struct statfs *st, char *name, char *mountpoint)
|
---|
159 | {
|
---|
160 | printf("%10s", name);
|
---|
161 |
|
---|
162 | if (human_readable) {
|
---|
163 | char tmp[1024];
|
---|
164 | size_to_human_readable(tmp, st->f_blocks * st->f_bsize);
|
---|
165 | printf(" %14s", tmp); /* Size */
|
---|
166 | size_to_human_readable(tmp, (st->f_blocks - st->f_bfree) * st->f_bsize);
|
---|
167 | printf(" %14s", tmp); /* Used */
|
---|
168 | size_to_human_readable(tmp, st->f_bfree * st->f_bsize);
|
---|
169 | printf(" %14s", tmp); /* Available */
|
---|
170 | printf(" %4llu%% %s\n",
|
---|
171 | (st->f_blocks)?PERCENTAGE(st->f_blocks - st->f_bfree, st->f_blocks):0L, /* Used% */
|
---|
172 | mountpoint /* Mounted on */
|
---|
173 | );
|
---|
174 | }
|
---|
175 | else
|
---|
176 | printf(" %15llu %14llu %14llu %4llu%% %s\n",
|
---|
177 | FSBK_TO_BK(st->f_blocks, st->f_bsize, unit_size), /* Blocks */
|
---|
178 | FSBK_TO_BK(st->f_blocks - st->f_bfree, st->f_bsize, unit_size), /* Used */
|
---|
179 | FSBK_TO_BK(st->f_bfree, st->f_bsize, unit_size), /* Available */
|
---|
180 | (st->f_blocks)?PERCENTAGE(st->f_blocks - st->f_bfree, st->f_blocks):0L, /* Used% */
|
---|
181 | mountpoint /* Mounted on */
|
---|
182 | );
|
---|
183 |
|
---|
184 | }
|
---|
185 |
|
---|
186 | static void print_usage(void)
|
---|
187 | {
|
---|
188 | printf("syntax: %s [-h] [-i]\n", NAME);
|
---|
189 | printf(" u : Print usage.\n");
|
---|
190 | printf(" h : \"Human-readable\" output.\n");
|
---|
191 | printf(" b : Scale block sizes by selected size.\n");
|
---|
192 | printf("\n");
|
---|
193 | }
|
---|
194 |
|
---|
195 | /** @}
|
---|
196 | */
|
---|