【发布时间】:2016-03-30 07:19:22
【问题描述】:
我正在尝试编写一个程序来比较 2 个文件并返回它们是否相等。
我只能使用以下函数:fork、dup、dup2、open、write、exec 和 read。
当我在 linux gcc 上编译程序时,它返回:
bash: ./comp.out: 权限被拒绝
代码:
#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.
*/
如何更改权限?
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
【问题讨论】:
-
运行
sudo ./comp.out,也许吧? -
但是我不能在终端上使用命令。
-
@ShayChercavsky 您实际上是在将源代码编译为可执行程序,还是只是想运行源文件?或者您可能正在创建一个目标文件?您需要向我们展示您如何构建源代码来创建程序。
-
@ShayChercavsky 然后展示我们你是如何做到的。 编辑您的问题以显示一个或多个命令,包括所有标志。
-
顺便说一句,在不相关的说明中,您应该真正检查程序是否有两个参数,否则您将有 未定义的行为,因为您可能会使用 @987654331 @越界。检查例如
if (argc < 3) { printf("To few arguments, must have two\n"); return 1; }在CheckSimilar调用之前。
标签: c linux gcc permissions