Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Determine disk free space in C program

4,123 views
Skip to first unread message

Nancee Toft

unread,
Aug 27, 2001, 6:13:26 PM8/27/01
to
Can anyone tell me how to determined free space on a disk within a C program?

Thanks in advance,
Nancee


Jens.T...@physik.fu-berlin.de

unread,
Aug 27, 2001, 6:27:55 PM8/27/01
to
Nancee Toft <nlt...@dcn.davis.ca.us> wrote:
> Can anyone tell me how to determined free space on a disk within a C program?

My first idea would be to use popen() to run the 'df' utility and analyse
the output, but probably there are more clever ways which, without doubt,
someone else will point out ;-)
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Jens.T...@physik.fu-berlin.de
_ | | | | | | AG Moebius, Institut fuer Molekuelphysik
| |_| | | | | | Fachbereich Physik, Freie Universitaet Berlin
\___/ens|_|homs|_|oerring Tel: ++49 (0)30 838 - 53394 / FAX: - 56046

Stephan Krings

unread,
Aug 27, 2001, 6:50:41 PM8/27/01
to
Nancee Toft wrote:

> Can anyone tell me how to determined free space on a disk within a C
> program?

"man statfs" might help.

Regards,

Stephan

Robert Nichols

unread,
Aug 27, 2001, 7:22:28 PM8/27/01
to
In article <9megm6$r0q$2...@woodrow.ucdavis.edu>,
Nancee Toft <nlt...@dcn.davis.ca.us> wrote:
:Can anyone tell me how to determined free space on a disk within a C program?

NAME
statfs, fstatfs - get file system statistics

SYNOPSIS
#include <sys/vfs.h>

int statfs(const char *path, struct statfs *buf);
int fstatfs(int fd, struct statfs *buf);

See `man 2 statfs` for details.

--
Bob Nichols rnic...@interaccess.com

Dave Blake

unread,
Aug 27, 2001, 7:15:00 PM8/27/01
to
Jens.T...@physik.fu-berlin.de <Jens.T...@physik.fu-berlin.de> scribed:

> Nancee Toft <nlt...@dcn.davis.ca.us> wrote:
> > Can anyone tell me how to determined free space on a disk within a C program?
>
> My first idea would be to use popen() to run the 'df' utility and analyse
> the output, but probably there are more clever ways which, without doubt,
> someone else will point out ;-)

download and read the source to df, maybe ?


--
Dave Blake
dbl...@phy.ucsf.edu

Nancee Toft

unread,
Aug 27, 2001, 8:26:54 PM8/27/01
to
In article <GIr29G.1HJ...@interaccess.com>, rnic...@interaccess.com
says...

Thanks for your input and the input from all the others that have responded.
Now I need help reconciling the info I get from statfs() with that from df.

Test program:

#include <stdio.h>
#include <sys/vfs.h>

int main (int argc, char *argv[])
{
struct statfs sStats;

if( statfs( "/home", &sStats ) == -1 )
printf( "statfs() failed\n" );
else
{
printf( "file system type: %x\n", sStats.f_type );
printf( "optimal transfer block size: %ld\n", sStats.f_bsize );
printf( "total data blocks in file system: %ld\n", sStats.f_blocks );
printf( "free blocks avail to non-superuser: %ld\n", sStats.f_bavail );
printf( "total file nodes in file system: %ld\n", sStats.f_files );
printf( "free file nodes in file system: %ld\n", sStats.f_ffree );
printf( "maximum length of filenames: %ld\n", sStats.f_namelen );
}
return 0;
}

Program output:

file system type: ef53
optimal transfer block size: 4096
total data blocks in file system: 2166335
free blocks avail to non-superuser: 1950418
total file nodes in file system: 1101056
free file nodes in file system: 1094495
maximum length of filenames: 255

df output:

Filesystem 1k-blocks Used Available Use% Mounted on
/dev/sdb6 8665340 423524 7801640 6% /home

How does the 'Available' amount from df relate to the members of struct statfs?

Thanks,
Nancee

Roberto Diaz

unread,
Aug 28, 2001, 1:49:05 AM8/28/01
to
> Can anyone tell me how to determined free space on a disk within a C program?

use statfs (2);

For example:

#include <sys/vfs.h>
#include <stdio.h>
#include <stdlib.h>

#define FSPATH "/dev/hda1"

int
main ()
{
struct statfs info;
statfs (FSPATH, &info);

fprintf (stdout, "block size :%ld,"
" total data blocks :%ld,"
" free blocks for root: %ld,"
" free blocks for user: %ld\n ",
info.f_bsize, info.f_blocks, info.f_bfree, info.f_bavail);
exit (EXIT_SUCCESS);

}


---
Roberto Diaz <rdiaz...@vivaldi.dhis.org>
..For a Brave GNU World..
Concerto Grosso Op. 3/8 A minor
Antonio Vivaldi (so... do you need beautiful words?)
-----

Roberto Diaz

unread,
Aug 28, 2001, 1:55:24 AM8/28/01
to
> How does the 'Available' amount from df relate to the members of struct statfs?

In bytes?

It is the block size multiplied by f_bavail member..

Wojtek Walczak

unread,
Oct 13, 2001, 7:11:49 AM10/13/01
to
Dnia Tue, 28 Aug 2001 00:26:54 +0000 (UTC), Nancee Toft napisał(a):
> if( statfs( "/home", &sStats ) == -1 )
> printf( "statfs() failed\n" );
> else
> {
> printf( "file system type: %x\n", sStats.f_type );
> printf( "optimal transfer block size: %ld\n", sStats.f_bsize );
> printf( "total data blocks in file system: %ld\n", sStats.f_blocks );
> printf( "free blocks avail to non-superuser: %ld\n", sStats.f_bavail );
> printf( "total file nodes in file system: %ld\n", sStats.f_files );
> printf( "free file nodes in file system: %ld\n", sStats.f_ffree );
> printf( "maximum length of filenames: %ld\n", sStats.f_namelen );

>How does the 'Available' amount from df relate to the members of struct statfs?
i think that you have cheat yourself here ;)
the theory says:
if one block has 1024 bytes, block size in your system
is 4096, then you need to multiply an output of your
program by 4.
output from your program:


>free blocks avail to non-superuser: 1950418

output from your df command:


>/dev/sdb6 8665340 423524 7801640 6% /home

now 1950418*4 should be 7801672, but here it is 7801640.
why ?
i think something was copied/created on your /home
partition between running df and your program commands.
if you can - check it again.

HTH.

--
[ Wojtek gminick Walczak ][ http://hacker.pl/gminick/ ]
[ gminick (at) hacker.pl ][ ]gminick (at) interia.pl[ ]

akashm...@gmail.com

unread,
Feb 20, 2020, 1:48:09 AM2/20/20
to

Joe Beanfish

unread,
Feb 20, 2020, 9:43:45 AM2/20/20
to
man statfs
0 new messages