Thread: How to determine if a file is executable or not?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    How to determine if a file is executable or not?

    Hi fellows,

    I have been given the darness task of determining whether a file is an executable or not. I know there is a shell command called file that does this. I was wondering if it can be done in c, or a combination of the two, since I need to perform some things to the executable file inside the c program.

    I have been googling alot but have found only that files have magic number at the header that tells the kernel what type it is. However there is nothing I could find that tells me how to access this number or what number it is that determines if the file is an executable or not. I need an answer asap please. thanks.

    oh btw, i'm working under the ext2 file system (linux) if that is of any relevance.

    supernewb.
    Last edited by supernewb; 04-13-2005 at 11:36 AM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    A simple way would be to use the access() function (I think it's in <unistd.h>). It takes two parameters, the file name, and what you're looking for. It returns 0 on success.

    Code:
    if(access("~/test",X_OK)) {
      printf("not executable");
    } else {
      printf("executable");
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Did you know that (under DOS and Windows) all executables begin with MZ? Maybe in Linux there is a similarly distinguishing label in executable headers...
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Here's another method I just discovered.
    Code:
    FILE *pipe = popen("file ~/test | grep -i executable","r");
    char buffer[2048];
    fgets(buffer,2048,pipe);
    if(strlen(buffer) > 5) printf("woot\n");
    else printf("doh\n");
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    man file

    file is a fairly standard unix utility to tell you the type of files, and it has a nice large database of lots of magic numbers which signify the start of certain file types.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM