#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

enum { buf_size = 4096 };

static void die(const char *msg, int code)
{
    perror("msg");
    exit(code);
}

int main()
{
    int fd, res, count;
    char buf[buf_size];

    fd = open("exec04.t", O_RDONLY);
    if (fd == -1)
        die("open", 1);

    int pid;
    pid = fork();
    if (pid == -1) {
        perror("fork");
        exit(1);
    }

    /* fcntl(fd, F_SETFD, FD_CLOEXEC); */
    if (pid == 0) {
        execlp("ls", "ls", "-l", NULL);
        while((count = read(fd, buf, buf_size) != 0)) {
            res = write(1, buf, count);
            if (res == -1)
                die("write", 1);
        }
        perror("ls");
        fflush(stderr);
        _exit(1);
    }

    res = close(fd);
    if (res == -1)
        die("close", 1);
    return 0;
}
