Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / fsspace.c
blob5c1acd45fd2482a039adb3b1f02045e87543574b
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* fsspace 3
6 /* SUMMARY
7 /* determine available file system space
8 /* SYNOPSIS
9 /* #include <fsspace.h>
11 /* struct fsspace {
12 /* .in +4
13 /* unsigned long block_size;
14 /* unsigned long block_free;
15 /* .in -4
16 /* };
18 /* void fsspace(path, sp)
19 /* const char *path;
20 /* struct fsspace *sp;
21 /* DESCRIPTION
22 /* fsspace() returns the amount of available space in the file
23 /* system specified in \fIpath\fR, in terms of the block size and
24 /* of the number of available blocks.
25 /* DIAGNOSTICS
26 /* All errors are fatal.
27 /* BUGS
28 /* Use caution when doing computations with the result from fsspace().
29 /* It is easy to cause overflow (by multiplying large numbers) or to
30 /* cause underflow (by subtracting unsigned numbers).
31 /* LICENSE
32 /* .ad
33 /* .fi
34 /* The Secure Mailer license must be distributed with this software.
35 /* AUTHOR(S)
36 /* Wietse Venema
37 /* IBM T.J. Watson Research
38 /* P.O. Box 704
39 /* Yorktown Heights, NY 10598, USA
40 /*--*/
42 /* System library. */
44 #include <sys_defs.h>
46 #if defined(STATFS_IN_SYS_MOUNT_H)
47 #include <sys/param.h>
48 #include <sys/mount.h>
49 #elif defined(STATFS_IN_SYS_VFS_H)
50 #include <sys/vfs.h>
51 #elif defined(STATVFS_IN_SYS_STATVFS_H)
52 #include <sys/statvfs.h>
53 #elif defined(STATFS_IN_SYS_STATFS_H)
54 #include <sys/statfs.h>
55 #else
56 #ifdef USE_STATFS
57 #error "please specify the include file with `struct statfs'"
58 #else
59 #error "please specify the include file with `struct statvfs'"
60 #endif
61 #endif
63 /* Utility library. */
65 #include <msg.h>
66 #include <fsspace.h>
68 /* fsspace - find amount of available file system space */
70 void fsspace(const char *path, struct fsspace * sp)
72 const char *myname = "fsspace";
74 #ifdef USE_STATFS
75 #ifdef USE_STRUCT_FS_DATA /* Ultrix */
76 struct fs_data fsbuf;
78 if (statfs(path, &fsbuf) < 0)
79 msg_fatal("statfs %s: %m", path);
80 sp->block_size = 1024;
81 sp->block_free = fsbuf.fd_bfreen;
82 #else
83 struct statfs fsbuf;
85 if (statfs(path, &fsbuf) < 0)
86 msg_fatal("statfs %s: %m", path);
87 sp->block_size = fsbuf.f_bsize;
88 sp->block_free = fsbuf.f_bavail;
89 #endif
90 #endif
91 #ifdef USE_STATVFS
92 struct statvfs fsbuf;
94 if (statvfs(path, &fsbuf) < 0)
95 msg_fatal("statvfs %s: %m", path);
96 sp->block_size = fsbuf.f_frsize;
97 sp->block_free = fsbuf.f_bavail;
98 #endif
99 if (msg_verbose)
100 msg_info("%s: %s: block size %lu, blocks free %lu",
101 myname, path, sp->block_size, sp->block_free);
104 #ifdef TEST
107 * Proof-of-concept test program: print free space unit and count for all
108 * listed file systems.
111 #include <vstream.h>
113 int main(int argc, char **argv)
115 struct fsspace sp;
117 if (argc == 1)
118 msg_fatal("usage: %s filesystem...", argv[0]);
120 while (--argc && *++argv) {
121 fsspace(*argv, &sp);
122 vstream_printf("%10s: block size %lu, blocks free %lu\n",
123 *argv, sp.block_size, sp.block_free);
124 vstream_fflush(VSTREAM_OUT);
126 return (0);
129 #endif