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(USG) || defined(STDC_HEADERS)
39 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
42 # if defined(MNT_MNTTAB) /* HP-UX. */
43 # define MOUNTED MNT_MNTTAB
45 # if defined(MNTTABNAME) /* Dynix. */
46 # define MOUNTED MNTTABNAME
51 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
52 #include <sys/mount.h>
55 #ifdef MOUNTED_GETMNT /* Ultrix. */
56 #include <sys/param.h>
57 #include <sys/mount.h>
58 #include <sys/fs_types.h>
61 #ifdef MOUNTED_FREAD /* SVR2. */
65 #ifdef MOUNTED_FREAD_FSTYP /* SVR3. */
67 #include <sys/fstyp.h>
68 #include <sys/statfs.h>
71 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
72 #include <sys/mnttab.h>
75 #ifdef MOUNTED_VMOUNT /* AIX. */
80 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
81 /* Return the value of the hexadecimal number represented by CP.
82 No prefix (like '0x') or suffix (like 'h') is expected to be
94 if (*cp >= 'a' && *cp <= 'f')
95 val = val * 16 + *cp - 'a' + 10;
96 else if (*cp >= 'A' && *cp <= 'F')
97 val = val * 16 + *cp - 'A' + 10;
98 else if (*cp >= '0' && *cp <= '9')
99 val = val * 16 + *cp - '0';
106 #endif /* MOUNTED_GETMNTENT1. */
108 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
141 #endif /* MOUNTED_GETMNTINFO */
143 #ifdef MOUNTED_VMOUNT /* AIX. */
150 e = getvfsbytype (t);
151 if (!e || !e->vfsent_name)
154 return e->vfsent_name;
156 #endif /* MOUNTED_VMOUNT */
158 /* Return a list of the currently mounted filesystems, or NULL on error.
159 Add each entry to the tail of the list so that they stay in order.
160 If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in
161 the returned list are valid. Otherwise, they might not be.
162 If ALL_FS is zero, do not return entries for filesystems that
163 are automounter (dummy) entries. */
166 read_filesystem_list (need_fs_type, all_fs)
167 int need_fs_type, all_fs;
169 struct mount_entry *mount_list;
170 struct mount_entry *me;
171 struct mount_entry *mtail;
173 /* Start the list off with a dummy entry. */
174 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
176 mount_list = mtail = me;
178 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
181 char *table = MOUNTED;
185 fp = setmntent (table, "r");
189 while ((mnt = getmntent (fp)))
191 if (!all_fs && (!strcmp (mnt->mnt_type, "ignore")
192 || !strcmp (mnt->mnt_type, "auto")))
195 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
196 me->me_devname = xstrdup (mnt->mnt_fsname);
197 me->me_mountdir = xstrdup (mnt->mnt_dir);
198 me->me_type = xstrdup (mnt->mnt_type);
199 devopt = strstr (mnt->mnt_opts, "dev=");
202 if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X'))
203 me->me_dev = xatoi (devopt + 6);
205 me->me_dev = xatoi (devopt + 4);
208 me->me_dev = -1; /* Magic; means not known yet. */
211 /* Add to the linked list. */
216 if (endmntent (fp) == 0)
219 #endif /* MOUNTED_GETMNTENT1. */
221 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
226 entries = getmntinfo (&fsp, MNT_NOWAIT);
229 while (entries-- > 0)
231 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
232 me->me_devname = xstrdup (fsp->f_mntfromname);
233 me->me_mountdir = xstrdup (fsp->f_mntonname);
234 me->me_type = fstype_to_string (fsp->f_type);
235 me->me_dev = -1; /* Magic; means not known yet. */
238 /* Add to the linked list. */
244 #endif /* MOUNTED_GETMNTINFO */
246 #ifdef MOUNTED_GETMNT /* Ultrix. */
252 while ((val = getmnt (&offset, &fsd, sizeof (fsd), NOSTAT_MANY,
255 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
256 me->me_devname = xstrdup (fsd.fd_req.devname);
257 me->me_mountdir = xstrdup (fsd.fd_req.path);
258 me->me_type = gt_names[fsd.fd_req.fstype];
259 me->me_dev = fsd.fd_req.dev;
262 /* Add to the linked list. */
269 #endif /* MOUNTED_GETMNT. */
271 #if defined (MOUNTED_FREAD) || defined (MOUNTED_FREAD_FSTYP) /* SVR[23]. */
274 char *table = "/etc/mnttab";
277 fp = fopen (table, "r");
281 while (fread (&mnt, sizeof mnt, 1, fp) > 0)
283 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
284 #ifdef GETFSTYP /* SVR3. */
285 me->me_devname = xstrdup (mnt.mt_dev);
287 me->me_devname = xmalloc (strlen (mnt.mt_dev) + 6);
288 strcpy (me->me_devname, "/dev/");
289 strcpy (me->me_devname + 5, mnt.mt_dev);
291 me->me_mountdir = xstrdup (mnt.mt_filsys);
292 me->me_dev = -1; /* Magic; means not known yet. */
294 #ifdef GETFSTYP /* SVR3. */
298 char typebuf[FSTYPSZ];
300 if (statfs (me->me_mountdir, &fsd, sizeof fsd, 0) != -1
301 && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1)
302 me->me_type = xstrdup (typebuf);
307 /* Add to the linked list. */
312 if (fclose (fp) == EOF)
315 #endif /* MOUNTED_FREAD || MOUNTED_FREAD_FSTYP. */
317 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
320 char *table = MNTTAB;
324 fp = fopen (table, "r");
328 while ((ret = getmntent (fp, &mnt)) == 0)
330 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
331 me->me_devname = xstrdup (mnt.mnt_special);
332 me->me_mountdir = xstrdup (mnt.mnt_mountp);
333 me->me_type = xstrdup (mnt.mnt_fstype);
334 me->me_dev = -1; /* Magic; means not known yet. */
337 /* Add to the linked list. */
344 if (fclose (fp) == EOF)
347 #endif /* MOUNTED_GETMNTENT2. */
349 #ifdef MOUNTED_VMOUNT /* AIX. */
352 char *entries, *thisent;
355 /* Ask how many bytes to allocate for the mounted filesystem info. */
356 mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize);
357 entries = xmalloc (bufsize);
359 /* Get the list of mounted filesystems. */
360 mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries);
362 for (thisent = entries; thisent < entries + bufsize;
363 thisent += vmp->vmt_length)
365 vmp = (struct vmount *) thisent;
366 me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
367 if (vmp->vmt_flags & MNT_REMOTE)
371 /* Prepend the remote pathname. */
372 host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off;
373 path = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off;
374 me->me_devname = xmalloc (strlen (host) + strlen (path) + 2);
375 strcpy (me->me_devname, host);
376 strcat (me->me_devname, ":");
377 strcat (me->me_devname, path);
381 me->me_devname = xstrdup (thisent +
382 vmp->vmt_data[VMT_OBJECT].vmt_off);
384 me->me_mountdir = xstrdup (thisent + vmp->vmt_data[VMT_STUB].vmt_off);
385 me->me_type = xstrdup (fstype_to_string (vmp->vmt_gfstype));
386 me->me_dev = -1; /* vmt_fsid might be the info we want. */
389 /* Add to the linked list. */
395 #endif /* MOUNTED_VMOUNT. */
397 /* Free the dummy head. */
399 mount_list = mount_list->me_next;