home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / fileutil.lha / fileutils-3.3 / src / df.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-02  |  9.4 KB  |  399 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] [--all] [--inodes] [--type fstype]
  19.    [--kilobytes] [--portability] [path...]
  20.  
  21.    Options:
  22.    -a, --all        List all filesystems, even zero-size ones.
  23.    -i, --inodes        List inode usage information instead of block usage.
  24.    -k, --kilobytes    Print sizes in 1K blocks instead of 512-byte blocks.
  25.    -P, --portability    Use the POSIX output format (one line per filesystem).
  26.    -t, --type fstype    Limit the listing to filesystems of type `fstype'.
  27.             Multiple -t options can be given.
  28.             By default, all filesystem types are listed.
  29.  
  30.    Written by David MacKenzie <djm@gnu.ai.mit.edu> */
  31.  
  32. #include <stdio.h>
  33. #include <sys/types.h>
  34. #include <getopt.h>
  35. #include "mountlist.h"
  36. #include "fsusage.h"
  37. #include "system.h"
  38.  
  39. char *strstr ();
  40. char *xmalloc ();
  41. char *xstrdup ();
  42. int fs_to_list ();
  43. void add_fs_type ();
  44. void error ();
  45. void print_header ();
  46. void show_entry ();
  47. void show_all_entries ();
  48. void show_dev ();
  49. void show_disk ();
  50. void show_point ();
  51. void usage ();
  52.  
  53. /* If nonzero, show inode information. */
  54. int inode_format;
  55.  
  56. /* If nonzero, show even filesystems with zero size or
  57.    uninteresting types. */
  58. int show_all_fs;
  59.  
  60. /* If nonzero, use 1K blocks instead of 512-byte blocks. */
  61. int kilobyte_blocks;
  62.  
  63. /* If nonzero, use the POSIX output format.  */
  64. int posix_format;
  65.  
  66. /* Nonzero if errors have occurred. */
  67. int exit_status;
  68.  
  69. /* Name this program was run with. */
  70. char *program_name;
  71.  
  72. /* A filesystem type to display. */
  73.  
  74. struct fs_select
  75. {
  76.   char *fs_name;
  77.   struct fs_select *fs_next;
  78. };
  79.  
  80. /* Linked list of filesystem types to display.
  81.    If `fs_list' is NULL, list all types.
  82.    This table is generated dynamically from command-line options,
  83.    rather than hardcoding into the program what it thinks are the
  84.    valid filesystem types; let the user specify any filesystem type
  85.    they want to, and if there are any filesystems of that type, they
  86.    will be shown.
  87.  
  88.    Some filesystem types:
  89.    4.2 4.3 ufs nfs swap ignore io vm */
  90.  
  91. struct fs_select *fs_list;
  92.  
  93. /* Linked list of mounted filesystems. */
  94. struct mount_entry *mount_list;
  95.  
  96. struct option long_options[] =
  97. {
  98.   {"all", 0, &show_all_fs, 1},
  99.   {"inodes", 0, &inode_format, 1},
  100.   {"kilobytes", 0, &kilobyte_blocks, 1},
  101.   {"portability", 0, &posix_format, 1},
  102.   {"type", 1, 0, 't'},
  103.   {NULL, 0, NULL, 0}
  104. };
  105.  
  106. void
  107. main (argc, argv)
  108.      int argc;
  109.      char **argv;
  110. {
  111.   int i;
  112.   struct stat *stats;
  113.  
  114.   program_name = argv[0];
  115.   fs_list = NULL;
  116.   inode_format = 0;
  117.   show_all_fs = 0;
  118.   kilobyte_blocks = getenv ("POSIXLY_CORRECT") == 0;
  119.   posix_format = 0;
  120.   exit_status = 0;
  121.  
  122.   while ((i = getopt_long (argc, argv, "aikPt:v", long_options, (int *) 0))
  123.      != EOF)
  124.     {
  125.       switch (i)
  126.     {
  127.     case 0:            /* Long option. */
  128.       break;
  129.     case 'a':
  130.       show_all_fs = 1;
  131.       break;
  132.     case 'i':
  133.       inode_format = 1;
  134.       break;
  135.     case 'k':
  136.       kilobyte_blocks = 1;
  137.       break;
  138.     case 'P':
  139.       posix_format = 1;
  140.       break;
  141.     case 't':
  142.       add_fs_type (optarg);
  143.       break;
  144.     case 'v':        /* For SysV compatibility. */
  145.       break;
  146.     default:
  147.       usage ();
  148.     }
  149.     }
  150.  
  151.   if (optind != argc)
  152.     {
  153.       /* Display explicitly requested empty filesystems. */
  154.       show_all_fs = 1;
  155.  
  156.       /* stat all the given entries to make sure they get automounted,
  157.      if necessary, before reading the filesystem table.  */
  158.       stats = (struct stat *)
  159.     xmalloc ((argc - optind) * sizeof (struct stat));
  160.       for (i = optind; i < argc; ++i)
  161.     if (stat (argv[i], &stats[i - optind]))
  162.       {
  163.         error (0, errno, "%s", argv[i]);
  164.         exit_status = 1;
  165.         argv[i] = NULL;
  166.       }
  167.     }
  168.  
  169.   mount_list = read_filesystem_list (fs_list != NULL, show_all_fs);
  170.   if (mount_list == NULL)
  171.     error (1, errno, "cannot read table of mounted filesystems");
  172.  
  173.   print_header ();
  174.   sync ();
  175.  
  176.   if (optind == argc)
  177.     show_all_entries ();
  178.   else
  179.     for (i = optind; i < argc; ++i)
  180.       if (argv[i])
  181.     show_entry (argv[i], &stats[i - optind]);
  182.  
  183.   exit (exit_status);
  184. }
  185.  
  186. void
  187. print_header ()
  188. {
  189.   if (inode_format)
  190.     printf ("Filesystem            Inodes   IUsed   IFree  %%IUsed");
  191.   else
  192.     printf ("Filesystem         %s  Used Available Capacity",
  193.         kilobyte_blocks ? "1024-blocks" : " 512-blocks");
  194.   printf (" Mounted on\n");
  195. }
  196.  
  197. /* Show all mounted filesystems, except perhaps those that are of
  198.    an unselected type or are empty. */
  199.  
  200. void
  201. show_all_entries ()
  202. {
  203.   struct mount_entry *me;
  204.  
  205.   for (me = mount_list; me; me = me->me_next)
  206.     show_dev (me->me_devname, me->me_mountdir, me->me_type);
  207. }
  208.  
  209. /* Determine what kind of node PATH is and show the disk usage
  210.    for it.  STATP is the results of `stat' on PATH.  */
  211.  
  212. void
  213. show_entry (path, statp)
  214.      char *path;
  215.      struct stat *statp;
  216. {
  217.   if (S_ISBLK (statp->st_mode) || S_ISCHR (statp->st_mode))
  218.     show_disk (path);
  219.   else
  220.     show_point (path, statp);
  221. }
  222.  
  223. /* Identify the directory, if any, that device
  224.    DISK is mounted on, and show its disk usage.  */
  225.  
  226. void
  227. show_disk (disk)
  228.      char *disk;
  229. {
  230.   struct mount_entry *me;
  231.  
  232.   for (me = mount_list; me; me = me->me_next)
  233.     if (!strcmp (disk, me->me_devname))
  234.       {
  235.     show_dev (me->me_devname, me->me_mountdir, me->me_type);
  236.     return;
  237.       }
  238.   /* No filesystem is mounted on DISK. */
  239.   show_dev (disk, (char *) NULL, (char *) NULL);
  240. }
  241.  
  242. /* Figure out which device file or directory POINT is mounted on
  243.    and show its disk usage.
  244.    STATP is the results of `stat' on POINT.  */
  245.  
  246. void
  247. show_point (point, statp)
  248.      char *point;
  249.      struct stat *statp;
  250. {
  251.   struct stat disk_stats;
  252.   struct mount_entry *me;
  253.  
  254.   for (me = mount_list; me; me = me->me_next)
  255.     {
  256.       if (me->me_dev == (dev_t) -1)
  257.     {
  258.       if (stat (me->me_mountdir, &disk_stats) == 0)
  259.         me->me_dev = disk_stats.st_dev;
  260.       else
  261.         {
  262.           error (0, errno, "%s", me->me_mountdir);
  263.           exit_status = 1;
  264.           me->me_dev = -2;    /* So we won't try and fail repeatedly. */
  265.         }
  266.     }
  267.  
  268.       if (statp->st_dev == me->me_dev)
  269.     {
  270.       show_dev (me->me_devname, me->me_mountdir, me->me_type);
  271.       return;
  272.     }
  273.     }
  274.   error (0, 0, "cannot find mount point for %s", point);
  275.   exit_status = 1;
  276. }
  277.  
  278. /* Display a space listing for the disk device with absolute path DISK.
  279.    If MOUNT_POINT is non-NULL, it is the path of the root of the
  280.    filesystem on DISK.
  281.    If FSTYPE is non-NULL, it is the type of the filesystem on DISK. */
  282.  
  283. void
  284. show_dev (disk, mount_point, fstype)
  285.      char *disk;
  286.      char *mount_point;
  287.      char *fstype;
  288. {
  289.   struct fs_usage fsu;
  290.   long blocks_used;
  291.   long blocks_percent_used;
  292.   long inodes_used;
  293.   long inodes_percent_used;
  294.   char *stat_file;
  295.  
  296.   if (!fs_to_list (fstype))
  297.     return;
  298.  
  299.   /* If MOUNT_POINT is NULL, then the filesystem is not mounted, and this
  300.      program reports on the filesystem that the special file is on.
  301.      It would be better to report on the unmounted filesystem,
  302.      but statfs doesn't do that on most systems.  */
  303.   stat_file = mount_point ? mount_point : disk;
  304.  
  305.   if (get_fs_usage (stat_file, disk, &fsu))
  306.     {
  307.       error (0, errno, "%s", stat_file);
  308.       exit_status = 1;
  309.       return;
  310.     }
  311.  
  312.   if (kilobyte_blocks)
  313.     {
  314.       fsu.fsu_blocks /= 2;
  315.       fsu.fsu_bfree /= 2;
  316.       fsu.fsu_bavail /= 2;
  317.     }
  318.  
  319.   if (fsu.fsu_blocks == 0)
  320.     {
  321.       if (show_all_fs == 0)
  322.     return;
  323.       blocks_used = fsu.fsu_bavail = blocks_percent_used = 0;
  324.     }
  325.   else
  326.     {
  327.       blocks_used = fsu.fsu_blocks - fsu.fsu_bfree;
  328.       blocks_percent_used = (long)
  329.     (blocks_used * 100.0 / (blocks_used + fsu.fsu_bavail) + 0.5);
  330.     }
  331.  
  332.   if (fsu.fsu_files == 0)
  333.     {
  334.       inodes_used = fsu.fsu_ffree = inodes_percent_used = 0;
  335.     }
  336.   else
  337.     {
  338.       inodes_used = fsu.fsu_files - fsu.fsu_ffree;
  339.       inodes_percent_used = (long)
  340.     (inodes_used * 100.0 / fsu.fsu_files + 0.5);
  341.     }
  342.  
  343.   printf ("%-20s", disk);
  344.   if (strlen (disk) > 20 && !posix_format)
  345.     printf ("\n                    ");
  346.  
  347.   if (inode_format)
  348.     printf (" %7ld %7ld %7ld %5ld%%",
  349.         fsu.fsu_files, inodes_used, fsu.fsu_ffree, inodes_percent_used);
  350.   else
  351.     printf (" %7ld %7ld  %7ld  %5ld%% ",
  352.         fsu.fsu_blocks, blocks_used, fsu.fsu_bavail, blocks_percent_used);
  353.  
  354.   if (mount_point)
  355.     printf ("  %s", mount_point);
  356.   putchar ('\n');
  357. }
  358.  
  359. /* Add FSTYPE to the list of filesystem types to display. */
  360.  
  361. void
  362. add_fs_type (fstype)
  363.      char *fstype;
  364. {
  365.   struct fs_select *fsp;
  366.  
  367.   fsp = (struct fs_select *) xmalloc (sizeof (struct fs_select));
  368.   fsp->fs_name = fstype;
  369.   fsp->fs_next = fs_list;
  370.   fs_list = fsp;
  371. }
  372.  
  373. /* If FSTYPE is a type of filesystem that should be listed,
  374.    return nonzero, else zero. */
  375.  
  376. int
  377. fs_to_list (fstype)
  378.      char *fstype;
  379. {
  380.   struct fs_select *fsp;
  381.  
  382.   if (fs_list == NULL || fstype == NULL)
  383.     return 1;
  384.   for (fsp = fs_list; fsp; fsp = fsp->fs_next)
  385.     if (!strcmp (fstype, fsp->fs_name))
  386.       return 1;
  387.   return 0;
  388. }
  389.  
  390. void
  391. usage ()
  392. {
  393.   fprintf (stderr, "\
  394. Usage: %s [-aikPv] [-t fstype] [--all] [--inodes] [--type fstype]\n\
  395.        [--kilobytes] [--portability] [path...]\n",
  396.        program_name);
  397.   exit (1);
  398. }
  399.