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

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

int main(int argc, char **argv)
{
    int r, fd;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <file name>\n", argv[0]);
        return 1;
    }

    r = close(1);
    if (r == -1)
        terminate("close", 2);
    fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0666);
    if (fd == -1)
        terminate("open", 2);

    printf("hello there\n");

    fflush(stdout);
    r = close(fd);
    if (r == -1)
        terminate("close", 2);
    return 0;
}
