home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / git-4.3 / git-4 / git-4.3.7 / src / fsusage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-08  |  5.3 KB  |  198 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. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21.  
  22. #include <sys/types.h>
  23.  
  24. #include "xio.h"
  25. #include "fsusage.h"
  26.  
  27. int statfs ();
  28.  
  29. #ifdef HAVE_SYS_PARAM_H
  30. #include <sys/param.h>
  31. #endif
  32.  
  33. #ifdef HAVE_SYS_MOUNT_H
  34. #include <sys/mount.h>
  35. #endif
  36.  
  37. #ifdef HAVE_SYS_VFS_H
  38. #include <sys/vfs.h>
  39. #endif
  40.  
  41. #ifdef HAVE_SYS_FILSYS_H
  42. #include <sys/filsys.h>         /* SVR2.  */
  43. #endif
  44.  
  45. #ifdef HAVE_FCNTL_H
  46. #include <fcntl.h>
  47. #endif
  48.  
  49. #ifdef HAVE_SYS_STATFS_H
  50. #include <sys/statfs.h>
  51. #endif
  52.  
  53. #ifdef HAVE_DUSTAT_H            /* AIX PS/2.  */
  54. #include <sys/dustat.h>
  55. #endif
  56.  
  57. #ifdef HAVE_SYS_STATVFS_H       /* SVR4.  */
  58. #include <sys/statvfs.h>
  59. int statvfs ();
  60. #endif
  61.  
  62. /* Return the number of TOSIZE-byte blocks used by
  63.    BLOCKS FROMSIZE-byte blocks, rounding away from zero.
  64.    TOSIZE must be positive.  Return -1 if FROMSIZE is not positive.  */
  65.  
  66. long
  67. fs_adjust_blocks (blocks, fromsize, tosize)
  68.      long blocks;
  69.      int fromsize, tosize;
  70. {
  71.   if (tosize <= 0)
  72.     abort ();
  73.   if (fromsize <= 0)
  74.     return -1;
  75.                                                                     
  76.   if (fromsize == tosize)       /* E.g., from 512 to 512.  */
  77.     return blocks;
  78.   else if (fromsize > tosize)   /* E.g., from 2048 to 512.  */
  79.     return blocks * (fromsize / tosize);
  80.   else                          /* E.g., from 256 to 512.  */
  81.     return (blocks + (blocks < 0 ? -1 : 1)) / (tosize / fromsize);
  82. }
  83.  
  84. /* Fill in the fields of FSP with information about space usage for
  85.    the filesystem on which PATH resides.
  86.    Return 0 if successful, -1 if not. */
  87.  
  88. int
  89. get_fs_usage (path, fsp)
  90.      char *path;
  91.      struct fs_usage *fsp;
  92. {
  93. #if defined (STAT_STATFS3_OSF1)
  94.   struct statfs fsd;
  95.  
  96.   if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
  97.     return -1;
  98. #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
  99. #endif /* STAT_STATFS3_OSF1 */
  100.  
  101. #ifdef STAT_STATFS2_FS_DATA     /* Ultrix.  */
  102.   struct fs_data fsd;
  103.  
  104.   if (statfs (path, &fsd) != 1)
  105.     return -1;
  106. #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), 1024, 512)
  107.   fsp->fsu_blocks = CONVERT_BLOCKS (fsd.fd_req.btot);
  108.   fsp->fsu_bfree = CONVERT_BLOCKS (fsd.fd_req.bfree);
  109.   fsp->fsu_bavail = CONVERT_BLOCKS (fsd.fd_req.bfreen);
  110.   fsp->fsu_files = fsd.fd_req.gtot;
  111.   fsp->fsu_ffree = fsd.fd_req.gfree;
  112. #endif
  113.  
  114. #ifdef STAT_STATFS2_BSIZE       /* 4.3BSD, SunOS 4, HP-UX, AIX.  */
  115.   struct statfs fsd;
  116.  
  117.   if (statfs (path, &fsd) < 0)
  118.     return -1;
  119. #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
  120. #endif
  121.  
  122. #ifdef STAT_STATFS2_FSIZE       /* 4.4BSD.  */
  123.   struct statfs fsd;
  124.  
  125.   if (statfs (path, &fsd) < 0)
  126.     return -1;
  127. #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
  128. #endif
  129.  
  130. #ifdef STAT_STATFS4             /* SVR3, Dynix, Irix, AIX.  */
  131.   struct statfs fsd;
  132.  
  133.   if (statfs (path, &fsd, sizeof fsd, 0) < 0)
  134.     return -1;
  135.   /* Empirically, the block counts on most SVR3 and SVR3-derived
  136.      systems seem to always be in terms of 512-byte blocks,
  137.      no matter what value f_bsize has.  */
  138. # if _AIX
  139. #  define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
  140. # else
  141. #  define CONVERT_BLOCKS(b) (b)
  142. #  ifndef _SEQUENT_             /* _SEQUENT_ is DYNIX/ptx.  */
  143. #   ifndef DOLPHIN              /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
  144. #    define f_bavail f_bfree
  145. #   endif
  146. #  endif
  147. # endif
  148. #endif
  149.  
  150. #ifdef STAT_STATVFS             /* SVR4.  */
  151.   struct statvfs fsd;
  152.  
  153.   if (statvfs (path, &fsd) < 0)
  154.     return -1;
  155.   /* f_frsize isn't guaranteed to be supported.  */
  156. #define CONVERT_BLOCKS(b) \
  157.   fs_adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
  158. #endif
  159.  
  160. #if defined(CONVERT_BLOCKS) && !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS) /* !Ultrix && !SVR2.  */
  161.   fsp->fsu_blocks = CONVERT_BLOCKS (fsd.f_blocks);
  162.   fsp->fsu_bfree = CONVERT_BLOCKS (fsd.f_bfree);
  163.   fsp->fsu_bavail = CONVERT_BLOCKS (fsd.f_bavail);
  164.   fsp->fsu_files = fsd.f_files;
  165.   fsp->fsu_ffree = fsd.f_ffree;
  166. #endif
  167.  
  168.   return 0;
  169. }
  170.  
  171. #if defined(_AIX) && defined(_I386)
  172. /* AIX PS/2 does not supply statfs.  */
  173.  
  174. int
  175. statfs (path, fsb)
  176.      char *path;
  177.      struct statfs *fsb;
  178. {
  179.   struct stat stats;
  180.   struct dustat fsd;
  181.  
  182.   if (xstat (path, &stats))
  183.     return -1;
  184.   if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
  185.     return -1;
  186.   fsb->f_type   = 0;
  187.   fsb->f_bsize  = fsd.du_bsize;
  188.   fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
  189.   fsb->f_bfree  = fsd.du_tfree;
  190.   fsb->f_bavail = fsd.du_tfree;
  191.   fsb->f_files  = (fsd.du_isize - 2) * fsd.du_inopb;
  192.   fsb->f_ffree  = fsd.du_tinode;
  193.   fsb->f_fsid.val[0] = fsd.du_site;
  194.   fsb->f_fsid.val[1] = fsd.du_pckno;
  195.   return 0;
  196. }
  197. #endif /* _AIX && _I386 */
  198.