summaryrefslogtreecommitdiff
path: root/Utility/libdiskfree.c
blob: 12d70e2592843bb2ed7fcfdfbc486343fa289f39 (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
/* disk free space checking, C mini-library
 *
 * Copyright 2012, 2014 Joey Hess <id@joeyh.name>
 *
 * License: BSD-2-clause
 */

/* Include appropriate headers for the OS, and define what will be used to
 * check the free space. */
#if defined(__APPLE__)
# define _DARWIN_FEATURE_64_BIT_INODE 1
# include <sys/param.h>
# include <sys/mount.h>
# define STATCALL statfs
# define STATSTRUCT statfs64
# define BSIZE f_bsize
#else
#if defined (__FreeBSD__)
# include <sys/param.h>
# include <sys/mount.h>
# define STATCALL statfs /* statfs64 not yet tested on a real FreeBSD machine */
# define STATSTRUCT statfs
# define BSIZE f_bsize
#else
#if defined __ANDROID__
# warning free space checking code not available for Android
# define UNKNOWN
#else
#if defined (__linux__) || defined (__FreeBSD_kernel__) || (defined (__SVR4) && defined (__sun))
/* Linux or Debian kFreeBSD or Solaris */
/* This is a POSIX standard, so might also work elsewhere too. */
# include <sys/statvfs.h>
# define STATCALL statvfs
# define STATSTRUCT statvfs
# define BSIZE f_frsize
#else
# warning free space checking code not available for this OS
# define UNKNOWN
#endif
#endif
#endif
#endif

#include <errno.h>
#include <stdio.h>

unsigned long long int get(const char *path, int req) {
#ifdef UNKNOWN
	errno = 1;
	return 0;
#else
	unsigned long long int v, blocksize;
	struct STATSTRUCT buf;

	if (STATCALL(path, &buf) != 0)
		return 0; /* errno is set */
	else
		errno = 0;

	switch (req) {
		case 0:
			v = buf.f_blocks;
			break;
		case 1:
			v = buf.f_bavail;
			break;
		default:
			v = 0;
	}

	blocksize = buf.BSIZE;
	return v * blocksize;
#endif
}

/* Checks the amount of disk that is available to regular (non-root) users.
 * (If there's an error, or this is not supported,
 * returns 0 and sets errno to nonzero.)
 */
unsigned long long int diskfree(const char *path) {
	return get(path, 1);
}

/* Gets the total size of the disk. */
unsigned long long int disksize(const char *path) {
	return get(path, 0);
}

/*
main () {
	printf("%lli\n", diskfree("."));
}
*/