[comp.unix.questions] How do "df" from within a C program?

rhg@cpsolv.UUCP (Richard H. Gumpertz) (12/21/89)

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.
	4) What are the portability issues of the answers to the above?
-- 
===============================================================================
| Richard H. Gumpertz rhg%cpsolv@uunet.uu.NET -or- ...uunet!amgraf!cpsolv!rhg |
| Computer Problem Solving, 8905 Mohawk Lane, Leawood, Kansas 66206-1749      |
===============================================================================

cpcahil@virtech.uucp (Conor P. Cahill) (12/22/89)

In article <470@cpsolv.UUCP>, rhg@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     |
+-----------------------------------------------------------------------+

chris@mimsy.umd.edu (Chris Torek) (12/23/89)

In article <1989Dec22.121735.18455@virtech.uucp> cpcahil@virtech.uucp
(Conor P. Cahill) writes:
>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.

Sufficiently recent versions of BSD (i.e., 4.4) have a statfs() system
call like the one in SunOS and SysRel 4.  (There are some differences
with which I am unfamiliar.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris