readdir() and S_ISDIR(stat.st_mode) question (OT ?)

E

electric sheep

I'm not sure if this is POSIX or not ... and indeed if POSIX
is OT here or not, but if the subject made sense to you ....

I have a very simple program here, but it seems to be returning
a "true" value to the query S_ISDIR() even when the directory
entry is not a directory.

#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>

int main(int argc, char *argv[]) {
DIR *dir;
struct dirent *entry;
struct stat eStat;
char *root;
int i;

root = argv[1];

while((entry = readdir(dir)) != NULL) {
lstat(entry->d_name, &eStat);
if(S_ISDIR(eStat.st_mode))
printf("found directory %s\n", entry->d_name);
else
printf("not a dir\n");
}
return 0;
}


sample output:
found directory .
found directory ..
found directory NCURSES-Programming-HOWTO-html.tar.gz
found directory ncurses_programs
found directory ncurses.html
 
M

Martien Verbruggen

I'm not sure if this is POSIX or not ... and indeed if POSIX
is OT here or not, but if the subject made sense to you ....

Yes, it is a POSIX question, and it is offtopic here.

comp.unix.programmer is the place where they discuss POSIX.

Martien
 
V

Villy Kruse

Yes, it is a POSIX question, and it is offtopic here.

comp.unix.programmer is the place where they discuss POSIX.


Regardless of that, it is still a good idea to check possible errors
also from non-ANSI-C functions.


Villy
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top