[66366470] | 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>
|
---|
[e8f0158] | 39 | #include <stdint.h>
|
---|
[be99d30] | 40 | #include <getopt.h>
|
---|
[66366470] | 41 | #include <errno.h>
|
---|
| 42 | #include <adt/list.h>
|
---|
| 43 | #include <vfs/vfs.h>
|
---|
[35b7d86e] | 44 | #include <vfs/vfs_mtab.h>
|
---|
[66366470] | 45 |
|
---|
| 46 | #define NAME "df"
|
---|
| 47 |
|
---|
[3432ddb] | 48 | #define HEADER_TABLE "Filesystem %4u-blocks Used Available Used%% Mounted on"
|
---|
| 49 | #define HEADER_TABLE_HR "Filesystem Size Used Available Used%% Mounted on"
|
---|
[66366470] | 50 |
|
---|
[737227c7] | 51 | #define PERCENTAGE(x, tot) (tot ? (100ULL * (x) / (tot)) : 0)
|
---|
[be99d30] | 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 |
|
---|
[f4cbf9dd] | 57 | static unsigned int unit_size;
|
---|
[3432ddb] | 58 | static unsigned int human_readable;
|
---|
[f4cbf9dd] | 59 |
|
---|
[737227c7] | 60 | static void size_to_human_readable(char *buf, uint64_t bytes);
|
---|
[3432ddb] | 61 | static void print_header(void);
|
---|
[be99d30] | 62 | static void print_statfs(struct statfs *, char *, char *);
|
---|
| 63 | static void print_usage(void);
|
---|
[66366470] | 64 |
|
---|
| 65 | int main(int argc, char *argv[])
|
---|
| 66 | {
|
---|
[be99d30] | 67 | int optres, errflg = 0;
|
---|
[66366470] | 68 | struct statfs st;
|
---|
[332513a] | 69 |
|
---|
[f4cbf9dd] | 70 | unit_size = 512;
|
---|
[3432ddb] | 71 | human_readable = 0;
|
---|
[f4cbf9dd] | 72 |
|
---|
[be99d30] | 73 | /******************************************/
|
---|
| 74 | /* Parse command line options... */
|
---|
| 75 | /******************************************/
|
---|
[523a4b6] | 76 | while ((optres = getopt(argc, argv, ":uhb:")) != -1) {
|
---|
[be99d30] | 77 | switch(optres) {
|
---|
[523a4b6] | 78 | case 'u':
|
---|
| 79 | print_usage();
|
---|
| 80 | return 0;
|
---|
| 81 |
|
---|
[be99d30] | 82 | case 'h':
|
---|
[3432ddb] | 83 | human_readable = 1;
|
---|
[be99d30] | 84 | break;
|
---|
[8a8a08d1] | 85 |
|
---|
[f4cbf9dd] | 86 | case 'b':
|
---|
| 87 | str_uint32_t(optarg, NULL, 0, 0, &unit_size);
|
---|
| 88 | break;
|
---|
[332513a] | 89 |
|
---|
| 90 | case ':':
|
---|
[737227c7] | 91 | fprintf(stderr, "Option -%c requires an operand\n",
|
---|
| 92 | optopt);
|
---|
[be99d30] | 93 | errflg++;
|
---|
| 94 | break;
|
---|
| 95 |
|
---|
| 96 | case '?':
|
---|
| 97 | fprintf(stderr, "Unrecognized option: -%c\n", optopt);
|
---|
| 98 | errflg++;
|
---|
| 99 | break;
|
---|
| 100 |
|
---|
| 101 | default:
|
---|
[737227c7] | 102 | fprintf(stderr,
|
---|
| 103 | "Unknown error while parsing command line options");
|
---|
[be99d30] | 104 | errflg++;
|
---|
| 105 | break;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
[66366470] | 108 |
|
---|
[be99d30] | 109 | if (optind > argc) {
|
---|
[737227c7] | 110 | fprintf(stderr, "Too many input parameters\n");
|
---|
[be99d30] | 111 | errflg++;
|
---|
| 112 | }
|
---|
[332513a] | 113 |
|
---|
[be99d30] | 114 | if (errflg) {
|
---|
| 115 | print_usage();
|
---|
| 116 | return 1;
|
---|
| 117 | }
|
---|
[332513a] | 118 |
|
---|
[66366470] | 119 | LIST_INITIALIZE(mtab_list);
|
---|
[6afc9d7] | 120 | vfs_get_mtab_list(&mtab_list);
|
---|
[737227c7] | 121 |
|
---|
[3432ddb] | 122 | print_header();
|
---|
[e1ec5a2] | 123 | list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
|
---|
[b5b5d84] | 124 | if (vfs_statfs_path(mtab_ent->mp, &st) == 0) {
|
---|
[6afc9d7] | 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 | }
|
---|
[66366470] | 130 | }
|
---|
[737227c7] | 131 |
|
---|
[332513a] | 132 | putchar('\n');
|
---|
[66366470] | 133 | return 0;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[737227c7] | 136 | static void size_to_human_readable(char *buf, uint64_t bytes)
|
---|
[3432ddb] | 137 | {
|
---|
| 138 | const char *units = "BkMGTPEZY";
|
---|
| 139 | int i = 0;
|
---|
| 140 |
|
---|
| 141 | while (bytes >= 1024) {
|
---|
| 142 | bytes /= 1024;
|
---|
| 143 | i++;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[737227c7] | 146 | snprintf(buf, 6, "%4llu%c", (unsigned long long) bytes, units[i]);
|
---|
[3432ddb] | 147 | }
|
---|
| 148 |
|
---|
| 149 | static void print_header(void)
|
---|
| 150 | {
|
---|
| 151 | if (human_readable)
|
---|
[332513a] | 152 | printf(HEADER_TABLE_HR);
|
---|
| 153 | else
|
---|
[3432ddb] | 154 | printf(HEADER_TABLE, unit_size);
|
---|
[737227c7] | 155 |
|
---|
[3432ddb] | 156 | putchar('\n');
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[be99d30] | 159 | static void print_statfs(struct statfs *st, char *name, char *mountpoint)
|
---|
| 160 | {
|
---|
[737227c7] | 161 | uint64_t const used_blocks = st->f_blocks - st->f_bfree;
|
---|
| 162 | unsigned const perc_used = PERCENTAGE(used_blocks, st->f_blocks);
|
---|
| 163 |
|
---|
[be99d30] | 164 | printf("%10s", name);
|
---|
[332513a] | 165 |
|
---|
[3432ddb] | 166 | if (human_readable) {
|
---|
| 167 | char tmp[1024];
|
---|
[737227c7] | 168 |
|
---|
| 169 | /* Print size */
|
---|
[3432ddb] | 170 | size_to_human_readable(tmp, st->f_blocks * st->f_bsize);
|
---|
[737227c7] | 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 */
|
---|
[3432ddb] | 178 | size_to_human_readable(tmp, st->f_bfree * st->f_bsize);
|
---|
[737227c7] | 179 | printf(" %14s", tmp);
|
---|
| 180 |
|
---|
| 181 | /* Percentage of used blocks */
|
---|
[651c8db] | 182 | printf(" %4u%%", perc_used);
|
---|
[737227c7] | 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);
|
---|
[3432ddb] | 194 | }
|
---|
[332513a] | 195 |
|
---|
[be99d30] | 196 | }
|
---|
| 197 |
|
---|
| 198 | static void print_usage(void)
|
---|
| 199 | {
|
---|
[332513a] | 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");
|
---|
[be99d30] | 205 | }
|
---|
| 206 |
|
---|
[66366470] | 207 | /** @}
|
---|
| 208 | */
|
---|