Search code examples
cdisk-access

File system stats


I use the following code to find the disk usage of my /

int main()
{
    struct statfs *stat;
    statfs64("/tmp",stat);
    perror("");
    printf("%lu \n",stat->f_bfree*stat->f_bsize);
    return 0;
}

The perror keeps on printing "Bad Address" and a random number for size.

Bad address

3264987920

PS:I tried sudo ./a.out,statfs("a.out",stat)

What may be the issue?


Solution

  • You've declared a pointer to a statfs struct but don't actually have space allocated for such a struct. The pointer points off into nowhereland. It's uninitialized, it doesn't point anywhere legal.

    struct statfs stat;
    
    if (statfs64("/tmp", &stat) == -1) {
        perror("statfs64");
    }
    else {
        printf("%lu\n", stat.f_bfree * stat.f_bsize);
    }