Applied MC indentation policy.
[kaloumi3.git] / src / mountlist.c
blob86dd361a0fd5a96ef188e97022fcd4c5e7f3a633
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 free_mount_entry (struct mount_entry *me)
156 if (!me)
157 return;
158 if (me->me_devname)
159 free (me->me_devname);
160 if (me->me_mountdir)
161 free (me->me_mountdir);
162 if (me->me_type)
163 free (me->me_type);
164 free (me);
167 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
168 /* Return the value of the hexadecimal number represented by CP.
169 No prefix (like '0x') or suffix (like 'h') is expected to be
170 part of CP. */
172 static int xatoi (const char *cp)
174 int val;
176 val = 0;
177 while (*cp) {
178 if (*cp >= 'a' && *cp <= 'f')
179 val = val * 16 + *cp - 'a' + 10;
180 else if (*cp >= 'A' && *cp <= 'F')
181 val = val * 16 + *cp - 'A' + 10;
182 else if (*cp >= '0' && *cp <= '9')
183 val = val * 16 + *cp - '0';
184 else
185 break;
186 cp++;
188 return val;
190 #endif /* MOUNTED_GETMNTENT1 */
192 #ifdef MOUNTED_GETMNTINFO
194 #ifndef HAVE_STRUCT_STATFS_F_FSTYPENAME
195 static char *fstype_to_string (short t)
197 switch (t) {
198 #ifdef MOUNT_PC
199 case MOUNT_PC:
200 return "pc";
201 #endif
202 #ifdef MOUNT_MFS
203 case MOUNT_MFS:
204 return "mfs";
205 #endif
206 #ifdef MOUNT_LO
207 case MOUNT_LO:
208 return "lo";
209 #endif
210 #ifdef MOUNT_TFS
211 case MOUNT_TFS:
212 return "tfs";
213 #endif
214 #ifdef MOUNT_TMP
215 case MOUNT_TMP:
216 return "tmp";
217 #endif
218 #ifdef MOUNT_UFS
219 case MOUNT_UFS:
220 return "ufs" ;
221 #endif
222 #ifdef MOUNT_NFS
223 case MOUNT_NFS:
224 return "nfs" ;
225 #endif
226 #ifdef MOUNT_MSDOS
227 case MOUNT_MSDOS:
228 return "msdos" ;
229 #endif
230 #ifdef MOUNT_LFS
231 case MOUNT_LFS:
232 return "lfs" ;
233 #endif
234 #ifdef MOUNT_LOFS
235 case MOUNT_LOFS:
236 return "lofs" ;
237 #endif
238 #ifdef MOUNT_FDESC
239 case MOUNT_FDESC:
240 return "fdesc" ;
241 #endif
242 #ifdef MOUNT_PORTAL
243 case MOUNT_PORTAL:
244 return "portal" ;
245 #endif
246 #ifdef MOUNT_NULL
247 case MOUNT_NULL:
248 return "null" ;
249 #endif
250 #ifdef MOUNT_UMAP
251 case MOUNT_UMAP:
252 return "umap" ;
253 #endif
254 #ifdef MOUNT_KERNFS
255 case MOUNT_KERNFS:
256 return "kernfs" ;
257 #endif
258 #ifdef MOUNT_PROCFS
259 case MOUNT_PROCFS:
260 return "procfs" ;
261 #endif
262 #ifdef MOUNT_AFS
263 case MOUNT_AFS:
264 return "afs" ;
265 #endif
266 #ifdef MOUNT_CD9660
267 case MOUNT_CD9660:
268 return "cd9660" ;
269 #endif
270 #ifdef MOUNT_UNION
271 case MOUNT_UNION:
272 return "union" ;
273 #endif
274 #ifdef MOUNT_DEVFS
275 case MOUNT_DEVFS:
276 return "devfs" ;
277 #endif
278 #ifdef MOUNT_EXT2FS
279 case MOUNT_EXT2FS:
280 return "ext2fs" ;
281 #endif
282 default:
283 return "?";
286 #endif /* ! HAVE_STRUCT_STATFS_F_FSTYPENAME */
288 #endif /* MOUNTED_GETMNTINFO */
290 #ifdef MOUNTED_VMOUNT /* AIX. */
291 static char *
292 fstype_to_string (int t)
294 struct vfs_ent *e;
296 e = getvfsbytype (t);
297 if (!e || !e->vfsent_name)
298 return "none";
299 else
300 return e->vfsent_name;
302 #endif /* MOUNTED_VMOUNT */
304 /* Return a list of the currently mounted filesystems, or NULL on error.
305 Add each entry to the tail of the list so that they stay in order.
306 If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in
307 the returned list are valid. Otherwise, they might not be.
308 If ALL_FS is zero, do not return entries for filesystems that
309 are automounter (dummy) entries. */
311 static struct mount_entry *
312 read_filesystem_list (int need_fs_type, int all_fs)
314 struct mount_entry *mlist;
315 struct mount_entry *me;
316 struct mount_entry *mtail;
318 (void) need_fs_type;
319 (void) all_fs;
321 /* Start the list off with a dummy entry. */
322 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
323 me->me_next = NULL;
324 mlist = mtail = me;
326 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
327 #ifdef MOUNTED
329 struct mntent *mnt;
330 FILE *fp;
331 const char *devopt;
333 fp = setmntent (MOUNTED, "r");
334 if (fp == NULL)
335 return NULL;
337 while ((mnt = getmntent (fp))) {
338 if (!all_fs && (!strcmp (mnt->mnt_type, "ignore")
339 || !strcmp (mnt->mnt_type, "auto")))
340 continue;
342 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
343 me->me_devname = strdup (mnt->mnt_fsname);
344 me->me_mountdir = strdup (mnt->mnt_dir);
345 me->me_type = strdup (mnt->mnt_type);
346 devopt = strstr (mnt->mnt_opts, "dev=");
347 if (devopt) {
348 if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X'))
349 me->me_dev = xatoi (devopt + 6);
350 else
351 me->me_dev = xatoi (devopt + 4);
352 } else
353 me->me_dev = -1; /* Magic; means not known yet. */
354 me->me_next = NULL;
356 /* Add to the linked list. */
357 mtail->me_next = me;
358 mtail = me;
361 if (endmntent (fp) == 0)
362 return NULL;
364 #endif /* MOUNTED */
365 #endif /* MOUNTED_GETMNTENT1 */
367 #ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */
369 struct statfs *fsp;
370 int entries;
372 entries = getmntinfo (&fsp, MNT_NOWAIT);
373 if (entries < 0)
374 return NULL;
375 while (entries-- > 0) {
376 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
377 me->me_devname = strdup (fsp->f_mntfromname);
378 me->me_mountdir = strdup (fsp->f_mntonname);
379 #ifdef HAVE_STRUCT_STATFS_F_FSTYPENAME
380 me->me_type = strdup (fsp->f_fstypename);
381 #else
382 me->me_type = fstype_to_string (fsp->f_type);
383 #endif
384 me->me_dev = -1; /* Magic; means not known yet. */
385 me->me_next = NULL;
387 /* Add to the linked list. */
388 mtail->me_next = me;
389 mtail = me;
390 fsp++;
393 #endif /* MOUNTED_GETMNTINFO */
395 #ifdef MOUNTED_GETMNTINFO2 /* NetBSD 3.0. */
397 struct statvfs *fsp;
398 int entries;
400 entries = getmntinfo (&fsp, MNT_NOWAIT);
401 if (entries < 0)
402 return NULL;
403 for (; entries-- > 0; fsp++) {
404 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
405 me->me_devname = strdup (fsp->f_mntfromname);
406 me->me_mountdir = strdup (fsp->f_mntonname);
407 me->me_type = strdup (fsp->f_fstypename);
408 me->me_dev = (dev_t) -1; /* Magic; means not known yet. */
410 /* Add to the linked list. */
411 mtail->me_next = me;
412 mtail = me;
415 #endif /* MOUNTED_GETMNTINFO2 */
417 #ifdef MOUNTED_GETMNT /* Ultrix. */
419 int offset = 0;
420 int val;
421 struct fs_data fsd;
423 while ((val = getmnt (&offset, &fsd, sizeof (fsd), NOSTAT_MANY,
424 NULL)) > 0) {
425 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
426 me->me_devname = strdup (fsd.fd_req.devname);
427 me->me_mountdir = strdup (fsd.fd_req.path);
428 me->me_type = gt_names[fsd.fd_req.fstype];
429 me->me_dev = fsd.fd_req.dev;
430 me->me_next = NULL;
432 /* Add to the linked list. */
433 mtail->me_next = me;
434 mtail = me;
436 if (val < 0)
437 return NULL;
439 #endif /* MOUNTED_GETMNT */
441 #ifdef MOUNTED_GETFSSTAT /* __alpha running OSF_1 */
443 int numsys, counter, bufsize;
444 struct statfs *stats;
446 numsys = getfsstat ((struct statfs *) 0, 0L, MNT_WAIT);
447 if (numsys < 0)
448 return (NULL);
450 bufsize = (1 + numsys) * sizeof (struct statfs);
451 stats = (struct statfs *) malloc (bufsize);
452 numsys = getfsstat (stats, bufsize, MNT_WAIT);
454 if (numsys < 0) {
455 free (stats);
456 return (NULL);
458 for (counter = 0; counter < numsys; counter++) {
459 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
460 me->me_devname = strdup (stats[counter].f_mntfromname);
461 me->me_mountdir = strdup (stats[counter].f_mntonname);
462 me->me_type = mnt_names[stats[counter].f_type];
463 me->me_dev = -1; /* Magic; means not known yet. */
464 me->me_next = NULL;
466 /* Add to the linked list. */
467 mtail->me_next = me;
468 mtail = me;
471 free (stats);
473 #endif /* MOUNTED_GETFSSTAT */
475 #if defined (MOUNTED_FREAD) || defined (MOUNTED_FREAD_FSTYP) /* SVR[23]. */
477 struct mnttab mnt;
478 char *table = "/etc/mnttab";
479 FILE *fp;
481 fp = fopen (table, "r");
482 if (fp == NULL)
483 return NULL;
485 while (fread (&mnt, sizeof mnt, 1, fp) > 0) {
486 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
487 #ifdef GETFSTYP /* SVR3. */
488 me->me_devname = strdup (mnt.mt_dev);
489 #else
490 me->me_devname = malloc (strlen (mnt.mt_dev) + 6);
491 strcpy (me->me_devname, "/dev/");
492 strcpy (me->me_devname + 5, mnt.mt_dev);
493 #endif
494 me->me_mountdir = strdup (mnt.mt_filsys);
495 me->me_dev = -1; /* Magic; means not known yet. */
496 me->me_type = "";
497 #ifdef GETFSTYP /* SVR3. */
498 if (need_fs_type) {
499 struct statfs fsd;
500 char typebuf[FSTYPSZ];
502 if (statfs (me->me_mountdir, &fsd, sizeof fsd, 0) != -1
503 && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1)
504 me->me_type = strdup (typebuf);
506 #endif
507 me->me_next = NULL;
509 /* Add to the linked list. */
510 mtail->me_next = me;
511 mtail = me;
514 if (fclose (fp) == EOF)
515 return NULL;
517 #endif /* MOUNTED_FREAD || MOUNTED_FREAD_FSTYP */
519 #ifdef MOUNTED_GETMNTTBL /* DolphinOS goes it's own way */
521 struct mntent **mnttbl = getmnttbl (), **ent;
522 for (ent = mnttbl; *ent; ent++) {
523 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
524 me->me_devname = strdup ((*ent)->mt_resource);
525 me->me_mountdir = strdup ((*ent)->mt_directory);
526 me->me_type = strdup ((*ent)->mt_fstype);
527 me->me_dev = -1; /* Magic; means not known yet. */
528 me->me_next = NULL;
530 /* Add to the linked list. */
531 mtail->me_next = me;
532 mtail = me;
534 endmnttbl ();
536 #endif /* MOUNTED_GETMNTTBL */
538 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
540 struct mnttab mnt;
541 char *table = MNTTAB;
542 FILE *fp;
543 int ret;
545 fp = fopen (table, "r");
546 if (fp == NULL)
547 return NULL;
549 while ((ret = getmntent (fp, &mnt)) == 0) {
550 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
551 me->me_devname = strdup (mnt.mnt_special);
552 me->me_mountdir = strdup (mnt.mnt_mountp);
553 me->me_type = strdup (mnt.mnt_fstype);
554 me->me_dev = -1; /* Magic; means not known yet. */
555 me->me_next = NULL;
556 /* Add to the linked list. */
557 mtail->me_next = me;
558 mtail = me;
561 if (ret > 0)
562 return NULL;
563 if (fclose (fp) == EOF)
564 return NULL;
566 #endif /* MOUNTED_GETMNTENT2 */
568 #ifdef MOUNTED_VMOUNT /* AIX. */
570 int bufsize;
571 char *entries, *thisent;
572 struct vmount *vmp;
574 /* Ask how many bytes to allocate for the mounted filesystem info. */
575 mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize);
576 entries = malloc (bufsize);
578 /* Get the list of mounted filesystems. */
579 mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries);
581 for (thisent = entries; thisent < entries + bufsize;
582 thisent += vmp->vmt_length) {
583 vmp = (struct vmount *) thisent;
584 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
585 if (vmp->vmt_flags & MNT_REMOTE) {
586 char *host, *path;
588 /* Prepend the remote pathname. */
589 host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off;
590 path = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off;
591 me->me_devname = malloc (strlen (host) + strlen (path) + 2);
592 strcpy (me->me_devname, host);
593 strcat (me->me_devname, ":");
594 strcat (me->me_devname, path);
595 } else {
596 me->me_devname = strdup (thisent +
597 vmp->vmt_data[VMT_OBJECT].vmt_off);
599 me->me_mountdir = strdup (thisent + vmp->vmt_data[VMT_STUB].vmt_off);
600 me->me_type = strdup (fstype_to_string (vmp->vmt_gfstype));
601 me->me_dev = -1; /* vmt_fsid might be the info we want. */
602 me->me_next = NULL;
604 /* Add to the linked list. */
605 mtail->me_next = me;
606 mtail = me;
608 free (entries);
610 #endif /* MOUNTED_VMOUNT */
612 /* Free the dummy head. */
613 me = mlist;
614 mlist = mlist->me_next;
615 free (me);
616 return mlist;
618 #endif /* HAVE_INFOMOUNT_LIST */
620 #ifdef HAVE_INFOMOUNT_QNX
622 ** QNX has no [gs]etmnt*(), [gs]etfs*(), or /etc/mnttab, but can do
623 ** this via the following code.
624 ** Note that, as this is based on CWD, it only fills one mount_entry
625 ** structure. See my_statfs() in utilunix.c for the "other side" of
626 ** this hack.
629 static struct mount_entry *
630 read_filesystem_list(int need_fs_type, int all_fs)
632 struct _disk_entry de;
633 struct statfs fs;
634 int i, fd;
635 char *tp, dev[_POSIX_NAME_MAX], dir[_POSIX_PATH_MAX];
637 static struct mount_entry *me = NULL;
639 if (me)
641 if (me->me_devname) free(me->me_devname);
642 if (me->me_mountdir) free(me->me_mountdir);
643 if (me->me_type) free(me->me_type);
645 else
646 me = (struct mount_entry *)malloc(sizeof(struct mount_entry));
648 if (!getcwd(dir, _POSIX_PATH_MAX)) return (NULL);
650 if ((fd = open(dir, O_RDONLY)) == -1) return (NULL);
652 i = disk_get_entry(fd, &de);
654 close(fd);
656 if (i == -1) return (NULL);
658 switch (de.disk_type)
660 case _UNMOUNTED: tp = "unmounted"; break;
661 case _FLOPPY: tp = "Floppy"; break;
662 case _HARD: tp = "Hard"; break;
663 case _RAMDISK: tp = "Ram"; break;
664 case _REMOVABLE: tp = "Removable"; break;
665 case _TAPE: tp = "Tape"; break;
666 case _CDROM: tp = "CDROM"; break;
667 default: tp = "unknown";
670 if (fsys_get_mount_dev(dir, &dev) == -1) return (NULL);
672 if (fsys_get_mount_pt(dev, &dir) == -1) return (NULL);
674 me->me_devname = strdup(dev);
675 me->me_mountdir = strdup(dir);
676 me->me_type = strdup(tp);
677 me->me_dev = de.disk_type;
679 #ifdef DEBUG
680 fprintf(stderr, "disk_get_entry():\n\tdisk_type=%d (%s)\n\tdriver_name='%-*.*s'\n\tdisk_drv=%d\n",
681 de.disk_type, tp, _DRIVER_NAME_LEN, _DRIVER_NAME_LEN, de.driver_name, de.disk_drv);
682 fprintf(stderr, "fsys_get_mount_dev():\n\tdevice='%s'\n", dev);
683 fprintf(stderr, "fsys_get_mount_pt():\n\tmount point='%s'\n", dir);
684 #endif /* DEBUG */
686 return (me);
688 #endif /* HAVE_INFOMOUNT_QNX */
690 void
691 init_my_statfs (void)
693 #ifdef HAVE_INFOMOUNT_LIST
694 while (mount_list) {
695 struct mount_entry *next = mount_list->me_next;
696 free_mount_entry (mount_list);
697 mount_list = next;
700 mount_list = read_filesystem_list (1, 1);
701 #endif /* HAVE_INFOMOUNT_LIST */
704 void
705 my_statfs (struct my_statfs *myfs_stats, const char *path)
707 #ifdef HAVE_INFOMOUNT_LIST
708 int i, len = 0;
709 struct mount_entry *entry = NULL;
710 struct mount_entry *temp = mount_list;
711 struct fs_usage fs_use;
713 while (temp){
714 i = strlen (temp->me_mountdir);
715 if (i > len && (strncmp (path, temp->me_mountdir, i) == 0))
716 if (!entry || (path [i] == PATH_SEP || path [i] == 0)){
717 len = i;
718 entry = temp;
720 temp = temp->me_next;
723 if (entry){
724 memset (&fs_use, 0, sizeof (struct fs_usage));
725 get_fs_usage (entry->me_mountdir, &fs_use);
727 myfs_stats->type = entry->me_dev;
728 myfs_stats->typename = entry->me_type;
729 myfs_stats->mpoint = entry->me_mountdir;
730 myfs_stats->device = entry->me_devname;
731 myfs_stats->avail = getuid () ? fs_use.fsu_bavail/2 : fs_use.fsu_bfree/2;
732 myfs_stats->total = fs_use.fsu_blocks/2;
733 myfs_stats->nfree = fs_use.fsu_ffree;
734 myfs_stats->nodes = fs_use.fsu_files;
735 } else
736 #endif /* HAVE_INFOMOUNT_LIST */
738 #ifdef HAVE_INFOMOUNT_QNX
740 ** This is the "other side" of the hack to read_filesystem_list() in
741 ** mountlist.c.
742 ** It's not the most efficient approach, but consumes less memory. It
743 ** also accomodates QNX's ability to mount filesystems on the fly.
745 struct mount_entry *entry;
746 struct fs_usage fs_use;
748 if ((entry = read_filesystem_list(0, 0)) != NULL)
750 get_fs_usage(entry->me_mountdir, &fs_use);
752 myfs_stats->type = entry->me_dev;
753 myfs_stats->typename = entry->me_type;
754 myfs_stats->mpoint = entry->me_mountdir;
755 myfs_stats->device = entry->me_devname;
757 myfs_stats->avail = fs_use.fsu_bfree / 2;
758 myfs_stats->total = fs_use.fsu_blocks / 2;
759 myfs_stats->nfree = fs_use.fsu_ffree;
760 myfs_stats->nodes = fs_use.fsu_files;
762 else
763 #endif /* HAVE_INFOMOUNT_QNX */
765 myfs_stats->type = 0;
766 myfs_stats->mpoint = "unknown";
767 myfs_stats->device = "unknown";
768 myfs_stats->avail = 0;
769 myfs_stats->total = 0;
770 myfs_stats->nfree = 0;
771 myfs_stats->nodes = 0;
775 #ifdef HAVE_INFOMOUNT
777 /* Return the number of TOSIZE-byte blocks used by
778 BLOCKS FROMSIZE-byte blocks, rounding away from zero.
779 TOSIZE must be positive. Return -1 if FROMSIZE is not positive. */
781 static fsblkcnt_t
782 fs_adjust_blocks (fsblkcnt_t blocks, int fromsize, int tosize)
784 if (tosize <= 0)
785 abort ();
786 if (fromsize <= 0)
787 return -1;
789 if (fromsize == tosize) /* E.g., from 512 to 512. */
790 return blocks;
791 else if (fromsize > tosize) /* E.g., from 2048 to 512. */
792 return blocks * (fromsize / tosize);
793 else /* E.g., from 256 to 512. */
794 return blocks / (tosize / fromsize);
797 #if defined(_AIX) && defined(_I386)
798 /* AIX PS/2 does not supply statfs. */
799 static int aix_statfs (char *path, struct statfs *fsb)
801 struct stat stats;
802 struct dustat fsd;
804 if (stat (path, &stats))
805 return -1;
806 if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
807 return -1;
808 fsb->f_type = 0;
809 fsb->f_bsize = fsd.du_bsize;
810 fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
811 fsb->f_bfree = fsd.du_tfree;
812 fsb->f_bavail = fsd.du_tfree;
813 fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb;
814 fsb->f_ffree = fsd.du_tinode;
815 fsb->f_fsid.val[0] = fsd.du_site;
816 fsb->f_fsid.val[1] = fsd.du_pckno;
817 return 0;
819 #define statfs(path,fsb) aix_statfs(path,fsb)
820 #endif /* _AIX && _I386 */
822 /* Fill in the fields of FSP with information about space usage for
823 the filesystem on which PATH resides.
824 Return 0 if successful, -1 if not. */
826 static int
827 get_fs_usage (char *path, struct fs_usage *fsp)
829 #ifdef STAT_STATFS3_OSF1
830 struct statfs fsd;
832 if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
833 return -1;
834 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
835 #endif /* STAT_STATFS3_OSF1 */
837 #ifdef STAT_STATFS2_FS_DATA /* Ultrix. */
838 struct fs_data fsd;
840 if (statfs (path, &fsd) != 1)
841 return -1;
842 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), 1024, 512)
843 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.fd_req.btot);
844 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.fd_req.bfree);
845 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.fd_req.bfreen);
846 fsp->fsu_files = fsd.fd_req.gtot;
847 fsp->fsu_ffree = fsd.fd_req.gfree;
848 #endif
850 #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX. */
851 struct statfs fsd;
853 if (statfs (path, &fsd) < 0)
854 return -1;
855 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
856 #endif
858 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD. */
859 struct statfs fsd;
861 if (statfs (path, &fsd) < 0)
862 return -1;
863 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
864 #endif
866 #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX. */
867 struct statfs fsd;
869 if (statfs (path, &fsd, sizeof fsd, 0) < 0)
870 return -1;
871 /* Empirically, the block counts on most SVR3 and SVR3-derived
872 systems seem to always be in terms of 512-byte blocks,
873 no matter what value f_bsize has. */
874 #if _AIX
875 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
876 #else
877 #define CONVERT_BLOCKS(b) (b)
878 #ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx. */
879 #ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
880 #define f_bavail f_bfree
881 #endif
882 #endif
883 #endif
884 #endif
886 #ifdef STAT_STATVFS /* SVR4. */
887 struct statvfs fsd;
889 if (statvfs (path, &fsd) < 0)
890 return -1;
891 /* f_frsize isn't guaranteed to be supported. */
892 #define CONVERT_BLOCKS(b) \
893 fs_adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
894 #endif
896 #if defined(CONVERT_BLOCKS) && !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS) /* !Ultrix && !SVR2. */
897 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.f_blocks);
898 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.f_bfree);
899 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.f_bavail);
900 fsp->fsu_files = fsd.f_files;
901 fsp->fsu_ffree = fsd.f_ffree;
902 #endif
904 return 0;
907 #endif /* HAVE_INFOMOUNT */