Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   finding size of directory (http://www.velocityreviews.com/forums/t437805-finding-size-of-directory.html)

minil 04-20-2005 02:01 PM

finding size of directory
 
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


lndresnick@gmail.com 04-20-2005 02:33 PM

Re: finding size of directory
 

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


Pan Jiaming 04-20-2005 03:27 PM

Re: finding size of directory
 
Wed, 20 Apr 2005 07:01:31 -0700minilд

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



Richard Bos 04-20-2005 03:59 PM

Re: finding size of directory
 
Pan Jiaming <Jiaming.Pan@gmail.com> wrote:

> Wed, 20 Apr 2005 07:01:31 -0700minilд
>
> > 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

SM Ryan 04-21-2005 02:57 AM

Re: finding size of directory
 
"minil" <llakshmanakumar@rediffmail.com> 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.

Keith Thompson 04-21-2005 03:14 AM

Re: finding size of directory
 
SM Ryan <wyrmwif@tango-sierra-oscar-foxtrot-tango.fake.org> writes:
> "minil" <llakshmanakumar@rediffmail.com> 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) kst-u@mib.org <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.

SM Ryan 04-21-2005 12:02 PM

Re: finding size of directory
 
Keith Thompson <kst-u@mib.org> wrote:
# SM Ryan <wyrmwif@tango-sierra-oscar-foxtrot-tango.fake.org> writes:
# > "minil" <llakshmanakumar@rediffmail.com> 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.

Keith Thompson 04-21-2005 12:24 PM

Re: finding size of directory
 
[Non-traditional '#' quoting character changed to '>'.]

SM Ryan <wyrmwif@tango-sierra-oscar-foxtrot-tango.fake.org> writes:
> Keith Thompson <kst-u@mib.org> wrote:
> > SM Ryan <wyrmwif@tango-sierra-oscar-foxtrot-tango.fake.org> writes:
> > > "minil" <llakshmanakumar@rediffmail.com> 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) kst-u@mib.org <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.

SM Ryan 04-22-2005 12:54 AM

Re: finding size of directory
 
# 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?

Keith Thompson 04-22-2005 01:29 AM

Re: finding size of directory
 
SM Ryan <wyrmwif@tango-sierra-oscar-foxtrot-tango.fake.org> 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) kst-u@mib.org <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.


All times are GMT. The time now is 01:57 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.