source: mainline/uspace/app/df/df.c@ 3e896e1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3e896e1 was 651c8db, checked in by Maurizio Lombardi <m.lombardi85@…>, 12 years ago

df: remove redundant code

  • Property mode set to 100644
File size: 5.2 KB
Line 
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) (tot ? (100ULL * (x) / (tot)) : 0)
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
58static unsigned int unit_size;
59static unsigned int human_readable;
60
61static void size_to_human_readable(char *buf, uint64_t bytes);
62static void print_header(void);
63static void print_statfs(struct statfs *, char *, char *);
64static void print_usage(void);
65
66int 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",
93 optopt);
94 errflg++;
95 break;
96
97 case '?':
98 fprintf(stderr, "Unrecognized option: -%c\n", optopt);
99 errflg++;
100 break;
101
102 default:
103 fprintf(stderr,
104 "Unknown error while parsing command line options");
105 errflg++;
106 break;
107 }
108 }
109
110 if (optind > argc) {
111 fprintf(stderr, "Too many input parameters\n");
112 errflg++;
113 }
114
115 if (errflg) {
116 print_usage();
117 return 1;
118 }
119
120 LIST_INITIALIZE(mtab_list);
121 get_mtab_list(&mtab_list);
122
123 print_header();
124 list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
125 statfs(mtab_ent->mp, &st);
126 print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp);
127 }
128
129 putchar('\n');
130 return 0;
131}
132
133static void size_to_human_readable(char *buf, uint64_t bytes)
134{
135 const char *units = "BkMGTPEZY";
136 int i = 0;
137
138 while (bytes >= 1024) {
139 bytes /= 1024;
140 i++;
141 }
142
143 snprintf(buf, 6, "%4llu%c", (unsigned long long) bytes, units[i]);
144}
145
146static void print_header(void)
147{
148 if (human_readable)
149 printf(HEADER_TABLE_HR);
150 else
151 printf(HEADER_TABLE, unit_size);
152
153 putchar('\n');
154}
155
156static void print_statfs(struct statfs *st, char *name, char *mountpoint)
157{
158 uint64_t const used_blocks = st->f_blocks - st->f_bfree;
159 unsigned const perc_used = PERCENTAGE(used_blocks, st->f_blocks);
160
161 printf("%10s", name);
162
163 if (human_readable) {
164 char tmp[1024];
165
166 /* Print size */
167 size_to_human_readable(tmp, st->f_blocks * st->f_bsize);
168 printf(" %14s", tmp);
169
170 /* Number of used blocks */
171 size_to_human_readable(tmp, used_blocks * st->f_bsize);
172 printf(" %14s", tmp);
173
174 /* Number of available blocks */
175 size_to_human_readable(tmp, st->f_bfree * st->f_bsize);
176 printf(" %14s", tmp);
177
178 /* Percentage of used blocks */
179 printf(" %4u%%", perc_used);
180
181 /* Mount point */
182 printf(" %s\n", mountpoint);
183 } else {
184 /* Blocks / Used blocks / Available blocks / Used% / Mounted on */
185 printf(" %15llu %14llu %14llu %4u%% %s\n",
186 FSBK_TO_BK(st->f_blocks, st->f_bsize, unit_size),
187 FSBK_TO_BK(used_blocks, st->f_bsize, unit_size),
188 FSBK_TO_BK(st->f_bfree, st->f_bsize, unit_size),
189 perc_used,
190 mountpoint);
191 }
192
193}
194
195static void print_usage(void)
196{
197 printf("syntax: %s [-u] [-h] [-b <size>] \n", NAME);
198 printf(" u : Show usage.\n");
199 printf(" h : \"Human-readable\" output.\n");
200 printf(" b : Scale block sizes by selected size.\n");
201 printf("\n");
202}
203
204/** @}
205 */
Note: See TracBrowser for help on using the repository browser.