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
command to find free disk space on solaris harishankar SUN Solaris 2 08-26-2007 08:47 AM
How to get free disk space size in C/C++ program( Solaris system) yidu High Level Programming 3 08-07-2007 02:26 AM
free space, associated by filesystem name... mr_manny SUN Solaris 2 10-31-2005 09:47 AM
available disk space on disk device??? alan UNIX for Dummies Questions & Answers 4 01-02-2004 12:06 AM
Device Free Space jorge.ferreira UNIX for Dummies Questions & Answers 1 12-12-2003 08:17 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 11-10-2006
Registered User
 

Join Date: Nov 2006
Posts: 5
free disk space calc

I everybody!!
How can i use statvfs() to calculate disk usage and free disk space??

Im using this code:

/* Any file on the filesystem in question */
char *filename = "/home/nesto/test/test.cpp";

struct statvfs buf;
if (!statvfs(filename, &buf)) {

unsigned long blksize, blocks, freeblks, disk_size, used, free;
blksize = buf.f_bsize;
blocks = buf.f_blocks;
freeblks = buf.f_bfree;
disk_size = blocks * blksize;
free = freeblks * blksize;
used = disk_size - free; printf("Disk usage : %lu \t Free space %lu\n", used, free);

} else {

printf("Couldn't get file system statistics\n");
}
return 0;

But when i execute "df ." the values dont match
Can anyone help me.

Best regards to all
Reply With Quote
Forum Sponsor
  #2  
Old 11-10-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,274
Two points:

POSIX df reports blocks, you are reporting blocks * blocksize

Since you are reporting on a filesystem, other users can create/delete files,
so that in the time you run your code, exit, and then do a df, a lot can happen.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:13 AM.


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

Content Relevant URLs by vBSEO 3.2.0