Calculating a directories size

  • Thread starter 193.123.19.116 [Daz]
  • Start date
1

193.123.19.116 [Daz]

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
 
B

Ben Pfaff

193.123.19.116 said:
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.
 
K

Kevin Wan

193.123.19.116 [Daz] wrote in message news: said:
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;
}
 
M

Martien Verbruggen

193.123.19.116 [Daz] wrote in message news: said:
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
 
D

Dan Pop

In said:
193.123.19.116 [Daz] wrote in message news: said:
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.
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
 
M

Mark McIntyre

193.123.19.116 [Daz] wrote in message news: said:
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.
 
C

claudibus

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
 
R

Richard Heathfield

claudibus said:
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.
 
J

Jack Klein

C

claudibus

there is no direct way to get the size of a file. You can open the
file, and

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
 
R

Richard Heathfield

Dan said:
In <[email protected]> Richard Heathfield


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>
 

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
474,075
Messages
2,570,564
Members
47,200
Latest member
Vanessa98N

Latest Threads

Top