使用popen编译外部程序
Using popen to compile external program
我正在创建一个在线法官。为此,我正在编写一个 CGI c 脚本,它获取程序内容并编译程序。
这是我编译外部c程序的函数。
char *compile_program(char *compile_script){
printf("%s\n", compile_script);
compile_script = "gcc /var/www/FilebCamFz.c -o /var/www/FilebCamFz";
FILE *output_file;
output_file = popen(compile_script, "w");
char *output, *full_output, *full_error;
output = malloc(50000);
full_output = malloc(50000);
full_error = malloc(50000);
setbuf(stderr, full_error);
setbuf(stdout, full_output);
if(output_file == NULL){
// Some error
fprintf(stderr, "Compilation failed. Try again.\n");
return full_error;
}
else{
// If command executed successfully
while (fgets(output, 5000000, output_file) != NULL){
strcat(full_output, output);
}
if (pclose (output_file) == 0){
return full_output;
}
else
fprintf (stderr, "Could not run more or other error.\n");
return full_error;
}
//return EXIT_SUCCESS; }
使用ls
、free -m
命令调用函数时,输出符合预期。
但是当 运行 命令如 python -v
等时,会得到空输出。
在 运行 命令如 gcc /var/www/FilebCamFz.c -o /var/www/FilebCamFz
时,我在 pclose()
.
上收到 stderr
所以我的问题是,popen 这种异常行为的原因是什么。
以及如何通过 popen()
编译外部程序(c、c++、python、java 等)
我们最终使用 docker 个容器解决了这个问题。
我正在创建一个在线法官。为此,我正在编写一个 CGI c 脚本,它获取程序内容并编译程序。
这是我编译外部c程序的函数。
char *compile_program(char *compile_script){
printf("%s\n", compile_script);
compile_script = "gcc /var/www/FilebCamFz.c -o /var/www/FilebCamFz";
FILE *output_file;
output_file = popen(compile_script, "w");
char *output, *full_output, *full_error;
output = malloc(50000);
full_output = malloc(50000);
full_error = malloc(50000);
setbuf(stderr, full_error);
setbuf(stdout, full_output);
if(output_file == NULL){
// Some error
fprintf(stderr, "Compilation failed. Try again.\n");
return full_error;
}
else{
// If command executed successfully
while (fgets(output, 5000000, output_file) != NULL){
strcat(full_output, output);
}
if (pclose (output_file) == 0){
return full_output;
}
else
fprintf (stderr, "Could not run more or other error.\n");
return full_error;
}
//return EXIT_SUCCESS; }
使用ls
、free -m
命令调用函数时,输出符合预期。
但是当 运行 命令如 python -v
等时,会得到空输出。
在 运行 命令如 gcc /var/www/FilebCamFz.c -o /var/www/FilebCamFz
时,我在 pclose()
.
所以我的问题是,popen 这种异常行为的原因是什么。
以及如何通过 popen()
我们最终使用 docker 个容器解决了这个问题。