• C

executable file

In a C program, How it is possible to know that a particular file is executable?
Who is Participating?
rbrConnect With a Mentor Commented:
#include <sys/stat.h>
#include <unistd.h>
main ()
char file_name[]="/usr/home/stc/friend"

struct stat stat_buf;

stat (file_name,&stat_buf);

if (stat_buf.st_mode & S_IXUSR)
printf ("Execute by user");
if (stat_buf.st_mode & S_IXGRP)
printf ("Execute by group");
if (stat_buf.st_mode & S_IXOTH)
printf ("Execute by others");

Are you just interested in the actual file name or the file structure?

if filename then you could use command line arguments since the first argument is the filename.  Then you could use strtok or strstr to point to the "." and pull in the extension.

if structure, then my reference says that the first word of an executable file should be EXE program identifier.  It lists this value as being 5A4DH.  It's not clear to me if this is a sample value or always the case.

It depends on your OS. Which OS do you use?
Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

That's always the case for a EXE
navin_agrawalAuthor Commented:
Let me be specific in telling the things. Following assumptions are made:

OS: Unix
File Name: /usr/home/stc/friend
Language: C

main ()
      char file_name[][] = "/usr/home/stc/friend"

      Here I want to know whether the file in file_name variable is executable or not. How to do this?
} /* end of mail () */
You'll have more luck posting in the UNIX Programming topic area.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.