File: pchild.c

package info (click to toggle)
yodl 4.05.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,724 kB
  • sloc: ansic: 7,803; perl: 683; cpp: 570; sh: 411; xml: 190; makefile: 163
file content (33 lines) | stat: -rw-r--r-- 1,402 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "process.ih"

/*
    This function specifies readPipe and writePipe, but in the prototype
    the names are reversed: there writePipe and readPipe are used.
    The prototype is given from the point of view of the parent code,
    which first specifies the writePipe.
    Here, we're in the child-process itself, and the parent's writePipe
    becomes the child's readPipe.
*/
int p_child(Process *pp, int *readPipe, int *writePipe)
{
    char **argv = p_split(pp);              /* construct argv               */

    close(readPipe[WRITE]);                 /* Close unused pipe-ends       */
    close(writePipe[READ]);

    if                                      /* Use our descriptors for      */
    (                                       /* standard streams             */
        dup2(readPipe[READ], STDIN_FILENO) < 0
        ||
        dup2(writePipe[WRITE], STDOUT_FILENO) < 0
    )
        if (message_show(MSG_ALERT))
            message("%s: Can't redirect STDIN and/or STDOUT");

    close(readPipe[READ]);                  /* close superfluous original  */
    close(writePipe[WRITE]);                /* parts                        */

    return execvp(*argv, argv);             /* exec the program             */
                                            /* (program exits or -1 is      */
                                            /* returned when exec fails     */
}