1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-20 18:56:39 +02:00
Files
coreutils/src/df.c

540 lines
14 KiB
C
Raw Normal View History

1992-10-31 20:42:48 +00:00
/* df - summarize free disk space
1995-03-10 04:15:06 +00:00
Copyright (C) 1991, 1995 Free Software Foundation, Inc.
1992-10-31 20:42:48 +00:00
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. */
1994-07-03 17:46:37 +00:00
/* Usage: df [-aikPT] [-t fstype] [-x fstype] [--all] [--inodes] [--print-type]
1993-04-15 01:58:23 +00:00
[--type fstype] [--exclude-type fstype] [--kilobytes] [--portability]
[file...]
1992-10-31 20:42:48 +00:00
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).
1994-07-03 17:46:37 +00:00
-T, --print-type Print filesystem type.
1992-10-31 20:42:48 +00:00
-t, --type fstype Limit the listing to filesystems of type `fstype'.
1993-04-15 01:58:23 +00:00
-x, --exclude-type=fstype
Limit the listing to filesystems not of type `fstype'.
Multiple -t and/or -x options can be given.
1992-10-31 20:42:48 +00:00
By default, all filesystem types are listed.
Written by David MacKenzie <djm@gnu.ai.mit.edu> */
1993-10-06 16:44:26 +00:00
#include <config.h>
1992-10-31 20:42:48 +00:00
#include <stdio.h>
#include <sys/types.h>
#include <getopt.h>
1992-10-31 20:42:48 +00:00
#include "mountlist.h"
#include "fsusage.h"
#include "system.h"
1993-04-02 04:26:26 +00:00
#include "version.h"
1994-07-30 16:34:17 +00:00
#include "safe-stat.h"
#include "error.h"
1992-10-31 20:42:48 +00:00
char *xmalloc ();
char *xstrdup ();
static int selected_fstype ();
static int excluded_fstype ();
static void add_excluded_fs_type ();
static void add_fs_type ();
static void print_header ();
static void show_entry ();
static void show_all_entries ();
static void show_dev ();
static void show_disk ();
static void show_point ();
static void usage ();
1992-10-31 20:42:48 +00:00
1993-04-02 04:26:26 +00:00
/* Name this program was run with. */
char *program_name;
1992-10-31 20:42:48 +00:00
/* If nonzero, show inode information. */
static int inode_format;
1992-10-31 20:42:48 +00:00
/* If nonzero, show even filesystems with zero size or
uninteresting types. */
static int show_all_fs;
1992-10-31 20:42:48 +00:00
1993-04-15 01:58:23 +00:00
/* If nonzero, output data for each filesystem corresponding to a
command line argument -- even if it's a dummy (automounter) entry. */
static int show_listed_fs;
1992-10-31 20:42:48 +00:00
/* If nonzero, use 1K blocks instead of 512-byte blocks. */
static int kilobyte_blocks;
1992-10-31 20:42:48 +00:00
/* If nonzero, use the POSIX output format. */
static int posix_format;
1992-10-31 20:42:48 +00:00
1994-10-27 23:36:15 +00:00
/* If nonzero, invoke the `sync' system call before getting any usage data.
Using this option can make df very slow, especially with many or very
1994-10-29 04:49:55 +00:00
busy disks. Default to non-zero because the sync call does make a
difference on some systems -- SunOs4.1.3, for one. I have been assured
that it is *not* necessary on Linux, so there should be a way to
configure this. FIXME. */
static int require_sync = 1;
1994-10-27 04:18:00 +00:00
1992-10-31 20:42:48 +00:00
/* Nonzero if errors have occurred. */
static int exit_status;
1992-10-31 20:42:48 +00:00
/* A filesystem type to display. */
struct fs_type_list
1992-10-31 20:42:48 +00:00
{
char *fs_name;
struct fs_type_list *fs_next;
1992-10-31 20:42:48 +00:00
};
/* Linked list of filesystem types to display.
If `fs_select_list' is NULL, list all types.
1992-10-31 20:42:48 +00:00
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 efs dbg */
1992-10-31 20:42:48 +00:00
static struct fs_type_list *fs_select_list;
/* Linked list of filesystem types to omit.
If the list is empty, don't exclude any types. */
static struct fs_type_list *fs_exclude_list;
1992-10-31 20:42:48 +00:00
/* Linked list of mounted filesystems. */
static struct mount_entry *mount_list;
1992-10-31 20:42:48 +00:00
1993-04-02 04:26:26 +00:00
/* If non-zero, display usage information and exit. */
1993-08-05 02:20:48 +00:00
static int show_help;
1993-04-02 04:26:26 +00:00
1993-10-06 00:24:55 +00:00
/* If non-zero, print the version on standard output and exit. */
1993-08-05 02:20:48 +00:00
static int show_version;
1993-04-02 04:26:26 +00:00
1994-07-03 17:46:37 +00:00
/* If non-zero, print filesystem type as well. */
static int print_type;
static struct option const long_options[] =
1992-10-31 20:42:48 +00:00
{
{"all", no_argument, &show_all_fs, 1},
{"inodes", no_argument, &inode_format, 1},
{"kilobytes", no_argument, &kilobyte_blocks, 1},
{"portability", no_argument, &posix_format, 1},
1994-07-03 17:46:37 +00:00
{"print-type", no_argument, &print_type, 1},
1994-10-29 04:49:55 +00:00
{"sync", no_argument, 0, 129},
{"no-sync", no_argument, 0, 130},
{"type", required_argument, 0, 't'},
{"exclude-type", required_argument, 0, 'x'},
1993-08-05 02:20:48 +00:00
{"help", no_argument, &show_help, 1},
{"version", no_argument, &show_version, 1},
1992-10-31 20:42:48 +00:00
{NULL, 0, NULL, 0}
};
void
main (argc, argv)
int argc;
char **argv;
{
int i;
struct stat *stats;
program_name = argv[0];
fs_select_list = NULL;
fs_exclude_list = NULL;
1992-10-31 20:42:48 +00:00
inode_format = 0;
show_all_fs = 0;
1993-04-15 01:58:23 +00:00
show_listed_fs = 0;
1992-10-31 20:42:48 +00:00
kilobyte_blocks = getenv ("POSIXLY_CORRECT") == 0;
1994-07-03 17:46:37 +00:00
print_type = 0;
1992-10-31 20:42:48 +00:00
posix_format = 0;
exit_status = 0;
1994-07-03 17:46:37 +00:00
while ((i = getopt_long (argc, argv, "aikPTt:vx:", long_options, (int *) 0))
1992-10-31 20:42:48 +00:00
!= 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;
1994-07-03 17:46:37 +00:00
case 'T':
print_type = 1;
break;
1992-10-31 20:42:48 +00:00
case 'P':
posix_format = 1;
break;
1994-10-29 04:49:55 +00:00
case 129:
1994-10-27 04:18:00 +00:00
require_sync = 1;
break;
1994-10-29 04:49:55 +00:00
case 130:
require_sync = 0;
break;
1992-10-31 20:42:48 +00:00
case 't':
add_fs_type (optarg);
break;
case 'v': /* For SysV compatibility. */
break;
case 'x':
add_excluded_fs_type (optarg);
break;
1992-10-31 20:42:48 +00:00
default:
1993-10-08 00:51:10 +00:00
usage (1);
1992-10-31 20:42:48 +00:00
}
}
1993-08-05 02:20:48 +00:00
if (show_version)
1993-05-22 02:09:05 +00:00
{
1993-10-06 00:24:55 +00:00
printf ("%s\n", version_string);
1993-05-22 02:09:05 +00:00
exit (0);
}
1993-04-02 04:26:26 +00:00
1993-08-05 02:20:48 +00:00
if (show_help)
1993-10-08 00:51:10 +00:00
usage (0);
1993-04-02 04:26:26 +00:00
1994-07-30 16:34:17 +00:00
if (optind == argc)
{
#ifdef lint
/* Suppress `used before initialized' warning. */
stats = NULL;
#endif
}
else
1992-10-31 20:42:48 +00:00
{
/* 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 (safe_stat (argv[i], &stats[i - optind]))
1992-10-31 20:42:48 +00:00
{
error (0, errno, "%s", argv[i]);
exit_status = 1;
argv[i] = NULL;
}
}
mount_list =
read_filesystem_list ((fs_select_list != NULL || fs_exclude_list != NULL),
show_all_fs);
1992-10-31 20:42:48 +00:00
if (mount_list == NULL)
error (1, errno, "cannot read table of mounted filesystems");
print_header ();
1994-10-27 04:18:00 +00:00
if (require_sync)
sync ();
1992-10-31 20:42:48 +00:00
if (optind == argc)
show_all_entries ();
else
1993-04-15 01:58:23 +00:00
{
/* Display explicitly requested empty filesystems. */
show_listed_fs = 1;
for (i = optind; i < argc; ++i)
if (argv[i])
show_entry (argv[i], &stats[i - optind]);
}
1992-10-31 20:42:48 +00:00
exit (exit_status);
}
static void
1992-10-31 20:42:48 +00:00
print_header ()
{
1994-07-03 17:46:37 +00:00
printf ("Filesystem ");
if (print_type)
printf (" Type");
else
printf (" ");
1992-10-31 20:42:48 +00:00
if (inode_format)
1994-07-03 17:46:37 +00:00
printf (" Inodes IUsed IFree %%IUsed");
1992-10-31 20:42:48 +00:00
else
1994-07-03 17:46:37 +00:00
printf (" %s Used Available Capacity",
1992-10-31 20:42:48 +00:00
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. */
static void
1992-10-31 20:42:48 +00:00
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. */
static void
1992-10-31 20:42:48 +00:00
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. */
static void
1992-10-31 20:42:48 +00:00
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. */
static void
1992-10-31 20:42:48 +00:00
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 (safe_stat (me->me_mountdir, &disk_stats) == 0)
1992-10-31 20:42:48 +00:00
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. */
static void
1992-10-31 20:42:48 +00:00
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 (!selected_fstype (fstype) || excluded_fstype (fstype))
1992-10-31 20:42:48 +00:00
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)
{
1993-04-15 01:58:23 +00:00
if (!show_all_fs && !show_listed_fs)
1992-10-31 20:42:48 +00:00
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);
}
1994-07-03 17:46:37 +00:00
printf ((print_type ? "%-13s" : "%-20s"), disk);
if (strlen (disk) > (print_type ? 13 : 20) && !posix_format)
printf ((print_type ? "\n%13s" : "\n%20s"), "");
if (print_type)
printf (" %-5s ", fstype);
1992-10-31 20:42:48 +00:00
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. */
static void
1992-10-31 20:42:48 +00:00
add_fs_type (fstype)
char *fstype;
{
struct fs_type_list *fsp;
fsp = (struct fs_type_list *) xmalloc (sizeof (struct fs_type_list));
fsp->fs_name = fstype;
fsp->fs_next = fs_select_list;
fs_select_list = fsp;
}
/* Add FSTYPE to the list of filesystem types to be omitted. */
static void
add_excluded_fs_type (fstype)
char *fstype;
{
struct fs_type_list *fsp;
1992-10-31 20:42:48 +00:00
fsp = (struct fs_type_list *) xmalloc (sizeof (struct fs_type_list));
1992-10-31 20:42:48 +00:00
fsp->fs_name = fstype;
fsp->fs_next = fs_exclude_list;
fs_exclude_list = fsp;
1992-10-31 20:42:48 +00:00
}
/* If FSTYPE is a type of filesystem that should be listed,
return nonzero, else zero. */
static int
selected_fstype (fstype)
1992-10-31 20:42:48 +00:00
char *fstype;
{
struct fs_type_list *fsp;
1992-10-31 20:42:48 +00:00
if (fs_select_list == NULL || fstype == NULL)
1992-10-31 20:42:48 +00:00
return 1;
for (fsp = fs_select_list; fsp; fsp = fsp->fs_next)
1992-10-31 20:42:48 +00:00
if (!strcmp (fstype, fsp->fs_name))
return 1;
return 0;
}
/* If FSTYPE is a type of filesystem that should be omitted,
return nonzero, else zero. */
static int
excluded_fstype (fstype)
char *fstype;
{
struct fs_type_list *fsp;
if (fs_exclude_list == NULL || fstype == NULL)
return 0;
for (fsp = fs_exclude_list; fsp; fsp = fsp->fs_next)
if (!strcmp (fstype, fsp->fs_name))
return 1;
return 0;
}
static void
1993-10-08 00:51:10 +00:00
usage (status)
int status;
1992-10-31 20:42:48 +00:00
{
1993-10-19 00:00:06 +00:00
if (status != 0)
fprintf (stderr, "Try `%s --help' for more information.\n",
program_name);
else
{
printf ("Usage: %s [OPTION]... [FILE]...\n", program_name);
1993-10-19 00:00:06 +00:00
printf ("\
\n\
1993-10-08 00:51:10 +00:00
-a, --all include filesystems having 0 blocks\n\
-i, --inodes list inode information instead of block usage\n\
-k, --kilobytes use 1024 blocks, not 512 despite POSIXLY_CORRECT\n\
1994-10-29 04:49:55 +00:00
--sync invoke sync before getting usage info (default)\n\
--no-sync do not invoke sync before getting usage info\n\
1993-10-26 23:39:04 +00:00
-t, --type=TYPE limit the listing to TYPE filesystems type\n\
-x, --exclude-type=TYPE limit the listing to not TYPE filesystems type\n\
1993-10-08 00:51:10 +00:00
-v (ignored)\n\
-P, --portability use the POSIX output format\n\
1994-07-03 17:46:37 +00:00
-T, --print-type print filesystem type\n\
1993-10-09 20:44:16 +00:00
--help display this help and exit\n\
--version output version information and exit\n\
1993-10-08 00:51:10 +00:00
\n\
If no FILEs are given, list all currently mounted filesystems.\n");
1993-10-19 00:00:06 +00:00
}
1993-10-08 00:51:10 +00:00
exit (status);
1992-10-31 20:42:48 +00:00
}