int print_refs(const char *git, const char *path,int serviceIndex)
{
int pid, fd[2];
if(pipe(fd)<0)
{
printf("pipe open failed");
//oops("pipe",1);
}
if((pid = fork() )<0 )
{
//oops("fork",1);
}else if(pid==0)
{
char repodir[4096]={0};
char sservice[12]={0};
if (fd[1] != STDOUT_FILENO) {
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO){
return -1;
}
close(fd[1]);
}
char *argv[]={"git",sservice,"--stateless-rpc","--advertise-refs",repodir,NULL};
strncpy(repodir,path,4095);
strncpy(sservice,service[serviceIndex],11);
if(execve(git,argv,NULL)==-1)
{
/
exit(0);
}
}else{
char buffer[4096]={0};
close(fd[1]);
int n=0;
while((n=read(fd[0],buffer,4096)))
{
int i=0;
while(i<n)
{
putc(buffer[i],stdout);
i++;
}
}
close(fd[0]);
}
return 0;
}
转载于:https://my.oschina.net/GIIoOS/blog/483567