For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > October 2004 > question on using stat









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 question on using stat
Janice

2004-10-18, 4:01 am

I am trying to emulate the unix command ls.
I have problem on using stat.
The stat statement returns -1, and the errno is always 2 which means ENOENT.
The component of the path file_name does not exist, or the path is an empty
string.
I reconize that it only success when the path of opendir is "."
Otherwise, it always fails.
What is the problem?
Thanx


struct dirent* dp;
struct stat* buf;
DIR* dir = opendir("./test/");
while(dp=readdir(dir)){
buf = (struct stat*)calloc(1,sizeof(struct stat));
if(stat(dp->d_name,buf)==0){
printf("%lu %s\n",(unsigned long)buf->st_ino,dp->d_name);
}else{
printf("ERROR%d\n",errno);
}
free(buf);
}
closedir(dir);


Michael Kerrisk

2004-10-18, 4:01 am

On Mon, 18 Oct 2004 12:47:55 +0800, "Janice" <no@mail.com> wrote:

>I am trying to emulate the unix command ls.
>I have problem on using stat.
>The stat statement returns -1, and the errno is always 2 which means ENOENT.
>The component of the path file_name does not exist, or the path is an empty
>string.
>I reconize that it only success when the path of opendir is "."
>Otherwise, it always fails.
>What is the problem?
>Thanx
>
>


I don't offhand see a cause for the problem. It would help people
help you if you post a short, complete, *well-formatted* program that
demonstrates the problem.


Cheers,

Michael
Jonathan Adams

2004-10-18, 4:01 am

In article <ckvhu1$8eb1@imsp212.netvigator.com>, "Janice" <no@mail.com>
wrote:

> I am trying to emulate the unix command ls.
> I have problem on using stat.
> The stat statement returns -1, and the errno is always 2 which means ENOENT.
> The component of the path file_name does not exist, or the path is an empty
> string.
> I reconize that it only success when the path of opendir is "."
> Otherwise, it always fails.
> What is the problem?
> Thanx
>


Could you please post complete examples (including the #include lines,
the main() definition, etc)? And please use indentation of some kind?

The problem you are running into is probably that you are trying to
stat(2) files in other directories without including their full path.
For example (using the shell):

% ls -f 2004-10-17
.. .. tmpc.c tmpc.c~ tmpc
% ls tmpc.c
tmpc.c: No such file or directory
% ls 2004-10-17/tmpc.c
2004-10-17/tmpc.c
%

So you need to prepend the argument to opendir(3C), plus a slash, before
the directory entry name. Something like:

char fname[PATH_MAX];
struct stat buf;
const char *dirname;
DIR *dir;
struct dirent *dp;

dirname = "./test/";
dir = opendir(dirname);
if (dir == NULL) {
/* handle failure */
}
while ((dp = readdir(dir)) != NULL) {
if (snprintf(fname, sizeof(fname), "%s/%s", dirname,
dp->d_name) >= sizeof (fname)) {
/* handle overflow */
}
if (stat(fname, &buf) == 0) {
/* success, use buf.st_ino, etc. */
} else {
/* failure */
}

}
closedir(dir);

Cheers,
- jonathan
Lew Pitcher

2004-10-18, 9:00 am

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Janice wrote:
> I am trying to emulate the unix command ls.
> I have problem on using stat.
> The stat statement returns -1, and the errno is always 2 which means ENOENT.
> The component of the path file_name does not exist, or the path is an empty
> string.
> I reconize that it only success when the path of opendir is "."
> Otherwise, it always fails.
> What is the problem?


stat(2) requires that the file_name parameter be a path. When you
opendir(3)/readdir(3), you don't get qualified paths in the dp->d_name string,
you only get filenames (relative to the path given in the opendir(3) call). So,
passing dp->d_name to stat(2) causes stat(2) to look for the unqualified
filename in the current working directory.

To fix this, either
a) chdir(2) to the directory that is the target of the opendir(3) call before
you call stat(2), or
b) reformat the dp->d_name to create a path (either absolute, or relative to
cwd) which you then will pass to stat(2).


[snip]

- --
Lew Pitcher
IT Consultant, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFBc7KdagVFX4UWr64RAmOXAKCXFkhtG0y9
XJ465bq6YxpTGcuMsgCgo441
xogJSX0BEBlLVanezZRTd4E=
=0mJc
-----END PGP SIGNATURE-----
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com