home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / uucp-1.04 / unix / fsusg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-13  |  5.4 KB  |  232 lines

  1. /* fsusage.c -- return space usage of mounted filesystems
  2.    Copyright (C) 1991, 1992 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.    This file was modified slightly by Ian Lance Taylor, December 1992,
  19.    for use with Taylor UUCP.  */
  20.  
  21. #include "uucp.h"
  22. #include "sysdep.h"
  23. #include "fsusg.h"
  24.  
  25. int statfs ();
  26.  
  27. #if STAT_STATFS2_BSIZE
  28. #ifndef _IBMR2            /* 4.3BSD, SunOS 4, HP-UX, AIX PS/2.  */
  29. #include <sys/vfs.h>
  30. #endif
  31. #endif
  32.  
  33. #if STAT_STATFS2_FSIZE        /* 4.4BSD.  */
  34. #include <sys/mount.h>
  35. #endif
  36.  
  37. #if STAT_STATFS2_FS_DATA    /* Ultrix.  */
  38. #include <sys/param.h>
  39. #include <sys/mount.h>
  40. #endif
  41.  
  42. #if STAT_USTAT            /* SVR2 and others.  */
  43. #include <ustat.h>
  44. #endif
  45.  
  46. #if STAT_STATFS4        /* SVR3, Dynix, Irix.  */
  47. #include <sys/statfs.h>
  48. #endif
  49. #ifdef _AIX
  50. #ifdef _IBMR2            /* AIX RS6000.  */
  51. #include <sys/statfs.h>
  52. #endif
  53. #endif
  54.  
  55. #ifdef _AIX
  56. #ifdef _I386            /* AIX PS/2.  */
  57. #include <sys/stat.h>
  58. #include <sys/dustat.h>
  59. #endif
  60. #endif
  61.  
  62. #if STAT_STATVFS        /* SVR4.  */
  63. #include <sys/statvfs.h>
  64. int statvfs ();
  65. #endif
  66.  
  67. #define STAT_NONE 0
  68.  
  69. #if ! STAT_STATVFS
  70. #if ! STAT_STATFS2_BSIZE
  71. #if ! STAT_STATFS2_FSIZE
  72. #if ! STAT_STATFS2_FS_DATA
  73. #if ! STAT_STATFS4
  74. #if ! STAT_USTAT
  75. #undef STAT_NONE
  76. #define STAT_NONE 1
  77. #endif
  78. #endif
  79. #endif
  80. #endif
  81. #endif
  82. #endif
  83.  
  84. #if ! STAT_NONE
  85.  
  86. /* Return the number of TOSIZE-byte blocks used by
  87.    BLOCKS FROMSIZE-byte blocks, rounding up.  */
  88.  
  89. static long
  90. adjust_blocks (blocks, fromsize, tosize)
  91.      long blocks;
  92.      int fromsize, tosize;
  93. {
  94.   if (fromsize == tosize)    /* E.g., from 512 to 512.  */
  95.     return blocks;
  96.   else if (fromsize > tosize)    /* E.g., from 2048 to 512.  */
  97.     return blocks * (fromsize / tosize);
  98.   else                /* E.g., from 256 to 512.  */
  99.     return (blocks + 1) / (tosize / fromsize);
  100. }
  101.  
  102. #endif
  103.  
  104. /* Fill in the fields of FSP with information about space usage for
  105.    the filesystem on which PATH resides.
  106.    DISK is the device on which PATH is mounted, for space-getting
  107.    methods that need to know it.
  108.    Return 0 if successful, -1 if not. */
  109.  
  110. int
  111. get_fs_usage (path, disk, fsp)
  112.      char *path, *disk;
  113.      struct fs_usage *fsp;
  114. {
  115. #if STAT_NONE
  116.   return -1;
  117. #endif
  118.  
  119. #if STAT_STATFS2_FS_DATA    /* Ultrix.  */
  120.   struct fs_data fsd;
  121.  
  122.   if (statfs (path, &fsd) != 1)
  123.     return -1;
  124. #define convert_blocks(b) adjust_blocks ((b), 1024, 512)
  125.   fsp->fsu_blocks = convert_blocks (fsd.fd_req.btot);
  126.   fsp->fsu_bfree = convert_blocks (fsd.fd_req.bfree);
  127.   fsp->fsu_bavail = convert_blocks (fsd.fd_req.bfreen);
  128.   fsp->fsu_files = fsd.fd_req.gtot;
  129.   fsp->fsu_ffree = fsd.fd_req.gfree;
  130. #endif
  131.  
  132. #if STAT_STATFS2_BSIZE        /* 4.3BSD, SunOS 4, HP-UX, AIX.  */
  133.   struct statfs fsd;
  134.  
  135.   if (statfs (path, &fsd) < 0)
  136.     return -1;
  137. #define convert_blocks(b) adjust_blocks ((b), fsd.f_bsize, 512)
  138. #endif
  139.  
  140. #if STAT_STATFS2_FSIZE        /* 4.4BSD.  */
  141.   struct statfs fsd;
  142.  
  143.   if (statfs (path, &fsd) < 0)
  144.     return -1;
  145. #define convert_blocks(b) adjust_blocks ((b), fsd.f_fsize, 512)
  146. #endif
  147.  
  148. #if STAT_STATFS4        /* SVR3, Dynix, Irix.  */
  149.   struct statfs fsd;
  150.  
  151.   if (statfs (path, &fsd, sizeof fsd, 0) < 0)
  152.     return -1;
  153.   /* Empirically, the block counts on most SVR3 and SVR3-derived
  154.      systems seem to always be in terms of 512-byte blocks,
  155.      no matter what value f_bsize has.  */
  156. #define convert_blocks(b) (b)
  157. #ifndef _SEQUENT_        /* _SEQUENT_ is DYNIX/ptx.  */
  158. #define f_bavail f_bfree
  159. #endif
  160. #endif
  161.  
  162. #if STAT_STATVFS        /* SVR4.  */
  163.   struct statvfs fsd;
  164.  
  165.   if (statvfs (path, &fsd) < 0)
  166.     return -1;
  167.   /* f_frsize isn't guaranteed to be supported.  */
  168. #define convert_blocks(b) \
  169.   adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
  170. #endif
  171.  
  172. #if STAT_USTAT
  173.   {
  174.     struct stat sstat;
  175.     struct ustat s;
  176.  
  177.     if (stat (path, &sstat) < 0
  178.     || ustat (sstat.st_dev, &s) < 0)
  179.       return -1;
  180.     fsp->fsu_blocks = -1;
  181.     fsp->fsu_bfree = f_tfree;
  182.     fsp->fsu_bavail = f_tfree;
  183.     fsp->fsu_files = -1;
  184.     fsp->fsu_ffree = -1;
  185.   }
  186. #endif
  187.  
  188. #if ! STAT_STATFS2_FS_DATA /* ! Ultrix */
  189. #if ! STAT_USTAT
  190. #if ! STAT_NONE
  191.   fsp->fsu_blocks = convert_blocks (fsd.f_blocks);
  192.   fsp->fsu_bfree = convert_blocks (fsd.f_bfree);
  193.   fsp->fsu_bavail = convert_blocks (fsd.f_bavail);
  194.   fsp->fsu_files = fsd.f_files;
  195.   fsp->fsu_ffree = fsd.f_ffree;
  196. #endif
  197. #endif
  198. #endif
  199.  
  200.   return 0;
  201. }
  202.  
  203. #ifdef _AIX
  204. #ifdef _I386
  205. /* AIX PS/2 does not supply statfs.  */
  206.  
  207. int
  208. statfs (path, fsb)
  209.      char *path;
  210.      struct statfs *fsb;
  211. {
  212.   struct stat stats;
  213.   struct dustat fsd;
  214.  
  215.   if (stat (path, &stats))
  216.     return -1;
  217.   if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
  218.     return -1;
  219.   fsb->f_type   = 0;
  220.   fsb->f_bsize  = fsd.du_bsize;
  221.   fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
  222.   fsb->f_bfree  = fsd.du_tfree;
  223.   fsb->f_bavail = fsd.du_tfree;
  224.   fsb->f_files  = (fsd.du_isize - 2) * fsd.du_inopb;
  225.   fsb->f_ffree  = fsd.du_tinode;
  226.   fsb->f_fsid.val[0] = fsd.du_site;
  227.   fsb->f_fsid.val[1] = fsd.du_pckno;
  228.   return 0;
  229. }
  230. #endif
  231. #endif /* _AIX && _I386 */
  232.