home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fileutils-3.6 / src / df.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-14  |  11.6 KB  |  476 lines

  1. /* df - summarize free disk space
  2.    Copyright (C) 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Usage: df [-aikP] [-t fstype] [-x fstype] [--all] [--inodes]
  19.    [--type fstype] [--exclude-type fstype] [--kilobytes] [--portability]
  20.    [path...]
  21.  
  22.    Options:
  23.    -a, --all        List all filesystems, even zero-size ones.
  24.    -i, --inodes        List inode usage information instead of block usage.
  25.    -k, --kilobytes    Print sizes in 1K blocks instead of 512-byte blocks.
  26.    -P, --portability    Use the POSIX output format (one line per filesystem).
  27.    -t, --type fstype    Limit the listing to filesystems of type `fstype'.
  28.    -x, --exclude-type=fstype
  29.             Limit the listing to filesystems not of type `fstype'.
  30.             Multiple -t and/or -x options can be given.
  31.             By default, all filesystem types are listed.
  32.  
  33.    Written by David MacKenzie <djm@gnu.ai.mit.edu> */
  34.  
  35. #include <stdio.h>
  36. #include <sys/types.h>
  37. #include <getopt.h>
  38. #include "mountlist.h"
  39. #include "fsusage.h"
  40. #include "system.h"
  41. #include "version.h"
  42.  
  43. char *strstr ();
  44. char *xmalloc ();
  45. char *xstrdup ();
  46. void error ();
  47.  
  48. static int selected_fstype ();
  49. static int excluded_fstype ();
  50. static void add_excluded_fs_type ();
  51. static void add_fs_type ();
  52. static void print_header ();
  53. static void show_entry ();
  54. static void show_all_entries ();
  55. static void show_dev ();
  56. static void show_disk ();
  57. static void show_point ();
  58. static void usage ();
  59.  
  60. /* Name this program was run with. */
  61. char *program_name;
  62.  
  63. /* If nonzero, show inode information. */
  64. static int inode_format;
  65.  
  66. /* If nonzero, show even filesystems with zero size or
  67.    uninteresting types. */
  68. static int show_all_fs;
  69.  
  70. /* If nonzero, output data for each filesystem corresponding to a
  71.    command line argument -- even if it's a dummy (automounter) entry.  */
  72. static int show_listed_fs;
  73.  
  74. /* If nonzero, use 1K blocks instead of 512-byte blocks. */
  75. static int kilobyte_blocks;
  76.  
  77. /* If nonzero, use the POSIX output format.  */
  78. static int posix_format;
  79.  
  80. /* Nonzero if errors have occurred. */
  81. static int exit_status;
  82.  
  83. /* A filesystem type to display. */
  84.  
  85. struct fs_type_list
  86. {
  87.   char *fs_name;
  88.   struct fs_type_list *fs_next;
  89. };
  90.  
  91. /* Linked list of filesystem types to display.
  92.    If `fs_select_list' is NULL, list all types.
  93.    This table is generated dynamically from command-line options,
  94.    rather than hardcoding into the program what it thinks are the
  95.    valid filesystem types; let the user specify any filesystem type
  96.    they want to, and if there are any filesystems of that type, they
  97.    will be shown.
  98.  
  99.    Some filesystem types:
  100.    4.2 4.3 ufs nfs swap ignore io vm efs dbg */
  101.  
  102. static struct fs_type_list *fs_select_list;
  103.  
  104. /* Linked list of filesystem types to omit.
  105.    If the list is empty, don't exclude any types.  */
  106.  
  107. static struct fs_type_list *fs_exclude_list;
  108.  
  109. /* Linked list of mounted filesystems. */
  110. static struct mount_entry *mount_list;
  111.  
  112. /* If non-zero, display usage information and exit.  */
  113. static int flag_help;
  114.  
  115. /* If non-zero, print the version on standard error.  */
  116. static int flag_version;
  117.  
  118. static struct option const long_options[] =
  119. {
  120.   {"all", no_argument, &show_all_fs, 1},
  121.   {"inodes", no_argument, &inode_format, 1},
  122.   {"kilobytes", no_argument, &kilobyte_blocks, 1},
  123.   {"portability", no_argument, &posix_format, 1},
  124.   {"type", required_argument, 0, 't'},
  125.   {"exclude-type", required_argument, 0, 'x'},
  126.   {"help", no_argument, &flag_help, 1},
  127.   {"version", no_argument, &flag_version, 1},
  128.   {NULL, 0, NULL, 0}
  129. };
  130.  
  131. void
  132. main (argc, argv)
  133.      int argc;
  134.      char **argv;
  135. {
  136.   int i;
  137.   struct stat *stats;
  138.  
  139.   program_name = argv[0];
  140.   fs_select_list = NULL;
  141.   fs_exclude_list = NULL;
  142.   inode_format = 0;
  143.   show_all_fs = 0;
  144.   show_listed_fs = 0;
  145.   kilobyte_blocks = getenv ("POSIXLY_CORRECT") == 0;
  146.   posix_format = 0;
  147.   exit_status = 0;
  148.  
  149.   while ((i = getopt_long (argc, argv, "aikPt:vx:", long_options, (int *) 0))
  150.      != EOF)
  151.     {
  152.       switch (i)
  153.     {
  154.     case 0:            /* Long option. */
  155.       break;
  156.     case 'a':
  157.       show_all_fs = 1;
  158.       break;
  159.     case 'i':
  160.       inode_format = 1;
  161.       break;
  162.     case 'k':
  163.       kilobyte_blocks = 1;
  164.       break;
  165.     case 'P':
  166.       posix_format = 1;
  167.       break;
  168.     case 't':
  169.       add_fs_type (optarg);
  170.       break;
  171.     case 'v':        /* For SysV compatibility. */
  172.       break;
  173.     case 'x':
  174.       add_excluded_fs_type (optarg);
  175.       break;
  176.     default:
  177.       usage ();
  178.     }
  179.     }
  180.  
  181.   if (flag_version)
  182.     {
  183.       fprintf (stderr, "%s\n", version_string);
  184.       exit (0);
  185.     }
  186.  
  187.   if (flag_help)
  188.     usage ();
  189.  
  190.   if (optind != argc)
  191.     {
  192.       /* stat all the given entries to make sure they get automounted,
  193.      if necessary, before reading the filesystem table.  */
  194.       stats = (struct stat *)
  195.     xmalloc ((argc - optind) * sizeof (struct stat));
  196.       for (i = optind; i < argc; ++i)
  197.     if (stat (argv[i], &stats[i - optind]))
  198.       {
  199.         error (0, errno, "%s", argv[i]);
  200.         exit_status = 1;
  201.         argv[i] = NULL;
  202.       }
  203.     }
  204.  
  205.   mount_list =
  206.     read_filesystem_list ((fs_select_list != NULL || fs_exclude_list != NULL),
  207.               show_all_fs);
  208.  
  209.   if (mount_list == NULL)
  210.     error (1, errno, "cannot read table of mounted filesystems");
  211.  
  212.   print_header ();
  213.   sync ();
  214.  
  215.   if (optind == argc)
  216.     show_all_entries ();
  217.   else
  218.     {
  219.       /* Display explicitly requested empty filesystems. */
  220.       show_listed_fs = 1;
  221.  
  222.       for (i = optind; i < argc; ++i)
  223.     if (argv[i])
  224.       show_entry (argv[i], &stats[i - optind]);
  225.     }
  226.  
  227.   exit (exit_status);
  228. }
  229.  
  230. static void
  231. print_header ()
  232. {
  233.   if (inode_format)
  234.     printf ("Filesystem            Inodes   IUsed   IFree  %%IUsed");
  235.   else
  236.     printf ("Filesystem         %s  Used Available Capacity",
  237.         kilobyte_blocks ? "1024-blocks" : " 512-blocks");
  238.   printf (" Mounted on\n");
  239. }
  240.  
  241. /* Show all mounted filesystems, except perhaps those that are of
  242.    an unselected type or are empty. */
  243.  
  244. static void
  245. show_all_entries ()
  246. {
  247.   struct mount_entry *me;
  248.  
  249.   for (me = mount_list; me; me = me->me_next)
  250.     show_dev (me->me_devname, me->me_mountdir, me->me_type);
  251. }
  252.  
  253. /* Determine what kind of node PATH is and show the disk usage
  254.    for it.  STATP is the results of `stat' on PATH.  */
  255.  
  256. static void
  257. show_entry (path, statp)
  258.      char *path;
  259.      struct stat *statp;
  260. {
  261.   if (S_ISBLK (statp->st_mode) || S_ISCHR (statp->st_mode))
  262.     show_disk (path);
  263.   else
  264.     show_point (path, statp);
  265. }
  266.  
  267. /* Identify the directory, if any, that device
  268.    DISK is mounted on, and show its disk usage.  */
  269.  
  270. static void
  271. show_disk (disk)
  272.      char *disk;
  273. {
  274.   struct mount_entry *me;
  275.  
  276.   for (me = mount_list; me; me = me->me_next)
  277.     if (!strcmp (disk, me->me_devname))
  278.       {
  279.     show_dev (me->me_devname, me->me_mountdir, me->me_type);
  280.     return;
  281.       }
  282.   /* No filesystem is mounted on DISK. */
  283.   show_dev (disk, (char *) NULL, (char *) NULL);
  284. }
  285.  
  286. /* Figure out which device file or directory POINT is mounted on
  287.    and show its disk usage.
  288.    STATP is the results of `stat' on POINT.  */
  289.  
  290. static void
  291. show_point (point, statp)
  292.      char *point;
  293.      struct stat *statp;
  294. {
  295.   struct stat disk_stats;
  296.   struct mount_entry *me;
  297.  
  298.   for (me = mount_list; me; me = me->me_next)
  299.     {
  300.       if (me->me_dev == (dev_t) -1)
  301.     {
  302.       if (stat (me->me_mountdir, &disk_stats) == 0)
  303.         me->me_dev = disk_stats.st_dev;
  304.       else
  305.         {
  306.           error (0, errno, "%s", me->me_mountdir);
  307.           exit_status = 1;
  308.           me->me_dev = -2;    /* So we won't try and fail repeatedly. */
  309.         }
  310.     }
  311.  
  312.       if (statp->st_dev == me->me_dev)
  313.     {
  314.       show_dev (me->me_devname, me->me_mountdir, me->me_type);
  315.       return;
  316.     }
  317.     }
  318.   error (0, 0, "cannot find mount point for %s", point);
  319.   exit_status = 1;
  320. }
  321.  
  322. /* Display a space listing for the disk device with absolute path DISK.
  323.    If MOUNT_POINT is non-NULL, it is the path of the root of the
  324.    filesystem on DISK.
  325.    If FSTYPE is non-NULL, it is the type of the filesystem on DISK. */
  326.  
  327. static void
  328. show_dev (disk, mount_point, fstype)
  329.      char *disk;
  330.      char *mount_point;
  331.      char *fstype;
  332. {
  333.   struct fs_usage fsu;
  334.   long blocks_used;
  335.   long blocks_percent_used;
  336.   long inodes_used;
  337.   long inodes_percent_used;
  338.   char *stat_file;
  339.  
  340.   if (!selected_fstype (fstype) || excluded_fstype (fstype))
  341.     return;
  342.  
  343.   /* If MOUNT_POINT is NULL, then the filesystem is not mounted, and this
  344.      program reports on the filesystem that the special file is on.
  345.      It would be better to report on the unmounted filesystem,
  346.      but statfs doesn't do that on most systems.  */
  347.   stat_file = mount_point ? mount_point : disk;
  348.  
  349.   if (get_fs_usage (stat_file, disk, &fsu))
  350.     {
  351.       error (0, errno, "%s", stat_file);
  352.       exit_status = 1;
  353.       return;
  354.     }
  355.  
  356.   if (kilobyte_blocks)
  357.     {
  358.       fsu.fsu_blocks /= 2;
  359.       fsu.fsu_bfree /= 2;
  360.       fsu.fsu_bavail /= 2;
  361.     }
  362.  
  363.   if (fsu.fsu_blocks == 0)
  364.     {
  365.       if (!show_all_fs && !show_listed_fs)
  366.     return;
  367.       blocks_used = fsu.fsu_bavail = blocks_percent_used = 0;
  368.     }
  369.   else
  370.     {
  371.       blocks_used = fsu.fsu_blocks - fsu.fsu_bfree;
  372.       blocks_percent_used = (long)
  373.     (blocks_used * 100.0 / (blocks_used + fsu.fsu_bavail) + 0.5);
  374.     }
  375.  
  376.   if (fsu.fsu_files == 0)
  377.     {
  378.       inodes_used = fsu.fsu_ffree = inodes_percent_used = 0;
  379.     }
  380.   else
  381.     {
  382.       inodes_used = fsu.fsu_files - fsu.fsu_ffree;
  383.       inodes_percent_used = (long)
  384.     (inodes_used * 100.0 / fsu.fsu_files + 0.5);
  385.     }
  386.  
  387.   printf ("%-20s", disk);
  388.   if (strlen (disk) > 20 && !posix_format)
  389.     printf ("\n                    ");
  390.  
  391.   if (inode_format)
  392.     printf (" %7ld %7ld %7ld %5ld%%",
  393.         fsu.fsu_files, inodes_used, fsu.fsu_ffree, inodes_percent_used);
  394.   else
  395.     printf (" %7ld %7ld  %7ld  %5ld%% ",
  396.         fsu.fsu_blocks, blocks_used, fsu.fsu_bavail, blocks_percent_used);
  397.  
  398.   if (mount_point)
  399.     printf ("  %s", mount_point);
  400.   putchar ('\n');
  401. }
  402.  
  403. /* Add FSTYPE to the list of filesystem types to display. */
  404.  
  405. static void
  406. add_fs_type (fstype)
  407.      char *fstype;
  408. {
  409.   struct fs_type_list *fsp;
  410.  
  411.   fsp = (struct fs_type_list *) xmalloc (sizeof (struct fs_type_list));
  412.   fsp->fs_name = fstype;
  413.   fsp->fs_next = fs_select_list;
  414.   fs_select_list = fsp;
  415. }
  416.  
  417. /* Add FSTYPE to the list of filesystem types to be omitted. */
  418.  
  419. static void
  420. add_excluded_fs_type (fstype)
  421.      char *fstype;
  422. {
  423.   struct fs_type_list *fsp;
  424.  
  425.   fsp = (struct fs_type_list *) xmalloc (sizeof (struct fs_type_list));
  426.   fsp->fs_name = fstype;
  427.   fsp->fs_next = fs_exclude_list;
  428.   fs_exclude_list = fsp;
  429. }
  430.  
  431. /* If FSTYPE is a type of filesystem that should be listed,
  432.    return nonzero, else zero. */
  433.  
  434. static int
  435. selected_fstype (fstype)
  436.      char *fstype;
  437. {
  438.   struct fs_type_list *fsp;
  439.  
  440.   if (fs_select_list == NULL || fstype == NULL)
  441.     return 1;
  442.   for (fsp = fs_select_list; fsp; fsp = fsp->fs_next)
  443.     if (!strcmp (fstype, fsp->fs_name))
  444.       return 1;
  445.   return 0;
  446. }
  447.  
  448.  
  449. /* If FSTYPE is a type of filesystem that should be omitted,
  450.    return nonzero, else zero. */
  451.  
  452. static int
  453. excluded_fstype (fstype)
  454.      char *fstype;
  455. {
  456.   struct fs_type_list *fsp;
  457.  
  458.   if (fs_exclude_list == NULL || fstype == NULL)
  459.     return 0;
  460.   for (fsp = fs_exclude_list; fsp; fsp = fsp->fs_next)
  461.     if (!strcmp (fstype, fsp->fs_name))
  462.       return 1;
  463.   return 0;
  464. }
  465.  
  466. static void
  467. usage ()
  468. {
  469.   fprintf (stderr, "\
  470. Usage: %s [-aikPv] [-t fstype] [-x fstype] [--all] [--inodes]\n\
  471. \t[--type=fstype] [--exclude-type=fstype] [--kilobytes] [--portability]\n\
  472. \t[--help] [--version] [path...]\n",
  473.        program_name);
  474.   exit (1);
  475. }
  476.