The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
free some space in file system fu4d UNIX for Dummies Questions & Answers 6 04-11-2008 01:27 AM
command to find free disk space on solaris harishankar SUN Solaris 2 08-26-2007 08:47 AM
How to re-allocate disk space on solaris 10 duke0001 UNIX for Advanced & Expert Users 2 12-13-2006 07:14 AM
free disk space calc ninjanesto High Level Programming 1 11-10-2006 07:03 AM
Free size for File System videsh77 UNIX for Dummies Questions & Answers 7 02-03-2005 02:44 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-20-2006
Registered User
 

Join Date: Aug 2006
Posts: 1
Lightbulb 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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-21-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,323
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).
Reply With Quote
  #3 (permalink)  
Old 08-23-2006
Registered User
 

Join Date: Aug 2006
Posts: 1
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.

Code:
/*
################################################################################
##                              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[i]);
                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.
Reply With Quote
  #4 (permalink)  
Old 08-07-2007
Registered User
 

Join Date: Aug 2007
Posts: 1
Exclamation need to help to test in linux

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)) < 0 ) {
printf("Failed to stat %s:\n", str);
} else {
printf("\tblock size: %u\n", fiData.f_bsize);
}
(*env)->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
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:07 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0