aboutsummaryrefslogtreecommitdiff
path: root/tests/fs-tests/utils/free_space.c
blob: 3421e7d81f314914fb7265820649fd7f00db7f22 (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
/*
 * Copyright (C) 2007 Nokia Corporation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * Author: Adrian Hunter
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/statvfs.h>

int main(int argc, char *argv[])
{
	const char *dir_name = ".";
	uint64_t free_space;
	struct statvfs fs_info;

	if (argc > 1) {
		if (strncmp(argv[1], "--help", 6) == 0 ||
				strncmp(argv[1], "-h", 2) == 0) {
			printf(	"Usage is: "
				"free_space [directory]\n"
				"\n"
				"Display the free space of the file system "
				"of the directory given\n"
				"or the current directory if no "
				"directory is given.\nThe value output is "
				"in bytes.\n"
				);
			return 1;
		}
		dir_name = argv[1];
	}
	if (statvfs(dir_name, &fs_info) == -1)
		return 1;

	free_space = (uint64_t) fs_info.f_bavail * (uint64_t) fs_info.f_frsize;

	printf("%llu\n", (unsigned long long) free_space);

	return 0;
}