mountlist: Use Linux code on Android.
[gnulib.git] / m4 / ls-mntd-fs.m4
blob05474c2ddda3c5fb27acec02b1fbaabcd76aa925
1 # serial 38
2 # How to list mounted file systems.
4 # Copyright (C) 1998-2004, 2006, 2009-2019 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 dnl From Jim Meyering.
12 AC_PREREQ([2.60])
14 dnl This is not pretty.  I've just taken the autoconf code and wrapped
15 dnl it in an AC_DEFUN and made some other fixes.
17 # Replace Autoconf's AC_FUNC_GETMNTENT to omit checks that are unnecessary
18 # nowadays.
19 AC_DEFUN([AC_FUNC_GETMNTENT],
21   # getmntent is in the standard C library on UNICOS, in -lsun on Irix 4,
22   # -lgen on Unixware.
23   AC_SEARCH_LIBS([getmntent], [sun gen])
24   AC_CHECK_FUNCS([getmntent])
27 # gl_LIST_MOUNTED_FILE_SYSTEMS([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
28 AC_DEFUN([gl_LIST_MOUNTED_FILE_SYSTEMS],
30   AC_REQUIRE([AC_CANONICAL_HOST])
31   AC_CHECK_FUNCS([listmntent])
32   AC_CHECK_HEADERS_ONCE([sys/param.h sys/statvfs.h])
34   # We must include grp.h before ucred.h on OSF V4.0, since ucred.h uses
35   # NGROUPS (as the array dimension for a struct member) without a definition.
36   AC_CHECK_HEADERS([sys/ucred.h], [], [], [#include <grp.h>])
38   AC_CHECK_HEADERS([sys/mount.h], [], [],
39     [AC_INCLUDES_DEFAULT
40      [#if HAVE_SYS_PARAM_H
41        #include <sys/param.h>
42       #endif
43     ]])
45   AC_CHECK_HEADERS([mntent.h sys/fs_types.h])
46   getfsstat_includes="\
47 $ac_includes_default
48 #if HAVE_SYS_PARAM_H
49 # include <sys/param.h> /* needed by powerpc-apple-darwin1.3.7 */
50 #endif
51 #if HAVE_SYS_UCRED_H
52 # include <grp.h> /* needed for definition of NGROUPS */
53 # include <sys/ucred.h> /* needed by powerpc-apple-darwin1.3.7 */
54 #endif
55 #if HAVE_SYS_MOUNT_H
56 # include <sys/mount.h>
57 #endif
58 #if HAVE_SYS_FS_TYPES_H
59 # include <sys/fs_types.h> /* needed by powerpc-apple-darwin1.3.7 */
60 #endif
62   AC_CHECK_MEMBERS([struct fsstat.f_fstypename],,,[$getfsstat_includes])
64   # Determine how to get the list of mounted file systems.
65   ac_list_mounted_fs=
67   # If the getmntent function is available but not in the standard library,
68   # make sure LIBS contains the appropriate -l option.
69   AC_FUNC_GETMNTENT
71   if test -z "$ac_list_mounted_fs"; then
72     # AIX.
73     AC_CACHE_CHECK([for mntctl function and struct vmount],
74       [fu_cv_sys_mounted_vmount],
75       [AC_PREPROC_IFELSE([AC_LANG_SOURCE([[#include <fshelp.h>]])],
76          [fu_cv_sys_mounted_vmount=yes],
77          [fu_cv_sys_mounted_vmount=no])])
78     if test $fu_cv_sys_mounted_vmount = yes; then
79       ac_list_mounted_fs=found
80       AC_DEFINE([MOUNTED_VMOUNT], [1],
81         [Define if there is a function named mntctl that can be used to read
82          the list of mounted file systems, and there is a system header file
83          that declares 'struct vmount'.  (AIX)])
84     fi
85   fi
87   if test $ac_cv_func_getmntent = yes; then
89     # This system has the getmntent function.
90     # Determine whether it's the one-argument variant or the two-argument one.
92     if test -z "$ac_list_mounted_fs"; then
93       # glibc, HP-UX, IRIX, Cygwin, Android, also (obsolete) 4.3BSD, SunOS.
94       AC_CACHE_CHECK([for one-argument getmntent function],
95         [fu_cv_sys_mounted_getmntent1],
96         [AC_COMPILE_IFELSE(
97            [AC_LANG_PROGRAM([[
98 /* SunOS 4.1.x /usr/include/mntent.h needs this for FILE */
99 #include <stdio.h>
101 #include <mntent.h>
102 #if defined __ANDROID__        /* Android */
103 # undef MOUNTED
104 # define MOUNTED "/proc/mounts"
105 #elif !defined MOUNTED
106 # if defined _PATH_MOUNTED     /* GNU libc  */
107 #  define MOUNTED _PATH_MOUNTED
108 # endif
109 # if defined MNT_MNTTAB        /* HP-UX.  */
110 #  define MOUNTED MNT_MNTTAB
111 # endif
112 #endif
114               [[struct mntent *mnt = 0; char *table = MOUNTED;
115                 if (sizeof mnt && sizeof table) return 0;
116               ]])],
117            [fu_cv_sys_mounted_getmntent1=yes],
118            [fu_cv_sys_mounted_getmntent1=no])
119         ])
120       if test $fu_cv_sys_mounted_getmntent1 = yes; then
121         ac_list_mounted_fs=found
122         AC_DEFINE([MOUNTED_GETMNTENT1], [1],
123           [Define if there is a function named getmntent for reading the list
124            of mounted file systems, and that function takes a single argument.
125            (4.3BSD, SunOS, HP-UX, Irix)])
126         AC_CHECK_FUNCS([setmntent endmntent hasmntopt])
127       fi
128     fi
130     if test -z "$ac_list_mounted_fs"; then
131       # Solaris >= 8.
132       AC_CACHE_CHECK([for getextmntent function],
133         [fu_cv_sys_mounted_getextmntent],
134         [AC_EGREP_HEADER([getextmntent], [sys/mnttab.h],
135            [fu_cv_sys_mounted_getextmntent=yes],
136            [fu_cv_sys_mounted_getextmntent=no])])
137       if test $fu_cv_sys_mounted_getextmntent = yes; then
138         ac_list_mounted_fs=found
139         AC_DEFINE([MOUNTED_GETEXTMNTENT], [1],
140           [Define if there is a function named getextmntent for reading the list
141            of mounted file systems.  (Solaris)])
142       fi
143     fi
145     if test -z "$ac_list_mounted_fs"; then
146       # Solaris < 8, also (obsolete) SVR4.
147       # Solaris >= 8 has the two-argument getmntent but is already handled above.
148       AC_CACHE_CHECK([for two-argument getmntent function],
149         [fu_cv_sys_mounted_getmntent2],
150         [AC_EGREP_HEADER([getmntent], [sys/mnttab.h],
151            [fu_cv_sys_mounted_getmntent2=yes],
152            [fu_cv_sys_mounted_getmntent2=no])
153         ])
154       if test $fu_cv_sys_mounted_getmntent2 = yes; then
155         ac_list_mounted_fs=found
156         AC_DEFINE([MOUNTED_GETMNTENT2], [1],
157           [Define if there is a function named getmntent for reading the list of
158            mounted file systems, and that function takes two arguments.  (SVR4)])
159         AC_CHECK_FUNCS([hasmntopt])
160       fi
161     fi
163   fi
165   if test -z "$ac_list_mounted_fs"; then
166     # OSF/1, also (obsolete) Apple Darwin 1.3.
167     # powerpc-apple-darwin1.3.7 needs sys/param.h sys/ucred.h sys/fs_types.h
169     AC_CACHE_CHECK([for getfsstat function],
170       [fu_cv_sys_mounted_getfsstat],
171       [AC_LINK_IFELSE(
172          [AC_LANG_PROGRAM([[
173 #include <sys/types.h>
174 #if HAVE_STRUCT_FSSTAT_F_FSTYPENAME
175 # define FS_TYPE(Ent) ((Ent).f_fstypename)
176 #else
177 # define FS_TYPE(Ent) mnt_names[(Ent).f_type]
178 #endif
179 $getfsstat_includes
180             ]],
181             [[struct statfs *stats;
182               int numsys = getfsstat ((struct statfs *)0, 0L, MNT_WAIT);
183               char *t = FS_TYPE (*stats);
184             ]])],
185          [fu_cv_sys_mounted_getfsstat=yes],
186          [fu_cv_sys_mounted_getfsstat=no])
187       ])
188     if test $fu_cv_sys_mounted_getfsstat = yes; then
189       ac_list_mounted_fs=found
190       AC_DEFINE([MOUNTED_GETFSSTAT], [1],
191         [Define if there is a function named getfsstat for reading the
192          list of mounted file systems.  (DEC Alpha running OSF/1)])
193     fi
194   fi
196   if test -z "$ac_list_mounted_fs"; then
197     # (obsolete) SVR3
198     AC_CACHE_CHECK([for FIXME existence of three headers],
199       [fu_cv_sys_mounted_fread_fstyp],
200       [AC_PREPROC_IFELSE([AC_LANG_SOURCE([[
201 #include <sys/statfs.h>
202 #include <sys/fstyp.h>
203 #include <mnttab.h>
204 ]])],
205          [fu_cv_sys_mounted_fread_fstyp=yes],
206          [fu_cv_sys_mounted_fread_fstyp=no])
207       ])
208     if test $fu_cv_sys_mounted_fread_fstyp = yes; then
209       ac_list_mounted_fs=found
210       AC_DEFINE([MOUNTED_FREAD_FSTYP], [1],
211         [Define if (like SVR2) there is no specific function for reading the
212          list of mounted file systems, and your system has these header files:
213          <sys/fstyp.h> and <sys/statfs.h>.  (SVR3)])
214     fi
215   fi
217   if test -z "$ac_list_mounted_fs"; then
218     # Mac OS X, FreeBSD, NetBSD, OpenBSD, Minix, also (obsolete) 4.4BSD.
219     # OSF/1 also has getmntinfo but is already handled above.
220     # We cannot use AC_CHECK_FUNCS([getmntinfo]) here, because at the linker
221     # level the function is sometimes called getmntinfo64 or getmntinfo$INODE64
222     # on Mac OS X, __getmntinfo13 on NetBSD and Minix, _F64_getmntinfo on OSF/1.
223     AC_CACHE_CHECK([for getmntinfo function],
224       [fu_cv_sys_mounted_getmntinfo],
225       [AC_LINK_IFELSE(
226          [AC_LANG_PROGRAM([[
227 #if HAVE_SYS_PARAM_H
228 # include <sys/param.h>
229 #endif
230 #include <sys/types.h>
231 #if HAVE_SYS_MOUNT_H
232 # include <sys/mount.h>
233 #endif
234 #if HAVE_SYS_STATVFS_H
235 # include <sys/statvfs.h>
236 #endif
237 #include <stdlib.h>
238             ]],
239             [[int count = getmntinfo (NULL, MNT_WAIT);
240             ]])],
241          [fu_cv_sys_mounted_getmntinfo=yes],
242          [fu_cv_sys_mounted_getmntinfo=no])
243       ])
244     if test $fu_cv_sys_mounted_getmntinfo = yes; then
245       AC_CACHE_CHECK([whether getmntinfo returns statvfs structures],
246         [fu_cv_sys_mounted_getmntinfo2],
247         [AC_COMPILE_IFELSE(
248            [AC_LANG_PROGRAM([[
249 #if HAVE_SYS_PARAM_H
250 # include <sys/param.h>
251 #endif
252 #include <sys/types.h>
253 #if HAVE_SYS_MOUNT_H
254 # include <sys/mount.h>
255 #endif
256 #if HAVE_SYS_STATVFS_H
257 # include <sys/statvfs.h>
258 #endif
259 extern
260 #ifdef __cplusplus
262 #endif
263 int getmntinfo (struct statfs **, int);
264               ]], [])],
265            [fu_cv_sys_mounted_getmntinfo2=no],
266            [fu_cv_sys_mounted_getmntinfo2=yes])
267         ])
268       if test $fu_cv_sys_mounted_getmntinfo2 = no; then
269         # Mac OS X, FreeBSD, OpenBSD, also (obsolete) 4.4BSD.
270         ac_list_mounted_fs=found
271         AC_DEFINE([MOUNTED_GETMNTINFO], [1],
272           [Define if there is a function named getmntinfo for reading the
273            list of mounted file systems and it returns an array of
274            'struct statfs'.  (4.4BSD, Darwin)])
275       else
276         # NetBSD, Minix.
277         ac_list_mounted_fs=found
278         AC_DEFINE([MOUNTED_GETMNTINFO2], [1],
279           [Define if there is a function named getmntinfo for reading the
280            list of mounted file systems and it returns an array of
281            'struct statvfs'.  (NetBSD 3.0)])
282       fi
283     fi
284   fi
286   if test -z "$ac_list_mounted_fs"; then
287     # Haiku, also (obsolete) BeOS.
288     AC_CHECK_FUNCS([next_dev fs_stat_dev])
289     AC_CHECK_HEADERS([fs_info.h])
290     AC_CACHE_CHECK([for BEOS mounted file system support functions],
291       [fu_cv_sys_mounted_fs_stat_dev],
292       [if test $ac_cv_header_fs_info_h = yes \
293           && test $ac_cv_func_next_dev = yes \
294           && test $ac_cv_func_fs_stat_dev = yes; then
295          fu_cv_sys_mounted_fs_stat_dev=yes
296        else
297          fu_cv_sys_mounted_fs_stat_dev=no
298        fi
299       ])
300     if test $fu_cv_sys_mounted_fs_stat_dev = yes; then
301       ac_list_mounted_fs=found
302       AC_DEFINE([MOUNTED_FS_STAT_DEV], [1],
303         [Define if there are functions named next_dev and fs_stat_dev for
304          reading the list of mounted file systems.  (BeOS)])
305     fi
306   fi
308   if test -z "$ac_list_mounted_fs"; then
309     # Interix / BSD alike statvfs
310     # the code is really interix specific, so make sure, we're on it.
311     case "$host" in
312       *-interix*)
313         AC_CHECK_FUNCS([statvfs])
314         if test $ac_cv_func_statvfs = yes; then
315           ac_list_mounted_fs=found
316           AC_DEFINE([MOUNTED_INTERIX_STATVFS], [1],
317             [Define if we are on interix, and ought to use statvfs plus
318              some special knowledge on where mounted file systems can be
319              found. (Interix)])
320         fi
321         ;;
322     esac
323   fi
325   if test -z "$ac_list_mounted_fs"; then
326     AC_MSG_ERROR([could not determine how to read list of mounted file systems])
327     # FIXME -- no need to abort building the whole package
328     # Can't build mountlist.c or anything that needs its functions
329   fi
331   AS_IF([test $ac_list_mounted_fs = found], [$1], [$2])