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. */
23 #include <sys/types.h>
24 #include "mountlist.h"
31 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
43 #ifdef HAVE_SYS_PARAM_H
44 #include <sys/param.h>
47 #if defined (MOUNTED_GETFSSTAT) /* __alpha running OSF_1 */
48 # include <sys/mount.h>
49 # include <sys/fs_types.h>
50 #endif /* MOUNTED_GETFSSTAT */
52 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
55 # if defined(MNT_MNTTAB) /* HP-UX. */
56 # define MOUNTED MNT_MNTTAB
58 # if defined(MNTTABNAME) /* Dynix. */
59 # define MOUNTED MNTTABNAME
64 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
65 #include <sys/mount.h>
68 #ifdef MOUNTED_GETMNT /* Ultrix. */
69 #include <sys/mount.h>
70 #include <sys/fs_types.h>
73 #ifdef MOUNTED_FREAD /* SVR2. */
77 #ifdef MOUNTED_FREAD_FSTYP /* SVR3. */
79 #include <sys/fstyp.h>
80 #include <sys/statfs.h>
83 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
84 #include <sys/mnttab.h>
87 #ifdef MOUNTED_VMOUNT /* AIX. */
93 /* So special that it's not worth putting this in autoconf. */
94 #undef MOUNTED_FREAD_FSTYP
95 #define MOUNTED_GETMNTTBL
98 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
99 /* Return the value of the hexadecimal number represented by CP.
100 No prefix (like '0x') or suffix (like 'h') is expected to be
112 if (*cp >= 'a' && *cp <= 'f')
113 val = val * 16 + *cp - 'a' + 10;
114 else if (*cp >= 'A' && *cp <= 'F')
115 val = val * 16 + *cp - 'A' + 10;
116 else if (*cp >= '0' && *cp <= '9')
117 val = val * 16 + *cp - '0';
124 #endif /* MOUNTED_GETMNTENT1. */
126 #if defined (MOUNTED_GETMNTINFO) && !defined (__NetBSD__)
161 #endif /* MOUNTED_GETMNTINFO */
163 #ifdef MOUNTED_VMOUNT /* AIX. */
170 e = getvfsbytype (t);
171 if (!e || !e->vfsent_name)
174 return e->vfsent_name;
176 #endif /* MOUNTED_VMOUNT */
178 /* Return a list of the currently mounted filesystems, or NULL on error.
179 Add each entry to the tail of the list so that they stay in order.
180 If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in
181 the returned list are valid. Otherwise, they might not be.
182 If ALL_FS is zero, do not return entries for filesystems that
183 are automounter (dummy) entries. */
186 read_filesystem_list (need_fs_type, all_fs)
187 int need_fs_type, all_fs;
189 struct mount_entry *mount_list;
190 struct mount_entry *me;
191 struct mount_entry *mtail;
193 /* Start the list off with a dummy entry. */
194 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
196 mount_list = mtail = me;
198 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
201 char *table = MOUNTED;
205 fp = setmntent (table, "r");
209 while ((mnt = getmntent (fp)))
211 if (!all_fs && (!strcmp (mnt->mnt_type, "ignore")
212 || !strcmp (mnt->mnt_type, "auto")))
215 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
216 me->me_devname = xstrdup (mnt->mnt_fsname);
217 me->me_mountdir = xstrdup (mnt->mnt_dir);
218 me->me_type = xstrdup (mnt->mnt_type);
219 devopt = strstr (mnt->mnt_opts, "dev=");
222 if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X'))
223 me->me_dev = xatoi (devopt + 6);
225 me->me_dev = xatoi (devopt + 4);
228 me->me_dev = -1; /* Magic; means not known yet. */
231 /* Add to the linked list. */
236 if (endmntent (fp) == 0)
239 #endif /* MOUNTED_GETMNTENT1. */
241 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
246 entries = getmntinfo (&fsp, MNT_NOWAIT);
249 while (entries-- > 0)
251 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
252 me->me_devname = xstrdup (fsp->f_mntfromname);
253 me->me_mountdir = xstrdup (fsp->f_mntonname);
255 me->me_type = xstrdup (fsp->f_fstypename);
257 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;