1 /* mountlist.c -- return a list of mounted filesystems
2 Copyright (C) 1991, 1992 Free Software Foundation, Inc.
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)
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.
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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #if defined (CONFIG_BROKETS)
20 /* We use <config.h> instead of "config.h" so that a compilation
21 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
22 (which it would do because it found this file in $srcdir). */
30 #include <sys/types.h>
31 #include "mountlist.h"
38 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
50 #if defined (MOUNTED_GETFSSTAT) /* __alpha running OSF_1 */
51 # include <sys/mount.h>
52 # include <sys/fs_types.h>
53 #endif /* MOUNTED_GETFSSTAT */
55 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
58 # if defined(MNT_MNTTAB) /* HP-UX. */
59 # define MOUNTED MNT_MNTTAB
61 # if defined(MNTTABNAME) /* Dynix. */
62 # define MOUNTED MNTTABNAME
67 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
68 #include <sys/mount.h>
71 #ifdef MOUNTED_GETMNT /* Ultrix. */
72 #include <sys/param.h>
73 #include <sys/mount.h>
74 #include <sys/fs_types.h>
77 #ifdef MOUNTED_FREAD /* SVR2. */
81 #ifdef MOUNTED_FREAD_FSTYP /* SVR3. */
83 #include <sys/fstyp.h>
84 #include <sys/statfs.h>
87 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
88 #include <sys/mnttab.h>
91 #ifdef MOUNTED_VMOUNT /* AIX. */
97 /* So special that it's not worth putting this in autoconf. */
98 #undef MOUNTED_FREAD_FSTYP
99 #define MOUNTED_GETMNTTBL
102 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
103 /* Return the value of the hexadecimal number represented by CP.
104 No prefix (like '0x') or suffix (like 'h') is expected to be
116 if (*cp >= 'a' && *cp <= 'f')
117 val = val * 16 + *cp - 'a' + 10;
118 else if (*cp >= 'A' && *cp <= 'F')
119 val = val * 16 + *cp - 'A' + 10;
120 else if (*cp >= '0' && *cp <= '9')
121 val = val * 16 + *cp - '0';
128 #endif /* MOUNTED_GETMNTENT1. */
130 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
165 #endif /* MOUNTED_GETMNTINFO */
167 #ifdef MOUNTED_VMOUNT /* AIX. */
174 e = getvfsbytype (t);
175 if (!e || !e->vfsent_name)
178 return e->vfsent_name;
180 #endif /* MOUNTED_VMOUNT */
182 /* Return a list of the currently mounted filesystems, or NULL on error.
183 Add each entry to the tail of the list so that they stay in order.
184 If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in
185 the returned list are valid. Otherwise, they might not be.
186 If ALL_FS is zero, do not return entries for filesystems that
187 are automounter (dummy) entries. */
190 read_filesystem_list (need_fs_type, all_fs)
191 int need_fs_type, all_fs;
193 struct mount_entry *mount_list;
194 struct mount_entry *me;
195 struct mount_entry *mtail;
197 /* Start the list off with a dummy entry. */
198 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
200 mount_list = mtail = me;
202 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
205 char *table = MOUNTED;
209 fp = setmntent (table, "r");
213 while ((mnt = getmntent (fp)))
215 if (!all_fs && (!strcmp (mnt->mnt_type, "ignore")
216 || !strcmp (mnt->mnt_type, "auto")))
219 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
220 me->me_devname = xstrdup (mnt->mnt_fsname);
221 me->me_mountdir = xstrdup (mnt->mnt_dir);
222 me->me_type = xstrdup (mnt->mnt_type);
223 devopt = strstr (mnt->mnt_opts, "dev=");
226 if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X'))
227 me->me_dev = xatoi (devopt + 6);
229 me->me_dev = xatoi (devopt + 4);
232 me->me_dev = -1; /* Magic; means not known yet. */
235 /* Add to the linked list. */
240 if (endmntent (fp) == 0)
243 #endif /* MOUNTED_GETMNTENT1. */
245 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
250 entries = getmntinfo (&fsp, MNT_NOWAIT);
253 while (entries-- > 0)
255 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
256 me->me_devname = xstrdup (fsp->f_mntfromname);
257 me->me_mountdir = xstrdup (fsp->f_mntonname);
258 me->me_type = fstype_to_string (fsp->f_type);
259 me->me_dev = -1; /* Magic; means not known yet. */
262 /* Add to the linked list. */
268 #endif /* MOUNTED_GETMNTINFO */
270 #ifdef MOUNTED_GETMNT /* Ultrix. */
276 while ((val = getmnt (&offset, &fsd, sizeof (fsd), NOSTAT_MANY,
279 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
280 me->me_devname = xstrdup (fsd.fd_req.devname);
281 me->me_mountdir = xstrdup (fsd.fd_req.path);
282 me->me_type = gt_names[fsd.fd_req.fstype];
283 me->me_dev = fsd.fd_req.dev;
286 /* Add to the linked list. */
293 #endif /* MOUNTED_GETMNT. */
295 #if defined (MOUNTED_GETFSSTAT) /* __alpha running OSF_1 */
297 int numsys, counter, bufsize;
298 struct statfs *stats;
300 numsys = getfsstat ((struct statfs *)0, 0L, MNT_WAIT);
304 bufsize = (1 + numsys) * sizeof (struct statfs);
305 stats = (struct statfs *)xmalloc (bufsize);
306 numsys = getfsstat (stats, bufsize, MNT_WAIT);
314 for (counter = 0; counter < numsys; counter++)
316 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
317 me->me_devname = xstrdup (stats[counter].f_mntfromname);
318 me->me_mountdir = xstrdup (stats[counter].f_mntonname);
319 me->me_type = mnt_names[stats[counter].f_type];
320 me->me_dev = -1; /* Magic; means not known yet. */
323 /* Add to the linked list. */
330 #endif /* MOUNTED_GETFSSTAT */
332 #if defined (MOUNTED_FREAD) || defined (MOUNTED_FREAD_FSTYP) /* SVR[23]. */
335 char *table = "/etc/mnttab";
338 fp = fopen (table, "r");
342 while (fread (&mnt, sizeof mnt, 1, fp) > 0)
344 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
345 #ifdef GETFSTYP /* SVR3. */
346 me->me_devname = xstrdup (mnt.mt_dev);
348 me->me_devname = xmalloc (strlen (mnt.mt_dev) + 6);
349 strcpy (me->me_devname, "/dev/");
350 strcpy (me->me_devname + 5, mnt.mt_dev);
352 me->me_mountdir = xstrdup (mnt.mt_filsys);
353 me->me_dev = -1; /* Magic; means not known yet. */
355 #ifdef GETFSTYP /* SVR3. */
359 char typebuf[FSTYPSZ];
361 if (statfs (me->me_mountdir, &fsd, sizeof fsd, 0) != -1
362 && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1)
363 me->me_type = xstrdup (typebuf);
368 /* Add to the linked list. */
373 if (fclose (fp) == EOF)
376 #endif /* MOUNTED_FREAD || MOUNTED_FREAD_FSTYP. */
378 #ifdef MOUNTED_GETMNTTBL /* DolphinOS goes it's own way */
380 struct mntent **mnttbl=getmnttbl(),**ent;
381 for (ent=mnttbl;*ent;ent++)
383 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
384 me->me_devname = xstrdup ( (*ent)->mt_resource);
385 me->me_mountdir = xstrdup( (*ent)->mt_directory);
386 me->me_type = xstrdup ((*ent)->mt_fstype);
387 me->me_dev = -1; /* Magic; means not known yet. */
390 /* Add to the linked list. */
398 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
401 char *table = MNTTAB;
405 fp = fopen (table, "r");
409 while ((ret = getmntent (fp, &mnt)) == 0)
411 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
412 me->me_devname = xstrdup (mnt.mnt_special);
413 me->me_mountdir = xstrdup (mnt.mnt_mountp);
414 me->me_type = xstrdup (mnt.mnt_fstype);
415 me->me_dev = -1; /* Magic; means not known yet. */
418 /* Add to the linked list. */
425 if (fclose (fp) == EOF)
428 #endif /* MOUNTED_GETMNTENT2. */
430 #ifdef MOUNTED_VMOUNT /* AIX. */
433 char *entries, *thisent;
436 /* Ask how many bytes to allocate for the mounted filesystem info. */
437 mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize);
438 entries = xmalloc (bufsize);
440 /* Get the list of mounted filesystems. */
441 mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries);
443 for (thisent = entries; thisent < entries + bufsize;
444 thisent += vmp->vmt_length)
446 vmp = (struct vmount *) thisent;
447 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
448 if (vmp->vmt_flags & MNT_REMOTE)
452 /* Prepend the remote pathname. */
453 host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off;
454 path = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off;
455 me->me_devname = xmalloc (strlen (host) + strlen (path) + 2);
456 strcpy (me->me_devname, host);
457 strcat (me->me_devname, ":");
458 strcat (me->me_devname, path);
462 me->me_devname = xstrdup (thisent +
463 vmp->vmt_data[VMT_OBJECT].vmt_off);
465 me->me_mountdir = xstrdup (thisent + vmp->vmt_data[VMT_STUB].vmt_off);
466 me->me_type = xstrdup (fstype_to_string (vmp->vmt_gfstype));
467 me->me_dev = -1; /* vmt_fsid might be the info we want. */
470 /* Add to the linked list. */
476 #endif /* MOUNTED_VMOUNT. */
478 /* Free the dummy head. */
480 mount_list = mount_list->me_next;