LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2006, 09:26 AM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
Disk usage info in C/C++


Well, the topic says pretty much everything. It's a simple job, just get the same info as df does. But how?
 
Old 03-12-2006, 01:21 PM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
I would suggest you look into the statfs function call, make sure you check your local man pages as I think it's very non standard.

More generally, if you want to know what system calls a program makes, you can use strace.
 
Old 03-12-2006, 01:47 PM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Quote:
Originally Posted by kev82
I would suggest you look into the statfs function call, make sure you check your local man pages as I think it's very non standard.
I looked at the statvfs() function, is statfs() similar? Cause as far as I understand it, I can only get the size of a certain file within a filesystem that way. What I want to know is the size of the disk/partition, just like the info provided by df.
 
Old 03-12-2006, 02:03 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
df doesn't give disk or partition sizes but filesystem information.
 
Old 03-12-2006, 02:40 PM   #5
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
by Ephracis
I looked at the statvfs() function, is statfs() similar?

Yes. I didn't know until now but it appears that statvfs calls statfs

by Ephracis
Cause as far as I understand it, I can only get the size of a certain file within a filesystem that way.

No, I quote from the man page for statvfs - "The function statvfs returns information about a mounted file system."

by Ephracis
What I want to know is the size of the disk/partition, just like the info provided by df.

As already stated by jlliagre, df does not return disk/partition information. It returns filesystem information. What would you expect df to say about a filesystem on a raid0 array? Information for each disk?

I quote from "strace df" to show you that it does use this system call (at least on my system)

Code:
statfs64("/home", 84, {f_type="REISERFS_SUPER_MAGIC", f_bsize=4096, f_blocks=6536975, f_bfree=1513580, f_bavail=1513580, f_files=0, f_ffree=0, f_fsid={0, 0}, f_namelen=255, f_frsize=4096}) = 0
write(1, "/dev/ida/c0d0p6       26147900  "..., 62/dev/ida/c0d0p6       26147900  20093580   6054320  77% /home
) = 62
 
Old 03-13-2006, 03:05 PM   #6
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
The hard, system-dependent way: pipe, fork, exec, parse.

Make a new pipe to share between two processes.
Fork; close the correct ends of the pipe.
Exec one child to "df", send output through the pipe.
Use the second child to parse the output from the first.

That's not very nice. But if you can't find the system call you want, it will work. Of course, with this method, C is hardly the ideal language...
 
Old 03-14-2006, 03:14 AM   #7
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
Example here:
http://www.linuxquestions.org/questi...d.php?t=419927
 
Old 03-14-2006, 03:30 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
And precisely your (nx5000) posting in this thread here:

http://www.linuxquestions.org/questi...37#post2126437

which shows that directly using the C apis and not popen/shell is simpler, faster and a more elegant answer to the question.

Edit: Ouch, but the sample code has a bug, the printf should be:
Code:
   printf("Bytes left:%ld\n", sbuf.f_frsize*sbuf.f_bavail);

Last edited by jlliagre; 03-14-2006 at 03:38 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getting realtime info on memory usage-cpu and harddrive usage steering Linux - Newbie 5 03-03-2005 08:43 PM
Disk Usage karan101 Linux - Software 8 10-17-2004 03:59 AM
Pango : Need info abt the compilation & it's usage Pawan_shirbhate Red Hat 0 04-20-2004 04:17 AM
101% disk usage on an empty disk tsiuser Linux - Software 3 01-28-2004 06:24 PM
Disk Usage dvong3 Linux - Software 2 06-13-2003 12:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration