[HAVE_SYS_FS_S5PARAM_H]: Include sys/fs/s5param.h
[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 Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include "fsusage.h"
25
26 int statfs ();
27
28 #ifdef HAVE_SYS_PARAM_H
29 #include <sys/param.h>
30 #endif
31
32 #ifdef HAVE_SYS_MOUNT_H
33 #include <sys/mount.h>
34 #endif
35
36 #ifdef HAVE_SYS_VFS_H
37 #include <sys/vfs.h>
38 #endif
39
40 #ifdef HAVE_SYS_FS_S5PARAM_H /* Fujitsu UXP/V */
41 #include <sys/fs/s5param.h>
42 #endif
43
44 #if defined(HAVE_SYS_FILSYS_H) && !defined(_CRAY)
45 #include <sys/filsys.h>         /* SVR2.  */
46 #endif
47
48 #ifdef HAVE_FCNTL_H
49 #include <fcntl.h>
50 #endif
51
52 #ifdef HAVE_SYS_STATFS_H
53 #include <sys/statfs.h>
54 #endif
55
56 #ifdef HAVE_DUSTAT_H            /* AIX PS/2.  */
57 #include <sys/dustat.h>
58 #endif
59
60 #ifdef HAVE_SYS_STATVFS_H       /* SVR4.  */
61 #include <sys/statvfs.h>
62 int statvfs ();
63 #endif
64
65 int safe_read ();
66
67 /* Return the number of TOSIZE-byte blocks used by
68    BLOCKS FROMSIZE-byte blocks, rounding away from zero.
69    TOSIZE must be positive.  Return -1 if FROMSIZE is not positive.  */
70
71 static long
72 adjust_blocks (blocks, fromsize, tosize)
73      long blocks;
74      int fromsize, tosize;
75 {
76   if (tosize <= 0)
77     abort ();
78   if (fromsize <= 0)
79     return -1;
80
81   if (fromsize == tosize)       /* E.g., from 512 to 512.  */
82     return blocks;
83   else if (fromsize > tosize)   /* E.g., from 2048 to 512.  */
84     return blocks * (fromsize / tosize);
85   else                          /* E.g., from 256 to 512.  */
86     return (blocks + (blocks < 0 ? -1 : 1)) / (tosize / fromsize);
87 }
88
89 /* Fill in the fields of FSP with information about space usage for
90    the filesystem on which PATH resides.
91    DISK is the device on which PATH is mounted, for space-getting
92    methods that need to know it.
93    Return 0 if successful, -1 if not. */
94
95 int
96 get_fs_usage (path, disk, fsp)
97      const char *path;
98      const char *disk;
99      struct fs_usage *fsp;
100 {
101 #if defined (STAT_STATFS3_OSF1)
102   struct statfs fsd;
103
104   if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
105     return -1;
106 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_fsize, 512)
107 #endif /* STAT_STATFS3_OSF1 */
108
109 #ifdef STAT_STATFS2_FS_DATA     /* Ultrix.  */
110   struct fs_data fsd;
111
112   if (statfs (path, &fsd) != 1)
113     return -1;
114 #define CONVERT_BLOCKS(b) adjust_blocks ((b), 1024, 512)
115   fsp->fsu_blocks = CONVERT_BLOCKS (fsd.fd_req.btot);
116   fsp->fsu_bfree = CONVERT_BLOCKS (fsd.fd_req.bfree);
117   fsp->fsu_bavail = CONVERT_BLOCKS (fsd.fd_req.bfreen);
118   fsp->fsu_files = fsd.fd_req.gtot;
119   fsp->fsu_ffree = fsd.fd_req.gfree;
120 #endif
121
122 #ifdef STAT_READ_FILSYS         /* SVR2.  */
123 #ifndef SUPERBOFF
124 #define SUPERBOFF (SUPERB * 512)
125 #endif
126   struct filsys fsd;
127   int fd;
128
129   fd = open (disk, O_RDONLY);
130   if (fd < 0)
131     return -1;
132   lseek (fd, (long) SUPERBOFF, 0);
133   if (safe_read (fd, (char *) &fsd, sizeof fsd) != sizeof fsd)
134     {
135       close (fd);
136       return -1;
137     }
138   close (fd);
139 #define CONVERT_BLOCKS(b) adjust_blocks ((b), (fsd.s_type == Fs2b ? 1024 : 512), 512)
140   fsp->fsu_blocks = CONVERT_BLOCKS (fsd.s_fsize);
141   fsp->fsu_bfree = CONVERT_BLOCKS (fsd.s_tfree);
142   fsp->fsu_bavail = CONVERT_BLOCKS (fsd.s_tfree);
143   fsp->fsu_files = (fsd.s_isize - 2) * INOPB * (fsd.s_type == Fs2b ? 2 : 1);
144   fsp->fsu_ffree = fsd.s_tinode;
145 #endif
146
147 #ifdef STAT_STATFS2_BSIZE       /* 4.3BSD, SunOS 4, HP-UX, AIX.  */
148   struct statfs fsd;
149
150   if (statfs (path, &fsd) < 0)
151     return -1;
152
153 #ifdef STATFS_TRUNCATES_BLOCK_COUNTS
154   /* In SunOS 4.1.2, 4.1.3, and 4.1.3_U1, the block counts in the
155      struct statfs are truncated to 2GB.  These conditions detect that
156      truncation, presumably without botching the 4.1.1 case, in which
157      the values are not truncated.  The correct counts are stored in
158      undocumented spare fields.  */
159   if (fsd.f_blocks == 0x1fffff && fsd.f_spare[0] > 0)
160     {
161       fsd.f_blocks = fsd.f_spare[0];
162       fsd.f_bfree = fsd.f_spare[1];
163       fsd.f_bavail = fsd.f_spare[2];
164     }
165 #endif /* STATFS_TRUNCATES_BLOCK_COUNTS */
166
167 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_bsize, 512)
168 #endif
169
170 #ifdef STAT_STATFS2_FSIZE       /* 4.4BSD.  */
171   struct statfs fsd;
172
173   if (statfs (path, &fsd) < 0)
174     return -1;
175 #define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_fsize, 512)
176 #endif
177
178 #ifdef STAT_STATFS4             /* SVR3, Dynix, Irix, AIX.  */
179   struct statfs fsd;
180
181   if (statfs (path, &fsd, sizeof fsd, 0) < 0)
182     return -1;
183   /* Empirically, the block counts on most SVR3 and SVR3-derived
184      systems seem to always be in terms of 512-byte blocks,
185      no matter what value f_bsize has.  */
186 # if _AIX
187 #  define CONVERT_BLOCKS(b) adjust_blocks ((b), fsd.f_bsize, 512)
188 # else
189 #  define CONVERT_BLOCKS(b) (b)
190 #  ifndef _SEQUENT_             /* _SEQUENT_ is DYNIX/ptx.  */
191 #   ifndef DOLPHIN              /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
192 #    define f_bavail f_bfree
193 #   endif
194 #  endif
195 # endif
196 #endif
197
198 #ifdef STAT_STATVFS             /* SVR4.  */
199   struct statvfs fsd;
200
201   if (statvfs (path, &fsd) < 0)
202     return -1;
203   /* f_frsize isn't guaranteed to be supported.  */
204 #define CONVERT_BLOCKS(b) \
205   adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
206 #endif
207
208 #if !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS) /* !Ultrix && !SVR2.  */
209   fsp->fsu_blocks = CONVERT_BLOCKS (fsd.f_blocks);
210   fsp->fsu_bfree = CONVERT_BLOCKS (fsd.f_bfree);
211   fsp->fsu_bavail = CONVERT_BLOCKS (fsd.f_bavail);
212   fsp->fsu_files = fsd.f_files;
213   fsp->fsu_ffree = fsd.f_ffree;
214 #endif
215
216   return 0;
217 }
218
219 #if defined(_AIX) && defined(_I386)
220 /* AIX PS/2 does not supply statfs.  */
221
222 int
223 statfs (path, fsb)
224      char *path;
225      struct statfs *fsb;
226 {
227   struct stat stats;
228   struct dustat fsd;
229
230   if (stat (path, &stats))
231     return -1;
232   if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
233     return -1;
234   fsb->f_type   = 0;
235   fsb->f_bsize  = fsd.du_bsize;
236   fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
237   fsb->f_bfree  = fsd.du_tfree;
238   fsb->f_bavail = fsd.du_tfree;
239   fsb->f_files  = (fsd.du_isize - 2) * fsd.du_inopb;
240   fsb->f_ffree  = fsd.du_tinode;
241   fsb->f_fsid.val[0] = fsd.du_site;
242   fsb->f_fsid.val[1] = fsd.du_pckno;
243   return 0;
244 }
245 #endif /* _AIX && _I386 */