【发布时间】:2017-03-09 21:01:35
【问题描述】:
我要编译这个源代码:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, const char *argv[]) {
While:
printf("MacBook-Pro-...:~ ...$ ");
char command[128];
gets_s(command);
if (strncmp(command, "exit", 4) == 0)
exit(0);
pid_t return_value = fork();
if (return_value == 0) {
int outfile;
if ((outfile = dup(1)) == -1)
return -1;
close(1);
if ((outfile = open("/Users/.../1.txt",
O_WRONLY | O_TRUNC | O_CREAT, 0644)) >= 0) {
execl("/bin/sh", "sh", "-c", command, NULL);
}
close(outfile);
exit(0);
} else {
wait();
FILE *fp;
char str[128];
fp = fopen("/Users/.../1.txt", "r");
while(!feof(fp)) {
if(fgets(str, 126, fp))
printf("%s", str);
}
fclose(fp);
goto While;
}
return 0;
}
但我有一些错误: 语义问题
- 函数“gets_s”的隐式声明在 C99 中无效
- 隐式声明库函数“exit”,类型为“void (int)attribute((noreturn))”
- 函数 'wait' 的隐式声明在 C99 中无效
- 函数调用的参数太少,预期为 1,有 0
系统:
产品名称:Mac OS X 产品版本:10.12.1 构建版本:16B2555
Xcode 版本 8.0 (8A218a)
Apple LLVM 版本 8.0.0 (clang-800.0.38) 目标:x86_64-apple-darwin16.1.0 线程模型:posix
【问题讨论】: