#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
#include "../lpi.h"             /* for listDir() */

int
main(int argc, char *argv[])
{
    pid_t pid;
    int fd;

    fd = open("vfork_2.c", O_RDONLY);
    if (fd == -1)
        err(EXIT_FAILURE, "vfork_2.c");

    printf("parent file descriptors before vfork(): ");
    listDir("/dev/fd");

    switch (pid = vfork()) {
    case -1:
        err(EXIT_FAILURE, "vfork");

    case 0:                 /* in vfork() child runs first */
        if (close(fd) == -1 || close(STDIN_FILENO) == -1) {
            fprintf(stderr, "child : error close(fd)\n");
            _exit(EXIT_FAILURE);
        }
        printf("child file desscriptors: ");
        listDir("/dev/fd");
        _exit(EXIT_SUCCESS);

    default:
        printf("parent file descriptors after vfork(): ");
        listDir("/dev/fd");
        break;
    }

    exit(EXIT_SUCCESS);
}
