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

Discussion in 'C Programming' started by minil, Mar 12, 2005.

  1. minil

    minil Guest

    Hi,

    i need C program or function to get me harddisk size, Free space,
    used space in linux operating system
    minil
     
    minil, Mar 12, 2005
    #1
    1. Advertising

  2. minil wrote:
    > 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
     
    Ulrich Eckhardt, Mar 12, 2005
    #2
    1. Advertising

  3. minil wrote:
    >
    > 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.

    --
    Peter
     
    Peter Nilsson, Mar 12, 2005
    #3
  4. <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.

    minil wrote:

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


    --
    Remove '.nospam' from e-mail address to reply by e-mail
     
    James McIninch, Mar 12, 2005
    #4
  5. minil

    Malcolm Guest

    "minil" <> wrote
    > 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;
    }
    }
     
    Malcolm, Mar 13, 2005
    #5
  6. minil

    jacob navia Guest

    Malcolm wrote:

    > "minil" <> wrote
    >
    >> 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;
    > }
    > }
    >
    >


    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. :)
     
    jacob navia, Mar 13, 2005
    #6
  7. minil wrote:
    > 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
     
    Thomas Matthews, Mar 13, 2005
    #7
  8. Re: How to get harddisk size , Free space , used space in Cfunction

    "Malcolm" <> writes:
    > "minil" <> wrote
    >> 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;
    > }
    > }


    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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 13, 2005
    #8
  9. Re: How to get harddisk size , Free space , used space in Cfunction

    jacob navia <> writes:
    > Malcolm wrote:
    >
    >> "minil" <> wrote
    >>
    >>> 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;
    >> }
    >> }
    >>

    >
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 13, 2005
    #9
  10. minil

    jacob navia Guest

    Keith Thompson wrote:
    > jacob navia <> writes:
    >
    >>Malcolm wrote:
    >>
    >>
    >>>"minil" <> wrote
    >>>
    >>>
    >>>> 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;
    >>> }
    >>>}
    >>>

    >>
    >>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.
    >



    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;
    }
     
    jacob navia, Mar 13, 2005
    #10
  11. minil

    jacob navia Guest

    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
     
    jacob navia, Mar 13, 2005
    #11
  12. In article <4234bc51$0$1235$>,
    jacob navia <> wrote:
    : 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.
    --
    This signature intentionally left... Oh, darn!
     
    Walter Roberson, Mar 14, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. angus

    how to access client harddisk?

    angus, Jun 15, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    758
    John Saunders
    Jun 15, 2004
  2. Phil Winstanley [Microsoft MVP ASP.NET]

    Re: how to access client harddisk?

    Phil Winstanley [Microsoft MVP ASP.NET], Jun 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    367
    angus
    Jun 15, 2004
  3. Replies:
    1
    Views:
    834
    Sebastian Millies
    Nov 2, 2006
  4. Steven Woody

    Get used/free local heap space

    Steven Woody, Nov 6, 2006, in forum: C Programming
    Replies:
    1
    Views:
    347
    Simon Biber
    Nov 6, 2006
  5. Jason Quek

    How to check size of harddisk?

    Jason Quek, Sep 28, 2004, in forum: Perl Misc
    Replies:
    10
    Views:
    214
    Thomas Kratz
    Oct 1, 2004
Loading...

Share This Page