d595c9baa922f305a9ab70666abd978cc9ba58c3
[gnulib.git] / m4 / fsusage.m4
1 # serial 29
2 # Obtaining file system usage information.
3
4 # Copyright (C) 1997-1998, 2000-2001, 2003-2011 Free Software Foundation, Inc.
5 #
6 # This file is free software; the Free Software Foundation
7 # gives unlimited permission to copy and/or distribute it,
8 # with or without modifications, as long as this notice is preserved.
9
10 # Written by Jim Meyering.
11
12 AC_DEFUN([gl_FSUSAGE],
13 [
14   AC_CHECK_HEADERS_ONCE([sys/param.h])
15   AC_CHECK_HEADERS_ONCE([sys/vfs.h sys/fs_types.h])
16   AC_CHECK_HEADERS([sys/mount.h], [], [],
17     [AC_INCLUDES_DEFAULT
18      [#if HAVE_SYS_PARAM_H
19        #include <sys/param.h>
20       #endif]])
21   gl_FILE_SYSTEM_USAGE([gl_cv_fs_space=yes], [gl_cv_fs_space=no])
22 ])
23
24 # Try to determine how a program can obtain file system usage information.
25 # If successful, define the appropriate symbol (see fsusage.c) and
26 # execute ACTION-IF-FOUND.  Otherwise, execute ACTION-IF-NOT-FOUND.
27 #
28 # gl_FILE_SYSTEM_USAGE([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
29
30 AC_DEFUN([gl_FILE_SYSTEM_USAGE],
31 [
32 dnl Enable large-file support. This has the effect of changing the size
33 dnl of field f_blocks in 'struct statvfs' from 32 bit to 64 bit on
34 dnl glibc/Hurd, HP-UX 11, Solaris (32-bit mode). It also changes the size
35 dnl of field f_blocks in 'struct statfs' from 32 bit to 64 bit on
36 dnl MacOS X >= 10.5 (32-bit mode).
37 AC_REQUIRE([AC_SYS_LARGEFILE])
38
39 AC_MSG_NOTICE([checking how to get file system space usage])
40 ac_fsusage_space=no
41
42 # Perform only the link test since it seems there are no variants of the
43 # statvfs function.  This check is more than just AC_CHECK_FUNCS([statvfs])
44 # because that got a false positive on SCO OSR5.  Adding the declaration
45 # of a `struct statvfs' causes this test to fail (as it should) on such
46 # systems.  That system is reported to work fine with STAT_STATFS4 which
47 # is what it gets when this test fails.
48 if test $ac_fsusage_space = no; then
49   # glibc/{Hurd,kFreeBSD}, FreeBSD >= 5.0, NetBSD >= 3.0,
50   # OpenBSD >= 4.4, AIX, HP-UX, IRIX, Solaris, Cygwin, Interix, BeOS.
51   AC_CACHE_CHECK([for statvfs function (SVR4)], [fu_cv_sys_stat_statvfs],
52                  [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <sys/types.h>
53 #if (defined __GLIBC__ || defined __UCLIBC__) && defined __linux__
54 Do not use statvfs on systems with GNU libc on Linux, because that function
55 stats all preceding entries in /proc/mounts, and that makes df hang if even
56 one of the corresponding file systems is hard-mounted, but not available.
57 statvfs in GNU libc on Hurd, BeOS, Haiku operates differently: it only makes
58 a system call.
59 #endif
60
61 #ifdef __osf__
62 "Do not use Tru64's statvfs implementation"
63 #endif
64
65 #include <limits.h>
66 #include <sys/statvfs.h>
67
68 /* Reject implementations, such as MacOS X 10.7, where f_blocks is a
69    32-bit quantity; that commonly limits file systems to 4 TiB, a
70    ridiculously small limit these days.  */
71 struct statvfs fsd;
72 int check_f_blocks_size[sizeof fsd.f_blocks * CHAR_BIT <= 32 ? -1 : 1];
73 ]],
74                                     [[statvfs (0, &fsd);]])],
75                                  [fu_cv_sys_stat_statvfs=yes],
76                                  [fu_cv_sys_stat_statvfs=no])])
77   if test $fu_cv_sys_stat_statvfs = yes; then
78     ac_fsusage_space=yes
79     AC_DEFINE([STAT_STATVFS], [1],
80               [  Define if there is a function named statvfs.  (SVR4)])
81   fi
82 fi
83
84 if test $ac_fsusage_space = no; then
85   # DEC Alpha running OSF/1
86   AC_MSG_CHECKING([for 3-argument statfs function (DEC OSF/1)])
87   AC_CACHE_VAL([fu_cv_sys_stat_statfs3_osf1],
88   [AC_RUN_IFELSE([AC_LANG_SOURCE([[
89 #include <sys/param.h>
90 #include <sys/types.h>
91 #include <sys/mount.h>
92   int
93   main ()
94   {
95     struct statfs fsd;
96     fsd.f_fsize = 0;
97     return statfs (".", &fsd, sizeof (struct statfs)) != 0;
98   }]])],
99     [fu_cv_sys_stat_statfs3_osf1=yes],
100     [fu_cv_sys_stat_statfs3_osf1=no],
101     [fu_cv_sys_stat_statfs3_osf1=no])])
102   AC_MSG_RESULT([$fu_cv_sys_stat_statfs3_osf1])
103   if test $fu_cv_sys_stat_statfs3_osf1 = yes; then
104     ac_fsusage_space=yes
105     AC_DEFINE([STAT_STATFS3_OSF1], [1],
106               [   Define if  statfs takes 3 args.  (DEC Alpha running OSF/1)])
107   fi
108 fi
109
110 if test $ac_fsusage_space = no; then
111   # glibc/Linux, MacOS X, FreeBSD < 5.0, NetBSD < 3.0, OpenBSD < 4.4.
112   # (glibc/{Hurd,kFreeBSD}, FreeBSD >= 5.0, NetBSD >= 3.0,
113   # OpenBSD >= 4.4, AIX, HP-UX, OSF/1, Cygwin already handled above.)
114   # (On IRIX you need to include <sys/statfs.h>, not only <sys/mount.h> and
115   # <sys/vfs.h>.)
116   # (On Solaris, statfs has 4 arguments.)
117   AC_MSG_CHECKING([for two-argument statfs with statfs.f_bsize dnl
118 member (AIX, 4.3BSD)])
119   AC_CACHE_VAL([fu_cv_sys_stat_statfs2_bsize],
120   [AC_RUN_IFELSE([AC_LANG_SOURCE([[
121 #ifdef HAVE_SYS_PARAM_H
122 #include <sys/param.h>
123 #endif
124 #ifdef HAVE_SYS_MOUNT_H
125 #include <sys/mount.h>
126 #endif
127 #ifdef HAVE_SYS_VFS_H
128 #include <sys/vfs.h>
129 #endif
130   int
131   main ()
132   {
133   struct statfs fsd;
134   fsd.f_bsize = 0;
135   return statfs (".", &fsd) != 0;
136   }]])],
137     [fu_cv_sys_stat_statfs2_bsize=yes],
138     [fu_cv_sys_stat_statfs2_bsize=no],
139     [fu_cv_sys_stat_statfs2_bsize=no])])
140   AC_MSG_RESULT([$fu_cv_sys_stat_statfs2_bsize])
141   if test $fu_cv_sys_stat_statfs2_bsize = yes; then
142     ac_fsusage_space=yes
143     AC_DEFINE([STAT_STATFS2_BSIZE], [1],
144 [  Define if statfs takes 2 args and struct statfs has a field named f_bsize.
145    (4.3BSD, SunOS 4, HP-UX, AIX PS/2)])
146   fi
147 fi
148
149 if test $ac_fsusage_space = no; then
150   # SVR3
151   # (Solaris already handled above.)
152   AC_MSG_CHECKING([for four-argument statfs (AIX-3.2.5, SVR3)])
153   AC_CACHE_VAL([fu_cv_sys_stat_statfs4],
154   [AC_RUN_IFELSE([AC_LANG_SOURCE([[
155 #include <sys/types.h>
156 #include <sys/statfs.h>
157   int
158   main ()
159   {
160   struct statfs fsd;
161   return statfs (".", &fsd, sizeof fsd, 0) != 0;
162   }]])],
163     [fu_cv_sys_stat_statfs4=yes],
164     [fu_cv_sys_stat_statfs4=no],
165     [fu_cv_sys_stat_statfs4=no])])
166   AC_MSG_RESULT([$fu_cv_sys_stat_statfs4])
167   if test $fu_cv_sys_stat_statfs4 = yes; then
168     ac_fsusage_space=yes
169     AC_DEFINE([STAT_STATFS4], [1],
170       [  Define if statfs takes 4 args.  (SVR3, Dynix, old Irix, old AIX, Dolphin)])
171   fi
172 fi
173
174 if test $ac_fsusage_space = no; then
175   # 4.4BSD and older NetBSD
176   # (OSF/1 already handled above.)
177   # (On AIX, you need to include <sys/statfs.h>, not only <sys/mount.h>.)
178   # (On Solaris, statfs has 4 arguments and 'struct statfs' is not declared in
179   # <sys/mount.h>.)
180   AC_MSG_CHECKING([for two-argument statfs with statfs.f_fsize dnl
181 member (4.4BSD and NetBSD)])
182   AC_CACHE_VAL([fu_cv_sys_stat_statfs2_fsize],
183   [AC_RUN_IFELSE([AC_LANG_SOURCE([[
184 #include <sys/types.h>
185 #ifdef HAVE_SYS_PARAM_H
186 #include <sys/param.h>
187 #endif
188 #ifdef HAVE_SYS_MOUNT_H
189 #include <sys/mount.h>
190 #endif
191   int
192   main ()
193   {
194   struct statfs fsd;
195   fsd.f_fsize = 0;
196   return statfs (".", &fsd) != 0;
197   }]])],
198     [fu_cv_sys_stat_statfs2_fsize=yes],
199     [fu_cv_sys_stat_statfs2_fsize=no],
200     [fu_cv_sys_stat_statfs2_fsize=no])])
201   AC_MSG_RESULT([$fu_cv_sys_stat_statfs2_fsize])
202   if test $fu_cv_sys_stat_statfs2_fsize = yes; then
203     ac_fsusage_space=yes
204     AC_DEFINE([STAT_STATFS2_FSIZE], [1],
205 [  Define if statfs takes 2 args and struct statfs has a field named f_fsize.
206    (4.4BSD, NetBSD)])
207   fi
208 fi
209
210 if test $ac_fsusage_space = no; then
211   # Ultrix
212   AC_MSG_CHECKING([for two-argument statfs with struct fs_data (Ultrix)])
213   AC_CACHE_VAL([fu_cv_sys_stat_fs_data],
214   [AC_RUN_IFELSE([AC_LANG_SOURCE([[
215 #include <sys/types.h>
216 #ifdef HAVE_SYS_PARAM_H
217 #include <sys/param.h>
218 #endif
219 #ifdef HAVE_SYS_MOUNT_H
220 #include <sys/mount.h>
221 #endif
222 #ifdef HAVE_SYS_FS_TYPES_H
223 #include <sys/fs_types.h>
224 #endif
225   int
226   main ()
227   {
228   struct fs_data fsd;
229   /* Ultrix's statfs returns 1 for success,
230      0 for not mounted, -1 for failure.  */
231   return statfs (".", &fsd) != 1;
232   }]])],
233     [fu_cv_sys_stat_fs_data=yes],
234     [fu_cv_sys_stat_fs_data=no],
235     [fu_cv_sys_stat_fs_data=no])])
236   AC_MSG_RESULT([$fu_cv_sys_stat_fs_data])
237   if test $fu_cv_sys_stat_fs_data = yes; then
238     ac_fsusage_space=yes
239     AC_DEFINE([STAT_STATFS2_FS_DATA], [1],
240 [  Define if statfs takes 2 args and the second argument has
241    type struct fs_data.  (Ultrix)])
242   fi
243 fi
244
245 if test $ac_fsusage_space = no; then
246   # SVR2
247   # (AIX, HP-UX, OSF/1 already handled above.)
248   AC_PREPROC_IFELSE([AC_LANG_SOURCE([[#include <sys/filsys.h>
249         ]])],
250     [AC_DEFINE([STAT_READ_FILSYS], [1],
251       [Define if there is no specific function for reading file systems usage
252        information and you have the <sys/filsys.h> header file.  (SVR2)])
253      ac_fsusage_space=yes])
254 fi
255
256 AS_IF([test $ac_fsusage_space = yes], [$1], [$2])
257
258 ])
259
260
261 # Check for SunOS statfs brokenness wrt partitions 2GB and larger.
262 # If <sys/vfs.h> exists and struct statfs has a member named f_spare,
263 # enable the work-around code in fsusage.c.
264 AC_DEFUN([gl_STATFS_TRUNCATES],
265 [
266   AC_MSG_CHECKING([for statfs that truncates block counts])
267   AC_CACHE_VAL([fu_cv_sys_truncating_statfs],
268   [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
269 #if !defined(sun) && !defined(__sun)
270 choke -- this is a workaround for a Sun-specific problem
271 #endif
272 #include <sys/types.h>
273 #include <sys/vfs.h>]],
274       [[struct statfs t; long c = *(t.f_spare);
275         if (c) return 0;]])],
276     [fu_cv_sys_truncating_statfs=yes],
277     [fu_cv_sys_truncating_statfs=no])])
278   if test $fu_cv_sys_truncating_statfs = yes; then
279     AC_DEFINE([STATFS_TRUNCATES_BLOCK_COUNTS], [1],
280       [Define if the block counts reported by statfs may be truncated to 2GB
281        and the correct values may be stored in the f_spare array.
282        (SunOS 4.1.2, 4.1.3, and 4.1.3_U1 are reported to have this problem.
283        SunOS 4.1.1 seems not to be affected.)])
284   fi
285   AC_MSG_RESULT([$fu_cv_sys_truncating_statfs])
286 ])
287
288
289 # Prerequisites of lib/fsusage.c not done by gl_FILE_SYSTEM_USAGE.
290 AC_DEFUN([gl_PREREQ_FSUSAGE_EXTRA],
291 [
292   AC_CHECK_HEADERS([dustat.h sys/fs/s5param.h sys/filsys.h sys/statfs.h])
293   gl_STATFS_TRUNCATES
294 ])