finding size of directory

Discussion in 'C Programming' started by minil, Apr 20, 2005.

  1. minil

    minil Guest

    Hi
    any c function in linux to return size of directory (Including its
    files & subdirectories & its files)..

    I can stat() only files . not for directories.

    please reply me soon

    Thanks in advance
     
    minil, Apr 20, 2005
    #1
    1. Advertising

  2. minil

    Guest

    minil wrote:
    > Hi
    > any c function in linux to return size of directory (Including its
    > files & subdirectories & its files)..
    >
    > I can stat() only files . not for directories.
    >
    > please reply me soon
    >
    > Thanks in advance


    Not in standard C, so not for this newsgroup. You want to be asking in
    a unix or linux group, say comp.unix.programmer. Followup set to
    there.

    -David
     
    , Apr 20, 2005
    #2
    1. Advertising

  3. minil

    Pan Jiaming Guest

    ÓÚ Wed, 20 Apr 2005 07:01:31 -0700£¬minilдµ½£º

    > Hi
    > any c function in linux to return size of directory (Including its
    > files & subdirectories & its files)..
    >
    > I can stat() only files . not for directories.
    >
    > please reply me soon
    >
    > Thanks in advance

    I am afraid there is no such function in c, you have to stat all the files
    and subdirectories recursively. That's why it takes a long time for
    "du" to stat a large directory.
     
    Pan Jiaming, Apr 20, 2005
    #3
  4. minil

    Richard Bos Guest

    Pan Jiaming <> wrote:

    > ÓÚ Wed, 20 Apr 2005 07:01:31 -0700£¬minilдµ½£º
    >
    > > any c function in linux to return size of directory (Including its
    > > files & subdirectories & its files)..
    > >
    > > I can stat() only files . not for directories.


    > I am afraid there is no such function in c, you have to stat all the files
    > and subdirectories recursively.


    Not even that. stat() isn't ISO C, either. The good news is that
    whichever extension you use which defines stat() (in the OP's case,
    POSIX), is also very likely to define directory walking functions. But
    these, too, are off-topic here, and on-topic in (in this case)
    comp.unix.programmer.

    Richard
     
    Richard Bos, Apr 20, 2005
    #4
  5. minil

    SM Ryan Guest

    "minil" <> wrote:
    # Hi
    # any c function in linux to return size of directory (Including its
    # files & subdirectories & its files)..

    Do something like
    system("du '/path/to/directory' | tail -1 | cut -f 1 >/tmp/directorysize") ;
    FILE *f = fopen("/tmp/directorysize","r");
    int size; fscanf(f,"%d",&size); fclose(f);

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    You hate people.
    But I love gatherings. Isn't it ironic.
     
    SM Ryan, Apr 21, 2005
    #5
  6. SM Ryan <> writes:
    > "minil" <> wrote:
    > > Hi
    > > any c function in linux to return size of directory (Including its
    > > files & subdirectories & its files)..

    >
    > Do something like
    > system("du '/path/to/directory' | tail -1 | cut -f 1 >/tmp/directorysize") ;
    > FILE *f = fopen("/tmp/directorysize","r");
    > int size; fscanf(f,"%d",&size); fclose(f);


    (Non-traditional '#' quoting character changed to '>'.)

    As long as you can assume the existence of the "du", "tail" and "cut"
    commands, the shell's "|" and ">" syntax, and the ability to create
    files whose names start with "/tmp/", you've already gone beyond any
    assumptions of portability. You might as well use system-specific
    routines that let you avoid creating a temporary file
    (<OT>popen()</OT>).

    For that matter, you shouldn't you can create "/tmp/directorysize".
    There may already be a file of that name, especially if other users
    happen to be running the same program. The standard provides the
    tmpnam() function for this purpose; your system may provide something
    better.

    It's also worth noting that the code fragment above assumes C99 (or at
    least extended C90), since it mixes declarations and statements.

    --
    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, Apr 21, 2005
    #6
  7. minil

    SM Ryan Guest

    Keith Thompson <> wrote:
    # SM Ryan <> writes:
    # > "minil" <> wrote:
    # > > Hi
    # > > any c function in linux to return size of directory (Including its
    # > > files & subdirectories & its files)..
    # >
    # > Do something like
    # > system("du '/path/to/directory' | tail -1 | cut -f 1 >/tmp/directorysize") ;
    # > FILE *f = fopen("/tmp/directorysize","r");
    # > int size; fscanf(f,"%d",&size); fclose(f);

    # As long as you can assume the existence of the "du", "tail" and "cut"
    # commands, the shell's "|" and ">" syntax, and the ability to create
    # files whose names start with "/tmp/", you've already gone beyond any
    # assumptions of portability. You might as well use system-specific
    # routines that let you avoid creating a temporary file
    # (<OT>popen()</OT>).

    popen() is not ANSI C. system() is.

    # For that matter, you shouldn't you can create "/tmp/directorysize".

    '...something like...'

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    I'm not even supposed to be here today.
     
    SM Ryan, Apr 21, 2005
    #7
  8. [Non-traditional '#' quoting character changed to '>'.]

    SM Ryan <> writes:
    > Keith Thompson <> wrote:
    > > SM Ryan <> writes:
    > > > "minil" <> wrote:
    > > > > Hi
    > > > > any c function in linux to return size of directory (Including its
    > > > > files & subdirectories & its files)..
    > > >
    > > > Do something like
    > > > system("du '/path/to/directory' | tail -1 | cut -f 1 >/tmp/directorysize") ;
    > > > FILE *f = fopen("/tmp/directorysize","r");
    > > > int size; fscanf(f,"%d",&size); fclose(f);

    >
    > > As long as you can assume the existence of the "du", "tail" and "cut"
    > > commands, the shell's "|" and ">" syntax, and the ability to create
    > > files whose names start with "/tmp/", you've already gone beyond any
    > > assumptions of portability. You might as well use system-specific
    > > routines that let you avoid creating a temporary file
    > > (<OT>popen()</OT>).

    >
    > popen() is not ANSI C. system() is.


    Yes, I know that. Did you read what I wrote? The code you posted is
    non-portable because of what it assumes about the argument to
    system(). Using popen() instead doesn't make it any less portable
    than it already is (except that it would then fail to compile on
    non-Unix systems rather than failing at run-time -- which I consider
    an improvement).

    --
    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, Apr 21, 2005
    #8
  9. minil

    SM Ryan Guest

    # Yes, I know that. Did you read what I wrote? The code you posted is
    # non-portable because of what it assumes about the argument to
    # system(). Using popen() instead doesn't make it any less portable

    Because if it used popen() you would complain that it is not ANSI C.
    The code given be made into working ANSI C conforming code despite the
    many false claims that it could not be done in ANSI C.

    # than it already is (except that it would then fail to compile on
    # non-Unix systems rather than failing at run-time -- which I consider
    # an improvement).

    Those of us used to working all multiple systems know how to deal with this.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Who's leading this mob?
     
    SM Ryan, Apr 22, 2005
    #9
  10. SM Ryan <> writes:
    > > Yes, I know that. Did you read what I wrote? The code you posted is
    > > non-portable because of what it assumes about the argument to
    > > system(). Using popen() instead doesn't make it any less portable

    >
    > Because if it used popen() you would complain that it is not ANSI C.


    ['#' --> '>', as usual]

    If it used popen() I would complain that it was posted to the wrong
    newsgroup (comp.lang.c rather than comp.unix.programmer).

    As it is, I'm complaining that it's non-portable *and* needlessly
    inefficient and error-prone. It's inappropriate for any newsgroup
    that doesn't deal with Unix-like systems because it depends on
    Unix-specific features. If it appeared on comp.unix.programmer, I'd
    complain about using a temp file rather than popen().

    > The code given be made into working ANSI C conforming code despite the
    > many false claims that it could not be done in ANSI C.


    Sure, it's conforming code in that it's going to compile, even on a
    Windows or DS9000 system. It's non-portable in that it makes
    implementation-specific assumptions about the argument to system().

    > > than it already is (except that it would then fail to compile on
    > > non-Unix systems rather than failing at run-time -- which I consider

    > . an improvement).
    >
    > Those of us used to working all multiple systems know how to deal with this.


    The usual way is to have several different versions of the code,
    either by having multiple versions of the same file, or using
    conditional compilation, or generating the source file from a
    template. I'm sure there are other ways. None of them require using
    a temporary file rather than popen() for the sake of ANSI C
    compliance.

    The fact is, there is no way in portable standard C to determine the
    size of a directory, but there are many ways to do it non-portably.
    Some non-portable techniques are better than others.

    --
    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, Apr 22, 2005
    #10
    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. =?Utf-8?B?TWljaGFlbA==?=

    Finding the size of a document in a IFrame

    =?Utf-8?B?TWljaGFlbA==?=, Apr 28, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    550
    Marina
    May 14, 2004
  2. Ragemare
    Replies:
    4
    Views:
    467
    Muhammad Naveed Yaseen
    Apr 8, 2004
  3. Sean Ross

    finding file size

    Sean Ross, Jan 2, 2004, in forum: Python
    Replies:
    17
    Views:
    555
    Martin v. Loewis
    Jan 4, 2004
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,653
    Michael Jung
    May 25, 2008
  5.  Tom
    Replies:
    3
    Views:
    141
    Robert Klemme
    Nov 11, 2009
Loading...

Share This Page