📄 df.c
字号:
/* df - summarize free disk space Copyright (C) 91, 1995-2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *//* Written by David MacKenzie <djm@gnu.ai.mit.edu>. --human-readable and --megabyte options added by lm@sgi.com. --si and large file support added by eggert@twinsun.com. */#ifdef _AIX #pragma alloca#endif#include <config.h>#if HAVE_INTTYPES_H# include <inttypes.h>#endif#include <stdio.h>#include <sys/types.h>#include <getopt.h>#include <assert.h>#include "system.h"#include "dirname.h"#include "error.h"#include "fsusage.h"#include "human.h"#include "mountlist.h"#include "path-concat.h"#include "quote.h"#include "save-cwd.h"/* The official name of this program (e.g., no `g' prefix). */#define PROGRAM_NAME "df"#define AUTHORS \ "Torbjorn Granlund, David MacKenzie, Larry McVoy, and Paul Eggert"char *xgetcwd ();/* Name this program was run with. */char *program_name;/* If nonzero, show inode information. */static int inode_format;/* If nonzero, show even filesystems with zero size or uninteresting types. */static int show_all_fs;/* If nonzero, show only local filesystems. */static int show_local_fs;/* 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;/* If positive, the units to use when printing sizes; if negative, the human-readable base. */static int output_block_size;/* If nonzero, use the POSIX output format. */static int posix_format;/* 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 busy disks. Note that this may make a difference on some systems -- SunOs4.1.3, for one. It is *not* necessary on Linux. */static int require_sync = 0;/* Nonzero if errors have occurred. */static int exit_status;/* A filesystem type to display. */struct fs_type_list{ char *fs_name; struct fs_type_list *fs_next;};/* Linked list of filesystem types to display. If `fs_select_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 efs dbg */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;/* Linked list of mounted filesystems. */static struct mount_entry *mount_list;/* If nonzero, print filesystem type as well. */static int print_type;/* For long options that have no equivalent short option, use a non-character as a pseudo short option, starting with CHAR_MAX + 1. */enum{ SYNC_OPTION = CHAR_MAX + 1, NO_SYNC_OPTION};static struct option const long_options[] ={ {"all", no_argument, NULL, 'a'}, {"block-size", required_argument, NULL, 'B'}, {"inodes", no_argument, NULL, 'i'}, {"human-readable", no_argument, NULL, 'h'}, {"si", no_argument, NULL, 'H'}, {"kilobytes", no_argument, NULL, 'k'}, /* long form is obsolescent */ {"local", no_argument, NULL, 'l'}, {"megabytes", no_argument, NULL, 'm'}, /* obsolescent */ {"portability", no_argument, NULL, 'P'}, {"print-type", no_argument, NULL, 'T'}, {"sync", no_argument, NULL, SYNC_OPTION}, {"no-sync", no_argument, NULL, NO_SYNC_OPTION}, {"type", required_argument, NULL, 't'}, {"exclude-type", required_argument, NULL, 'x'}, {GETOPT_HELP_OPTION_DECL}, {GETOPT_VERSION_OPTION_DECL}, {NULL, 0, NULL, 0}};static voidprint_header (void){ printf (_("Filesystem ")); if (print_type) printf (_(" Type")); else printf (" "); if (inode_format) printf (_(" Inodes IUsed IFree IUse%%")); else if (output_block_size < 0) { if (output_block_size == -1000) printf (_(" Size Used Avail Use%%")); else printf (_(" Size Used Avail Use%%")); } else if (posix_format) printf (_(" %4d-blocks Used Available Capacity"), output_block_size); else { char buf[LONGEST_HUMAN_READABLE + 1]; char *p = human_readable (output_block_size, buf, 1, -1024); /* Replace e.g. "1.0K" by "1K". */ size_t plen = strlen (p); if (3 <= plen && strncmp (p + plen - 3, ".0", 2) == 0) strcpy (p + plen - 3, p + plen - 1); printf (_(" %4s-blocks Used Available Use%%"), p); } printf (_(" Mounted on\n"));}/* If FSTYPE is a type of filesystem that should be listed, return nonzero, else zero. */static intselected_fstype (const char *fstype){ const struct fs_type_list *fsp; if (fs_select_list == NULL || fstype == NULL) return 1; for (fsp = fs_select_list; fsp; fsp = fsp->fs_next) if (STREQ (fstype, fsp->fs_name)) return 1; return 0;}/* If FSTYPE is a type of filesystem that should be omitted, return nonzero, else zero. */static intexcluded_fstype (const char *fstype){ const 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 (STREQ (fstype, fsp->fs_name)) return 1; return 0;}/* Like human_readable_inexact with a human_ceiling human_inexact_style, except return "-" if the argument is -1, and if NEGATIVE is 1 then N represents a negative number, expressed in two's complement. */static char const *df_readable (int negative, uintmax_t n, char *buf, int from_block_size, int t_output_block_size){ if (n == -1) return "-"; else { char *p = human_readable_inexact (negative ? - n : n, buf + negative, from_block_size, t_output_block_size, human_ceiling); if (negative) *--p = '-'; return p; }}/* 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. If MOUNT_POINT is non-NULL, then DISK may be NULL -- certain systems may not be able to produce statistics in this case. ME_DUMMY and ME_REMOTE are the mount entry flags. */static voidshow_dev (const char *disk, const char *mount_point, const char *fstype, int me_dummy, int me_remote){ struct fs_usage fsu; const char *stat_file; char buf[3][LONGEST_HUMAN_READABLE + 2]; int width; int use_width; int input_units; int output_units; uintmax_t total; uintmax_t available; int negate_available; uintmax_t available_to_root; uintmax_t used; int negate_used; double pct = -1; if (me_remote && show_local_fs) return; if (me_dummy && show_all_fs == 0 && !show_listed_fs) return; if (!selected_fstype (fstype) || excluded_fstype (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", quote (stat_file)); exit_status = 1; return; } if (fsu.fsu_blocks == 0 && !show_all_fs && !show_listed_fs) return; if (! disk) disk = "-"; /* unknown */ if (! fstype) fstype = "-"; /* unknown */ /* df.c reserved 5 positions for fstype, but that does not suffice for type iso9660 */ if (print_type) { int disk_name_len = (int) strlen (disk); int fstype_len = (int) strlen (fstype); if (disk_name_len + fstype_len + 2 < 20) printf ("%s%*s ", disk, 18 - disk_name_len, fstype); else if (!posix_format) printf ("%s\n%18s ", disk, fstype); else printf ("%s %s", disk, fstype); } else { if ((int) strlen (disk) > 20 && !posix_format) printf ("%s\n%20s", disk, ""); else printf ("%-20s", disk); } if (inode_format) { width = 7; use_width = 5; input_units = 1; output_units = output_block_size < 0 ? output_block_size : 1; total = fsu.fsu_files; available = fsu.fsu_ffree; negate_available = 0; available_to_root = available; } else { width = output_block_size < 0 ? 5 + (output_block_size == -1000) : 9; use_width = (posix_format && 0 <= output_block_size) ? 8 : 4; input_units = fsu.fsu_blocksize; output_units = output_block_size; total = fsu.fsu_blocks; available = fsu.fsu_bavail; negate_available = fsu.fsu_bavail_top_bit_set; available_to_root = fsu.fsu_bfree; } used = -1; negate_used = 0; if (total != -1 && available_to_root != -1) { used = total - available_to_root; if (total < available_to_root) { negate_used = 1; used = - used; } } printf (" %*s %*s %*s ", width, df_readable (0, total, buf[0], input_units, output_units), width, df_readable (negate_used, used, buf[1], input_units, output_units), width, df_readable (negate_available, available, buf[2], input_units, output_units)); if (used == -1 || available == -1) ; else if (!negate_used && used <= TYPE_MAXIMUM (uintmax_t) / 100 && used + available != 0 && (used + available < used) == negate_available) { uintmax_t u100 = used * 100; uintmax_t nonroot_total = used + available; pct = u100 / nonroot_total + (u100 % nonroot_total != 0); } else { /* The calculation cannot be done easily with integer arithmetic. Fall back on floating point. This can suffer from minor rounding errors, but doing it exactly requires multiple precision arithmetic, and it's not worth the aggravation. */ double u = negate_used ? - (double) - used : used; double a = negate_available ? - (double) - available : available; double nonroot_total = u + a; if (nonroot_total) { double ipct; pct = u * 100 / nonroot_total; ipct = (long) pct; /* Like `pct = ceil (dpct);', but avoid ceil so that the math library needn't be linked. */ if (ipct - 1 < pct && pct <= ipct + 1) pct = ipct + (ipct < pct); } } if (0 <= pct) printf ("%*.0f%%", use_width - 1, pct); else printf ("%*s", use_width, "- "); if (mount_point) {#ifdef HIDE_AUTOMOUNT_PREFIX /* Don't print the first directory name in MOUNT_POINT if it's an artifact of an automounter. This is a bit too aggressive to be the default. */ if (strncmp ("/auto/", mount_point, 6) == 0) mount_point += 5; else if (strncmp ("/tmp_mnt/", mount_point, 9) == 0) mount_point += 8;#endif printf (" %s", mount_point); } putchar ('\n');}/* Identify the directory, if any, that device DISK is mounted on, and show its disk usage. */static voidshow_disk (const char *disk){ struct mount_entry *me; for (me = mount_list; me; me = me->me_next) if (STREQ (disk, me->me_devname)) { show_dev (me->me_devname, me->me_mountdir, me->me_type, me->me_dummy, me->me_remote); return; } /* No filesystem is mounted on DISK. */ show_dev (disk, (char *) NULL, (char *) NULL, 0, 0);}/* Return the root mountpoint of the filesystem on which FILE exists, in malloced storage. FILE_STAT should be the result of stating FILE. */static char *find_mount_point (const char *file, const struct stat *file_stat){ struct saved_cwd cwd; struct stat last_stat; char *mp = 0; /* The malloced mount point path. */ if (save_cwd (&cwd)) return NULL; if (S_ISDIR (file_stat->st_mode)) /* FILE is a directory, so just chdir there directly. */ { last_stat = *file_stat; if (chdir (file) < 0) return NULL; } else /* FILE is some other kind of file, we need to use its directory. */ { char *dir = dir_name (file); int rv = chdir (dir); free (dir); if (rv < 0) return NULL; if (stat (".", &last_stat) < 0) goto done; } /* Now walk up FILE's parents until we find another filesystem or /, chdiring as we go. LAST_STAT holds stat information for the last place we visited. */ for (;;) { struct stat st; if (stat ("..", &st) < 0) goto done; if (st.st_dev != last_stat.st_dev || st.st_ino == last_stat.st_ino) /* cwd is the mount point. */ break; if (chdir ("..") < 0) goto done; last_stat = st; } /* Finally reached a mount point, see what it's called. */ mp = xgetcwd ();done:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -