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 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
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;
}
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?
|
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
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 =---
>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
> > >there is no direct way to get the size of a file. You can open the file, andcount 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
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.
claudibus wrote: there is no direct way to get the size of a file. You can open the file, andcount 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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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=''
|
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
|
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...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| |