summaryrefslogtreecommitdiffstats
path: root/src/util/fsspace.c
blob: 50a4aa7bcb4e44048c300f640b2d6855b019cc30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*++
/* NAME
/*	fsspace 3
/* SUMMARY
/*	determine available file system space
/* SYNOPSIS
/*	#include <fsspace.h>
/*
/*	struct fsspace {
/* .in +4
/*		unsigned long block_size;
/*		unsigned long block_free;
/* .in -4
/*	};
/*
/*	void	fsspace(path, sp)
/*	const char *path;
/*	struct fsspace *sp;
/* DESCRIPTION
/*	fsspace() returns the amount of available space in the file
/*	system specified in \fIpath\fR, in terms of the block size and
/*	of the number of available blocks.
/* DIAGNOSTICS
/*	All errors are fatal.
/* BUGS
/*	Use caution when doing computations with the result from fsspace().
/*	It is easy to cause overflow (by multiplying large numbers) or to
/*	cause underflow (by subtracting unsigned numbers).
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

/* System library. */

#include <sys_defs.h>

#if defined(STATFS_IN_SYS_MOUNT_H)
#include <sys/param.h>
#include <sys/mount.h>
#elif defined(STATFS_IN_SYS_VFS_H)
#include <sys/vfs.h>
#elif defined(STATVFS_IN_SYS_STATVFS_H)
#include <sys/statvfs.h>
#elif defined(STATFS_IN_SYS_STATFS_H)
#include <sys/statfs.h>
#else
#ifdef USE_STATFS
#error "please specify the include file with `struct statfs'"
#else
#error "please specify the include file with `struct statvfs'"
#endif
#endif

/* Utility library. */

#include <msg.h>
#include <fsspace.h>

/* fsspace - find amount of available file system space */

void    fsspace(const char *path, struct fsspace * sp)
{
    const char *myname = "fsspace";

#ifdef USE_STATFS
#ifdef USE_STRUCT_FS_DATA			/* Ultrix */
    struct fs_data fsbuf;

    if (statfs(path, &fsbuf) < 0)
	msg_fatal("statfs %s: %m", path);
    sp->block_size = 1024;
    sp->block_free = fsbuf.fd_bfreen;
#else
    struct statfs fsbuf;

    if (statfs(path, &fsbuf) < 0)
	msg_fatal("statfs %s: %m", path);
    sp->block_size = fsbuf.f_bsize;
    sp->block_free = fsbuf.f_bavail;
#endif
#endif
#ifdef USE_STATVFS
    struct statvfs fsbuf;

    if (statvfs(path, &fsbuf) < 0)
	msg_fatal("statvfs %s: %m", path);
    sp->block_size = fsbuf.f_frsize;
    sp->block_free = fsbuf.f_bavail;
#endif
    if (msg_verbose)
	msg_info("%s: %s: block size %lu, blocks free %lu",
		 myname, path, sp->block_size, sp->block_free);
}

#ifdef TEST

 /*
  * Proof-of-concept test program: print free space unit and count for all
  * listed file systems.
  */

#include <vstream.h>

int     main(int argc, char **argv)
{
    struct fsspace sp;

    if (argc == 1)
	msg_fatal("usage: %s filesystem...", argv[0]);

    while (--argc && *++argv) {
	fsspace(*argv, &sp);
	vstream_printf("%10s: block size %lu, blocks free %lu\n",
		       *argv, sp.block_size, sp.block_free);
	vstream_fflush(VSTREAM_OUT);
    }
    return (0);
}

#endif