How to check if a file is executable?

How to check if a file is executable?

Post by hou » Tue, 08 Apr 2003 06:07:59



Hello everyone
     I am writing a simple shell-like program.
Before I use exec(), I want to check if what the user typed in is an
executable file and if it isn't, then print some error message to the
user.  How can I check if a file is the one user can execute?
Thank you very much
Dean
 
 
 

How to check if a file is executable?

Post by Barry Kimelma » Tue, 08 Apr 2003 22:28:38


[This followup was posted to comp.unix.questions]


says...

Quote:> Hello everyone
>      I am writing a simple shell-like program.
> Before I use exec(), I want to check if what the user typed in is an
> executable file and if it isn't, then print some error message to the
> user.  How can I check if a file is the one user can execute?
> Thank you very much
> Dean

You can use the system call access() to test for execute permissions as
follows :

    if ( access(pathname,X_OK) != 0 ) {
        printf("%s is NOT executable.\n",pathname);
    }

Note : This depends on you hvaing a fully qualified path to the file
(unless the current directory just happens to be the same directory as
where the program resides).

--
---------

Barry Kimelman
Winnipeg, Manitoba, Canada


 
 
 

How to check if a file is executable?

Post by Chuck Dillo » Tue, 08 Apr 2003 22:59:14



> Hello everyone
>      I am writing a simple shell-like program.
> Before I use exec(), I want to check if what the user typed in is an
> executable file and if it isn't, then print some error message to the
> user.  How can I check if a file is the one user can execute?
> Thank you very much
> Dean

Why not just try and execute it and report any error encountered?  It's
problemattic to try and anticipate errors.  In the end, you still have
to handle errors so you're just adding complexity.

-- ced

--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.

 
 
 

How to check if a file is executable?

Post by Dhek Bhun Kh » Fri, 18 Apr 2003 04:40:53


Quote:> user.  How can I check if a file is the one user can execute?

Hey there. Have you tried checking the man pages for fstat() ?

--
Eschew obfuscation.

 
 
 

1. Executable Binary File vs. Executable Script File

Hi,

I need to write a simple script to "strip off" all the binary files in a
certain directory that contains some sort of executable script files,
i.e. perl, shell, tcl, etc.  I tried the following script that I named
"/tmp/junk":

        #!/bin/sh
        for ff in `find . -type f`
        do
                ! test -r $ff || strip $ff
        done

and it did stripped off the executable binary files as well as giving me
some error messages saying "File format not recognized" when it tried to
strip off the script files.  The question is: How to modify the above
script that will by pass the stripping process if the file is not really
an executable binary file?

Can anyone please help?  TIA.

--

PS> Remove the "4" from e-mail address to respond.

2. Ident daemon

3. How to check if a file is executable?

4. Programatically deiconify/raise window in aix 4.3.3

5. About checking executable files

6. Partition Magic Stumps Linux Newbie.

7. Can os compatibility be checked for an executable

8. filecopying unix-unix over telephone line ?

9. CVS question -- executable bit turned ON automatically after checking into the repository...

10. How to input file to a executable file use csh?

11. How to link object files from gcc and absoft f90 into a executable file?

12. A script that will only check file if it has been modified since last check

13. executable object code file vs script file