473,513 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5362
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2701
by: Alex | last post by:
I've just noticed that directories in Unix have different sizes whereas in Windows they don't. Why is this so? Thanks. Alex.
12
12216
by: Ola Natvig | last post by:
Hi all Does anyone know of a fast way to calculate checksums for a large file. I need a way to generate ETag keys for a webserver, the ETag of large files are not realy nececary, but it would be...
25
5134
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to...
2
2501
by: kuhni | last post by:
Hi everybody! After searching newsgroups for a couple of hours, I now try asking directly (running the risk of asking again the same question). My problem is to predict when the size of the...
2
6425
by: hokieghal99 | last post by:
I wish to place all files and directories that are within a user defined path (on a Linux x86 PC) into some type of array and then examine those items for the existence of certain charaters such as...
5
11938
by: sugaray | last post by:
Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below: int...
6
1312
by: Theo v. Werkhoven | last post by:
Goodday, Something strange going on here. A piece of code I wrote bombs out in one of de directories under $HOME, but not in others. Here's a snipped: #v+ def bin2asc(c): s=''
4
2082
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Say I have a class like, class Sample { public decimal first = 10; public decimal second = 20; } I have initialized it
1
3528
by: cmb3587 | last post by:
My code runs fine for the most part...the only time it fails is when I type in a negative to end the array. I don't want the negative number to be included in the array and I thought that is what...
0
7265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7539
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5692
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5095
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4751
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3228
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.