/*
 * wc.c
 *
 * Copyright (C) 2017 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <monolithium.h>

handle_t __crt_get_raw_handle(int fd);

static char *wd_path = NULL;

char *getcwd(char *buf, size_t size)
{
    return strncpy(buf, wd_path, size);
}

char *getwd(char *buf)
{
    return strcpy(buf, wd_path);
}

char *get_current_dir_name(void)
{
    return strdup(wd_path);
}

int fchdir(int fd)
{
    char path[MAX_PATH];
    handle_t handle = __crt_get_raw_handle(fd);

    sysret_t ret = syscall_query_file(handle, FILE_INFO_NAME, path, sizeof(path));

    if (ret != ERR_SUCCESS)
    {
        switch (ret)
        {
        case ERR_FORBIDDEN:
            errno = EACCES;

        default:
            errno = __crt_translate_error(ret);
        }

        return -1;
    }

    if (wd_path)
    {
        free(wd_path);
        wd_path = NULL;
    }

    wd_path = strdup(path);
    return 0;
}

int chdir(const char *path)
{
    int fd = open(path, O_RDONLY, 0);
    if (fd < 0) return -1;

    int ret = fchdir(fd);
    close(fd);
    return ret;
}
