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

Last change on this file since bb4d0b5 was c111da2, checked in by Jiri Svoboda <jiri@…>, 5 months ago

Create non-zero size file in Navigator, new newfile utility.

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Copyright (c) 2025 Jiri Svoboda
3 * Copyright (c) 2013 Manuele Conti
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup df
31 * @brief Print amounts of free and used disk space.
32 * @{
33 */
34/**
35 * @file
36 */
37
38#include <capa.h>
39#include <stdbool.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <stdint.h>
43#include <str_error.h>
44#include <getopt.h>
45#include <errno.h>
46#include <adt/list.h>
47#include <vfs/vfs.h>
48#include <vfs/vfs_mtab.h>
49
50#define NAME "df"
51
52#define HEADER_TABLE "Filesystem Size Used Available Used%% Mounted on"
53#define HEADER_TABLE_BLK "Filesystem Blk. Size Total Used Available Used%% Mounted on"
54
55#define PERCENTAGE(x, tot) (tot ? (100ULL * (x) / (tot)) : 0)
56
57static bool display_blocks;
58
59static void print_header(void);
60static void 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
68 display_blocks = false;
69
70 /* Parse command-line options */
71 while ((optres = getopt(argc, argv, "ubh")) != -1) {
72 switch (optres) {
73 case 'h':
74 print_usage();
75 return 0;
76
77 case 'b':
78 display_blocks = true;
79 break;
80
81 case '?':
82 fprintf(stderr, "Unrecognized option: -%c\n", optopt);
83 errflg++;
84 break;
85
86 default:
87 fprintf(stderr,
88 "Unknown error while parsing command line options");
89 errflg++;
90 break;
91 }
92 }
93
94 if (optind > argc) {
95 fprintf(stderr, "Too many input parameters\n");
96 errflg++;
97 }
98
99 if (errflg) {
100 print_usage();
101 return 1;
102 }
103
104 LIST_INITIALIZE(mtab_list);
105 vfs_get_mtab_list(&mtab_list);
106
107 print_header();
108 list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
109 if (vfs_statfs_path(mtab_ent->mp, &st) == 0) {
110 print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp);
111 } else {
112 fprintf(stderr, "Cannot get information for '%s' (%s).\n",
113 mtab_ent->mp, str_error(errno));
114 }
115 }
116
117 putchar('\n');
118 return 0;
119}
120
121static void print_header(void)
122{
123 if (!display_blocks)
124 printf(HEADER_TABLE);
125 else
126 printf(HEADER_TABLE_BLK);
127
128 putchar('\n');
129}
130
131static void print_statfs(vfs_statfs_t *st, char *name, char *mountpoint)
132{
133 uint64_t const used_blocks = st->f_blocks - st->f_bfree;
134 unsigned const perc_used = PERCENTAGE(used_blocks, st->f_blocks);
135 char str[CAPA_BLOCKS_BUFSIZE];
136
137 printf("%10s", name);
138
139 if (!display_blocks) {
140 /* Print size */
141 capa_blocks_format_buf(st->f_blocks, st->f_bsize, str,
142 sizeof(str));
143 printf(" %14s", str);
144
145 /* Number of used blocks */
146 capa_blocks_format_buf(used_blocks, st->f_bsize, str,
147 sizeof(str));
148 printf(" %14s", str);
149
150 /* Number of available blocks */
151 capa_blocks_format_buf(st->f_bfree, st->f_bsize, str,
152 sizeof(str));
153 printf(" %14s", str);
154
155 /* Percentage of used blocks */
156 printf(" %4u%%", perc_used);
157
158 /* Mount point */
159 printf(" %s\n", mountpoint);
160 } else {
161 /* Block size / Blocks / Used blocks / Available blocks / Used% / Mounted on */
162 printf(" %10" PRIu32 " %9" PRIu64 " %11" PRIu64 " %11" PRIu64 " %4u%% %s\n",
163 st->f_bsize, st->f_blocks, used_blocks, st->f_bfree,
164 perc_used, mountpoint);
165 }
166}
167
168static void print_usage(void)
169{
170 printf("Syntax: %s [<options>] \n", NAME);
171 printf("Options:\n");
172 printf(" -h Print help\n");
173 printf(" -b Print exact block sizes and numbers\n");
174}
175
176/** @}
177 */
Note: See TracBrowser for help on using the repository browser.