void cgi_run(const char* filename)
{
char buffer[1024] = { 0 };
int len;
int pfd[2];
int status;
pid_t pid;
/* create pipe */
if (pipe(pfd)<0)
return -1;
/* fork to execute external program or scripts */
pid = fork();
if (pid<0) {
return 0;
} else if (pid==0) { /* child process */
dup2(pfd[1], STDOUT_FILENO);
close(pfd[0]);
/* execute CGI */
execlp(filename, filename, NULL);
exit(0);
} else { /* parent process */
close(pfd[1]);
/* print output from CGI */
while((len=read(pfd[0], buffer, 1023))>0) {
buffer[len] = '\0';
printf("%s\n", buffer);
}
/* waiting for CGI */
waitpid((pid_t)pid, &status, 0);
}
}