How do "df" from within a C program?

Conor P. Cahill cpcahil at virtech.uucp
Fri Dec 22 23:17:35 AEST 1989


In article <470 at cpsolv.UUCP>, rhg at cpsolv.UUCP (Richard H. Gumpertz) writes:
> I have a C program that would like to take special action when disk
> space is getting tight for the group of files that it writes (to avoid
> running out completely).
> 	1) How can a C program get the space available on a file system
> 	   (i.e., do the equivalent of a "df" command)?
> 	2) How can a C program determine which file system a particular
> 	   directory is on (so that it can find the appropriate argument
> 	   for doing the equivalent of "df")?
> 	3) I am using an AT&T Unix PC, OS release 3.51 (roughly Vr2); a
> 	   system-specific answer will do IF NECESSARY.

You can solve all these problems by using the ustat(2) system call.
Simply do the following:

	stat("file_whose_fs_you_want_to_check", &stbuf);
	
	ustat(stbuf.st_dev,&ustatbuf);

	ustatbuf.f_tfree = total blocks free;
	ustatbuf.f_tinode = total inodes free;

Note that this only works on mounted file systems.

> 	4) What are the portability issues of the answers to the above?

This should work on any system that is System V release 2.0 (or higher) 
compatible.  For systems that are System V release 3.0 (or higher) you 
also have the option of using the statfs(2) system call, but ustat is
still usable.  

For BSD I don't know of  any special system call to support this kind
of operation.  You might need to read the superblock or parse the df outupt.
this
-- 
+-----------------------------------------------------------------------+
| Conor P. Cahill     uunet!virtech!cpcahil      	703-430-9247	!
| Virtual Technologies Inc.,    P. O. Box 876,   Sterling, VA 22170     |
+-----------------------------------------------------------------------+



More information about the Comp.unix.questions mailing list