Unix Programming - erroneous file size

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2004 > erroneous file size





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author erroneous file size
Aaron Walker

2004-01-23, 5:02 pm

I have the following function, send_dir(), that sends a dir listing to a
file descriptor. For some reason, it is not printing the correct file
size for each file it lists.

static void send_dir(int clientfd, char *url)
{
struct dirent *entry;
DIR *dir;
FILE *fp;

fp = fdopen(clientfd, "w");
dir = opendir(url);

fprintf(fp, dir_start, url, url);

while((entry = readdir(dir))) {
struct stat finfo;

if((!strcmp(entry->d_name, ".")) ||
(!strcmp(entry->d_name, "..")))
continue;

stat(entry->d_name, &finfo);
fprintf(fp, "%s %ld<br>\n",
entry->d_name, (long)finfo.st_size);
}

fprintf(fp, dir_end);
fflush(fp);
closedir(dir);
}

For example, I have the following output from ls:

# ls -l
total 4
-rw-r--r-- 1 root root 22 Dec 2 07:32 lala
-rw-r--r-- 1 root root 0 Nov 23 17:41 test

But the code above produces:

lala 134527336
test 4096

Any ideas?

Thanks,
Aaron

Lew Pitcher

2004-01-23, 5:02 pm

Aaron Walker wrote:
quote:

> Please disregard my last post as I am an idiot for not checking the
> return value of stat() :|



You will also want to correct the parameters given to stat()

You have
quote:

> stat(entry->d_name, &finfo);


and stat() will look for entry->dname within the current working directory,
and not the directory that the dirent was derived from.

Here, you'll have to format the fully qualified pathname of the dirent
retrieved, and pass /that/ to stat().

--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Aaron Walker

2004-01-23, 5:02 pm

Please disregard my last post as I am an idiot for not checking the
return value of stat() :|

Aaron

=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=

2004-01-23, 5:02 pm

Lew Pitcher <Lew.Pitcher@td.com> writes:
[QUOTE][color=darkred]

Has anyone else noticed this very same error appearing here rather
frequently for a couple of weeks?

--
Måns Rullgård
mru@kth.se
Lew Pitcher

2004-01-23, 5:02 pm

Aaron Walker wrote:
quote:

> Please disregard my last post as I am an idiot for not checking the
> return value of stat() :|



You will also want to correct the parameters given to stat()

You have
quote:

> stat(entry->d_name, &finfo);


and stat() will look for entry->dname within the current working directory,
and not the directory that the dirent was derived from.

Here, you'll have to format the fully qualified pathname of the dirent
retrieved, and pass /that/ to stat().

--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

=?iso-8859-1?q?M=E5ns_Rullg=E5rd?=

2004-01-23, 5:02 pm

Lew Pitcher <Lew.Pitcher@td.com> writes:
[QUOTE][color=darkred]

Has anyone else noticed this very same error appearing here rather
frequently for a couple of weeks?

--
Måns Rullgård
mru@kth.se
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com