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 #if defined (MOUNTED_GETFSSTAT) /* __alpha running OSF_1 */
44 # include <sys/mount.h>
45 # include <sys/fs_types.h>
46 #endif /* MOUNTED_GETFSSTAT */
48 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
51 # if defined(MNT_MNTTAB) /* HP-UX. */
52 # define MOUNTED MNT_MNTTAB
54 # if defined(MNTTABNAME) /* Dynix. */
55 # define MOUNTED MNTTABNAME
60 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
61 #include <sys/mount.h>
64 #ifdef MOUNTED_GETMNT /* Ultrix. */
65 #include <sys/param.h>
66 #include <sys/mount.h>
67 #include <sys/fs_types.h>
70 #ifdef MOUNTED_FREAD /* SVR2. */
74 #ifdef MOUNTED_FREAD_FSTYP /* SVR3. */
76 #include <sys/fstyp.h>
77 #include <sys/statfs.h>
80 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
81 #include <sys/mnttab.h>
84 #ifdef MOUNTED_VMOUNT /* AIX. */
90 /* So special that it's not worth putting this in autoconf. */
91 #undef MOUNTED_FREAD_FSTYP
92 #define MOUNTED_GETMNTTBL
95 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
96 /* Return the value of the hexadecimal number represented by CP.
97 No prefix (like '0x') or suffix (like 'h') is expected to be
109 if (*cp >= 'a' && *cp <= 'f')
110 val = val * 16 + *cp - 'a' + 10;
111 else if (*cp >= 'A' && *cp <= 'F')
112 val = val * 16 + *cp - 'A' + 10;
113 else if (*cp >= '0' && *cp <= '9')
114 val = val * 16 + *cp - '0';
121 #endif /* MOUNTED_GETMNTENT1. */
123 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
158 #endif /* MOUNTED_GETMNTINFO */
160 #ifdef MOUNTED_VMOUNT /* AIX. */
167 e = getvfsbytype (t);
168 if (!e || !e->vfsent_name)
171 return e->vfsent_name;
173 #endif /* MOUNTED_VMOUNT */
175 /* Return a list of the currently mounted filesystems, or NULL on error.
176 Add each entry to the tail of the list so that they stay in order.
177 If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in
178 the returned list are valid. Otherwise, they might not be.
179 If ALL_FS is zero, do not return entries for filesystems that
180 are automounter (dummy) entries. */
183 read_filesystem_list (need_fs_type, all_fs)
184 int need_fs_type, all_fs;
186 struct mount_entry *mount_list;
187 struct mount_entry *me;
188 struct mount_entry *mtail;
190 /* Start the list off with a dummy entry. */
191 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
193 mount_list = mtail = me;
195 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
198 char *table = MOUNTED;
202 fp = setmntent (table, "r");
206 while ((mnt = getmntent (fp)))
208 if (!all_fs && (!strcmp (mnt->mnt_type, "ignore")
209 || !strcmp (mnt->mnt_type, "auto")))
212 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
213 me->me_devname = xstrdup (mnt->mnt_fsname);
214 me->me_mountdir = xstrdup (mnt->mnt_dir);
215 me->me_type = xstrdup (mnt->mnt_type);
216 devopt = strstr (mnt->mnt_opts, "dev=");
219 if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X'))
220 me->me_dev = xatoi (devopt + 6);
222 me->me_dev = xatoi (devopt + 4);
225 me->me_dev = -1; /* Magic; means not known yet. */
228 /* Add to the linked list. */
233 if (endmntent (fp) == 0)
236 #endif /* MOUNTED_GETMNTENT1. */
238 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
243 entries = getmntinfo (&fsp, MNT_NOWAIT);
246 while (entries-- > 0)
248 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
249 me->me_devname = xstrdup (fsp->f_mntfromname);
250 me->me_mountdir = xstrdup (fsp->f_mntonname);
251 me->me_type = fstype_to_string (fsp->f_type);
252 me->me_dev = -1; /* Magic; means not known yet. */
255 /* Add to the linked list. */
261 #endif /* MOUNTED_GETMNTINFO */
263 #ifdef MOUNTED_GETMNT /* Ultrix. */
269 while ((val = getmnt (&offset, &fsd, sizeof (fsd), NOSTAT_MANY,
272 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
273 me->me_devname = xstrdup (fsd.fd_req.devname);
274 me->me_mountdir = xstrdup (fsd.fd_req.path);
275 me->me_type = gt_names[fsd.fd_req.fstype];
276 me->me_dev = fsd.fd_req.dev;
279 /* Add to the linked list. */
286 #endif /* MOUNTED_GETMNT. */
288 #if defined (MOUNTED_GETFSSTAT) /* __alpha running OSF_1 */
290 int numsys, counter, bufsize;
291 struct statfs *stats;
293 numsys = getfsstat ((struct statfs *)0, 0L, MNT_WAIT);
297 bufsize = (1 + numsys) * sizeof (struct statfs);
298 stats = (struct statfs *)xmalloc (bufsize);
299 numsys = getfsstat (stats, bufsize, MNT_WAIT);
307 for (counter = 0; counter < numsys; counter++)
309 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
310 me->me_devname = xstrdup (stats[counter].f_mntfromname);
311 me->me_mountdir = xstrdup (stats[counter].f_mntonname);
312 me->me_type = mnt_names[stats[counter].f_type];
313 me->me_dev = -1; /* Magic; means not known yet. */
316 /* Add to the linked list. */
323 #endif /* MOUNTED_GETFSSTAT */
325 #if defined (MOUNTED_FREAD) || defined (MOUNTED_FREAD_FSTYP) /* SVR[23]. */
328 char *table = "/etc/mnttab";
331 fp = fopen (table, "r");
335 while (fread (&mnt, sizeof mnt, 1, fp) > 0)
337 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
338 #ifdef GETFSTYP /* SVR3. */
339 me->me_devname = xstrdup (mnt.mt_dev);
341 me->me_devname = xmalloc (strlen (mnt.mt_dev) + 6);
342 strcpy (me->me_devname, "/dev/");
343 strcpy (me->me_devname + 5, mnt.mt_dev);
345 me->me_mountdir = xstrdup (mnt.mt_filsys);
346 me->me_dev = -1; /* Magic; means not known yet. */
348 #ifdef GETFSTYP /* SVR3. */
352 char typebuf[FSTYPSZ];
354 if (statfs (me->me_mountdir, &fsd, sizeof fsd, 0) != -1
355 && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1)
356 me->me_type = xstrdup (typebuf);
361 /* Add to the linked list. */
366 if (fclose (fp) == EOF)
369 #endif /* MOUNTED_FREAD || MOUNTED_FREAD_FSTYP. */
371 #ifdef MOUNTED_GETMNTTBL /* DolphinOS goes it's own way */
373 struct mntent **mnttbl=getmnttbl(),**ent;
374 for (ent=mnttbl;*ent;ent++)
376 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
377 me->me_devname = xstrdup ( (*ent)->mt_resource);
378 me->me_mountdir = xstrdup( (*ent)->mt_directory);
379 me->me_type = xstrdup ((*ent)->mt_fstype);
380 me->me_dev = -1; /* Magic; means not known yet. */
383 /* Add to the linked list. */
391 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
394 char *table = MNTTAB;
398 fp = fopen (table, "r");
402 while ((ret = getmntent (fp, &mnt)) == 0)
404 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
405 me->me_devname = xstrdup (mnt.mnt_special);
406 me->me_mountdir = xstrdup (mnt.mnt_mountp);
407 me->me_type = xstrdup (mnt.mnt_fstype);
408 me->me_dev = -1; /* Magic; means not known yet. */
411 /* Add to the linked list. */
418 if (fclose (fp) == EOF)
421 #endif /* MOUNTED_GETMNTENT2. */
423 #ifdef MOUNTED_VMOUNT /* AIX. */
426 char *entries, *thisent;
429 /* Ask how many bytes to allocate for the mounted filesystem info. */
430 mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize);
431 entries = xmalloc (bufsize);
433 /* Get the list of mounted filesystems. */
434 mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries);
436 for (thisent = entries; thisent < entries + bufsize;
437 thisent += vmp->vmt_length)
439 vmp = (struct vmount *) thisent;
440 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
441 if (vmp->vmt_flags & MNT_REMOTE)
445 /* Prepend the remote pathname. */
446 host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off;
447 path = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off;
448 me->me_devname = xmalloc (strlen (host) + strlen (path) + 2);
449 strcpy (me->me_devname, host);
450 strcat (me->me_devname, ":");
451 strcat (me->me_devname, path);
455 me->me_devname = xstrdup (thisent +
456 vmp->vmt_data[VMT_OBJECT].vmt_off);
458 me->me_mountdir = xstrdup (thisent + vmp->vmt_data[VMT_STUB].vmt_off);
459 me->me_type = xstrdup (fstype_to_string (vmp->vmt_gfstype));
460 me->me_dev = -1; /* vmt_fsid might be the info we want. */
463 /* Add to the linked list. */
469 #endif /* MOUNTED_VMOUNT. */
471 /* Free the dummy head. */
473 mount_list = mount_list->me_next;