#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <err.h>
#include <string.h>
#include <unistd.h>
#include "../lpi.h"

int
main(void)
{
    int fd1, fd2, fd3;
    char pathname[30];

    fd1 = open("/dev/fd/1", O_WRONLY);
    if (fd1 == -1)
        err(EXIT_FAILURE, "open fd1");

    if (lseek(fd1, 1, SEEK_SET) == -1)
        err(EXIT_FAILURE, "lseek fd1");

    sprintf(pathname, "/dev/fd/%d", fd1);
    fd2 = open(pathname, O_WRONLY);
    if (fd2 == -1)
        err(EXIT_FAILURE, "open fd2");
    if (lseek(fd2, 50, SEEK_CUR) == -1)
        err(EXIT_FAILURE, "fd2 unseekable");

    fd3 = dup(fd1);
    if (fd3 == -1)
        err(EXIT_FAILURE, "dup fd1");

    /*
    write(fd1, "salam", 5);
    write(fd2, "adios", 5);
    write(fd3, "ajab", 4);
    */

    printf("fd1=%d, fd2=%d, fd3=%d\n", fd1, fd2, fd3);

    /*
    printf("tell(fd1)=%lld\n", (long long) tell(fd1));
    printf("tell(fd2)=%lld\n", (long long) tell(fd2));
    printf("tell(fd3)=%lld\n", (long long) tell(fd3));
    */
    listDir("/dev/fd");

    exit(EXIT_SUCCESS);
}
