checking disk space from c program

Dieter Muller dieter at titan.nmt.edu
Mon Apr 11 21:20:02 AEST 1988


In article <3448 at csli.STANFORD.EDU> gandalf at csli.stanford.edu (Juergen Wagner) writes:
>In article <71 at kenobi.UUCP> ford at kenobi.UUCP (Mike Ditto) writes:
>>...
>>Read /etc/mnttab to see what devices are mounted, and use the ustat
>>system call to check the status of each....
>>...
>
>The problem with ustat is that it doesn't exist on BSD systems (even on
>Suns), and the problem with stat is that you can't get the whole 
>information without actually reading the super-block, i.e. without having
>root privileges. Normal users have to stick with "df" pipes thru some
>filter.

If you have a Sun, what's wrong with statfs?  True, it isn't on our
4.3BSD Vax, and it doesn't seem real happy with NFS, but it's there
and it works (SunOS 3.5).

Here's an example:

	Filesystem:  /titan1
	Block size:  1024
	Num blocks:  490369
	Total free:  97943
	Avail free:  48906
	Total nodes: 96256
	Free nodes:  77991

Looks like exactly what was asked for.  Yes, I know all the world's not
a Sun.  The code for this example follows.  I didn't run it as root.

Dieter Muller
----------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/vfs.h>

main ()
{
	struct statfs buffer;

	if (statfs ("/titan1", & buffer) == -1)
	    perror ("/titan1");
	else print_statfs ("/titan1", & buffer);

	exit (0);
}

print_statfs (name, buffer)
char * name;
struct statfs * buffer;
{
	printf ("Filesystem:  %s\n", name);
	printf ("Block size:  %d\n", buffer -> f_bsize);
	printf ("Num blocks:  %d\n", buffer -> f_blocks);
	printf ("Total free:  %d\n", buffer -> f_bfree);
	printf ("Avail free:  %d\n", buffer -> f_bavail);
	printf ("Total nodes: %d\n", buffer -> f_files);
	printf ("Free nodes:  %d\n", buffer -> f_ffree);

	fflush (stdout);
	
	return;
}
-- 
...{cmcl2, ihnp4}!lanl!unm-la!unmvax!nmtsun!dieter
...gemini!crunch!unmvax!nmtsun!dieter
dieter at nmtsun.nmt.edu



More information about the Comp.unix.questions mailing list