Here it is Salem :
Code:
void map(char* cmd, char *arg){ int i;
int pid;
int fd[2];
char* buffer;
char* novo;
int status;
FILE *stream;
pipe(fd);
pid=fork();
stream = fopen("merdas.txt","a+");
if(pid==-1) /* erro */
{
perror("impossivel de criar um filho") ;
exit(-1) ;
}
else if(pid==0) {
/* filho */
dup2(fd[1],1);
close(fd[1]);
// read(1,buffer,1000);
printf("li %s\n",&buffer);
//write(1,buffer,1000);
printf("buffer:a%sa\n",&buffer);
printf("Resultado do execlp:\n");
int a = execlp(cmd,cmd,arg,NULL); //o comando é o primeiro argumento da main e o argumento é a linha/ficheiro...
_exit(pid); //filho morra
//sleep(1);
}
printf("adoro merda antes do wait \n");
wait(&pid); //esperar que o filho termine
printf("ainda mais depois \n");
/*dup2(fd[0],fd[1]);
close(fd[0]);*/
//fprintf(stream,"%s\n",buffer);
read(0,buffer,strlen(arg));
printf("-----> %s \n",buffer);
}
int main(int argc, char** argv){
// FILE *stream;
char *str;
//if((stream = freopen("merdas.txt", "w", stdout)) == NULL)
//exit(-1);
//stream = fopen("merdas.txt","a+");
char *aux;
size_t tam=0;
int line=0;
int i;
// init();
// printf("iq\n");
while(line=getline(&str,&tam,stdin)!=-1){
tam=strlen(str);
str[tam-1]='\0';
map(argv[1],str);
// fprintf(stream,"%s\n",str);
}
//fprintf(stream,"END");
//freopen ("/dev/tty", "a", stdout);
//map2();
return(1);
}