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