source: mainline/uspace/app/df/df.c@ c24b0dcb

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

Rename capacity-related 'cap' to 'capa'

This allows to use 'cap' for capabilities.

  • Property mode set to 100644
File size: 5.0 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 Print amounts of free and used disk space.
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <capa.h>
38#include <stdbool.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <stdint.h>
42#include <str_error.h>
43#include <getopt.h>
44#include <errno.h>
45#include <adt/list.h>
46#include <vfs/vfs.h>
47#include <vfs/vfs_mtab.h>
48
49#define NAME "df"
50
51#define HEADER_TABLE "Filesystem Size Used Available Used%% Mounted on"
52#define HEADER_TABLE_BLK "Filesystem Blk. Size Total Used Available Used%% Mounted on"
53
54#define PERCENTAGE(x, tot) (tot ? (100ULL * (x) / (tot)) : 0)
55
56static bool display_blocks;
57
58static errno_t size_to_human_readable(uint64_t, size_t, char **);
59static void print_header(void);
60static errno_t print_statfs(vfs_statfs_t *, char *, char *);
61static void print_usage(void);
62
63int main(int argc, char *argv[])
64{
65 int optres, errflg = 0;
66 vfs_statfs_t st;
67 errno_t rc;
68
69 display_blocks = false;
70
71 /* Parse command-line options */
72 while ((optres = getopt(argc, argv, "ubh")) != -1) {
73 switch (optres) {
74 case 'h':
75 print_usage();
76 return 0;
77
78 case 'b':
79 display_blocks = true;
80 break;
81
82 case '?':
83 fprintf(stderr, "Unrecognized option: -%c\n", optopt);
84 errflg++;
85 break;
86
87 default:
88 fprintf(stderr,
89 "Unknown error while parsing command line options");
90 errflg++;
91 break;
92 }
93 }
94
95 if (optind > argc) {
96 fprintf(stderr, "Too many input parameters\n");
97 errflg++;
98 }
99
100 if (errflg) {
101 print_usage();
102 return 1;
103 }
104
105 LIST_INITIALIZE(mtab_list);
106 vfs_get_mtab_list(&mtab_list);
107
108 print_header();
109 list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
110 if (vfs_statfs_path(mtab_ent->mp, &st) == 0) {
111 rc = print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp);
112 if (rc != EOK)
113 return 1;
114 } else {
115 fprintf(stderr, "Cannot get information for '%s' (%s).\n",
116 mtab_ent->mp, str_error(errno));
117 }
118 }
119
120 putchar('\n');
121 return 0;
122}
123
124static errno_t size_to_human_readable(uint64_t nblocks, size_t block_size, char **rptr)
125{
126 capa_spec_t capa;
127
128 capa_from_blocks(nblocks, block_size, &capa);
129 capa_simplify(&capa);
130 return capa_format(&capa, rptr);
131}
132
133static void print_header(void)
134{
135 if (!display_blocks)
136 printf(HEADER_TABLE);
137 else
138 printf(HEADER_TABLE_BLK);
139
140 putchar('\n');
141}
142
143static errno_t print_statfs(vfs_statfs_t *st, char *name, char *mountpoint)
144{
145 uint64_t const used_blocks = st->f_blocks - st->f_bfree;
146 unsigned const perc_used = PERCENTAGE(used_blocks, st->f_blocks);
147 char *str;
148 errno_t rc;
149
150 printf("%10s", name);
151
152 if (!display_blocks) {
153 /* Print size */
154 rc = size_to_human_readable(st->f_blocks, st->f_bsize, &str);
155 if (rc != EOK)
156 goto error;
157 printf(" %14s", str);
158 free(str);
159
160 /* Number of used blocks */
161 rc = size_to_human_readable(used_blocks, st->f_bsize, &str);
162 if (rc != EOK)
163 goto error;
164 printf(" %14s", str);
165 free(str);
166
167 /* Number of available blocks */
168 rc = size_to_human_readable(st->f_bfree, st->f_bsize, &str);
169 if (rc != EOK)
170 goto error;
171 printf(" %14s", str);
172 free(str);
173
174 /* Percentage of used blocks */
175 printf(" %4u%%", perc_used);
176
177 /* Mount point */
178 printf(" %s\n", mountpoint);
179 } else {
180 /* Block size / Blocks / Used blocks / Available blocks / Used% / Mounted on */
181 printf(" %10" PRIu32 " %9" PRIu64 " %11" PRIu64 " %11" PRIu64 " %4u%% %s\n",
182 st->f_bsize, st->f_blocks, used_blocks, st->f_bfree,
183 perc_used, mountpoint);
184 }
185
186 return EOK;
187error:
188 printf("\nError: Out of memory.\n");
189 return ENOMEM;
190}
191
192static void print_usage(void)
193{
194 printf("Syntax: %s [<options>] \n", NAME);
195 printf("Options:\n");
196 printf(" -h Print help\n");
197 printf(" -b Print exact block sizes and numbers\n");
198}
199
200/** @}
201 */
Note: See TracBrowser for help on using the repository browser.