finding size of directory

M

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
 
L

lndresnick

minil said:
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
 
P

Pan Jiaming

ÓÚ 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.
 
R

Richard Bos

Pan Jiaming said:
ÓÚ Wed, 20 Apr 2005 07:01:31 -0700£¬minilдµ½£º
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
 
S

SM Ryan

# 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);
 
K

Keith Thompson

SM Ryan said:
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.
 
S

SM Ryan

# > > 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...'
 
K

Keith Thompson

[Non-traditional '#' quoting character changed to '>'.]

SM Ryan said:
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).
 
S

SM Ryan

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

Keith Thompson

SM Ryan said:
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().
. 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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top