source: rtems/cpukit/libmisc/shell/main_df.c @ 7bb71c7

4.115
Last change on this file since 7bb71c7 was 7bb71c7, checked in by Andrei Mozzhuhin <nopscmn@…>, on 05/16/13 at 09:24:09

shell: Add df command

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/**
2 *  df Shell Command Implmentation
3 *
4 * @brief Obtain MS-DOS filesystem information
5 * @ingroup libfs_msdos MSDOS FileSystem
6 */
7
8/*
9 *  Copyright (c) 2013 Andrey Mozzhuhin
10 *
11 *  The license and distribution terms for this file may be
12 *  found in the file LICENSE in this distribution or at
13 *  http://www.rtems.com/license/LICENSE.
14 */
15
16#ifdef HAVE_CONFIG_H
17#include "config.h"
18#endif
19
20#include <rtems/shell.h>
21
22#define __need_getopt_newlib
23#include <getopt.h>
24
25static const char suffixes[] =
26{ 'B', 'K', 'M', 'G', 'T' };
27
28struct df_context
29{
30  unsigned block_size;
31};
32
33static unsigned rtems_shell_df_parse_size(const char *str)
34{
35  unsigned result;
36  char suffix;
37  int i;
38
39  if (sscanf(str, "%d%c", &result, &suffix) == 2)
40  {
41    for (i = 0; i < sizeof(suffixes) / sizeof(suffixes[0]); i++)
42    {
43      if (suffix == suffixes[i])
44        break;
45      result *= 1024;
46    }
47  }
48  else if (sscanf(str, "%d", &result) != 1)
49  {
50    result = 0;
51  }
52
53  return result;
54}
55
56static char *rtems_shell_df_humanize_size(unsigned block_size, char *buf,
57    size_t size)
58{
59  int i = 0;
60
61  while (block_size >= 1024 && i < sizeof(suffixes) / sizeof(suffixes[0]) - 1)
62  {
63    block_size /= 1024;
64    i++;
65  }
66
67  snprintf(buf, size, "%d%c", block_size, suffixes[i]);
68  return buf;
69}
70
71static bool rtems_shell_df_print_entry(
72    const rtems_filesystem_mount_table_entry_t *mt_entry, void *arg)
73{
74  struct df_context *context = arg;
75
76  struct statvfs svfs;
77  int code;
78  char f_buf[16], u_buf[16], a_buf[16];
79
80  if ((code = statvfs(mt_entry->target, &svfs)))
81    return false;
82
83  if (context->block_size > 0)
84  {
85    printf("%-15s %10llu %9llu %11llu %9llu%% %14s\n",
86        mt_entry->dev == NULL ? "none" : mt_entry->dev,
87        (svfs.f_blocks * svfs.f_frsize + (context->block_size - 1)) / context->block_size,
88        ((svfs.f_blocks - svfs.f_bfree) * svfs.f_frsize + (context->block_size - 1)) / context->block_size,
89        (svfs.f_bfree * svfs.f_frsize + (context->block_size - 1)) / context->block_size,
90        ((svfs.f_blocks - svfs.f_bfree) * 100 / svfs.f_blocks),
91        mt_entry->target == NULL ? "none" : mt_entry->target);
92  }
93  else
94  {
95    rtems_shell_df_humanize_size(svfs.f_blocks * svfs.f_frsize, f_buf,
96        sizeof(f_buf));
97    rtems_shell_df_humanize_size((svfs.f_blocks - svfs.f_bfree) * svfs.f_frsize,
98        u_buf, sizeof(u_buf));
99    rtems_shell_df_humanize_size(svfs.f_bfree * svfs.f_frsize, a_buf,
100        sizeof(a_buf));
101    printf("%-15s %10s %9s %11s %9llu%% %14s\n",
102        mt_entry->dev == NULL ? "none" : mt_entry->dev, f_buf, u_buf, a_buf,
103        (svfs.f_blocks - svfs.f_bfree) * 100 / svfs.f_blocks,
104        mt_entry->target == NULL ? "none" : mt_entry->target);
105  }
106
107  return false;
108}
109
110static int rtems_shell_main_df(int argc, char **argv)
111{
112  int c;
113  struct getopt_data optdata;
114  struct df_context context;
115  char buf[32];
116
117  memset(&optdata, 0, sizeof(optdata));
118  context.block_size = 1024;
119
120  while ((c = getopt_r(argc, (char**) argv, ":hB:", &optdata)) != -1)
121  {
122    switch (c)
123    {
124    case 'h':
125      context.block_size = 0;
126      break;
127    case 'B':
128      context.block_size = rtems_shell_df_parse_size(optdata.optarg);
129      break;
130    default:
131      return -1;
132    }
133  }
134
135  if (context.block_size == 0)
136    printf(
137        "Filesystem     Size             Used   Available       Use%%     Mounted on\n");
138  else
139    printf(
140        "Filesystem     %s-blocks        Used   Available       Use%%     Mounted on\n",
141        rtems_shell_df_humanize_size(context.block_size, buf, sizeof(buf)));
142
143  rtems_filesystem_mount_iterate(rtems_shell_df_print_entry, &context);
144
145  return 0;
146}
147
148rtems_shell_cmd_t rtems_shell_DF_Command =
149{
150    "df", /* name */
151    "[-hB]\n"
152    " -h  human-readable output\n"
153    " -B  scale sizes by SIZE before printing them\n", /* usage */
154    "files", /* topic */
155    rtems_shell_main_df, /* command */
156    NULL, /* alias */
157    NULL /* next */
158};
Note: See TracBrowser for help on using the repository browser.