About checking executable files

M

mhk

Hi ,

i am writing a c language program in Unix to see the executable files in
a directory and it works if the directory is current but if i change to
another directory than current directory then it show all the files even
if not executable.

here is the main code

while (p = readdir(dp))
{
stat(p->d_name, &s);

if (strcmp( p->d_name,".")!= 0 && strcmp( p->d_name,"..")!= 0 )
{
if (s.st_mode & S_IXUSR)
{
cc = ctime(&s.st_mtime);
printf("%s| %s \n", p->d_name, cc);
}
}
}

can anyone help?

Thanks alot.

Jeff
 
D

Derk Gwen

# Hi ,
#
# i am writing a c language program in Unix to see the executable files in
# a directory and it works if the directory is current but if i change to
# another directory than current directory then it show all the files even
# if not executable.
#
# here is the main code
#
# while (p = readdir(dp))

Returns the path relative to the directory being read. Unless that
is the current directory, you're looking for files in the wrong
directory. You have to prefix the directory path to d_name.

# stat(p->d_name, &s);

If you checked the return value and errno of stat, it would be telling
you you've got nonexistent files.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top