| 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 512-blocks Used Available Used% Mounted on"
|
|---|
| 50 |
|
|---|
| 51 | #define PERCENTAGE(x, tot) ((unsigned long long) (100L * (x) / (tot)))
|
|---|
| 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 |
|
|---|
| 57 | static void print_statfs(struct statfs *, char *, char *);
|
|---|
| 58 | static void print_usage(void);
|
|---|
| 59 |
|
|---|
| 60 | int main(int argc, char *argv[])
|
|---|
| 61 | {
|
|---|
| 62 | int optres, errflg = 0;
|
|---|
| 63 | struct statfs st;
|
|---|
| 64 |
|
|---|
| 65 | /******************************************/
|
|---|
| 66 | /* Parse command line options... */
|
|---|
| 67 | /******************************************/
|
|---|
| 68 | while ((optres = getopt(argc, argv, ":hi")) != -1) {
|
|---|
| 69 | switch(optres) {
|
|---|
| 70 | case 'h':
|
|---|
| 71 | break;
|
|---|
| 72 |
|
|---|
| 73 | case 'i':
|
|---|
| 74 | break;
|
|---|
| 75 |
|
|---|
| 76 | case ':':
|
|---|
| 77 | fprintf(stderr, "Option -%c requires an operand\n", optopt);
|
|---|
| 78 | errflg++;
|
|---|
| 79 | break;
|
|---|
| 80 |
|
|---|
| 81 | case '?':
|
|---|
| 82 | fprintf(stderr, "Unrecognized option: -%c\n", optopt);
|
|---|
| 83 | errflg++;
|
|---|
| 84 | break;
|
|---|
| 85 |
|
|---|
| 86 | default:
|
|---|
| 87 | fprintf(stderr, "Unknown error while parsing command line options.");
|
|---|
| 88 | errflg++;
|
|---|
| 89 | break;
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | if (optind > argc) {
|
|---|
| 94 | fprintf(stderr, "Too many input parameter\n");
|
|---|
| 95 | errflg++;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | if (errflg) {
|
|---|
| 99 | print_usage();
|
|---|
| 100 | return 1;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | LIST_INITIALIZE(mtab_list);
|
|---|
| 104 | get_mtab_list(&mtab_list);
|
|---|
| 105 | printf("%s\n", HEADER_TABLE);
|
|---|
| 106 | list_foreach(mtab_list, cur) {
|
|---|
| 107 | mtab_ent_t *mtab_ent = list_get_instance(cur, mtab_ent_t,
|
|---|
| 108 | link);
|
|---|
| 109 | statfs(mtab_ent->mp, &st);
|
|---|
| 110 | print_statfs(&st, mtab_ent->fs_name, mtab_ent->mp);
|
|---|
| 111 | }
|
|---|
| 112 | putchar('\n');
|
|---|
| 113 | return 0;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | static void print_statfs(struct statfs *st, char *name, char *mountpoint)
|
|---|
| 117 | {
|
|---|
| 118 | printf("%10s", name);
|
|---|
| 119 | printf(" %14llu %14llu %14llu %4llu%% %s\n",
|
|---|
| 120 | FSBK_TO_BK(st->f_blocks, st->f_bsize, 512), /* Blocks */
|
|---|
| 121 | FSBK_TO_BK(st->f_blocks - st->f_bfree, st->f_bsize, 512), /* Used */
|
|---|
| 122 | FSBK_TO_BK(st->f_bfree, st->f_bsize, 512), /* Available */
|
|---|
| 123 | (st->f_blocks)?PERCENTAGE(st->f_blocks - st->f_bfree, st->f_blocks):0L, /* Used% */
|
|---|
| 124 | mountpoint /* Mounted on */
|
|---|
| 125 | );
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | static void print_usage(void)
|
|---|
| 129 | {
|
|---|
| 130 | printf("syntax: %s [-h] [-i]\n", NAME);
|
|---|
| 131 | printf(" h : \"Human-readable\" output.\n");
|
|---|
| 132 | printf(" i : Include statistics on the number of free inodes. \n");
|
|---|
| 133 | printf("\n");
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /** @}
|
|---|
| 137 | */
|
|---|