469,648 Members | 1,398 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

Calculating a directories size


Is there any way using C library functions to get the size of a directory and its contents (without resorting to using a shell command)?

==================================
Poster's IP address: 193.123.19.116
Posted via http://nodevice.com
Linux Programmer's Site
Nov 13 '05 #1
11 5035
193.123.19.116 [Daz] writes:
Is there any way using C library functions to get the size of a
directory and its contents (without resorting to using a shell
command)?


No.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
Nov 13 '05 #2
193.123.19.116 [Daz] wrote in message news:<f4d49c98ee2e411a4b22a4cdeefe59a4@TeraNews>.. .
Is there any way using C library functions to get the size of a directory and its contents (without resorting to using a shell command)?

==================================
Poster's IP address: 193.123.19.116
Posted via http://nodevice.com
Linux Programmer's Site


Sure! You can calculate the size of each file, then accumulate them!

Quote from busybox:

long du(char *filename)
{
struct stat statbuf;
long sum;

if ((lstat(filename, &statbuf)) != 0) {
perror_msg("%s", filename);
return 0;
}
if (du_depth == 0)
dir_dev = statbuf.st_dev;
else if (one_file_system && dir_dev != statbuf.st_dev)
return 0;

du_depth++;
sum = (statbuf.st_blocks >> 1);

/* Don't add in stuff pointed to by symbolic links */
if (S_ISLNK(statbuf.st_mode)) {
sum = 0L;
if (du_depth == 1) {
}
}
if (S_ISDIR(statbuf.st_mode)) {
DIR *dir;
struct dirent *entry;
char *newfile;

dir = opendir(filename);
if (!dir) {
du_depth--;
return 0;
}

newfile = last_char_is(filename, '/');
if (newfile)
*newfile = '\0';

while ((entry = readdir(dir))) {
char *name = entry->d_name;

if ((strcmp(name, "..") == 0)
|| (strcmp(name, ".") == 0)) {
continue;
}
newfile = concat_path_file(filename, name);
sum += du(newfile);
free(newfile);
}
closedir(dir);
print(sum, filename);
}
else if (statbuf.st_nlink > 1 && !count_hardlinks) {
/* Add files with hard links only once */
if (is_in_ino_dev_hashtable(&statbuf, NULL)) {
sum = 0L;
if (du_depth == 1)
print(sum, filename);
}
else {
add_to_ino_dev_hashtable(&statbuf, NULL);
}
}
du_depth--;
return sum;
}
Nov 13 '05 #3
On 24 Jul 2003 19:18:15 -0700,
Kevin Wan <jf***@vip.sina.com> wrote:
193.123.19.116 [Daz] wrote in message news:<f4d49c98ee2e411a4b22a4cdeefe59a4@TeraNews>.. .
Is there any way using C library functions to get the size of a
directory and its contents (without resorting to using a shell
command)?
Sure! You can calculate the size of each file, then accumulate them!

Quote from busybox:

long du(char *filename)
{
struct stat statbuf;


This is not C.
long sum;

if ((lstat(filename, &statbuf)) != 0) {
perror_msg("%s", filename);


And these aren't either.

You're confusing C with POSIX, or one of the other Unix-related
standards. The OP did not state they were on a system that implements
the POSIX standard.

To the OP: ANSI C has no concept of a directory, and no, there is no
direct way to get the size of a file. You can open the file, and count
the characters, but you're probably looking for some system specific
implementations, like the ones mentioned in the previous post. You
should ask these questions in a group that talks about your platform,
because you are most likely to get the best answer there.

Martien
--
|
Martien Verbruggen |
Trading Post Australia | Hi, Dave here, what's the root password?
|
Nov 13 '05 #4
In <sl*****************@verbruggen.comdyn.com.au> Martien Verbruggen <mg**@tradingpost.com.au> writes:
On 24 Jul 2003 19:18:15 -0700,
Kevin Wan <jf***@vip.sina.com> wrote:
193.123.19.116 [Daz] wrote in message news:<f4d49c98ee2e411a4b22a4cdeefe59a4@TeraNews>.. .
Is there any way using C library functions to get the size of a
directory and its contents (without resorting to using a shell
command)?
Sure! You can calculate the size of each file, then accumulate them!

Quote from busybox:

long du(char *filename)
{
struct stat statbuf;


This is not C.


Looks like a valid definition of a struct object to me.
long sum;

if ((lstat(filename, &statbuf)) != 0) {
perror_msg("%s", filename);


And these aren't either.


Can't see anything a C compiler would have to complain about.
You're confusing C with POSIX, or one of the other Unix-related
standards.


Nope, you're the confused one. A C program is not limited to using the
standard C library. It's just that such a C program is not a portable
program. If I write a C program using the POSIX-defined libraries, it is
still a C program: it can't be a POSIX program because POSIX is not a
programming language.

Objecting to non-portable code is fine. Claiming that it is not C code
is downright idiotic, unless the code actually requires a diagnostic.

And the C standard itself is even more permissive in what it considers to
be a C program: see the definition of conforming C program.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #5
On 24 Jul 2003 19:18:15 -0700, in comp.lang.c , jf***@vip.sina.com
(Kevin Wan) wrote:
193.123.19.116 [Daz] wrote in message news:<f4d49c98ee2e411a4b22a4cdeefe59a4@TeraNews>.. .
Is there any way using C library functions to get the size of a directory and its contents (without resorting to using a shell command)?

==================================
Poster's IP address: 193.123.19.116
Posted via http://nodevice.com
Linux Programmer's Site


Sure! You can calculate the size of each file, then accumulate them!


Not only is your code not ISO C, but hte method is also wrong. You
have no way of knowing whether the OS allocates extra space between
files, allocates in fixed block sizes, or whatever.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #6
>there is no direct way to get the size of a file. You can open the
file, and
count the characters, but you're probably looking for some system specificimplementations.


fseek(f, 0, SEEK_END);
size = ftell(f);

could be a standard C way to get the size of a file, but you probably
already know that.

Claudio
Nov 13 '05 #7
On 26 Jul 2003 13:31:03 -0700, si*******@yahoo.it (claudibus) wrote in
comp.lang.c:
there is no direct way to get the size of a file. You can open the

file, and
count the characters, but you're probably looking for some system

specific
implementations.


fseek(f, 0, SEEK_END);
size = ftell(f);

could be a standard C way to get the size of a file, but you probably
already know that.

Claudio


Indeed, it is standard C. But it is not guaranteed to give you the
actual size of the file.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #8
> > >there is no direct way to get the size of a file. You can open the
file, and
count the characters, but you're probably looking for some system specificimplementations.


fseek(f, 0, SEEK_END);
size = ftell(f);

could be a standard C way to get the size of a file, but you probably
already know that.

Claudio


Indeed, it is standard C. But it is not guaranteed to give you the
actual size of the file.


Yes, only works with binary files whose size fit in a long. It even
depends on what we mean with 'size of the file' (if we mean the length
of the content in bytes we might get away with it - still no clue on
actual blocks occupied on disk).

Cld
Nov 13 '05 #9
Ben Pfaff wrote:
193.123.19.116 [Daz] writes:
Is there any way using C library functions to get the size of a
directory and its contents (without resorting to using a shell
command)?


No.


Great answer! Exactly what the OP wanted to know.
Nov 13 '05 #10
claudibus wrote:
there is no direct way to get the size of a file. You can open the

file, and
count the characters, but you're probably looking for some system

specific
implementations.


fseek(f, 0, SEEK_END);
size = ftell(f);

could be a standard C way to get the size of a file, but you probably
already know that.


Doesn't work (portably). If the file is binary, SEEK_END need not be
meaningful. If the file is text, the count may not take into account
line-end conversions.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #11
Dan Pop wrote:
In <3f******@news2.power.net.uk> Richard Heathfield
<in*****@address.co.uk.invalid> writes:
claudibus wrote:
fseek(f, 0, SEEK_END);
size = ftell(f);

could be a standard C way to get the size of a file, but you probably
already know that.


Doesn't work (portably). If the file is binary, SEEK_END need not be
meaningful. If the file is text, the count may not take into account
line-end conversions.


If the file is text, the value returned by ftell() need not be a count
at all:

For a text stream, its file position indicator
contains unspecified information, usable by the fseek function
^^^^^^^^^^^^^^^^^^^^^^^


Ah, I knew I'd forgotten something. I just didn't know what it was.

Thanks.

<snip>

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Alex | last post: by
12 posts views Thread by Ola Natvig | last post: by
2 posts views Thread by hokieghal99 | last post: by
5 posts views Thread by sugaray | last post: by
6 posts views Thread by Theo v. Werkhoven | last post: by
4 posts views Thread by =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post: by
1 post views Thread by cmb3587 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.