source: mainline/uspace/app/df/df.c@ 582a0b8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 582a0b8 was 582a0b8, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove unistd.h

  • Rename usleep() and sleep() to thread_usleep() and thread_sleep() and move to thread.[hc].
  • Include stddef.h in order to provide NULL.
  • Move getpagesize() to libposix.
  • Sync uspace/dist/src/c/demos with originals.
  • Property mode set to 100644
File size: 5.3 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 <stdint.h>
40#include <getopt.h>
41#include <errno.h>
42#include <adt/list.h>
43#include <vfs/vfs.h>
44#include <vfs/vfs_mtab.h>
45
46#define NAME "df"
47
48#define HEADER_TABLE "Filesystem %4u-blocks Used Available Used%% Mounted on"
49#define HEADER_TABLE_HR "Filesystem Size Used Available Used%% Mounted on"
50
51#define PERCENTAGE(x, tot) (tot ? (100ULL * (x) / (tot)) : 0)
52#define FSBK_TO_BK(x, fsbk, bk) \
53 (((fsbk) != 0 && (fsbk) < (bk)) ? \
54 (unsigned long long) ((x) / ((bk) / (fsbk))) : \
55 (unsigned long long) ((x) * ((fsbk) / (bk))))
56
57static unsigned int unit_size;
58static unsigned int human_readable;
59
60static void size_to_human_readable(char *buf, uint64_t bytes);
61static void print_header(void);
62static void print_statfs(struct statfs *, char *, char *);
63static void print_usage(void);
64
65int main(int argc, char *argv[])
66{
67 int optres, errflg = 0;
68 struct statfs st;
69
70 unit_size = 512;
71 human_readable = 0;
72
73 /******************************************/
74 /* Parse command line options... */
75 /******************************************/
76 while ((optres = getopt(argc, argv, ":uhb:")) != -1) {
77 switch(optres) {
78 case 'u':
79 print_usage();
80 return 0;
81
82 case 'h':
83 human_readable = 1;
84 break;
85
86 case 'b':
87 str_uint32_t(optarg, NULL, 0, 0, &unit_size);
88 break;
89
90 case ':':
91 fprintf(stderr, "Option -%c requires an operand\n",
92 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,
103 "Unknown error while parsing command line options");
104 errflg++;
105 break;
106 }
107 }
108
109 if (optind > argc) {
110 fprintf(stderr, "Too many input parameters\n");
111 errflg++;
112 }
113
114 if (errflg) {
115 print_usage();
116 return 1;
117 }
118
119 LIST_INITIALIZE(mtab_list);
120 vfs_get_mtab_list(&mtab_list);
121
122 print_header();
123 list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
124 if (vfs_statfs_path(mtab_ent->mp, &st) == 0) {
125 print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp);
126 } else {
127 fprintf(stderr, "Cannot get information for '%s' (%d).\n",
128 mtab_ent->mp, errno);
129 }
130 }
131
132 putchar('\n');
133 return 0;
134}
135
136static void size_to_human_readable(char *buf, uint64_t bytes)
137{
138 const char *units = "BkMGTPEZY";
139 int i = 0;
140
141 while (bytes >= 1024) {
142 bytes /= 1024;
143 i++;
144 }
145
146 snprintf(buf, 6, "%4llu%c", (unsigned long long) bytes, units[i]);
147}
148
149static void print_header(void)
150{
151 if (human_readable)
152 printf(HEADER_TABLE_HR);
153 else
154 printf(HEADER_TABLE, unit_size);
155
156 putchar('\n');
157}
158
159static void print_statfs(struct statfs *st, char *name, char *mountpoint)
160{
161 uint64_t const used_blocks = st->f_blocks - st->f_bfree;
162 unsigned const perc_used = PERCENTAGE(used_blocks, st->f_blocks);
163
164 printf("%10s", name);
165
166 if (human_readable) {
167 char tmp[1024];
168
169 /* Print size */
170 size_to_human_readable(tmp, st->f_blocks * st->f_bsize);
171 printf(" %14s", tmp);
172
173 /* Number of used blocks */
174 size_to_human_readable(tmp, used_blocks * st->f_bsize);
175 printf(" %14s", tmp);
176
177 /* Number of available blocks */
178 size_to_human_readable(tmp, st->f_bfree * st->f_bsize);
179 printf(" %14s", tmp);
180
181 /* Percentage of used blocks */
182 printf(" %4u%%", perc_used);
183
184 /* Mount point */
185 printf(" %s\n", mountpoint);
186 } else {
187 /* Blocks / Used blocks / Available blocks / Used% / Mounted on */
188 printf(" %15llu %14llu %14llu %4u%% %s\n",
189 FSBK_TO_BK(st->f_blocks, st->f_bsize, unit_size),
190 FSBK_TO_BK(used_blocks, st->f_bsize, unit_size),
191 FSBK_TO_BK(st->f_bfree, st->f_bsize, unit_size),
192 perc_used,
193 mountpoint);
194 }
195
196}
197
198static void print_usage(void)
199{
200 printf("syntax: %s [-u] [-h] [-b <size>] \n", NAME);
201 printf(" u : Show usage.\n");
202 printf(" h : \"Human-readable\" output.\n");
203 printf(" b : Scale block sizes by selected size.\n");
204 printf("\n");
205}
206
207/** @}
208 */
Note: See TracBrowser for help on using the repository browser.