Statvfs issues....

I wrote a program (code bellow) to get the disk sizes information and yet it does not seem to multiply the values right. did i do something wrong?
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
#include <sys/statvfs.h>
#include <string>
#include <stdio.h>
#include <iostream>
#include <sys/types.h>

struct diskInfo
{
	unsigned long size;
	unsigned long used;
	unsigned long free;
};

diskInfo getDiskUsage(char * path,unsigned int function)
{
	struct statvfs buf;
	diskInfo di;
	unsigned long blksize;
	unsigned long blocks;
	unsigned long freeblks;
	unsigned long disk_size, used, free;
	
	int ret;

	ret = statvfs(path,&buf);

	//blksize = buf.f_bsize;
	blksize = buf.f_frsize;
	blocks = buf.f_blocks;
	freeblks = buf.f_bfree;

	printf("total blocks: %ld\nblock size: %ld\n",buf.f_blocks,buf.f_frsize);

	di.size = blksize * blocks;
	di.free = blksize * freeblks;
	di.used = di.size - di.free;

	return di;
}

int main(int argc, char *argv[])
{
	diskInfo retI;
	retI = getDiskUsage("/",1);
	printf("total space: %ld\nfree space: %ld\nused space: %ld\n\n",retI.size,retI.free,retI.used);
	return 0;
}

and it returns this:

total blocks: 4178445
block size: 4096
total space: -64958464
free space: 141606912
used space: -206565376


the disk it's measuring is a 18GB scsi disk.

this is a debian lenny system. up-to-date.
-64958464 = 4178445 * 4096 rounded to signed 32 bits.
I tried other types like long double and float and they yielded equally perplexing results. any suggestions on the type i should use to get accurate math results? thanks for the assist.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <stdint.h>

int main()
{
        int32_t i32 = (int32_t)4178445 * 4096;
        int64_t i64 = (int64_t)4178445 * 4096;

        std::cout << i32 << std::endl;
        std::cout << i64 << std::endl;

        return 0;
}


1
2
-64958464
17114910720
Thank you!
Topic archived. No new replies allowed.