How to determine if a file is executable or not?

This is a discussion on How to determine if a file is executable or not? within the C Programming forums, part of the General Programming Boards category; Hi fellows, I have been given the darness task of determining whether a file is an executable or not. I ...

  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
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    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...
    MSVC++ 6.0

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21