/* Copyright (c) 2016, 2017, 2018, 2019, 2022 Dennis Wölfing
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* libc/src/stdio/stdout.c
 * Standard output. (C89)
 */

#define isatty __isatty
#include <errno.h>
#include <unistd.h>
#include "FILE.h"

static unsigned char buffer[BUFSIZ];
static FILE __stdout = {
    .fd = 1,
    .flags = FILE_FLAG_BUFFERED | FILE_FLAG_USER_BUFFER | FILE_FLAG_WRITABLE,
    .buffer = buffer,
    .bufferSize = sizeof(buffer),
    .readPosition = UNGET_BYTES,
    .readEnd = UNGET_BYTES,
    .mutex = _MUTEX_INIT(_MUTEX_RECURSIVE),
    .read = __file_read,
    .write = __file_write,
    .seek = __file_seek,
};

FILE* stdout = &__stdout;

__attribute__((constructor)) static void setStdoutLinebuffer(void) {
    // We must linebuffer stdout when it refers to a terminal.
    int oldErrno = errno;
    if (isatty(1)) {
        __stdout.flags |= FILE_FLAG_LINEBUFFER;
    }
    errno = oldErrno;
}
