• [^] # Re: C

    Posté par (site web personnel) . En réponse au journal C. Évalué à 1.

    Voici un truc que j'avais écris pour controller l'entrée standard d'un programme (nommé apt, mais rien avoir avec deb) et récupérer ce qui en sortait.
    ----
    int childPid;
    int pipefdin[2],pipefdout[2];

    /* Pipes init */
    if( pipe(pipefdin) == -1 || pipe(pipefdout) == -1 ){
    perror("Creating apt communication pipes");
    exit(1);
    }

    childPid = fork();

    if( !childPid ){

    // son, so we launch apt and we connect
    // its standards channels to pipes

    // close unused channels
    close(pipefdin[1]);
    close(pipefdout[0]);

    // Duplicate stdin to our pipe
    close(STDIN);
    if( dup(pipefdin[0]) == -1 ){
    perror("duplicating apt stdin");
    exit(1);
    }

    // Duplicate stdout to our pipe
    close(STDOUT);
    if( dup(pipefdout[1]) == -1 ){
    perror("duplicating apt stdout");
    exit(1);
    }

    // Duplicate stderr to the same pipe as stdout
    close(STDERR);
    if( dup(pipefdout[1]) == -1 ){
    perror("duplicating apt stderr");
    exit(1);
    }

    // Launch apt
    if( execl(apt_path,apt_path,NULL) == -1 ){
    perror("can't find apt");
    exit(1);
    }

    }else{

    // father, this process, continues execution

    // Close unusable channels
    close(pipefdin[0]);
    close(pipefdout[1]);

    // Return the right values to control the apt
    *aptin_k = pipefdin[1];
    *aptout_k = pipefdout[0];

    }
    ----
    aptin_k doit être le fd pour écrire sur la stdin du prog que tu veux controller, et aptou_k doit être là où tu lis pour récupérer sa stdout...
    En espérant te filer un coup de main...