Added two macro examples:
[zuwinko.git] / src / filemanager / mountlist.c
blobbdc56482afe8309f776d1fad06f2adac36a51221
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 #define statfs statvfs
58 #endif
60 #ifdef MOUNTED_GETMNT /* Ultrix. */
61 #include <sys/mount.h>
62 #include <sys/fs_types.h>
63 #endif
65 #ifdef MOUNTED_FREAD /* SVR2. */
66 #include <mnttab.h>
67 #endif
69 #ifdef MOUNTED_FREAD_FSTYP /* SVR3. */
70 #include <mnttab.h>
71 #include <sys/fstyp.h>
72 #include <sys/statfs.h>
73 #endif
75 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
76 #include <sys/mnttab.h>
77 #endif
79 #ifdef MOUNTED_VMOUNT /* AIX. */
80 #include <fshelp.h>
81 #include <sys/vfs.h>
82 #endif
84 #ifdef HAVE_SYS_STATFS_H
85 #include <sys/statfs.h>
86 #endif
88 #ifdef HAVE_INFOMOUNT_QNX
89 #include <sys/disk.h>
90 #include <sys/fsys.h>
91 #endif
93 #ifdef HAVE_SYS_MOUNT_H
94 #include <sys/mount.h>
95 #endif
97 #ifdef HAVE_SYS_VFS_H
98 #include <sys/vfs.h>
99 #endif
101 #ifdef HAVE_SYS_FILSYS_H
102 #include <sys/filsys.h> /* SVR2. */
103 #endif
105 #ifdef HAVE_DUSTAT_H /* AIX PS/2. */
106 #include <sys/dustat.h>
107 #endif
109 #ifdef HAVE_SYS_STATVFS_H /* SVR4. */
110 #include <sys/statvfs.h>
111 #endif
113 #include "lib/global.h"
114 #include "mountlist.h"
116 /*** global variables ****************************************************************************/
118 /*** file scope macro definitions ****************************************************************/
120 #ifdef DOLPHIN
121 /* So special that it's not worth putting this in autoconf. */
122 #undef MOUNTED_FREAD_FSTYP
123 #define MOUNTED_GETMNTTBL
124 #endif
126 #if defined (__QNX__) && !defined(__QNXNTO__) && !defined (HAVE_INFOMOUNT_LIST)
127 #define HAVE_INFOMOUNT_QNX
128 #endif
130 #if defined(HAVE_INFOMOUNT_LIST) || defined(HAVE_INFOMOUNT_QNX)
131 #define HAVE_INFOMOUNT
132 #endif
134 /*** file scope type declarations ****************************************************************/
136 /* A mount table entry. */
137 struct mount_entry
139 char *me_devname; /* Device node pathname, including "/dev/". */
140 char *me_mountdir; /* Mount point directory pathname. */
141 char *me_type; /* "nfs", "4.2", etc. */
142 dev_t me_dev; /* Device number of me_mountdir. */
143 struct mount_entry *me_next;
146 struct fs_usage
148 fsblkcnt_t fsu_blocks; /* Total blocks. */
149 fsblkcnt_t fsu_bfree; /* Free blocks available to superuser. */
150 fsblkcnt_t fsu_bavail; /* Free blocks available to non-superuser. */
151 fsfilcnt_t fsu_files; /* Total file nodes. */
152 fsfilcnt_t fsu_ffree; /* Free file nodes. */
155 /*** file scope variables ************************************************************************/
157 static int get_fs_usage (char *path, struct fs_usage *fsp);
159 #ifdef HAVE_INFOMOUNT_LIST
160 static struct mount_entry *mount_list = NULL;
161 #endif /* HAVE_INFOMOUNT_LIST */
163 /*** file scope functions ************************************************************************/
164 /* --------------------------------------------------------------------------------------------- */
166 #ifdef HAVE_INFOMOUNT_LIST
167 static void
168 free_mount_entry (struct mount_entry *me)
170 if (!me)
171 return;
172 if (me->me_devname)
173 free (me->me_devname);
174 if (me->me_mountdir)
175 free (me->me_mountdir);
176 if (me->me_type)
177 free (me->me_type);
178 free (me);
181 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
183 /* --------------------------------------------------------------------------------------------- */
184 /* Return the value of the hexadecimal number represented by CP.
185 No prefix (like '0x') or suffix (like 'h') is expected to be
186 part of CP. */
188 static int
189 xatoi (const char *cp)
191 int val;
193 val = 0;
194 while (*cp)
196 if (*cp >= 'a' && *cp <= 'f')
197 val = val * 16 + *cp - 'a' + 10;
198 else if (*cp >= 'A' && *cp <= 'F')
199 val = val * 16 + *cp - 'A' + 10;
200 else if (*cp >= '0' && *cp <= '9')
201 val = val * 16 + *cp - '0';
202 else
203 break;
204 cp++;
206 return val;
208 #endif /* MOUNTED_GETMNTENT1 */
210 #ifdef MOUNTED_GETMNTINFO
212 /* --------------------------------------------------------------------------------------------- */
214 #ifndef HAVE_STRUCT_STATFS_F_FSTYPENAME
215 static char *
216 fstype_to_string (short t)
218 switch (t)
220 #ifdef MOUNT_PC
221 case MOUNT_PC:
222 return "pc";
223 #endif
224 #ifdef MOUNT_MFS
225 case MOUNT_MFS:
226 return "mfs";
227 #endif
228 #ifdef MOUNT_LO
229 case MOUNT_LO:
230 return "lo";
231 #endif
232 #ifdef MOUNT_TFS
233 case MOUNT_TFS:
234 return "tfs";
235 #endif
236 #ifdef MOUNT_TMP
237 case MOUNT_TMP:
238 return "tmp";
239 #endif
240 #ifdef MOUNT_UFS
241 case MOUNT_UFS:
242 return "ufs";
243 #endif
244 #ifdef MOUNT_NFS
245 case MOUNT_NFS:
246 return "nfs";
247 #endif
248 #ifdef MOUNT_MSDOS
249 case MOUNT_MSDOS:
250 return "msdos";
251 #endif
252 #ifdef MOUNT_LFS
253 case MOUNT_LFS:
254 return "lfs";
255 #endif
256 #ifdef MOUNT_LOFS
257 case MOUNT_LOFS:
258 return "lofs";
259 #endif
260 #ifdef MOUNT_FDESC
261 case MOUNT_FDESC:
262 return "fdesc";
263 #endif
264 #ifdef MOUNT_PORTAL
265 case MOUNT_PORTAL:
266 return "portal";
267 #endif
268 #ifdef MOUNT_NULL
269 case MOUNT_NULL:
270 return "null";
271 #endif
272 #ifdef MOUNT_UMAP
273 case MOUNT_UMAP:
274 return "umap";
275 #endif
276 #ifdef MOUNT_KERNFS
277 case MOUNT_KERNFS:
278 return "kernfs";
279 #endif
280 #ifdef MOUNT_PROCFS
281 case MOUNT_PROCFS:
282 return "procfs";
283 #endif
284 #ifdef MOUNT_AFS
285 case MOUNT_AFS:
286 return "afs";
287 #endif
288 #ifdef MOUNT_CD9660
289 case MOUNT_CD9660:
290 return "cd9660";
291 #endif
292 #ifdef MOUNT_UNION
293 case MOUNT_UNION:
294 return "union";
295 #endif
296 #ifdef MOUNT_DEVFS
297 case MOUNT_DEVFS:
298 return "devfs";
299 #endif
300 #ifdef MOUNT_EXT2FS
301 case MOUNT_EXT2FS:
302 return "ext2fs";
303 #endif
304 default:
305 return "?";
308 #endif /* ! HAVE_STRUCT_STATFS_F_FSTYPENAME */
310 #endif /* MOUNTED_GETMNTINFO */
312 /* --------------------------------------------------------------------------------------------- */
314 #ifdef MOUNTED_VMOUNT /* AIX. */
315 static char *
316 fstype_to_string (int t)
318 struct vfs_ent *e;
320 e = getvfsbytype (t);
321 if (!e || !e->vfsent_name)
322 return "none";
323 else
324 return e->vfsent_name;
326 #endif /* MOUNTED_VMOUNT */
328 /* --------------------------------------------------------------------------------------------- */
329 /** Return a list of the currently mounted filesystems, or NULL on error.
330 Add each entry to the tail of the list so that they stay in order.
331 If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in
332 the returned list are valid. Otherwise, they might not be.
333 If ALL_FS is zero, do not return entries for filesystems that
334 are automounter (dummy) entries. */
336 static struct mount_entry *
337 read_filesystem_list (int need_fs_type, int all_fs)
339 struct mount_entry *mlist;
340 struct mount_entry *me;
341 struct mount_entry *mtail;
343 (void) need_fs_type;
344 (void) all_fs;
346 /* Start the list off with a dummy entry. */
347 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
348 me->me_next = NULL;
349 mlist = mtail = me;
351 #ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */
352 #ifdef MOUNTED
354 struct mntent *mnt;
355 FILE *fp;
356 const char *devopt;
358 fp = setmntent (MOUNTED, "r");
359 if (fp == NULL)
360 return NULL;
362 while ((mnt = getmntent (fp)))
364 if (!all_fs && (!strcmp (mnt->mnt_type, "ignore") || !strcmp (mnt->mnt_type, "auto")))
365 continue;
367 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
368 me->me_devname = strdup (mnt->mnt_fsname);
369 me->me_mountdir = strdup (mnt->mnt_dir);
370 me->me_type = strdup (mnt->mnt_type);
371 devopt = strstr (mnt->mnt_opts, "dev=");
372 if (devopt)
374 if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X'))
375 me->me_dev = xatoi (devopt + 6);
376 else
377 me->me_dev = xatoi (devopt + 4);
379 else
380 me->me_dev = -1; /* Magic; means not known yet. */
381 me->me_next = NULL;
383 /* Add to the linked list. */
384 mtail->me_next = me;
385 mtail = me;
388 if (endmntent (fp) == 0)
389 return NULL;
391 #endif /* MOUNTED */
392 #endif /* MOUNTED_GETMNTENT1 */
394 #if defined(MOUNTED_GETMNTINFO) || defined(MOUNTED_GETMNTINFO2) /* 4.4BSD and NetBSD >= 3 */
396 struct statfs *fsp;
397 int entries;
399 entries = getmntinfo (&fsp, MNT_NOWAIT);
400 if (entries < 0)
401 return NULL;
402 while (entries-- > 0)
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 #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(MOUNTED_GETMNTINFO2)
408 me->me_type = strdup (fsp->f_fstypename);
409 #else
410 me->me_type = fstype_to_string (fsp->f_type);
411 #endif
412 me->me_dev = -1; /* Magic; means not known yet. */
413 me->me_next = NULL;
415 /* Add to the linked list. */
416 mtail->me_next = me;
417 mtail = me;
418 fsp++;
421 #endif /* MOUNTED_GETMNTINFO || MOUNTED_GETMNTINFO2 */
423 #ifdef MOUNTED_GETMNT /* Ultrix. */
425 int offset = 0;
426 int val;
427 struct fs_data fsd;
429 while ((val = getmnt (&offset, &fsd, sizeof (fsd), NOSTAT_MANY, NULL)) > 0)
431 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
432 me->me_devname = strdup (fsd.fd_req.devname);
433 me->me_mountdir = strdup (fsd.fd_req.path);
434 me->me_type = gt_names[fsd.fd_req.fstype];
435 me->me_dev = fsd.fd_req.dev;
436 me->me_next = NULL;
438 /* Add to the linked list. */
439 mtail->me_next = me;
440 mtail = me;
442 if (val < 0)
443 return NULL;
445 #endif /* MOUNTED_GETMNT */
447 #ifdef MOUNTED_GETFSSTAT /* __alpha running OSF_1 */
449 int numsys, counter, bufsize;
450 struct statfs *stats;
452 numsys = getfsstat ((struct statfs *) 0, 0L, MNT_WAIT);
453 if (numsys < 0)
454 return (NULL);
456 bufsize = (1 + numsys) * sizeof (struct statfs);
457 stats = (struct statfs *) malloc (bufsize);
458 numsys = getfsstat (stats, bufsize, MNT_WAIT);
460 if (numsys < 0)
462 free (stats);
463 return (NULL);
465 for (counter = 0; counter < numsys; counter++)
467 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
468 me->me_devname = strdup (stats[counter].f_mntfromname);
469 me->me_mountdir = strdup (stats[counter].f_mntonname);
470 me->me_type = mnt_names[stats[counter].f_type];
471 me->me_dev = -1; /* Magic; means not known yet. */
472 me->me_next = NULL;
474 /* Add to the linked list. */
475 mtail->me_next = me;
476 mtail = me;
479 free (stats);
481 #endif /* MOUNTED_GETFSSTAT */
483 #if defined (MOUNTED_FREAD) || defined (MOUNTED_FREAD_FSTYP) /* SVR[23]. */
485 struct mnttab mnt;
486 char *table = "/etc/mnttab";
487 FILE *fp;
489 fp = fopen (table, "r");
490 if (fp == NULL)
491 return NULL;
493 while (fread (&mnt, sizeof mnt, 1, fp) > 0)
495 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
496 #ifdef GETFSTYP /* SVR3. */
497 me->me_devname = strdup (mnt.mt_dev);
498 #else
499 me->me_devname = malloc (strlen (mnt.mt_dev) + 6);
500 strcpy (me->me_devname, "/dev/");
501 strcpy (me->me_devname + 5, mnt.mt_dev);
502 #endif
503 me->me_mountdir = strdup (mnt.mt_filsys);
504 me->me_dev = -1; /* Magic; means not known yet. */
505 me->me_type = "";
506 #ifdef GETFSTYP /* SVR3. */
507 if (need_fs_type)
509 struct statfs fsd;
510 char typebuf[FSTYPSZ];
512 if (statfs (me->me_mountdir, &fsd, sizeof fsd, 0) != -1
513 && sysfs (GETFSTYP, fsd.f_fstyp, typebuf) != -1)
514 me->me_type = strdup (typebuf);
516 #endif
517 me->me_next = NULL;
519 /* Add to the linked list. */
520 mtail->me_next = me;
521 mtail = me;
524 if (fclose (fp) == EOF)
525 return NULL;
527 #endif /* MOUNTED_FREAD || MOUNTED_FREAD_FSTYP */
529 #ifdef MOUNTED_GETMNTTBL /* DolphinOS goes it's own way */
531 struct mntent **mnttbl = getmnttbl (), **ent;
532 for (ent = mnttbl; *ent; ent++)
534 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
535 me->me_devname = strdup ((*ent)->mt_resource);
536 me->me_mountdir = strdup ((*ent)->mt_directory);
537 me->me_type = strdup ((*ent)->mt_fstype);
538 me->me_dev = -1; /* Magic; means not known yet. */
539 me->me_next = NULL;
541 /* Add to the linked list. */
542 mtail->me_next = me;
543 mtail = me;
545 endmnttbl ();
547 #endif /* MOUNTED_GETMNTTBL */
549 #ifdef MOUNTED_GETMNTENT2 /* SVR4. */
551 struct mnttab mnt;
552 const char *table = MNTTAB;
553 FILE *fp;
554 int ret;
556 fp = fopen (table, "r");
557 if (fp == NULL)
558 return NULL;
560 while ((ret = getmntent (fp, &mnt)) == 0)
562 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
563 me->me_devname = strdup (mnt.mnt_special);
564 me->me_mountdir = strdup (mnt.mnt_mountp);
565 me->me_type = strdup (mnt.mnt_fstype);
566 me->me_dev = -1; /* Magic; means not known yet. */
567 me->me_next = NULL;
568 /* Add to the linked list. */
569 mtail->me_next = me;
570 mtail = me;
573 if (ret > 0)
574 return NULL;
575 if (fclose (fp) == EOF)
576 return NULL;
578 #endif /* MOUNTED_GETMNTENT2 */
580 #ifdef MOUNTED_VMOUNT /* AIX. */
582 int bufsize;
583 char *entries, *thisent;
584 struct vmount *vmp;
586 /* Ask how many bytes to allocate for the mounted filesystem info. */
587 mntctl (MCTL_QUERY, sizeof bufsize, (struct vmount *) &bufsize);
588 entries = malloc (bufsize);
590 /* Get the list of mounted filesystems. */
591 mntctl (MCTL_QUERY, bufsize, (struct vmount *) entries);
593 for (thisent = entries; thisent < entries + bufsize; thisent += vmp->vmt_length)
595 vmp = (struct vmount *) thisent;
596 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
597 if (vmp->vmt_flags & MNT_REMOTE)
599 char *host, *path;
601 /* Prepend the remote pathname. */
602 host = thisent + vmp->vmt_data[VMT_HOSTNAME].vmt_off;
603 path = thisent + vmp->vmt_data[VMT_OBJECT].vmt_off;
604 me->me_devname = malloc (strlen (host) + strlen (path) + 2);
605 strcpy (me->me_devname, host);
606 strcat (me->me_devname, ":");
607 strcat (me->me_devname, path);
609 else
611 me->me_devname = strdup (thisent + vmp->vmt_data[VMT_OBJECT].vmt_off);
613 me->me_mountdir = strdup (thisent + vmp->vmt_data[VMT_STUB].vmt_off);
614 me->me_type = strdup (fstype_to_string (vmp->vmt_gfstype));
615 me->me_dev = -1; /* vmt_fsid might be the info we want. */
616 me->me_next = NULL;
618 /* Add to the linked list. */
619 mtail->me_next = me;
620 mtail = me;
622 free (entries);
624 #endif /* MOUNTED_VMOUNT */
626 /* Free the dummy head. */
627 me = mlist;
628 mlist = mlist->me_next;
629 free (me);
630 return mlist;
632 #endif /* HAVE_INFOMOUNT_LIST */
634 /* --------------------------------------------------------------------------------------------- */
636 #ifdef HAVE_INFOMOUNT_QNX
638 ** QNX has no [gs]etmnt*(), [gs]etfs*(), or /etc/mnttab, but can do
639 ** this via the following code.
640 ** Note that, as this is based on CWD, it only fills one mount_entry
641 ** structure. See my_statfs() in utilunix.c for the "other side" of
642 ** this hack.
645 static struct mount_entry *
646 read_filesystem_list (int need_fs_type, int all_fs)
648 struct _disk_entry de;
649 struct statfs fs;
650 int i, fd;
651 char *tp, dev[_POSIX_NAME_MAX], dir[_POSIX_PATH_MAX];
653 static struct mount_entry *me = NULL;
655 if (me)
657 if (me->me_devname)
658 free (me->me_devname);
659 if (me->me_mountdir)
660 free (me->me_mountdir);
661 if (me->me_type)
662 free (me->me_type);
664 else
665 me = (struct mount_entry *) malloc (sizeof (struct mount_entry));
667 if (!getcwd (dir, _POSIX_PATH_MAX))
668 return (NULL);
670 fd = open (dir, O_RDONLY);
671 if (fd == -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 #ifdef HAVE_INFOMOUNT
732 /** Return the number of TOSIZE-byte blocks used by
733 BLOCKS FROMSIZE-byte blocks, rounding away from zero.
734 TOSIZE must be positive. Return -1 if FROMSIZE is not positive. */
736 static fsblkcnt_t
737 fs_adjust_blocks (fsblkcnt_t blocks, int fromsize, int tosize)
739 if (tosize <= 0)
740 abort ();
741 if (fromsize <= 0)
742 return -1;
744 if (fromsize == tosize) /* E.g., from 512 to 512. */
745 return blocks;
746 else if (fromsize > tosize) /* E.g., from 2048 to 512. */
747 return blocks * (fromsize / tosize);
748 else /* E.g., from 256 to 512. */
749 return blocks / (tosize / fromsize);
752 /* --------------------------------------------------------------------------------------------- */
753 #if defined(_AIX) && defined(_I386)
754 /* AIX PS/2 does not supply statfs. */
755 static int
756 aix_statfs (char *path, struct statfs *fsb)
758 struct stat stats;
759 struct dustat fsd;
761 if (stat (path, &stats))
762 return -1;
763 if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
764 return -1;
765 fsb->f_type = 0;
766 fsb->f_bsize = fsd.du_bsize;
767 fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
768 fsb->f_bfree = fsd.du_tfree;
769 fsb->f_bavail = fsd.du_tfree;
770 fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb;
771 fsb->f_ffree = fsd.du_tinode;
772 fsb->f_fsid.val[0] = fsd.du_site;
773 fsb->f_fsid.val[1] = fsd.du_pckno;
774 return 0;
777 #define statfs(path,fsb) aix_statfs(path,fsb)
778 #endif /* _AIX && _I386 */
780 /* Fill in the fields of FSP with information about space usage for
781 the filesystem on which PATH resides.
782 Return 0 if successful, -1 if not. */
784 /* --------------------------------------------------------------------------------------------- */
785 static int
786 get_fs_usage (char *path, struct fs_usage *fsp)
788 #ifdef STAT_STATFS3_OSF1
789 struct statfs fsd;
791 if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
792 return -1;
793 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
794 #endif /* STAT_STATFS3_OSF1 */
796 #ifdef STAT_STATFS2_FS_DATA /* Ultrix. */
797 struct fs_data fsd;
799 if (statfs (path, &fsd) != 1)
800 return -1;
801 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), 1024, 512)
802 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.fd_req.btot);
803 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.fd_req.bfree);
804 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.fd_req.bfreen);
805 fsp->fsu_files = fsd.fd_req.gtot;
806 fsp->fsu_ffree = fsd.fd_req.gfree;
807 #endif
809 #ifdef STAT_STATFS2_BSIZE /* 4.3BSD, SunOS 4, HP-UX, AIX. */
810 struct statfs fsd;
812 if (statfs (path, &fsd) < 0)
813 return -1;
814 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
815 #endif
817 #ifdef STAT_STATFS2_FSIZE /* 4.4BSD. */
818 struct statfs fsd;
820 if (statfs (path, &fsd) < 0)
821 return -1;
822 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
823 #endif
825 #ifdef STAT_STATFS4 /* SVR3, Dynix, Irix, AIX. */
826 struct statfs fsd;
828 if (statfs (path, &fsd, sizeof fsd, 0) < 0)
829 return -1;
830 /* Empirically, the block counts on most SVR3 and SVR3-derived
831 systems seem to always be in terms of 512-byte blocks,
832 no matter what value f_bsize has. */
833 #if _AIX
834 #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
835 #else
836 #define CONVERT_BLOCKS(b) (b)
837 #ifndef _SEQUENT_ /* _SEQUENT_ is DYNIX/ptx. */
838 #ifndef DOLPHIN /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
839 #define f_bavail f_bfree
840 #endif
841 #endif
842 #endif
843 #endif
845 #ifdef STAT_STATVFS /* SVR4. */
846 struct statvfs fsd;
848 if (statvfs (path, &fsd) < 0)
849 return -1;
850 /* f_frsize isn't guaranteed to be supported. */
851 #define CONVERT_BLOCKS(b) \
852 fs_adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
853 #endif
855 #if defined(CONVERT_BLOCKS) && !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS) /* !Ultrix && !SVR2. */
856 fsp->fsu_blocks = CONVERT_BLOCKS (fsd.f_blocks);
857 fsp->fsu_bfree = CONVERT_BLOCKS (fsd.f_bfree);
858 fsp->fsu_bavail = CONVERT_BLOCKS (fsd.f_bavail);
859 fsp->fsu_files = fsd.f_files;
860 fsp->fsu_ffree = fsd.f_ffree;
861 #endif
863 return 0;
866 #endif /* HAVE_INFOMOUNT */
868 /* --------------------------------------------------------------------------------------------- */
869 /*** public functions ****************************************************************************/
870 /* --------------------------------------------------------------------------------------------- */
872 void
873 free_my_statfs (void)
875 #ifdef HAVE_INFOMOUNT_LIST
876 while (mount_list)
878 struct mount_entry *next = mount_list->me_next;
879 free_mount_entry (mount_list);
880 mount_list = next;
882 mount_list = NULL;
883 #endif /* HAVE_INFOMOUNT_LIST */
886 /* --------------------------------------------------------------------------------------------- */
888 void
889 init_my_statfs (void)
891 #ifdef HAVE_INFOMOUNT_LIST
892 free_my_statfs ();
893 mount_list = read_filesystem_list (1, 1);
894 #endif /* HAVE_INFOMOUNT_LIST */
897 /* --------------------------------------------------------------------------------------------- */
899 void
900 my_statfs (struct my_statfs *myfs_stats, const char *path)
902 #ifdef HAVE_INFOMOUNT_LIST
903 size_t i, len = 0;
904 struct mount_entry *entry = NULL;
905 struct mount_entry *temp = mount_list;
906 struct fs_usage fs_use;
908 while (temp)
910 i = strlen (temp->me_mountdir);
911 if (i > len && (strncmp (path, temp->me_mountdir, i) == 0))
912 if (!entry || (path[i] == PATH_SEP || path[i] == '\0'))
914 len = i;
915 entry = temp;
917 temp = temp->me_next;
920 if (entry)
922 memset (&fs_use, 0, sizeof (struct fs_usage));
923 get_fs_usage (entry->me_mountdir, &fs_use);
925 myfs_stats->type = entry->me_dev;
926 myfs_stats->typename = entry->me_type;
927 myfs_stats->mpoint = entry->me_mountdir;
928 myfs_stats->device = entry->me_devname;
929 myfs_stats->avail = getuid ()? fs_use.fsu_bavail / 2 : fs_use.fsu_bfree / 2;
930 myfs_stats->total = fs_use.fsu_blocks / 2;
931 myfs_stats->nfree = fs_use.fsu_ffree;
932 myfs_stats->nodes = fs_use.fsu_files;
934 else
935 #endif /* HAVE_INFOMOUNT_LIST */
937 #ifdef HAVE_INFOMOUNT_QNX
939 ** This is the "other side" of the hack to read_filesystem_list() in
940 ** mountlist.c.
941 ** It's not the most efficient approach, but consumes less memory. It
942 ** also accomodates QNX's ability to mount filesystems on the fly.
944 struct mount_entry *entry;
945 struct fs_usage fs_use;
947 entry = read_filesystem_list (0, 0);
948 if (entry != NULL)
950 get_fs_usage (entry->me_mountdir, &fs_use);
952 myfs_stats->type = entry->me_dev;
953 myfs_stats->typename = entry->me_type;
954 myfs_stats->mpoint = entry->me_mountdir;
955 myfs_stats->device = entry->me_devname;
957 myfs_stats->avail = fs_use.fsu_bfree / 2;
958 myfs_stats->total = fs_use.fsu_blocks / 2;
959 myfs_stats->nfree = fs_use.fsu_ffree;
960 myfs_stats->nodes = fs_use.fsu_files;
962 else
963 #endif /* HAVE_INFOMOUNT_QNX */
965 myfs_stats->type = 0;
966 myfs_stats->mpoint = "unknown";
967 myfs_stats->device = "unknown";
968 myfs_stats->avail = 0;
969 myfs_stats->total = 0;
970 myfs_stats->nfree = 0;
971 myfs_stats->nodes = 0;
975 /* --------------------------------------------------------------------------------------------- */