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. */
85 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
86 /* Return the value of the hexadecimal number represented by CP.
87 No prefix (like '0x') or suffix (like 'h') is expected to be
99 if (*cp >= 'a' && *cp <= 'f')
100 val = val * 16 + *cp - 'a' + 10;
101 else if (*cp >= 'A' && *cp <= 'F')
102 val = val * 16 + *cp - 'A' + 10;
103 else if (*cp >= '0' && *cp <= '9')
104 val = val * 16 + *cp - '0';
111 #endif /* MOUNTED_GETMNTENT1. */
113 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
148 #endif /* MOUNTED_GETMNTINFO */
150 #ifdef MOUNTED_VMOUNT /* AIX. */
157 e = getvfsbytype (t);
158 if (!e || !e->vfsent_name)
161 return e->vfsent_name;
163 #endif /* MOUNTED_VMOUNT */
165 /* Return a list of the currently mounted filesystems, or NULL on error.
166 Add each entry to the tail of the list so that they stay in order.
167 If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in
168 the returned list are valid. Otherwise, they might not be.
169 If ALL_FS is zero, do not return entries for filesystems that
170 are automounter (dummy) entries. */
173 read_filesystem_list (need_fs_type, all_fs)
174 int need_fs_type, all_fs;
176 struct mount_entry *mount_list;
177 struct mount_entry *me;
178 struct mount_entry *mtail;
180 /* Start the list off with a dummy entry. */
181 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
183 mount_list = mtail = me;
185 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
188 char *table = MOUNTED;
192 fp = setmntent (table, "r");
196 while ((mnt = getmntent (fp)))
198 if (!all_fs && (!strcmp (mnt->mnt_type, "ignore")
199 || !strcmp (mnt->mnt_type, "auto")))
202 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
203 me->me_devname = xstrdup (mnt->mnt_fsname);
204 me->me_mountdir = xstrdup (mnt->mnt_dir);
205 me->me_type = xstrdup (mnt->mnt_type);
206 devopt = strstr (mnt->mnt_opts, "dev=");
209 if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X'))
210 me->me_dev = xatoi (devopt + 6);
212 me->me_dev = xatoi (devopt + 4);
215 me->me_dev = -1; /* Magic; means not known yet. */
218 /* Add to the linked list. */
223 if (endmntent (fp) == 0)
226 #endif /* MOUNTED_GETMNTENT1. */
228 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
233 entries = getmntinfo (&fsp, MNT_NOWAIT);
236 while (entries-- > 0)
238 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
239 me->me_devname = xstrdup (fsp->f_mntfromname);
240 me->me_mountdir = xstrdup (fsp->f_mntonname);
241 me->me_type = fstype_to_string (fsp->f_type);
242 me->me_dev = -1; /* Magic; means not known yet. */
245 /* Add to the linked list. */
251 #endif /* MOUNTED_GETMNTINFO */
253 #ifdef MOUNTED_GETMNT /* Ultrix. */
259 while ((val = getmnt (&offset, &fsd, sizeof (fsd), NOSTAT_MANY,
262 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
263 me->me_devname = xstrdup (fsd.fd_req.devname);
264 me->me_mountdir = xstrdup (fsd.fd_req.path);
265 me->me_type = gt_names[fsd.fd_req.fstype];
266 me->me_dev = fsd.fd_req.dev;
269 /* Add to the linked list. */
276 #endif /* MOUNTED_GETMNT. */
278 #if defined (MOUNTED_GETFSSTAT) /* __alpha running OSF_1 */
280 int numsys, counter, bufsize;
281 struct statfs *stats;
283 numsys = getfsstat ((struct statfs *)0, 0L, MNT_WAIT);
287 bufsize = (1 + numsys) * sizeof (struct statfs);
288 stats = (struct statfs *)xmalloc (bufsize);
289 numsys = getfsstat (stats, bufsize, MNT_WAIT);
297 for (counter = 0; counter < numsys; counter++)
299 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
300 me->me_devname = xstrdup (stats[counter].f_mntfromname);
301 me->me_mountdir = xstrdup (stats[counter].f_mntonname);
302 me->me_type = mnt_names[stats[counter].f_type];
303 me->me_dev = -1; /* Magic; means not known yet. */
306 /* Add to the linked list. */
313 #endif /* MOUNTED_GETFSSTAT */
315 #if defined (MOUNTED_FREAD) || defined (MOUNTED_FREAD_FSTYP) /* SVR[23]. */
318 char *table = "/etc/mnttab";
321 fp = fopen (table, "r");
325 while (fread (&mnt, sizeof mnt, 1, fp) > 0)
327 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
328 #ifdef GETFSTYP /* SVR3. */
329 me->me_devname = xstrdup (mnt.mt_dev);
331 me->me_devname = xmalloc (strlen (mnt.mt_dev) + 6);
332 strcpy (me->me_devname, "/dev/");
333 strcpy (me->me_devname + 5, mnt.mt_dev);
335 me->me_mountdir = xstrdup (mnt.mt_filsys);
336 me->me_dev = -1; /* Magic; means not known yet. */
338 #ifdef GETFSTYP /* SVR3. */
342 char typebuf[FSTYPSZ];
344 if (statfs (me->me_mountdir, &fsd, sizeof fsd, 0) != -1
345 && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1)
346 me->me_type = xstrdup (typebuf);
351 /* Add to the linked list. */
356 if (fclose (fp) == EOF)
359 #endif /* MOUNTED_FREAD || MOUNTED_FREAD_FSTYP. */
361 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
364 char *table = MNTTAB;
368 fp = fopen (table, "r");
372 while ((ret = getmntent (fp, &mnt)) == 0)
374 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
375 me->me_devname = xstrdup (mnt.mnt_special);
376 me->me_mountdir = xstrdup (mnt.mnt_mountp);
377 me->me_type = xstrdup (mnt.mnt_fstype);
378 me->me_dev = -1; /* Magic; means not known yet. */
381 /* Add to the linked list. */
388 if (fclose (fp) == EOF)
391 #endif /* MOUNTED_GETMNTENT2. */
393 #ifdef MOUNTED_VMOUNT /* AIX. */
396 char *entries, *thisent;
399 /* Ask how many bytes to allocate for the mounted filesystem info. */
400 mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize);
401 entries = xmalloc (bufsize);
403 /* Get the list of mounted filesystems. */
404 mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries);
406 for (thisent = entries; thisent < entries + bufsize;
407 thisent += vmp->vmt_length)
409 vmp = (struct vmount *) thisent;
410 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
411 if (vmp->vmt_flags & MNT_REMOTE)
415 /* Prepend the remote pathname. */
416 host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off;
417 path = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off;
418 me->me_devname = xmalloc (strlen (host) + strlen (path) + 2);
419 strcpy (me->me_devname, host);
420 strcat (me->me_devname, ":");
421 strcat (me->me_devname, path);
425 me->me_devname = xstrdup (thisent +
426 vmp->vmt_data[VMT_OBJECT].vmt_off);
428 me->me_mountdir = xstrdup (thisent + vmp->vmt_data[VMT_STUB].vmt_off);
429 me->me_type = xstrdup (fstype_to_string (vmp->vmt_gfstype));
430 me->me_dev = -1; /* vmt_fsid might be the info we want. */
433 /* Add to the linked list. */
439 #endif /* MOUNTED_VMOUNT. */
441 /* Free the dummy head. */
443 mount_list = mount_list->me_next;