I am trying to enumerate only files with the execute (+x) bit set. My code seems to list all files. It also seems to enumerate directory and the above directory which I do not want. Example:
..
should_not_be_executable.sh
.
Is there a way to filter '..' and '.' without strstr()? Here is my code
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
int
main (void)
{
DIR *dp;
struct dirent *ep;
dp = opendir ("/tmp/hi");
if (dp != NULL)
{
while (ep = readdir (dp))
{
struct stat sb;
if ((stat(ep->d_name, &sb) >= 0) && (sb.st_mode > 0) && (S_IEXEC & sb.st_mode));
puts(ep->d_name);
}
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
return 0;
}
Thanks in advance
ep->d_name
contains only relative pathname of the directory entry. So you should change the Current Working Directory to/tmp/hi
before callingstat(2)
As noted in the comments by @Andrew Medico, remove that extra
;
at the end of yourif
line to avoid unnecessary printing of thatputs()
line.readdir()
returns aNULL
pointer when it reaches at the end of directory, so you should rewrite your while loop as follows in order to suppress a compiler warning:In order to avoid printing
.
and..
, use anif
condition like this in thewhile
body:Similarly, you can use
S_ISDIR(m)
macro to find out if the current entry is a directory and choose to not printing it.