Shay Chercavsky
Shay Chercavsky

Reputation: 59

bash: ./comp.out: Permission denied

I am trying to write a program that compares 2 files and returns if they are equal or not.

I can only use the functions: fork, dup, dup2, open, write, exec and read.

When I compile the program on linux gcc, it returns :

bash: ./comp.out: Permission denied

the code:

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int CheckSimilar(char *path1, char *path2);

int main(int argc, char **argv) {

    int returnValue = CheckSimilar(argv[1], argv[2]);

    switch (returnValue){

        case 1:
            write(1, "1\n", 3);
            break;
        case 2:
            write(1, "2\n", 3);
            break;
        case 3:
            write(1, "3\n", 3);
            break;
        default:
            break;
    }

    return 0;
}

/*
* This function checks if the files are similar or similar by case     sensitive
* it gets 2 files, and returns: 3 if identical, 2 if identical but only if not
* case sensitive or 1 else.
*/

How can I change the permissions?

shay@shay-Latitude-E6410 ~/workspace/targ1OS $ gcc -c ec11.c -o      comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ls
comp.out  Debug  ec11.c
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out       /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: Permission denied
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
comp.out: command not found
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: Permission denied
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ^C
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ls -al ./comp.out
-rw-r--r-- 1 shay shay 2640 Mar 30 10:05 ./comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ chmod ogu+x ./comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: cannot execute binary file: Exec format error
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ gcc -c ec11.c -o comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: Permission denied
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ chmod ogu+x ./comp.out
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ ./comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
bash: ./comp.out: cannot execute binary file: Exec format error
shay@shay-Latitude-E6410 ~/workspace/targ1OS $ comp.out /home/shay/Downloads/input.txt /home/shay/Downloads/input.txt
comp.out: command not found

Upvotes: 1

Views: 1108

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409442

The command

gcc -c ec11.c -o      comp.out

creates an object file, not an executable program file. Object files are the compiled translation units (roughly source file with all included header files) and nothing more. It's the -c flags that tells the compiler fronteng program gcc to create an object file, so either remove the -c flag, or explicitly link.

So either

$ gcc ec11.c -o comp.out

Or

$ gcc -c ec11.c
$ gcc ec11.o -o comp.out

On an unrelated note, I advice you to add warning flags when compiling so the compiler will give you more warnings about things that might cause trouble when running (like undefined behaviors or logical/semantic problems). I personally use at least the flags -Wall -Wextra -pedantic.

Upvotes: 3

Related Questions