[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
 

C rutine til exec + pipe til og fra



Hej der

Er der en af jer, der har en _elegant_ ansi C rutine til at lave en exec af
et eksternt program, hvor man give et dataområde til stdin og få udfyldt to
dataområder til hhv stdout og stderr???

Jeg har selv lavet nedenstående som jeg tror vil lave deadlock hvis ikke
system-pipes er store nok til at rumme data til de 3 data strømme.

(Nedenstående har desuden det problem at jeg ikke kan få exit-værdien
overført korrekt...)
------------------------------------------------------------------
/* todo: this might deadlock on large amounts of data */
int cmdline(char *cmdline, char *pIn, int lenin, char **pOut, int *lenout,
            char **pErr, int *lenerr)
{
    int rc, status;
    pid_t pid;
    int fdi[2];
    int fdo[2];
    int fde[2];

    pipe(fdi);
    pipe(fdo);
    pipe(fde);

    pid = fork();
    if(pid == 0) {
        close(fdi[1]);          /* I want to READ from stdin, so close WRITE
side */
        close(fdo[0]);          /* I want to WRITE to stdout, so close READ
side */
        close(fde[0]);          /* I want to WRITE to stderr, so close READ
side */

        /* Close stdin, duplicate the input side of pipe to stdin */
        dup2(fdi[0], 0);        /* stdin */
        /* Close stdout, duplicate the output side of pipe to stdout */
        dup2(fdo[1], 1);        /* stdout */
        /* Close stderr, duplicate the output side of pipe to stderr */
        dup2(fde[1], 2);        /* stderr */

        rc = system(cmdline);

        if (rc)
            fprintf(stderr, "DBG: rc=%d\n", rc);

        close(fdi[0]);
        close(fdo[1]);
        close(fde[1]);

        exit(rc);
    }
    close(fdi[0]);
    close(fdo[1]);
    close(fde[1]);

    write(fdi[1], pIn, lenin);
    close(fdi[1]);
    /* This way you can use the same buffer for both stdout and stderr
       stderr will be appended to stdout
     */
    *lenout = read(fdo[0], *pOut, *lenout);
    *pOut += *lenout;
    **pOut = '\0';

    *lenerr = read(fde[0], *pErr, *lenerr);
    *pErr += *lenerr;
    **pErr = '\0';

    close(fdo[0]);
    close(fde[0]);

    waitpid(pid, &status, 0);

    return status;
}

------------------------------------------------------------------

Mvh

Holger Bille




 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:43 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] *