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 #include <sys/types.h>
20 #include "mountlist.h"
27 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
39 #if defined (MOUNTED_GETFSSTAT) /* __alpha running OSF_1 */
40 # include <sys/mount.h>
41 # include <sys/fs_types.h>
42 #endif /* MOUNTED_GETFSSTAT */
44 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
47 # if defined(MNT_MNTTAB) /* HP-UX. */
48 # define MOUNTED MNT_MNTTAB
50 # if defined(MNTTABNAME) /* Dynix. */
51 # define MOUNTED MNTTABNAME
56 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
57 #include <sys/mount.h>
60 #ifdef MOUNTED_GETMNT /* Ultrix. */
61 #include <sys/param.h>
62 #include <sys/mount.h>
63 #include <sys/fs_types.h>
66 #ifdef MOUNTED_FREAD /* SVR2. */
70 #ifdef MOUNTED_FREAD_FSTYP /* SVR3. */
72 #include <sys/fstyp.h>
73 #include <sys/statfs.h>
76 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
77 #include <sys/mnttab.h>
80 #ifdef MOUNTED_VMOUNT /* AIX. */
86 /* So special that it's not worth putting this in autoconf. */
87 #undef MOUNTED_FREAD_FSTYP
88 #define MOUNTED_GETMNTTBL
91 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
92 /* Return the value of the hexadecimal number represented by CP.
93 No prefix (like '0x') or suffix (like 'h') is expected to be
105 if (*cp >= 'a' && *cp <= 'f')
106 val = val * 16 + *cp - 'a' + 10;
107 else if (*cp >= 'A' && *cp <= 'F')
108 val = val * 16 + *cp - 'A' + 10;
109 else if (*cp >= '0' && *cp <= '9')
110 val = val * 16 + *cp - '0';
117 #endif /* MOUNTED_GETMNTENT1. */
119 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
154 #endif /* MOUNTED_GETMNTINFO */
156 #ifdef MOUNTED_VMOUNT /* AIX. */
163 e = getvfsbytype (t);
164 if (!e || !e->vfsent_name)
167 return e->vfsent_name;
169 #endif /* MOUNTED_VMOUNT */
171 /* Return a list of the currently mounted filesystems, or NULL on error.
172 Add each entry to the tail of the list so that they stay in order.
173 If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in
174 the returned list are valid. Otherwise, they might not be.
175 If ALL_FS is zero, do not return entries for filesystems that
176 are automounter (dummy) entries. */
179 read_filesystem_list (need_fs_type, all_fs)
180 int need_fs_type, all_fs;
182 struct mount_entry *mount_list;
183 struct mount_entry *me;
184 struct mount_entry *mtail;
186 /* Start the list off with a dummy entry. */
187 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
189 mount_list = mtail = me;
191 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
194 char *table = MOUNTED;
198 fp = setmntent (table, "r");
202 while ((mnt = getmntent (fp)))
204 if (!all_fs && (!strcmp (mnt->mnt_type, "ignore")
205 || !strcmp (mnt->mnt_type, "auto")))
208 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
209 me->me_devname = xstrdup (mnt->mnt_fsname);
210 me->me_mountdir = xstrdup (mnt->mnt_dir);
211 me->me_type = xstrdup (mnt->mnt_type);
212 devopt = strstr (mnt->mnt_opts, "dev=");
215 if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X'))
216 me->me_dev = xatoi (devopt + 6);
218 me->me_dev = xatoi (devopt + 4);
221 me->me_dev = -1; /* Magic; means not known yet. */
224 /* Add to the linked list. */
229 if (endmntent (fp) == 0)
232 #endif /* MOUNTED_GETMNTENT1. */
234 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
239 entries = getmntinfo (&fsp, MNT_NOWAIT);
242 while (entries-- > 0)
244 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
245 me->me_devname = xstrdup (fsp->f_mntfromname);
246 me->me_mountdir = xstrdup (fsp->f_mntonname);
247 me->me_type = fstype_to_string (fsp->f_type);
248 me->me_dev = -1; /* Magic; means not known yet. */
251 /* Add to the linked list. */
257 #endif /* MOUNTED_GETMNTINFO */
259 #ifdef MOUNTED_GETMNT /* Ultrix. */
265 while ((val = getmnt (&offset, &fsd, sizeof (fsd), NOSTAT_MANY,
268 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
269 me->me_devname = xstrdup (fsd.fd_req.devname);
270 me->me_mountdir = xstrdup (fsd.fd_req.path);
271 me->me_type = gt_names[fsd.fd_req.fstype];
272 me->me_dev = fsd.fd_req.dev;
275 /* Add to the linked list. */
282 #endif /* MOUNTED_GETMNT. */
284 #if defined (MOUNTED_GETFSSTAT) /* __alpha running OSF_1 */
286 int numsys, counter, bufsize;
287 struct statfs *stats;
289 numsys = getfsstat ((struct statfs *)0, 0L, MNT_WAIT);
293 bufsize = (1 + numsys) * sizeof (struct statfs);
294 stats = (struct statfs *)xmalloc (bufsize);
295 numsys = getfsstat (stats, bufsize, MNT_WAIT);
303 for (counter = 0; counter < numsys; counter++)
305 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
306 me->me_devname = xstrdup (stats[counter].f_mntfromname);
307 me->me_mountdir = xstrdup (stats[counter].f_mntonname);
308 me->me_type = mnt_names[stats[counter].f_type];
309 me->me_dev = -1; /* Magic; means not known yet. */
312 /* Add to the linked list. */
319 #endif /* MOUNTED_GETFSSTAT */
321 #if defined (MOUNTED_FREAD) || defined (MOUNTED_FREAD_FSTYP) /* SVR[23]. */
324 char *table = "/etc/mnttab";
327 fp = fopen (table, "r");
331 while (fread (&mnt, sizeof mnt, 1, fp) > 0)
333 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
334 #ifdef GETFSTYP /* SVR3. */
335 me->me_devname = xstrdup (mnt.mt_dev);
337 me->me_devname = xmalloc (strlen (mnt.mt_dev) + 6);
338 strcpy (me->me_devname, "/dev/");
339 strcpy (me->me_devname + 5, mnt.mt_dev);
341 me->me_mountdir = xstrdup (mnt.mt_filsys);
342 me->me_dev = -1; /* Magic; means not known yet. */
344 #ifdef GETFSTYP /* SVR3. */
348 char typebuf[FSTYPSZ];
350 if (statfs (me->me_mountdir, &fsd, sizeof fsd, 0) != -1
351 && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1)
352 me->me_type = xstrdup (typebuf);
357 /* Add to the linked list. */
362 if (fclose (fp) == EOF)
365 #endif /* MOUNTED_FREAD || MOUNTED_FREAD_FSTYP. */
367 #ifdef MOUNTED_GETMNTTBL /* DolphinOS goes it's own way */
369 struct mntent **mnttbl=getmnttbl(),**ent;
370 for (ent=mnttbl;*ent;ent++)
372 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
373 me->me_devname = xstrdup ( (*ent)->mt_resource);
374 me->me_mountdir = xstrdup( (*ent)->mt_directory);
375 me->me_type = xstrdup ((*ent)->mt_fstype);
376 me->me_dev = -1; /* Magic; means not known yet. */
379 /* Add to the linked list. */
387 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
390 char *table = MNTTAB;
394 fp = fopen (table, "r");
398 while ((ret = getmntent (fp, &mnt)) == 0)
400 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
401 me->me_devname = xstrdup (mnt.mnt_special);
402 me->me_mountdir = xstrdup (mnt.mnt_mountp);
403 me->me_type = xstrdup (mnt.mnt_fstype);
404 me->me_dev = -1; /* Magic; means not known yet. */
407 /* Add to the linked list. */
414 if (fclose (fp) == EOF)
417 #endif /* MOUNTED_GETMNTENT2. */
419 #ifdef MOUNTED_VMOUNT /* AIX. */
422 char *entries, *thisent;
425 /* Ask how many bytes to allocate for the mounted filesystem info. */
426 mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize);
427 entries = xmalloc (bufsize);
429 /* Get the list of mounted filesystems. */
430 mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries);
432 for (thisent = entries; thisent < entries + bufsize;
433 thisent += vmp->vmt_length)
435 vmp = (struct vmount *) thisent;
436 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
437 if (vmp->vmt_flags & MNT_REMOTE)
441 /* Prepend the remote pathname. */
442 host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off;
443 path = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off;
444 me->me_devname = xmalloc (strlen (host) + strlen (path) + 2);
445 strcpy (me->me_devname, host);
446 strcat (me->me_devname, ":");
447 strcat (me->me_devname, path);
451 me->me_devname = xstrdup (thisent +
452 vmp->vmt_data[VMT_OBJECT].vmt_off);
454 me->me_mountdir = xstrdup (thisent + vmp->vmt_data[VMT_STUB].vmt_off);
455 me->me_type = xstrdup (fstype_to_string (vmp->vmt_gfstype));
456 me->me_dev = -1; /* vmt_fsid might be the info we want. */
459 /* Add to the linked list. */
465 #endif /* MOUNTED_VMOUNT. */
467 /* Free the dummy head. */
469 mount_list = mount_list->me_next;