summaryrefslogtreecommitdiff
path: root/src/df.c
blob: e13f1ce1db0ac5986773c214b22af6ff00b27528 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* df - summarize free disk space
   Copyright (C) 1991 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* Usage: df [-aikP] [-t fstype] [--all] [--inodes] [--type fstype]
   [--kilobytes] [--portability] [path...]

   Options:
   -a, --all		List all filesystems, even zero-size ones.
   -i, --inodes		List inode usage information instead of block usage.
   -k, --kilobytes	Print sizes in 1K blocks instead of 512-byte blocks.
   -P, --portability	Use the POSIX output format (one line per filesystem).
   -t, --type fstype	Limit the listing to filesystems of type `fstype'.
			Multiple -t options can be given.
			By default, all filesystem types are listed.

   Written by David MacKenzie <djm@gnu.ai.mit.edu> */

#include <stdio.h>
#include <sys/types.h>
#include <getopt.h>
#include "mountlist.h"
#include "fsusage.h"
#include "system.h"

char *strstr ();
char *xmalloc ();
char *xstrdup ();
int fs_to_list ();
void add_fs_type ();
void error ();
void print_header ();
void show_entry ();
void show_all_entries ();
void show_dev ();
void show_disk ();
void show_point ();
void usage ();

/* If nonzero, show inode information. */
int inode_format;

/* If nonzero, show even filesystems with zero size or
   uninteresting types. */
int show_all_fs;

/* If nonzero, use 1K blocks instead of 512-byte blocks. */
int kilobyte_blocks;

/* If nonzero, use the POSIX output format.  */
int posix_format;

/* Nonzero if errors have occurred. */
int exit_status;

/* Name this program was run with. */
char *program_name;

/* A filesystem type to display. */

struct fs_select
{
  char *fs_name;
  struct fs_select *fs_next;
};

/* Linked list of filesystem types to display.
   If `fs_list' is NULL, list all types.
   This table is generated dynamically from command-line options,
   rather than hardcoding into the program what it thinks are the
   valid filesystem types; let the user specify any filesystem type
   they want to, and if there are any filesystems of that type, they
   will be shown.

   Some filesystem types:
   4.2 4.3 ufs nfs swap ignore io vm */

struct fs_select *fs_list;

/* Linked list of mounted filesystems. */
struct mount_entry *mount_list;

struct option long_options[] =
{
  {"all", 0, &show_all_fs, 1},
  {"inodes", 0, &inode_format, 1},
  {"kilobytes", 0, &kilobyte_blocks, 1},
  {"portability", 0, &posix_format, 1},
  {"type", 1, 0, 't'},
  {NULL, 0, NULL, 0}
};

void
main (argc, argv)
     int argc;
     char **argv;
{
  int i;
  struct stat *stats;

  program_name = argv[0];
  fs_list = NULL;
  inode_format = 0;
  show_all_fs = 0;
  kilobyte_blocks = getenv ("POSIXLY_CORRECT") == 0;
  posix_format = 0;
  exit_status = 0;

  while ((i = getopt_long (argc, argv, "aikPt:v", long_options, (int *) 0))
	 != EOF)
    {
      switch (i)
	{
	case 0:			/* Long option. */
	  break;
	case 'a':
	  show_all_fs = 1;
	  break;
	case 'i':
	  inode_format = 1;
	  break;
	case 'k':
	  kilobyte_blocks = 1;
	  break;
	case 'P':
	  posix_format = 1;
	  break;
	case 't':
	  add_fs_type (optarg);
	  break;
	case 'v':		/* For SysV compatibility. */
	  break;
	default:
	  usage ();
	}
    }

  if (optind != argc)
    {
      /* Display explicitly requested empty filesystems. */
      show_all_fs = 1;

      /* stat all the given entries to make sure they get automounted,
	 if necessary, before reading the filesystem table.  */
      stats = (struct stat *)
	xmalloc ((argc - optind) * sizeof (struct stat));
      for (i = optind; i < argc; ++i)
	if (stat (argv[i], &stats[i - optind]))
	  {
	    error (0, errno, "%s", argv[i]);
	    exit_status = 1;
	    argv[i] = NULL;
	  }
    }

  mount_list = read_filesystem_list (fs_list != NULL, show_all_fs);
  if (mount_list == NULL)
    error (1, errno, "cannot read table of mounted filesystems");

  print_header ();
  sync ();

  if (optind == argc)
    show_all_entries ();
  else
    for (i = optind; i < argc; ++i)
      if (argv[i])
	show_entry (argv[i], &stats[i - optind]);

  exit (exit_status);
}

void
print_header ()
{
  if (inode_format)
    printf ("Filesystem            Inodes   IUsed   IFree  %%IUsed");
  else
    printf ("Filesystem         %s  Used Available Capacity",
	    kilobyte_blocks ? "1024-blocks" : " 512-blocks");
  printf (" Mounted on\n");
}

/* Show all mounted filesystems, except perhaps those that are of
   an unselected type or are empty. */

void
show_all_entries ()
{
  struct mount_entry *me;

  for (me = mount_list; me; me = me->me_next)
    show_dev (me->me_devname, me->me_mountdir, me->me_type);
}

/* Determine what kind of node PATH is and show the disk usage
   for it.  STATP is the results of `stat' on PATH.  */

void
show_entry (path, statp)
     char *path;
     struct stat *statp;
{
  if (S_ISBLK (statp->st_mode) || S_ISCHR (statp->st_mode))
    show_disk (path);
  else
    show_point (path, statp);
}

/* Identify the directory, if any, that device
   DISK is mounted on, and show its disk usage.  */

void
show_disk (disk)
     char *disk;
{
  struct mount_entry *me;

  for (me = mount_list; me; me = me->me_next)
    if (!strcmp (disk, me->me_devname))
      {
	show_dev (me->me_devname, me->me_mountdir, me->me_type);
	return;
      }
  /* No filesystem is mounted on DISK. */
  show_dev (disk, (char *) NULL, (char *) NULL);
}

/* Figure out which device file or directory POINT is mounted on
   and show its disk usage.
   STATP is the results of `stat' on POINT.  */

void
show_point (point, statp)
     char *point;
     struct stat *statp;
{
  struct stat disk_stats;
  struct mount_entry *me;

  for (me = mount_list; me; me = me->me_next)
    {
      if (me->me_dev == (dev_t) -1)
	{
	  if (stat (me->me_mountdir, &disk_stats) == 0)
	    me->me_dev = disk_stats.st_dev;
	  else
	    {
	      error (0, errno, "%s", me->me_mountdir);
	      exit_status = 1;
	      me->me_dev = -2;	/* So we won't try and fail repeatedly. */
	    }
	}

      if (statp->st_dev == me->me_dev)
	{
	  show_dev (me->me_devname, me->me_mountdir, me->me_type);
	  return;
	}
    }
  error (0, 0, "cannot find mount point for %s", point);
  exit_status = 1;
}

/* Display a space listing for the disk device with absolute path DISK.
   If MOUNT_POINT is non-NULL, it is the path of the root of the
   filesystem on DISK.
   If FSTYPE is non-NULL, it is the type of the filesystem on DISK. */

void
show_dev (disk, mount_point, fstype)
     char *disk;
     char *mount_point;
     char *fstype;
{
  struct fs_usage fsu;
  long blocks_used;
  long blocks_percent_used;
  long inodes_used;
  long inodes_percent_used;
  char *stat_file;

  if (!fs_to_list (fstype))
    return;

  /* If MOUNT_POINT is NULL, then the filesystem is not mounted, and this
     program reports on the filesystem that the special file is on.
     It would be better to report on the unmounted filesystem,
     but statfs doesn't do that on most systems.  */
  stat_file = mount_point ? mount_point : disk;

  if (get_fs_usage (stat_file, disk, &fsu))
    {
      error (0, errno, "%s", stat_file);
      exit_status = 1;
      return;
    }

  if (kilobyte_blocks)
    {
      fsu.fsu_blocks /= 2;
      fsu.fsu_bfree /= 2;
      fsu.fsu_bavail /= 2;
    }

  if (fsu.fsu_blocks == 0)
    {
      if (show_all_fs == 0)
	return;
      blocks_used = fsu.fsu_bavail = blocks_percent_used = 0;
    }
  else
    {
      blocks_used = fsu.fsu_blocks - fsu.fsu_bfree;
      blocks_percent_used = (long)
	(blocks_used * 100.0 / (blocks_used + fsu.fsu_bavail) + 0.5);
    }

  if (fsu.fsu_files == 0)
    {
      inodes_used = fsu.fsu_ffree = inodes_percent_used = 0;
    }
  else
    {
      inodes_used = fsu.fsu_files - fsu.fsu_ffree;
      inodes_percent_used = (long)
	(inodes_used * 100.0 / fsu.fsu_files + 0.5);
    }

  printf ("%-20s", disk);
  if (strlen (disk) > 20 && !posix_format)
    printf ("\n                    ");

  if (inode_format)
    printf (" %7ld %7ld %7ld %5ld%%",
	    fsu.fsu_files, inodes_used, fsu.fsu_ffree, inodes_percent_used);
  else
    printf (" %7ld %7ld  %7ld  %5ld%% ",
	    fsu.fsu_blocks, blocks_used, fsu.fsu_bavail, blocks_percent_used);

  if (mount_point)
    printf ("  %s", mount_point);
  putchar ('\n');
}

/* Add FSTYPE to the list of filesystem types to display. */

void
add_fs_type (fstype)
     char *fstype;
{
  struct fs_select *fsp;

  fsp = (struct fs_select *) xmalloc (sizeof (struct fs_select));
  fsp->fs_name = fstype;
  fsp->fs_next = fs_list;
  fs_list = fsp;
}

/* If FSTYPE is a type of filesystem that should be listed,
   return nonzero, else zero. */

int
fs_to_list (fstype)
     char *fstype;
{
  struct fs_select *fsp;

  if (fs_list == NULL || fstype == NULL)
    return 1;
  for (fsp = fs_list; fsp; fsp = fsp->fs_next)
    if (!strcmp (fstype, fsp->fs_name))
      return 1;
  return 0;
}

void
usage ()
{
  fprintf (stderr, "\
Usage: %s [-aikPv] [-t fstype] [--all] [--inodes] [--type fstype]\n\
       [--kilobytes] [--portability] [path...]\n",
	   program_name);
  exit (1);
}