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