Sync with gnulib 23671eec22f5221a2dff50942f24c41bcf9caeaf.
[midnight-commander.git] / m4.include / gnulib / fsusage.m4
blobecf1cc57e7f38633c0fb624efd4d5a5895cc6fbd
1 # serial 34
2 # Obtaining file system usage information.
4 # Copyright (C) 1997-1998, 2000-2001, 2003-2017 Free Software Foundation, Inc.
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.
10 # Written by Jim Meyering.
12 AC_DEFUN([gl_FSUSAGE],
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])
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.
28 # gl_FILE_SYSTEM_USAGE([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
30 AC_DEFUN([gl_FILE_SYSTEM_USAGE],
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 Mac OS X >= 10.5 (32-bit mode).
37   AC_REQUIRE([AC_SYS_LARGEFILE])
39   AC_MSG_CHECKING([how to get file system space usage])
40   ac_fsusage_space=no
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)],
52       [fu_cv_sys_stat_statvfs],
53       [AC_LINK_IFELSE(
54          [AC_LANG_PROGRAM([[
55 #include <sys/types.h>
56 #ifdef __osf__
57 "Do not use Tru64's statvfs implementation"
58 #endif
60 #include <sys/statvfs.h>
62 struct statvfs fsd;
64 #if defined __APPLE__ && defined __MACH__
65 #include <limits.h>
66 /* On Mac OS X >= 10.5, f_blocks in 'struct statvfs' is a 32-bit quantity;
67    that commonly limits file systems to 4 TiB.  Whereas f_blocks in
68    'struct statfs' is a 64-bit type, thanks to the large-file support
69    that was enabled above.  In this case, don't use statvfs(); use statfs()
70    instead.  */
71 int check_f_blocks_size[sizeof fsd.f_blocks * CHAR_BIT <= 32 ? -1 : 1];
72 #endif
73 ]],
74             [[statvfs (0, &fsd);]])],
75          [fu_cv_sys_stat_statvfs=yes],
76          [fu_cv_sys_stat_statvfs=no])
77       ])
78     if test $fu_cv_sys_stat_statvfs = yes; then
79       ac_fsusage_space=yes
80       # AIX >= 5.2 has statvfs64 that has a wider f_blocks field than statvfs.
81       # glibc, HP-UX, IRIX, Solaris have statvfs64 as well, but on these systems
82       # statvfs with large-file support is already equivalent to statvfs64.
83       AC_CACHE_CHECK([whether to use statvfs64],
84         [fu_cv_sys_stat_statvfs64],
85         [AC_LINK_IFELSE(
86            [AC_LANG_PROGRAM(
87               [[#include <sys/types.h>
88                 #include <sys/statvfs.h>
89                 struct statvfs64 fsd;
90                 int check_f_blocks_larger_in_statvfs64
91                   [sizeof (((struct statvfs64 *) 0)->f_blocks)
92                    > sizeof (((struct statvfs *) 0)->f_blocks)
93                    ? 1 : -1];
94               ]],
95               [[statvfs64 (0, &fsd);]])],
96            [fu_cv_sys_stat_statvfs64=yes],
97            [fu_cv_sys_stat_statvfs64=no])
98         ])
99       if test $fu_cv_sys_stat_statvfs64 = yes; then
100         AC_DEFINE([STAT_STATVFS64], [1],
101           [Define if statvfs64 should be preferred over statvfs.])
102       else
103         AC_DEFINE([STAT_STATVFS], [1],
104           [Define if there is a function named statvfs.  (SVR4)])
105       fi
106     fi
107   fi
109   # Check for this unconditionally so we have a
110   # good fallback on glibc/Linux > 2.6 < 2.6.36
111   AC_CACHE_CHECK([for two-argument statfs with statfs.f_frsize member],
112     [fu_cv_sys_stat_statfs2_frsize],
113     [AC_RUN_IFELSE(
114        [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_frsize = 0;
129     return statfs (".", &fsd) != 0;
130   }]])],
131        [fu_cv_sys_stat_statfs2_frsize=yes],
132        [fu_cv_sys_stat_statfs2_frsize=no],
133        [fu_cv_sys_stat_statfs2_frsize=no])
134     ])
135   if test $fu_cv_sys_stat_statfs2_frsize = yes; then
136     ac_fsusage_space=yes
137     AC_DEFINE([STAT_STATFS2_FRSIZE], [1],
138       [Define if statfs takes 2 args and struct statfs has a field named f_frsize.
139        (glibc/Linux > 2.6)])
140   fi
142   if test $ac_fsusage_space = no; then
143     # DEC Alpha running OSF/1
144     AC_CACHE_CHECK([for 3-argument statfs function (DEC OSF/1)],
145       [fu_cv_sys_stat_statfs3_osf1],
146       [AC_RUN_IFELSE([AC_LANG_SOURCE([[
147 #include <sys/param.h>
148 #include <sys/types.h>
149 #include <sys/mount.h>
150   int
151   main ()
152   {
153     struct statfs fsd;
154     fsd.f_fsize = 0;
155     return statfs (".", &fsd, sizeof (struct statfs)) != 0;
156   }]])],
157          [fu_cv_sys_stat_statfs3_osf1=yes],
158          [fu_cv_sys_stat_statfs3_osf1=no],
159          [fu_cv_sys_stat_statfs3_osf1=no])
160       ])
161     if test $fu_cv_sys_stat_statfs3_osf1 = yes; then
162       ac_fsusage_space=yes
163       AC_DEFINE([STAT_STATFS3_OSF1], [1],
164         [Define if statfs takes 3 args.  (DEC Alpha running OSF/1)])
165     fi
166   fi
168   if test $ac_fsusage_space = no; then
169     # glibc/Linux, Mac OS X, FreeBSD < 5.0, NetBSD < 3.0, OpenBSD < 4.4.
170     # (glibc/{Hurd,kFreeBSD}, FreeBSD >= 5.0, NetBSD >= 3.0,
171     # OpenBSD >= 4.4, AIX, HP-UX, OSF/1, Cygwin already handled above.)
172     # (On IRIX you need to include <sys/statfs.h>, not only <sys/mount.h> and
173     # <sys/vfs.h>.)
174     # (On Solaris, statfs has 4 arguments.)
175     AC_CACHE_CHECK([for two-argument statfs with statfs.f_bsize member (AIX, 4.3BSD)],
176       [fu_cv_sys_stat_statfs2_bsize],
177       [AC_RUN_IFELSE([AC_LANG_SOURCE([[
178 #ifdef HAVE_SYS_PARAM_H
179 #include <sys/param.h>
180 #endif
181 #ifdef HAVE_SYS_MOUNT_H
182 #include <sys/mount.h>
183 #endif
184 #ifdef HAVE_SYS_VFS_H
185 #include <sys/vfs.h>
186 #endif
187   int
188   main ()
189   {
190     struct statfs fsd;
191     fsd.f_bsize = 0;
192     return statfs (".", &fsd) != 0;
193   }]])],
194          [fu_cv_sys_stat_statfs2_bsize=yes],
195          [fu_cv_sys_stat_statfs2_bsize=no],
196          [fu_cv_sys_stat_statfs2_bsize=no])
197       ])
198     if test $fu_cv_sys_stat_statfs2_bsize = yes; then
199       ac_fsusage_space=yes
200       AC_DEFINE([STAT_STATFS2_BSIZE], [1],
201         [Define if statfs takes 2 args and struct statfs has a field named f_bsize.
202          (4.3BSD, SunOS 4, HP-UX)])
203     fi
204   fi
206   if test $ac_fsusage_space = no; then
207     # SVR3
208     # (Solaris already handled above.)
209     AC_CACHE_CHECK([for four-argument statfs (SVR3)],
210       [fu_cv_sys_stat_statfs4],
211       [AC_RUN_IFELSE([AC_LANG_SOURCE([[
212 #include <sys/types.h>
213 #include <sys/statfs.h>
214   int
215   main ()
216   {
217     struct statfs fsd;
218     return statfs (".", &fsd, sizeof fsd, 0) != 0;
219   }]])],
220          [fu_cv_sys_stat_statfs4=yes],
221          [fu_cv_sys_stat_statfs4=no],
222          [fu_cv_sys_stat_statfs4=no])
223       ])
224     if test $fu_cv_sys_stat_statfs4 = yes; then
225       ac_fsusage_space=yes
226       AC_DEFINE([STAT_STATFS4], [1],
227         [Define if statfs takes 4 args.  (SVR3, old Irix)])
228     fi
229   fi
231   if test $ac_fsusage_space = no; then
232     # 4.4BSD and older NetBSD
233     # (OSF/1 already handled above.)
234     # (On AIX, you need to include <sys/statfs.h>, not only <sys/mount.h>.)
235     # (On Solaris, statfs has 4 arguments and 'struct statfs' is not declared in
236     # <sys/mount.h>.)
237     AC_CACHE_CHECK([for two-argument statfs with statfs.f_fsize member (4.4BSD and NetBSD)],
238       [fu_cv_sys_stat_statfs2_fsize],
239       [AC_RUN_IFELSE([AC_LANG_SOURCE([[
240 #include <sys/types.h>
241 #ifdef HAVE_SYS_PARAM_H
242 #include <sys/param.h>
243 #endif
244 #ifdef HAVE_SYS_MOUNT_H
245 #include <sys/mount.h>
246 #endif
247   int
248   main ()
249   {
250     struct statfs fsd;
251     fsd.f_fsize = 0;
252     return statfs (".", &fsd) != 0;
253   }]])],
254          [fu_cv_sys_stat_statfs2_fsize=yes],
255          [fu_cv_sys_stat_statfs2_fsize=no],
256          [fu_cv_sys_stat_statfs2_fsize=no])
257       ])
258     if test $fu_cv_sys_stat_statfs2_fsize = yes; then
259       ac_fsusage_space=yes
260       AC_DEFINE([STAT_STATFS2_FSIZE], [1],
261         [Define if statfs takes 2 args and struct statfs has a field named f_fsize.
262          (4.4BSD, NetBSD)])
263     fi
264   fi
266   if test $ac_fsusage_space = no; then
267     # Ultrix
268     AC_CACHE_CHECK([for two-argument statfs with struct fs_data (Ultrix)],
269       [fu_cv_sys_stat_fs_data],
270       [AC_RUN_IFELSE([AC_LANG_SOURCE([[
271 #include <sys/types.h>
272 #ifdef HAVE_SYS_PARAM_H
273 #include <sys/param.h>
274 #endif
275 #ifdef HAVE_SYS_MOUNT_H
276 #include <sys/mount.h>
277 #endif
278 #ifdef HAVE_SYS_FS_TYPES_H
279 #include <sys/fs_types.h>
280 #endif
281   int
282   main ()
283   {
284     struct fs_data fsd;
285     /* Ultrix's statfs returns 1 for success,
286        0 for not mounted, -1 for failure.  */
287     return statfs (".", &fsd) != 1;
288   }]])],
289          [fu_cv_sys_stat_fs_data=yes],
290          [fu_cv_sys_stat_fs_data=no],
291          [fu_cv_sys_stat_fs_data=no])
292       ])
293     if test $fu_cv_sys_stat_fs_data = yes; then
294       ac_fsusage_space=yes
295       AC_DEFINE([STAT_STATFS2_FS_DATA], [1],
296         [Define if statfs takes 2 args and the second argument has
297          type struct fs_data.  (Ultrix)])
298     fi
299   fi
301   AS_IF([test $ac_fsusage_space = yes], [$1], [$2])
306 # Check for SunOS statfs brokenness wrt partitions 2GB and larger.
307 # If <sys/vfs.h> exists and struct statfs has a member named f_spare,
308 # enable the work-around code in fsusage.c.
309 AC_DEFUN([gl_STATFS_TRUNCATES],
311   AC_CACHE_CHECK([for statfs that truncates block counts],
312     [fu_cv_sys_truncating_statfs],
313     [AC_COMPILE_IFELSE(
314        [AC_LANG_PROGRAM([[
315 #if !defined(sun) && !defined(__sun)
316 choke -- this is a workaround for a Sun-specific problem
317 #endif
318 #include <sys/types.h>
319 #include <sys/vfs.h>
320          ]],
321          [[struct statfs t; long c = *(t.f_spare);
322            if (c) return 0;
323          ]])],
324        [fu_cv_sys_truncating_statfs=yes],
325        [fu_cv_sys_truncating_statfs=no])
326     ])
327   if test $fu_cv_sys_truncating_statfs = yes; then
328     AC_DEFINE([STATFS_TRUNCATES_BLOCK_COUNTS], [1],
329       [Define if the block counts reported by statfs may be truncated to 2GB
330        and the correct values may be stored in the f_spare array.
331        (SunOS 4.1.2, 4.1.3, and 4.1.3_U1 are reported to have this problem.
332        SunOS 4.1.1 seems not to be affected.)])
333   fi
337 # Prerequisites of lib/fsusage.c not done by gl_FILE_SYSTEM_USAGE.
338 AC_DEFUN([gl_PREREQ_FSUSAGE_EXTRA],
340   AC_CHECK_HEADERS([sys/fs/s5param.h sys/statfs.h])
341   gl_STATFS_TRUNCATES