A
A
alex5e2015-05-12 21:17:58
linux
alex5e, 2015-05-12 21:17:58

How to find executable files in ubuntu in C++?

Good evening. Can you tell me how to find all the files in the directory that are executable in C ++ in Ubuntu?
Here is the code:

#include <stdio.h>
#include <dirent.h>
#include <string.h>

int main(int argc, char **argv)
{
  struct dirent *DirEntry;
  unsigned char isFile =0x8;
  DIR *dfd;
  struct dirent *dp;
  char filename[NAME_MAX];

  if ( argc < 2 )
  strcpy(filename, ".");
  else
  strcpy(filename, argv[1]);

  printf("%s\n\n", filename);
  dfd=opendir(filename);

  while((dp=readdir(dfd)) != NULL )
  {
    if(dp->d_type == isFile)
     	printf("%s\n", dp->d_name);
    
  }

  closedir(dfd);
  return 0;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman Hinex, 2015-05-12
@alex5e

stackoverflow.com/questions/5719694/having-a-path-...

#include <sys/stat.h>

bool can_exec(const char *file)
{
    struct stat  st;

    if (stat(file, &st) < 0)
        return false;
    if ((st.st_mode & S_IEXEC) != 0)
        return true;
    return false;
}

M
marble, 2015-05-12
_

stat() can help you

D
D', 2015-05-12
@Denormalization

linux.die.net/man/2/access
More specifically here:
stackoverflow.com/questions/16038486/how-do-i-chec...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question