How to get harddisk size , Free space , used space in C function

M

minil

Hi,

i need C program or function to get me harddisk size, Free space,
used space in linux operating system
minil
 
U

Ulrich Eckhardt

minil said:
i need C program or function to get me harddisk size, Free space,
used space in linux operating system
minil

The program 'df' does that (not only on Linux), its sources are available
Out There(tm).

Uli
 
P

Peter Nilsson

minil said:
i need C program or function to get me harddisk size, Free
space, used space in linux operating system

Then please ask a Linux specific group. ISO C does not supply
any immediate mechansims for such things, so your post is
off-topic in comp.lang.c.
 
J

James McIninch

<posted & mailed>

No way to get this info in C. Look for a library for your target platforms
that might provide this functionality and consult the documentation for
instruction on how to call it from C and link to it.
 
M

Malcolm

minil said:
i need C program or function to get me harddisk size, Free space,
used space in linux operating system
minil

unsigned long harddisksize(void)
{
unsigned long answer = 0;

do
{
FILE *fp = fopen( tmpnam(0), "wb"));
if(!fp(break));
if(fputc('x', fp) == EOF)
break;
answer++;
}
while(fclose(fp) == 0);

printf("You had %lu bytes of disk space\n", answer);

return answer;
}
}
 
J

jacob navia

Malcolm said:
unsigned long harddisksize(void)
{
unsigned long answer = 0;

do
{
FILE *fp = fopen( tmpnam(0), "wb"));
if(!fp(break));
if(fputc('x', fp) == EOF)
break;
answer++;
}
while(fclose(fp) == 0);

printf("You had %lu bytes of disk space\n", answer);

return answer;
}
}

I think it would be slightly better if you did:
char *tmpname = tmpnam(0);
FILE *fp = fopen(tmpname,"wb");
...
remove(tmpname); // this restores the free space. :)
 
T

Thomas Matthews

minil said:
Hi,

i need C program or function to get me harddisk size, Free space,
used space in linux operating system
minil

Doesn't anybody read the FAQ before posting?
See below.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
K

Keith Thompson

Malcolm said:
unsigned long harddisksize(void)
{
unsigned long answer = 0;

do
{
FILE *fp = fopen( tmpnam(0), "wb"));
if(!fp(break));
if(fputc('x', fp) == EOF)
break;
answer++;
}
while(fclose(fp) == 0);

printf("You had %lu bytes of disk space\n", answer);

return answer;
}
}

I presume this was meant as a joke. It attempts to fill the file
system with 1-byte files and reports how many it was able to create.

Apart from the folly of filling the disk to determine how big it is
(and not freeing the allocated disk space; the use of the past tense
in the printed message acknowledges this), tmpnam() can't necessarily
return more than TMP_MAX unique file names, and TMP_MAX is only
required to be at least 25. Creating a 1-byte file will almost
certainly use more than 1 byte of disk space. Even assuming each file
occupies only 1 byte, and ignoring the TMP_MAX issue, the amount of
free disk space is likely to exceed ULONG_MAX; if so, the answer
returned will be the actual amount modulo (ULONG_MAX+1). Finally, the
function should either print the result or return it; doing both is
silly.

I'd put the "while" on the same line as the closing brace; on first
reading I thought it was an empty while loop rather than a do-while
loop.

And of course typical systems have multiple disjoint file systems;
your function has no way to control which one is used (you're going to
get whichever one tmpnam() happens to use).

If I were really going to do this in this way, I'd probably see how
many bytes I can write to a single file. Depending on the system, I'd
likely exceed my quota before running out of space and/or seriously
interfere with other users.

This is one of those problems for which there are no decent portable
solutions, but many perfectly good system-specific solutions.
 
K

Keith Thompson

jacob navia said:
I think it would be slightly better if you did:
char *tmpname = tmpnam(0);
FILE *fp = fopen(tmpname,"wb");
...
remove(tmpname); // this restores the free space. :)

No, I think the original version is better. Your version (if tmpnam()
behaves in certain ways) runs forever without using much disk space.
The original version is more likely to get whoever runs it kicked off
the system so he can't do any more damage.
 
J

jacob navia

Keith said:
No, I think the original version is better. Your version (if tmpnam()
behaves in certain ways) runs forever without using much disk space.
The original version is more likely to get whoever runs it kicked off
the system so he can't do any more damage.


You misunderstood apparently. The lines I posted should be *added*
to the code. Here is the full version:
unsigned long harddisksize(void)
{
unsigned long answer = 0;
do
{
// replace this line
// FILE *fp = fopen( tmpnam(0), "wb"));
// with
char *tmpname = tmpnam(0);
FILE *fp = fopen(tmpname,"wb");
// OK? The rest is the same
if(!fp(break));
if(fputc('x', fp) == EOF)
break;
answer++;
}
while(fclose(fp) == 0);
printf("You had %lu bytes of disk space\n", answer);
// ADD THIS
remove(tmpname); // this restores the free space. :)
return answer;
}
 
J

jacob navia

Jeeezz...

I did not see the bug in the code posted...

He is opening gigabytes of temporary files to
fill the disk instead of writing a single file...

OK. Sorry
 
W

Walter Roberson

: FILE *fp = fopen(tmpname,"wb");

: if(!fp(break));

I've been trying to figure out what second statement does. fp is
a FILE* there, not a function pointer. It isn't [] so we aren't
using one of those reverse indexing instances. So if it parses
at all, it would appear to try to convert fp from a FILE* to
a function pointer (defaulting to returning an integer value)
and so would evaluate the part in () as an expression whose
value is to be passed to the byte-stream at location fp.
But break is not a valid expression -- break followed
by a semi-colon is a statement... but break is a keyword
and so cannot be an identifier.

So it seems to me that if(!fp(break)); is unlikely to parse.
Have I missed something? if (!fp) break; would be a lot clearer
there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top