How to get free disk space size in C/C++ program( Solaris system)

How to get free disk space size in C/C++ program( Solaris system)?
Is there any standard function or system function? Just like "df" or
"getdfree" in Linux.

How do you want to do it? Run a system() call that runs 'df' or do you want to do this via system calls? For the latter, check the man page of statvfs(2). For the first method, check the man page of system(3).

Needed to write something to get information from the disks and send it to a web app, so here is the code (the html stuff removed), this should give you the info (works on our AIX servers atleast). Only thing is it does give a slightly higher free space value than df, not certain what is causing it.

/*
################################################################################
##                              diskstat.c                                    ##
################################################################################
## diskstat returns (or will soon return to be more precice) webified diskstat for specific disk, should change to     ##
## XML'ified info at some point.....                                          ##
################################################################################
*/

#include <sys/statvfs.h>

int main( int argc, char *argv[] )
{
        struct statvfs fiData;
        struct statvfs *fpData;
        char fnPath[128];
        int i;

        if( argc < 2 ) {
                printf("Usage, webdisk DEVICE0 ..... DEVICEX\n");
                return(2);
        }

        //Lets loopyloop through the argvs
        for( i = 1 ; i<argc; i++ ) {
                strcpy(fnPath, argv);
                if((statvfs(fnPath,&fiData)) < 0 ) {
                        printf("Failed to stat %s:\n", fnPath);
                } else {
                        printf("Disk %s: \n", fnPath);
                        printf("\tblock size: %u\n", fiData.f_bsize);
                        printf("\ttotal no blocks: %i\n", fiData.f_blocks);
                        printf("\tfree blocks: %i\n", fiData.f_bfree);
                }
        }
}

For the other members of the struct, check sys/statvfs.h.

Just compile and send it the mountpoint of the disk.
So for example ./a.out /

BTW Thanks blowtorch for pointing me in the right direction whith statvls.

hi,

i have written the code to find free space for linux using native code of java like below.

#include <sys/statvfs.h>

JNIEXPORT jlong JNICALL Java_NativeUtils_getDiskFreeSpace
(JNIEnv *env, jclass obj, jstring path) {
struct statvfs fiData;
struct statvfs *fpData;
//char fnPath[128];
int i;
const char *str = (*env)->GetStringUTFChars(env, path, 0);
jlong ret;

            if\(\(statvfs\(str,&fiData\)\) &lt; 0 \) \{
                    printf\("Failed to stat %s:\\n", str\);
            \} else \{                       
                    printf\("\\tblock size: %u\\n", fiData.f_bsize\);                        
            \}            
	\(*env\)-&gt;ReleaseStringUTFChars\(env, path, str\);

ret = (jlong)fiData.QuadPart;
return ret;
}

if any person is having the linux os and can u please test and get back to me

thanks,
prasad

I used ur code and compliled it using gcc compiler on Linux. But its giving the following error.

error: `JNIEXPORT' does not name a type

any solution to that ???