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