[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] popen i read/write mode



On Thu, 2 May 2002 09:33:26 +0200,
Robert Larsen <sslug@sslug> wrote:

> Hejsa
> 
> Jeg sidder og leger med at lægge en grafisk brugerflade på nogle
> eksisterende programmer, som kræver input fra brugeren. Til det bruger jeg
> 'popen()' funktionen, som kan åbne strømmen til programmet i enten read
> ELLER write mode. Det er bare ikke godt nok, da jeg gerne ville læse output.
> Er der et alternativ til 'popen()', så jeg både kan læse fra og skrive til
> komandoen ?

Sidst jeg skulle bruge det, tastede jeg følgende:

//This implementation is inspirred by Program 14.9 in 
//Richard W. Stevens: Advanced programming in the UNIX Environment

#define ERROR -1

int popenrw(char *command, char **args, FILE *&in, FILE *&out)
{
    int fToCommand[2], fFromCommand[2];
    int error;

    if (error=pipe(fToCommand)) return error;
    if (error=pipe(fFromCommand))
    {
        close(fToCommand[0]);
        close(fToCommand[1]);
        return ERROR;
    }

    pid_t pid=fork();

    if (pid<0) //fork error
        return ERROR;

    if (pid > 0)
    {
        //we are the parent
        char line[1024];

        close(fToCommand[0]);
        close(fFromCommand[1]);

        if(!(in = fdopen(fFromCommand[0],"r"))) return ERROR;
        if(!(out = fdopen(fToCommand[1],"w"))) return ERROR;

        return 0;

    }
    else
    {
        //we are the child
        close(fToCommand[1]);
        close(fFromCommand[0]);

        if (fToCommand[0]!=STDIN_FILENO)
        {
            if (dup2(fToCommand[0], STDIN_FILENO)==-1)
                exit(1); //duplication was unsuccessful
            close(fToCommand[0]);
        }

        if (fFromCommand[1]!=STDOUT_FILENO)
        {
            if (dup2(fFromCommand[1], STDOUT_FILENO)==-1)
                exit(1); //duplication was unsuccessful
            close(fFromCommand[1]);
        }

        if (execvp(command, args) < 0)
        {
            exit(1);
        }

        exit(0);

    }
}


-- 
Anders Bo Rasmussen


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *