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

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

    if ((fd1 = open("list_open_fds.c", O_RDONLY)) == -1)
        err(EXIT_FAILURE, "open file list_open_fds.c");

    if ((fd2 = open("list_open_fds.c", O_RDONLY)) == -1)
        err(EXIT_FAILURE, "open file list_open_fds.c");

    if ((pid = fork()) == -1)
        err(EXIT_FAILURE, "fork");

    if (pid == 0) {
        if (close(fd2) == -1)
            err(EXIT_FAILURE, "close");

        printf("child closed fd=%d\n", fd2);
        printf("Child-------------");
        _exit(listDir("/dev/fd"));
    }

    sleep(3);                   /* Give chance to child to run first */
    printf("Parent-----------");
    listDir("/dev/fd");
    exit(EXIT_SUCCESS);
}
