我正在创建一个在线评委。为此,我正在编写一个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()
答案 0 :(得分:0)
我们终于通过使用docker容器解决了这个问题。