2017-05-06 14 views
0

3 개의 인수, ./a.out abc를받는 프로그램을 수행합니다. 여기서 a와 c는 열 번호이고 b와 로 구분 된 피연산자 :.stdin (and store value) 파이프를 자식에게 읽기, 잘라 내기 및 값 반환

true로 설정하면 stdin이 재생되고 그렇지 않으면 결과가 재생되지 않습니다.

예 :

$ ./a.out 1 > 2 
$ 5:2:1:6 
5:2:1:6 

$ ./a.out 2 = 4 
$ 1:2:3:4 
$ 

나는 파이프를 doint 및 절단이 요구할 때 표준 입력에서 읽고, 내 첫 번째 버전에서 시도했습니다,하지만 내 문제는 내가 입력을 잃을 것입니다.

이제 아이 내부의 표준 입력을 읽고 저장하고 파이프를 통해 전달하려고합니다. 그러나 테스트를 위해 execlp가 표준 입력을받지 못하고 있다고 생각합니다.

나는 awk를 학업에 사용할 수 없습니다.

이 순간에 내 코드 : 이런 식으로 해결

int main(int argc, char const *argv[]){ 

    int n,f; 
    char coluna1[16]; 
    char coluna2[16]; 
    char strin[PIPE_BUF]; 
    //put together args cut 
    char buffer[PIPE_BUF]; 
    sprintf(buffer, "-f%s,%s",argv[1],argv[3]); 

    //pipes 
    int fd[2]; 
    int fd2[2]; 
    pipe(fd); 
    pipe(fd2); 

    if(!fork()) { 
     close(fd[0]); //close read 
     dup2(fd[1],1); //std output duplicated to pipe write 
     close(fd2[0]); //close read 
     //readline stdin 
     n = read(0,strin,PIPE_BUF); 
     write(fd2[1],strin,n); 
     //cut -d: -f2,4 - 
     execlp("cut","cut","-d:",buffer,"-",NULL); 
    } 
    //pai recebe do pipe 
    close(fd[1]); //close write 
    close(fd2[1]); //close write 
    n = read(fd2[0],strin,PIPE_BUF); //read stdin from pipe 
    f = read(fd[0],buffer,PIPE_BUF); //stdout from cut 
    sscanf(buffer,"%[^:]:%s",coluna1,coluna2); 

    //write the result from the cut to "bug check" 
    write(1,buffer,f); 

    //printfs just to check if everything worked 
    if(strcmp(argv[2],"=")) if(atoi(coluna1) == atoi(coluna2)) { write(1,strin,n); printf("ACERTEI NO =\n"); } 
    if(strcmp(argv[2],">=")) if(atoi(coluna1) >= atoi(coluna2)) { write(1,strin,n); printf("ACERTEI NO >=\n"); } 
    if(strcmp(argv[2],"<=")) if(atoi(coluna1) <= atoi(coluna2)) { write(1,strin,n); printf("ACERTEI NO <=\n"); } 
    if(strcmp(argv[2],">")) if(atoi(coluna1) > atoi(coluna2)) { write(1,strin,n); printf("ACERTEI NO >\n"); } 
    if(strcmp(argv[2],"<")) if(atoi(coluna1) < atoi(coluna2)) { write(1,strin,n); printf("ACERTEI NO <\n"); } 
    if(strcmp(argv[2],"!=")) if(atoi(coluna1) != atoi(coluna2)) { write(1,strin,n); printf("ACERTEI NO !=\n"); } 

} 
+0

한 가지 문제는 많은 껍질에 '>'및 '='문자는 특별한 가지고있다 의미. fd에 리터럴을 사용하는 것은 약간 추한 것입니다. 나는 그것을 명확히하기 위해 매크로를 선호한다. –

+0

쉘에서 시도 할 때 ">"사용하여 재발 문제를 피하십시오. 매크로는 STDIN과 같은 의미입니까? –

+0

내가 말하는 매크로는 STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO입니다. 그리고 예, 부호를 인용하거나 빠져 나와서 그런 종류의 문제를 해결해야합니다. –

답변

0

는 :

내가 볼 수
if(!fork()) { 
    close(fd[0]); //close read 
    dup2(fd[1],1); //std output duplicated to pipe write 
    close(fd2[1]); //close write 
    dup2(fd2[0],0); //std input from father duplicated to pipe read 
    //cut -d: -f2,4 - 
    execlp("cut","cut","-d:",buffer,"-",NULL); 
} 
//father 
close(fd[1]); //close write 
close(fd2[0]); //close read 
n = read(0,strin,PIPE_BUF); 
write(fd2[1],strin,n); 
close(fd2[1]); 
//n = read(fd2[0],strin,PIPE_BUF); //read stdin from pipe 
f = read(fd[0],buffer,PIPE_BUF); //stdout from cut