#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <err.h>

int
main(void)
{
    int     fd;

    fd = open("startup", O_RDONLY);
    if (fd == -1)
        err(EXIT_FAILURE, "open");

    fd = open("myfile", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    if (fd == -1)
        err(EXIT_FAILURE, "open2");

    fd = open("w.log", O_RDONLY | O_CREAT | O_TRUNC | O_APPEND,
                       S_IRUSR | S_IWUSR);

    if (fd == -1)
        err(EXIT_FAILURE, "open3");

    if (close(STDOUT_FILENO) == -1)
        err(EXIT_FAILURE, "close stdout");

    fd = open("log2", O_WRONLY | O_CREAT | O_APPEND,
                      S_IRUSR | S_IWUSR | S_IRGRP);
    if (fd == -1)
        err(EXIT_FAILURE, "log2");

    printf("salam\n");

    if (close(fd) == -1)
        err(EXIT_FAILURE, "close log2");

    printf("?\n");

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

    fd = open("log2", O_DIRECTORY | O_RDONLY);
    if (fd == -1)
        err(EXIT_FAILURE, "log2 directory");

    exit(EXIT_SUCCESS);
}
