|
这个是newc.c文件,我要编一个对数据库操作的程序,主要实现把这个newc.c文件编译之后的结果写入数据库mysql中,我是新手,不知道如何下手,请大家帮忙
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
char *env_init[]={"ATH=/usr/bin",NULL};
pid_t pid,pid2;
int fp1;
char *err1[]={"Compile err","Timeout","resent err","wrong answer","Accept"};
static void sig_usr(int signo)
{
if(signo==SIGALRM)
{ printf("Timeout.Killing...!");
fflush(stdout);
kill(pid2,SIGKILL);
write(fp1,err1[1],7);
close(fp1);
exit(0);
}
}
int main(void)
{
int compile=0;
int result_match=0;
char exec_result[3000];
char exec1_result[3000];
char saved_result[3000];
char ch;
pid_t pid,pid2;
int status;
int fd,fd1,fd2;
if((fd1=open("./input.txt",O_RDONLY))<0)
printf("open error!\n");
dup2(fd1,0);
if((fd=open("./output.txt",O_WRONLY|O_CREAT|O_TRUNC|O_RDONLY))<0)
printf("open error!\n");
dup2(fd,1);
if((fd2=open("./daan.txt",O_WRONLY|O_CREAT|O_TRUNC|O_RDONLY))<0)
{printf("open error\n");
exit(0);
}
if ((fp1=open("./end.txt",O_WRONLY|O_CREAT|O_TRUNC|O_RDONLY))<0)
{
printf("cannot open file\n");
exit(0);
}
pid=fork();
if(pid<0) printf("fork error!\n");
else if(pid==0)
{if(execle("/usr/bin/gcc","gcc","/usr/local/newc/my.c","-o","/usr/local/newc/myt.out",(char *)0,env_init)<0)
printf("exec error!\n");
exit(0);
}
if((pid=waitpid(pid,&status,0))<0)
printf("waitpid error!\n");
/* i=ftell(fd)
{
if(i==-1L) printf("error\n")
else if (i!=0)
{
compile=1;
if(compile==1){
fprintf(in,"Compile Error!");
fclose(in);
exit(0);}
}
}
*/
printf("pid=%d\n",pid);
pid2=fork();
if(pid2<0) printf("fork error!\n");
else if(pid2==0)
{if(execlp("./myt.out",(char *)0)<0)
printf("exec error!\n");
exit(0);
}
if (signal(SIGALRM,sig_usr)==SIG_ERR)
printf("can't catch SIGUSR!\n");
alarm(2);
if (signal(SIGCHLD,sig_usr)==SIG_ERR)
printf("can't catch SIGUSR!\n");
printf("pid2=%d\n",pid2);
write(fp1,err1[4],6);
close(fp1);
pause();
} |
|