472,800 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,800 software developers and data experts.

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 5315
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
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
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
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
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
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
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
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
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
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
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.