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. */
24 #include <sys/types.h>
25 #include "mountlist.h"
30 void free (void *ptr
);
32 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
38 #ifdef HAVE_SYS_PARAM_H
39 #include <sys/param.h>
42 #if defined (MOUNTED_GETFSSTAT) /* __alpha running OSF_1 */
43 #include <sys/mount.h>
44 #include <sys/fs_types.h>
45 #endif /* MOUNTED_GETFSSTAT */
47 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
50 #if defined(MNT_MNTTAB) /* HP-UX. */
51 #define MOUNTED MNT_MNTTAB
53 #if defined(MNTTABNAME) /* Dynix. */
54 #define MOUNTED MNTTABNAME
59 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
60 #include <sys/mount.h>
63 #ifdef MOUNTED_GETMNT /* Ultrix. */
64 #include <sys/mount.h>
65 #include <sys/fs_types.h>
68 #ifdef MOUNTED_FREAD /* SVR2. */
72 #ifdef MOUNTED_FREAD_FSTYP /* SVR3. */
74 #include <sys/fstyp.h>
75 #include <sys/statfs.h>
78 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
79 #include <sys/mnttab.h>
82 #ifdef MOUNTED_VMOUNT /* AIX. */
87 /* 4.4BSD2 derived systems */
88 #if defined(__bsdi__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
94 /* void error (void); FIXME -- needed? */
97 /* So special that it's not worth putting this in autoconf. */
98 #undef MOUNTED_FREAD_FSTYP
99 #define MOUNTED_GETMNTTBL
102 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
103 /* Return the value of the hexadecimal number represented by CP.
104 No prefix (like '0x') or suffix (like 'h') is expected to be
107 static int xatoi (char *cp
)
113 if (*cp
>= 'a' && *cp
<= 'f')
114 val
= val
* 16 + *cp
- 'a' + 10;
115 else if (*cp
>= 'A' && *cp
<= 'F')
116 val
= val
* 16 + *cp
- 'A' + 10;
117 else if (*cp
>= '0' && *cp
<= '9')
118 val
= val
* 16 + *cp
- '0';
125 #endif /* MOUNTED_GETMNTENT1. */
127 #if defined (MOUNTED_GETMNTINFO) && !defined (xBSD)
128 static char *fstype_to_string (short t
)
159 #endif /* MOUNTED_GETMNTINFO */
161 #ifdef MOUNTED_VMOUNT /* AIX. */
162 static char *fstype_to_string (int t
)
166 e
= getvfsbytype (t
);
167 if (!e
|| !e
->vfsent_name
)
170 return e
->vfsent_name
;
172 #endif /* MOUNTED_VMOUNT */
174 /* Return a list of the currently mounted filesystems, or NULL on error.
175 Add each entry to the tail of the list so that they stay in order.
176 If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in
177 the returned list are valid. Otherwise, they might not be.
178 If ALL_FS is zero, do not return entries for filesystems that
179 are automounter (dummy) entries. */
181 struct mount_entry
*read_filesystem_list (int need_fs_type
, int all_fs
)
183 struct mount_entry
*mount_list
;
184 struct mount_entry
*me
;
185 struct mount_entry
*mtail
;
187 /* Start the list off with a dummy entry. */
188 me
= (struct mount_entry
*) malloc (sizeof (struct mount_entry
));
190 mount_list
= mtail
= me
;
192 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
195 char *table
= MOUNTED
;
199 fp
= setmntent (table
, "r");
203 while ((mnt
= getmntent (fp
))) {
204 if (!all_fs
&& (!strcmp (mnt
->mnt_type
, "ignore")
205 || !strcmp (mnt
->mnt_type
, "auto")))
208 me
= (struct mount_entry
*) malloc (sizeof (struct mount_entry
));
209 me
->me_devname
= strdup (mnt
->mnt_fsname
);
210 me
->me_mountdir
= strdup (mnt
->mnt_dir
);
211 me
->me_type
= strdup (mnt
->mnt_type
);
212 devopt
= strstr (mnt
->mnt_opts
, "dev=");
214 if (devopt
[4] == '0' && (devopt
[5] == 'x' || devopt
[5] == 'X'))
215 me
->me_dev
= xatoi (devopt
+ 6);
217 me
->me_dev
= xatoi (devopt
+ 4);
219 me
->me_dev
= -1; /* Magic; means not known yet. */
222 /* Add to the linked list. */
227 if (endmntent (fp
) == 0)
230 #endif /* MOUNTED_GETMNTENT1. */
232 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
237 entries
= getmntinfo (&fsp
, MNT_NOWAIT
);
240 while (entries
-- > 0) {
241 me
= (struct mount_entry
*) malloc (sizeof (struct mount_entry
));
242 me
->me_devname
= strdup (fsp
->f_mntfromname
);
243 me
->me_mountdir
= strdup (fsp
->f_mntonname
);
245 me
->me_type
= strdup (fsp
->f_fstypename
);
247 me
->me_type
= fstype_to_string (fsp
->f_type
);
249 me
->me_dev
= -1; /* Magic; means not known yet. */
252 /* Add to the linked list. */
258 #endif /* MOUNTED_GETMNTINFO */
260 #ifdef MOUNTED_GETMNT /* Ultrix. */
266 while ((val
= getmnt (&offset
, &fsd
, sizeof (fsd
), NOSTAT_MANY
,
268 me
= (struct mount_entry
*) malloc (sizeof (struct mount_entry
));
269 me
->me_devname
= strdup (fsd
.fd_req
.devname
);
270 me
->me_mountdir
= strdup (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
*) malloc (bufsize
);
295 numsys
= getfsstat (stats
, bufsize
, MNT_WAIT
);
301 for (counter
= 0; counter
< numsys
; counter
++) {
302 me
= (struct mount_entry
*) malloc (sizeof (struct mount_entry
));
303 me
->me_devname
= strdup (stats
[counter
].f_mntfromname
);
304 me
->me_mountdir
= strdup (stats
[counter
].f_mntonname
);
305 me
->me_type
= mnt_names
[stats
[counter
].f_type
];
306 me
->me_dev
= -1; /* Magic; means not known yet. */
309 /* Add to the linked list. */
316 #endif /* MOUNTED_GETFSSTAT */
318 #if defined (MOUNTED_FREAD) || defined (MOUNTED_FREAD_FSTYP) /* SVR[23]. */
321 char *table
= "/etc/mnttab";
324 fp
= fopen (table
, "r");
328 while (fread (&mnt
, sizeof mnt
, 1, fp
) > 0) {
329 me
= (struct mount_entry
*) malloc (sizeof (struct mount_entry
));
330 #ifdef GETFSTYP /* SVR3. */
331 me
->me_devname
= strdup (mnt
.mt_dev
);
333 me
->me_devname
= malloc (strlen (mnt
.mt_dev
) + 6);
334 strcpy (me
->me_devname
, "/dev/");
335 strcpy (me
->me_devname
+ 5, mnt
.mt_dev
);
337 me
->me_mountdir
= strdup (mnt
.mt_filsys
);
338 me
->me_dev
= -1; /* Magic; means not known yet. */
340 #ifdef GETFSTYP /* SVR3. */
343 char typebuf
[FSTYPSZ
];
345 if (statfs (me
->me_mountdir
, &fsd
, sizeof fsd
, 0) != -1
346 && sysfs (GETFSTYP
, fsd
.f_fstyp
, typebuf
) != -1)
347 me
->me_type
= strdup (typebuf
);
352 /* Add to the linked list. */
357 if (fclose (fp
) == EOF
)
360 #endif /* MOUNTED_FREAD || MOUNTED_FREAD_FSTYP. */
362 #ifdef MOUNTED_GETMNTTBL /* DolphinOS goes it's own way */
364 struct mntent
**mnttbl
= getmnttbl (), **ent
;
365 for (ent
= mnttbl
; *ent
; ent
++) {
366 me
= (struct mount_entry
*) malloc (sizeof (struct mount_entry
));
367 me
->me_devname
= strdup ((*ent
)->mt_resource
);
368 me
->me_mountdir
= strdup ((*ent
)->mt_directory
);
369 me
->me_type
= strdup ((*ent
)->mt_fstype
);
370 me
->me_dev
= -1; /* Magic; means not known yet. */
373 /* Add to the linked list. */
381 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
384 char *table
= MNTTAB
;
388 fp
= fopen (table
, "r");
392 while ((ret
= getmntent (fp
, &mnt
)) == 0) {
393 me
= (struct mount_entry
*) malloc (sizeof (struct mount_entry
));
394 me
->me_devname
= strdup (mnt
.mnt_special
);
395 me
->me_mountdir
= strdup (mnt
.mnt_mountp
);
396 me
->me_type
= strdup (mnt
.mnt_fstype
);
397 me
->me_dev
= -1; /* Magic; means not known yet. */
399 /* Add to the linked list. */
406 if (fclose (fp
) == EOF
)
409 #endif /* MOUNTED_GETMNTENT2. */
411 #ifdef MOUNTED_VMOUNT /* AIX. */
414 char *entries
, *thisent
;
417 /* Ask how many bytes to allocate for the mounted filesystem info. */
418 mntctl (MCTL_QUERY
, sizeof bufsize
, (struct vmount
*) &bufsize
);
419 entries
= malloc (bufsize
);
421 /* Get the list of mounted filesystems. */
422 mntctl (MCTL_QUERY
, bufsize
, (struct vmount
*) entries
);
424 for (thisent
= entries
; thisent
< entries
+ bufsize
;
425 thisent
+= vmp
->vmt_length
) {
426 vmp
= (struct vmount
*) thisent
;
427 me
= (struct mount_entry
*) malloc (sizeof (struct mount_entry
));
428 if (vmp
->vmt_flags
& MNT_REMOTE
) {
431 /* Prepend the remote pathname. */
432 host
= thisent
+ vmp
->vmt_data
[VMT_HOSTNAME
].vmt_off
;
433 path
= thisent
+ vmp
->vmt_data
[VMT_OBJECT
].vmt_off
;
434 me
->me_devname
= malloc (strlen (host
) + strlen (path
) + 2);
435 strcpy (me
->me_devname
, host
);
436 strcat (me
->me_devname
, ":");
437 strcat (me
->me_devname
, path
);
439 me
->me_devname
= strdup (thisent
+
440 vmp
->vmt_data
[VMT_OBJECT
].vmt_off
);
442 me
->me_mountdir
= strdup (thisent
+ vmp
->vmt_data
[VMT_STUB
].vmt_off
);
443 me
->me_type
= strdup (fstype_to_string (vmp
->vmt_gfstype
));
444 me
->me_dev
= -1; /* vmt_fsid might be the info we want. */
447 /* Add to the linked list. */
453 #endif /* MOUNTED_VMOUNT. */
455 /* Free the dummy head. */
457 mount_list
= mount_list
->me_next
;
461 #endif /* NO_INFOMOUNT */
463 #if defined(NO_INFOMOUNT) && defined(__QNX__)
465 ** QNX has no [gs]etmnt*(), [gs]etfs*(), or /etc/mnttab, but can do
466 ** this via the following code.
467 ** Note that, as this is based on CWD, it only fills one mount_entry
468 ** structure. See my_statfs() in utilunix.c for the "other side" of
479 #include <sys/disk.h>
480 #include <sys/fsys.h>
481 #include <sys/statfs.h>
483 #include "mountlist.h"
485 struct mount_entry
*read_filesystem_list(int need_fs_type
, int all_fs
)
487 struct _disk_entry de
;
490 char *tp
, dev
[_POSIX_NAME_MAX
], dir
[_POSIX_PATH_MAX
];
492 static struct mount_entry
*me
= NULL
;
496 if (me
->me_devname
) free(me
->me_devname
);
497 if (me
->me_mountdir
) free(me
->me_mountdir
);
498 if (me
->me_type
) free(me
->me_type
);
501 me
= (struct mount_entry
*)malloc(sizeof(struct mount_entry
));
503 if (!getcwd(dir
, _POSIX_PATH_MAX
)) return (NULL
);
505 if ((fd
= open(dir
, O_RDONLY
)) == -1) return (NULL
);
507 i
= disk_get_entry(fd
, &de
);
511 if (i
== -1) return (NULL
);
513 switch (de
.disk_type
)
515 case _UNMOUNTED
: tp
= "unmounted"; break;
516 case _FLOPPY
: tp
= "Floppy"; break;
517 case _HARD
: tp
= "Hard"; break;
518 case _RAMDISK
: tp
= "Ram"; break;
519 case _REMOVABLE
: tp
= "Removable"; break;
520 case _TAPE
: tp
= "Tape"; break;
521 case _CDROM
: tp
= "CDROM"; break;
522 default: tp
= "unknown";
525 if (fsys_get_mount_dev(dir
, &dev
) == -1) return (NULL
);
527 if (fsys_get_mount_pt(dev
, &dir
) == -1) return (NULL
);
529 me
->me_devname
= strdup(dev
);
530 me
->me_mountdir
= strdup(dir
);
531 me
->me_type
= strdup(tp
);
532 me
->me_dev
= de
.disk_type
;
535 fprintf(stderr
, "disk_get_entry():\n\tdisk_type=%d (%s)\n\tdriver_name='%-*.*s'\n\tdisk_drv=%d\n",
536 de
.disk_type
, tp
, _DRIVER_NAME_LEN
, _DRIVER_NAME_LEN
, de
.driver_name
, de
.disk_drv
);
537 fprintf(stderr
, "fsys_get_mount_dev():\n\tdevice='%s'\n", dev
);
538 fprintf(stderr
, "fsys_get_mount_pt():\n\tmount point='%s'\n", dir
);