Merge branch '2121_dir_symlink'
[kaloumi3.git] / src / mountlist.c
blob199a66ca973319e44477bbfb024f21ce5a7050af
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)
7 any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /** \file mountlist.c
19 * \brief Source: list of mounted filesystems
22 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include <sys/types.h>
29 /* This header needs to be included before sys/mount.h on *BSD */
30 #ifdef HAVE_SYS_PARAM_H
31 #include <sys/param.h>
32 #endif
34 #if defined (MOUNTED_GETFSSTAT) /* __alpha running OSF_1 */
35 #include <sys/mount.h>
36 #include <sys/fs_types.h>
37 #endif /* MOUNTED_GETFSSTAT */
39 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
40 #include <mntent.h>
41 #if !defined(MOUNTED)
42 #if defined(MNT_MNTTAB) /* HP-UX. */
43 #define MOUNTED MNT_MNTTAB
44 #endif
45 #if defined(MNTTABNAME) /* Dynix. */
46 #define MOUNTED MNTTABNAME
47 #endif
48 #endif
49 #endif
51 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
52 #include <sys/mount.h>
53 #endif
55 #ifdef MOUNTED_GETMNTINFO2 /* NetBSD 3.0. */
56 #include <sys/statvfs.h>
57 #endif
59 #ifdef MOUNTED_GETMNT /* Ultrix. */
60 #include <sys/mount.h>
61 #include <sys/fs_types.h>
62 #endif
64 #ifdef MOUNTED_FREAD /* SVR2. */
65 #include <mnttab.h>
66 #endif
68 #ifdef MOUNTED_FREAD_FSTYP /* SVR3. */
69 #include <mnttab.h>
70 #include <sys/fstyp.h>
71 #include <sys/statfs.h>
72 #endif
74 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
75 #include <sys/mnttab.h>
76 #endif
78 #ifdef MOUNTED_VMOUNT /* AIX. */
79 #include <fshelp.h>
80 #include <sys/vfs.h>
81 #endif
83 #ifdef HAVE_SYS_STATFS_H
84 #include <sys/statfs.h>
85 #endif
87 #ifdef HAVE_INFOMOUNT_QNX
88 #include <sys/disk.h>
89 #include <sys/fsys.h>
90 #endif
92 #ifdef HAVE_SYS_MOUNT_H
93 #include <sys/mount.h>
94 #endif
96 #ifdef HAVE_SYS_VFS_H
97 #include <sys/vfs.h>
98 #endif
100 #ifdef HAVE_SYS_FILSYS_H
101 #include <sys/filsys.h> /* SVR2. */
102 #endif
104 #ifdef HAVE_DUSTAT_H /* AIX PS/2. */
105 #include <sys/dustat.h>
106 #endif
108 #ifdef HAVE_SYS_STATVFS_H /* SVR4. */
109 #include <sys/statvfs.h>
110 #endif
112 #include "lib/global.h"
113 #include "mountlist.h"
115 #ifdef DOLPHIN
116 /* So special that it's not worth putting this in autoconf. */
117 #undef MOUNTED_FREAD_FSTYP
118 #define MOUNTED_GETMNTTBL
119 #endif
121 #if defined (__QNX__) && !defined(__QNXNTO__) && !defined (HAVE_INFOMOUNT_LIST)
122 # define HAVE_INFOMOUNT_QNX
123 #endif
125 #if defined(HAVE_INFOMOUNT_LIST) || defined(HAVE_INFOMOUNT_QNX)
126 # define HAVE_INFOMOUNT
127 #endif
129 /* A mount table entry. */
130 struct mount_entry
132 char *me_devname; /* Device node pathname, including "/dev/". */
133 char *me_mountdir; /* Mount point directory pathname. */
134 char *me_type; /* "nfs", "4.2", etc. */
135 dev_t me_dev; /* Device number of me_mountdir. */
136 struct mount_entry *me_next;
139 struct fs_usage
141 fsblkcnt_t fsu_blocks; /* Total blocks. */
142 fsblkcnt_t fsu_bfree; /* Free blocks available to superuser. */
143 fsblkcnt_t fsu_bavail; /* Free blocks available to non-superuser. */
144 fsfilcnt_t fsu_files; /* Total file nodes. */
145 fsfilcnt_t fsu_ffree; /* Free file nodes. */
148 static int get_fs_usage (char *path, struct fs_usage *fsp);
150 #ifdef HAVE_INFOMOUNT_LIST
152 static struct mount_entry *mount_list = NULL;
154 static void
155 free_mount_entry (struct mount_entry *me)
157 if (!me)
158 return;
159 if (me->me_devname)
160 free (me->me_devname);
161 if (me->me_mountdir)
162 free (me->me_mountdir);
163 if (me->me_type)
164 free (me->me_type);
165 free (me);
168 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
169 /* Return the value of the hexadecimal number represented by CP.
170 No prefix (like '0x') or suffix (like 'h') is expected to be
171 part of CP. */
173 static int
174 xatoi (const char *cp)
176 int val;
178 val = 0;
179 while (*cp)
181 if (*cp >= 'a' && *cp <= 'f')
182 val = val * 16 + *cp - 'a' + 10;
183 else if (*cp >= 'A' && *cp <= 'F')
184 val = val * 16 + *cp - 'A' + 10;
185 else if (*cp >= '0' && *cp <= '9')
186 val = val * 16 + *cp - '0';
187 else
188 break;
189 cp++;
191 return val;
193 #endif /* MOUNTED_GETMNTENT1 */
195 #ifdef MOUNTED_GETMNTINFO
197 #ifndef HAVE_STRUCT_STATFS_F_FSTYPENAME
198 static char *
199 fstype_to_string (short t)
201 switch (t)
203 #ifdef MOUNT_PC
204 case MOUNT_PC:
205 return "pc";
206 #endif
207 #ifdef MOUNT_MFS
208 case MOUNT_MFS:
209 return "mfs";
210 #endif
211 #ifdef MOUNT_LO
212 case MOUNT_LO:
213 return "lo";
214 #endif
215 #ifdef MOUNT_TFS
216 case MOUNT_TFS:
217 return "tfs";
218 #endif
219 #ifdef MOUNT_TMP
220 case MOUNT_TMP:
221 return "tmp";
222 #endif
223 #ifdef MOUNT_UFS
224 case MOUNT_UFS:
225 return "ufs";
226 #endif
227 #ifdef MOUNT_NFS
228 case MOUNT_NFS:
229 return "nfs";
230 #endif
231 #ifdef MOUNT_MSDOS
232 case MOUNT_MSDOS:
233 return "msdos";
234 #endif
235 #ifdef MOUNT_LFS
236 case MOUNT_LFS:
237 return "lfs";
238 #endif
239 #ifdef MOUNT_LOFS
240 case MOUNT_LOFS:
241 return "lofs";
242 #endif
243 #ifdef MOUNT_FDESC
244 case MOUNT_FDESC:
245 return "fdesc";
246 #endif
247 #ifdef MOUNT_PORTAL
248 case MOUNT_PORTAL:
249 return "portal";
250 #endif
251 #ifdef MOUNT_NULL
252 case MOUNT_NULL:
253 return "null";
254 #endif
255 #ifdef MOUNT_UMAP
256 case MOUNT_UMAP:
257 return "umap";
258 #endif
259 #ifdef MOUNT_KERNFS
260 case MOUNT_KERNFS:
261 return "kernfs";
262 #endif
263 #ifdef MOUNT_PROCFS
264 case MOUNT_PROCFS:
265 return "procfs";
266 #endif
267 #ifdef MOUNT_AFS
268 case MOUNT_AFS:
269 return "afs";
270 #endif
271 #ifdef MOUNT_CD9660
272 case MOUNT_CD9660:
273 return "cd9660";
274 #endif
275 #ifdef MOUNT_UNION
276 case MOUNT_UNION:
277 return "union";
278 #endif
279 #ifdef MOUNT_DEVFS
280 case MOUNT_DEVFS:
281 return "devfs";
282 #endif
283 #ifdef MOUNT_EXT2FS
284 case MOUNT_EXT2FS:
285 return "ext2fs";
286 #endif
287 default:
288 return "?";
291 #endif /* ! HAVE_STRUCT_STATFS_F_FSTYPENAME */
293 #endif /* MOUNTED_GETMNTINFO */
295 #ifdef MOUNTED_VMOUNT /* AIX. */
296 static char *
297 fstype_to_string (int t)
299 struct vfs_ent *e;
301 e = getvfsbytype (t);
302 if (!e || !e->vfsent_name)
303 return "none";
304 else
305 return e->vfsent_name;
307 #endif /* MOUNTED_VMOUNT */
309 /* Return a list of the currently mounted filesystems, or NULL on error.
310 Add each entry to the tail of the list so that they stay in order.
311 If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in
312 the returned list are valid. Otherwise, they might not be.
313 If ALL_FS is zero, do not return entries for filesystems that
314 are automounter (dummy) entries. */
316 static struct mount_entry *
317 read_filesystem_list (int need_fs_type, int all_fs)
319 struct mount_entry *mlist;
320 struct mount_entry *me;
321 struct mount_entry *mtail;
323 (void) need_fs_type;
324 (void) all_fs;
326 /* Start the list off with a dummy entry. */
327 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
328 me->me_next = NULL;
329 mlist = mtail = me;
331 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
332 #ifdef MOUNTED
334 struct mntent *mnt;
335 FILE *fp;
336 const char *devopt;
338 fp = setmntent (MOUNTED, "r");
339 if (fp == NULL)
340 return NULL;
342 while ((mnt = getmntent (fp)))
344 if (!all_fs && (!strcmp (mnt->mnt_type, "ignore") || !strcmp (mnt->mnt_type, "auto")))
345 continue;
347 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
348 me->me_devname = strdup (mnt->mnt_fsname);
349 me->me_mountdir = strdup (mnt->mnt_dir);
350 me->me_type = strdup (mnt->mnt_type);
351 devopt = strstr (mnt->mnt_opts, "dev=");
352 if (devopt)
354 if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X'))
355 me->me_dev = xatoi (devopt + 6);
356 else
357 me->me_dev = xatoi (devopt + 4);
359 else
360 me->me_dev = -1; /* Magic; means not known yet. */
361 me->me_next = NULL;
363 /* Add to the linked list. */
364 mtail->me_next = me;
365 mtail = me;
368 if (endmntent (fp) == 0)
369 return NULL;
371 #endif /* MOUNTED */
372 #endif /* MOUNTED_GETMNTENT1 */
374 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
376 struct statfs *fsp;
377 int entries;
379 entries = getmntinfo (&fsp, MNT_NOWAIT);
380 if (entries < 0)
381 return NULL;
382 while (entries-- > 0)
384 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
385 me->me_devname = strdup (fsp->f_mntfromname);
386 me->me_mountdir = strdup (fsp->f_mntonname);
387 #ifdef HAVE_STRUCT_STATFS_F_FSTYPENAME
388 me->me_type = strdup (fsp->f_fstypename);
389 #else
390 me->me_type = fstype_to_string (fsp->f_type);
391 #endif
392 me->me_dev = -1; /* Magic; means not known yet. */
393 me->me_next = NULL;
395 /* Add to the linked list. */
396 mtail->me_next = me;
397 mtail = me;
398 fsp++;
401 #endif /* MOUNTED_GETMNTINFO */
403 #ifdef MOUNTED_GETMNTINFO2 /* NetBSD 3.0. */
405 struct statvfs *fsp;
406 int entries;
408 entries = getmntinfo (&fsp, MNT_NOWAIT);
409 if (entries < 0)
410 return NULL;
411 for (; entries-- > 0; fsp++)
413 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
414 me->me_devname = strdup (fsp->f_mntfromname);
415 me->me_mountdir = strdup (fsp->f_mntonname);
416 me->me_type = strdup (fsp->f_fstypename);
417 me->me_dev = (dev_t) - 1; /* Magic; means not known yet. */
419 /* Add to the linked list. */
420 mtail->me_next = me;
421 mtail = me;
424 #endif /* MOUNTED_GETMNTINFO2 */
426 #ifdef MOUNTED_GETMNT /* Ultrix. */
428 int offset = 0;
429 int val;
430 struct fs_data fsd;
432 while ((val = getmnt (&offset, &fsd, sizeof (fsd), NOSTAT_MANY, NULL)) > 0)
434 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
435 me->me_devname = strdup (fsd.fd_req.devname);
436 me->me_mountdir = strdup (fsd.fd_req.path);
437 me->me_type = gt_names[fsd.fd_req.fstype];
438 me->me_dev = fsd.fd_req.dev;
439 me->me_next = NULL;
441 /* Add to the linked list. */
442 mtail->me_next = me;
443 mtail = me;
445 if (val < 0)
446 return NULL;
448 #endif /* MOUNTED_GETMNT */
450 #ifdef MOUNTED_GETFSSTAT /* __alpha running OSF_1 */
452 int numsys, counter, bufsize;
453 struct statfs *stats;
455 numsys = getfsstat ((struct statfs *) 0, 0L, MNT_WAIT);
456 if (numsys < 0)
457 return (NULL);
459 bufsize = (1 + numsys) * sizeof (struct statfs);
460 stats = (struct statfs *) malloc (bufsize);
461 numsys = getfsstat (stats, bufsize, MNT_WAIT);
463 if (numsys < 0)
465 free (stats);
466 return (NULL);
468 for (counter = 0; counter < numsys; counter++)
470 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
471 me->me_devname = strdup (stats[counter].f_mntfromname);
472 me->me_mountdir = strdup (stats[counter].f_mntonname);
473 me->me_type = mnt_names[stats[counter].f_type];
474 me->me_dev = -1; /* Magic; means not known yet. */
475 me->me_next = NULL;
477 /* Add to the linked list. */
478 mtail->me_next = me;
479 mtail = me;
482 free (stats);
484 #endif /* MOUNTED_GETFSSTAT */
486 #if defined (MOUNTED_FREAD) || defined (MOUNTED_FREAD_FSTYP) /* SVR[23]. */
488 struct mnttab mnt;
489 char *table = "/etc/mnttab";
490 FILE *fp;
492 fp = fopen (table, "r");
493 if (fp == NULL)
494 return NULL;
496 while (fread (&mnt, sizeof mnt, 1, fp) > 0)
498 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
499 #ifdef GETFSTYP /* SVR3. */
500 me->me_devname = strdup (mnt.mt_dev);
501 #else
502 me->me_devname = malloc (strlen (mnt.mt_dev) + 6);
503 strcpy (me->me_devname, "/dev/");
504 strcpy (me->me_devname + 5, mnt.mt_dev);
505 #endif
506 me->me_mountdir = strdup (mnt.mt_filsys);
507 me->me_dev = -1; /* Magic; means not known yet. */
508 me->me_type = "";
509 #ifdef GETFSTYP /* SVR3. */
510 if (need_fs_type)
512 struct statfs fsd;
513 char typebuf[FSTYPSZ];
515 if (statfs (me->me_mountdir, &fsd, sizeof fsd, 0) != -1
516 && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1)
517 me->me_type = strdup (typebuf);
519 #endif
520 me->me_next = NULL;
522 /* Add to the linked list. */
523 mtail->me_next = me;
524 mtail = me;
527 if (fclose (fp) == EOF)
528 return NULL;
530 #endif /* MOUNTED_FREAD || MOUNTED_FREAD_FSTYP */
532 #ifdef MOUNTED_GETMNTTBL /* DolphinOS goes it's own way */
534 struct mntent **mnttbl = getmnttbl (), **ent;
535 for (ent = mnttbl; *ent; ent++)
537 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
538 me->me_devname = strdup ((*ent)->mt_resource);
539 me->me_mountdir = strdup ((*ent)->mt_directory);
540 me->me_type = strdup ((*ent)->mt_fstype);
541 me->me_dev = -1; /* Magic; means not known yet. */
542 me->me_next = NULL;
544 /* Add to the linked list. */
545 mtail->me_next = me;
546 mtail = me;
548 endmnttbl ();
550 #endif /* MOUNTED_GETMNTTBL */
552 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
554 struct mnttab mnt;
555 char *table = MNTTAB;
556 FILE *fp;
557 int ret;
559 fp = fopen (table, "r");
560 if (fp == NULL)
561 return NULL;
563 while ((ret = getmntent (fp, &mnt)) == 0)
565 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
566 me->me_devname = strdup (mnt.mnt_special);
567 me->me_mountdir = strdup (mnt.mnt_mountp);
568 me->me_type = strdup (mnt.mnt_fstype);
569 me->me_dev = -1; /* Magic; means not known yet. */
570 me->me_next = NULL;
571 /* Add to the linked list. */
572 mtail->me_next = me;
573 mtail = me;
576 if (ret > 0)
577 return NULL;
578 if (fclose (fp) == EOF)
579 return NULL;
581 #endif /* MOUNTED_GETMNTENT2 */
583 #ifdef MOUNTED_VMOUNT /* AIX. */
585 int bufsize;
586 char *entries, *thisent;
587 struct vmount *vmp;
589 /* Ask how many bytes to allocate for the mounted filesystem info. */
590 mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize);
591 entries = malloc (bufsize);
593 /* Get the list of mounted filesystems. */
594 mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries);
596 for (thisent = entries; thisent < entries + bufsize; thisent += vmp->vmt_length)
598 vmp = (struct vmount *) thisent;
599 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
600 if (vmp->vmt_flags & MNT_REMOTE)
602 char *host, *path;
604 /* Prepend the remote pathname. */
605 host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off;
606 path = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off;
607 me->me_devname = malloc (strlen (host) + strlen (path) + 2);
608 strcpy (me->me_devname, host);
609 strcat (me->me_devname, ":");
610 strcat (me->me_devname, path);
612 else
614 me->me_devname = strdup (thisent + vmp->vmt_data[VMT_OBJECT].vmt_off);
616 me->me_mountdir = strdup (thisent + vmp->vmt_data[VMT_STUB].vmt_off);
617 me->me_type = strdup (fstype_to_string (vmp->vmt_gfstype));
618 me->me_dev = -1; /* vmt_fsid might be the info we want. */
619 me->me_next = NULL;
621 /* Add to the linked list. */
622 mtail->me_next = me;
623 mtail = me;
625 free (entries);
627 #endif /* MOUNTED_VMOUNT */
629 /* Free the dummy head. */
630 me = mlist;
631 mlist = mlist->me_next;
632 free (me);
633 return mlist;
635 #endif /* HAVE_INFOMOUNT_LIST */
637 #ifdef HAVE_INFOMOUNT_QNX
639 ** QNX has no [gs]etmnt*(), [gs]etfs*(), or /etc/mnttab, but can do
640 ** this via the following code.
641 ** Note that, as this is based on CWD, it only fills one mount_entry
642 ** structure. See my_statfs() in utilunix.c for the "other side" of
643 ** this hack.
646 static struct mount_entry *
647 read_filesystem_list (int need_fs_type, int all_fs)
649 struct _disk_entry de;
650 struct statfs fs;
651 int i, fd;
652 char *tp, dev[_POSIX_NAME_MAX], dir[_POSIX_PATH_MAX];
654 static struct mount_entry *me = NULL;
656 if (me)
658 if (me->me_devname)
659 free (me->me_devname);
660 if (me->me_mountdir)
661 free (me->me_mountdir);
662 if (me->me_type)
663 free (me->me_type);
665 else
666 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
668 if (!getcwd (dir, _POSIX_PATH_MAX))
669 return (NULL);
671 if ((fd = open (dir, O_RDONLY)) == -1)
672 return (NULL);
674 i = disk_get_entry (fd, &de);
676 close (fd);
678 if (i == -1)
679 return (NULL);
681 switch (de.disk_type)
683 case _UNMOUNTED:
684 tp = "unmounted";
685 break;
686 case _FLOPPY:
687 tp = "Floppy";
688 break;
689 case _HARD:
690 tp = "Hard";
691 break;
692 case _RAMDISK:
693 tp = "Ram";
694 break;
695 case _REMOVABLE:
696 tp = "Removable";
697 break;
698 case _TAPE:
699 tp = "Tape";
700 break;
701 case _CDROM:
702 tp = "CDROM";
703 break;
704 default:
705 tp = "unknown";
708 if (fsys_get_mount_dev (dir, &dev) == -1)
709 return (NULL);
711 if (fsys_get_mount_pt (dev, &dir) == -1)
712 return (NULL);
714 me->me_devname = strdup (dev);
715 me->me_mountdir = strdup (dir);
716 me->me_type = strdup (tp);
717 me->me_dev = de.disk_type;
719 #ifdef DEBUG
720 fprintf (stderr,
721 "disk_get_entry():\n\tdisk_type=%d (%s)\n\tdriver_name='%-*.*s'\n\tdisk_drv=%d\n",
722 de.disk_type, tp, _DRIVER_NAME_LEN, _DRIVER_NAME_LEN, de.driver_name, de.disk_drv);
723 fprintf (stderr, "fsys_get_mount_dev():\n\tdevice='%s'\n", dev);
724 fprintf (stderr, "fsys_get_mount_pt():\n\tmount point='%s'\n", dir);
725 #endif /* DEBUG */
727 return (me);
729 #endif /* HAVE_INFOMOUNT_QNX */
731 void
732 free_my_statfs (void)
734 #ifdef HAVE_INFOMOUNT_LIST
735 while (mount_list)
737 struct mount_entry *next = mount_list->me_next;
738 free_mount_entry (mount_list);
739 mount_list = next;
741 mount_list = NULL;
742 #endif /* HAVE_INFOMOUNT_LIST */
746 void
747 init_my_statfs (void)
749 #ifdef HAVE_INFOMOUNT_LIST
750 free_my_statfs ();
751 mount_list = read_filesystem_list (1, 1);
752 #endif /* HAVE_INFOMOUNT_LIST */
755 void
756 my_statfs (struct my_statfs *myfs_stats, const char *path)
758 #ifdef HAVE_INFOMOUNT_LIST
759 int i, len = 0;
760 struct mount_entry *entry = NULL;
761 struct mount_entry *temp = mount_list;
762 struct fs_usage fs_use;
764 while (temp)
766 i = strlen (temp->me_mountdir);
767 if (i > len && (strncmp (path, temp->me_mountdir, i) == 0))
768 if (!entry || (path[i] == PATH_SEP || path[i] == 0))
770 len = i;
771 entry = temp;
773 temp = temp->me_next;
776 if (entry)
778 memset (&fs_use, 0, sizeof (struct fs_usage));
779 get_fs_usage (entry->me_mountdir, &fs_use);
781 myfs_stats->type = entry->me_dev;
782 myfs_stats->typename = entry->me_type;
783 myfs_stats->mpoint = entry->me_mountdir;
784 myfs_stats->device = entry->me_devname;
785 myfs_stats->avail = getuid ()? fs_use.fsu_bavail / 2 : fs_use.fsu_bfree / 2;
786 myfs_stats->total = fs_use.fsu_blocks / 2;
787 myfs_stats->nfree = fs_use.fsu_ffree;
788 myfs_stats->nodes = fs_use.fsu_files;
790 else
791 #endif /* HAVE_INFOMOUNT_LIST */
793 #ifdef HAVE_INFOMOUNT_QNX
795 ** This is the "other side" of the hack to read_filesystem_list() in
796 ** mountlist.c.
797 ** It's not the most efficient approach, but consumes less memory. It
798 ** also accomodates QNX's ability to mount filesystems on the fly.
800 struct mount_entry *entry;
801 struct fs_usage fs_use;
803 if ((entry = read_filesystem_list (0, 0)) != NULL)
805 get_fs_usage (entry->me_mountdir, &fs_use);
807 myfs_stats->type = entry->me_dev;
808 myfs_stats->typename = entry->me_type;
809 myfs_stats->mpoint = entry->me_mountdir;
810 myfs_stats->device = entry->me_devname;
812 myfs_stats->avail = fs_use.fsu_bfree / 2;
813 myfs_stats->total = fs_use.fsu_blocks / 2;
814 myfs_stats->nfree = fs_use.fsu_ffree;
815 myfs_stats->nodes = fs_use.fsu_files;
817 else
818 #endif /* HAVE_INFOMOUNT_QNX */
820 myfs_stats->type = 0;
821 myfs_stats->mpoint = "unknown";
822 myfs_stats->device = "unknown";
823 myfs_stats->avail = 0;
824 myfs_stats->total = 0;
825 myfs_stats->nfree = 0;
826 myfs_stats->nodes = 0;
830 #ifdef HAVE_INFOMOUNT
832 /* Return the number of TOSIZE-byte blocks used by
833 BLOCKS FROMSIZE-byte blocks, rounding away from zero.
834 TOSIZE must be positive. Return -1 if FROMSIZE is not positive. */
836 static fsblkcnt_t
837 fs_adjust_blocks (fsblkcnt_t blocks, int fromsize, int tosize)
839 if (tosize <= 0)
840 abort ();
841 if (fromsize <= 0)
842 return -1;
844 if (fromsize == tosize) /* E.g., from 512 to 512. */
845 return blocks;
846 else if (fromsize > tosize) /* E.g., from 2048 to 512. */
847 return blocks * (fromsize / tosize);
848 else /* E.g., from 256 to 512. */
849 return blocks / (tosize / fromsize);
852 #if defined(_AIX) && defined(_I386)
853 /* AIX PS/2 does not supply statfs. */
854 static int
855 aix_statfs (char *path, struct statfs *fsb)
857 struct stat stats;
858 struct dustat fsd;
860 if (stat (path, &stats))
861 return -1;
862 if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
863 return -1;
864 fsb->f_type = 0;
865 fsb->f_bsize = fsd.du_bsize;
866 fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
867 fsb->f_bfree = fsd.du_tfree;
868 fsb->f_bavail = fsd.du_tfree;
869 fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb;
870 fsb->f_ffree = fsd.du_tinode;
871 fsb->f_fsid.val[0] = fsd.du_site;
872 fsb->f_fsid.val[1] = fsd.du_pckno;
873 return 0;
876 #define statfs(path,fsb) aix_statfs(path,fsb)
877 #endif /* _AIX && _I386 */
879 /* Fill in the fields of FSP with information about space usage for
880 the filesystem on which PATH resides.
881 Return 0 if successful, -1 if not. */
883 static int
884 get_fs_usage (char *path, struct fs_usage *fsp)
886 #ifdef STAT_STATFS3_OSF1
887 struct statfs fsd;
889 if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
890 return -1;
891 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
892 #endif /* STAT_STATFS3_OSF1 */
894 #ifdef STAT_STATFS2_FS_DATA /* Ultrix. */
895 struct fs_data fsd;
897 if (statfs (path, &fsd) != 1)
898 return -1;
899 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), 1024, 512)
900 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.fd_req.btot);
901 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.fd_req.bfree);
902 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.fd_req.bfreen);
903 fsp->fsu_files = fsd.fd_req.gtot;
904 fsp->fsu_ffree = fsd.fd_req.gfree;
905 #endif
907 #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX. */
908 struct statfs fsd;
910 if (statfs (path, &fsd) < 0)
911 return -1;
912 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
913 #endif
915 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD. */
916 struct statfs fsd;
918 if (statfs (path, &fsd) < 0)
919 return -1;
920 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
921 #endif
923 #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX. */
924 struct statfs fsd;
926 if (statfs (path, &fsd, sizeof fsd, 0) < 0)
927 return -1;
928 /* Empirically, the block counts on most SVR3 and SVR3-derived
929 systems seem to always be in terms of 512-byte blocks,
930 no matter what value f_bsize has. */
931 #if _AIX
932 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
933 #else
934 #define CONVERT_BLOCKS(b) (b)
935 #ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx. */
936 #ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
937 #define f_bavail f_bfree
938 #endif
939 #endif
940 #endif
941 #endif
943 #ifdef STAT_STATVFS /* SVR4. */
944 struct statvfs fsd;
946 if (statvfs (path, &fsd) < 0)
947 return -1;
948 /* f_frsize isn't guaranteed to be supported. */
949 #define CONVERT_BLOCKS(b) \
950 fs_adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
951 #endif
953 #if defined(CONVERT_BLOCKS) && !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS) /* !Ultrix && !SVR2. */
954 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.f_blocks);
955 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.f_bfree);
956 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.f_bavail);
957 fsp->fsu_files = fsd.f_files;
958 fsp->fsu_ffree = fsd.f_ffree;
959 #endif
961 return 0;
964 #endif /* HAVE_INFOMOUNT */