Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / djbunix.h / sa_realpathat.c

/* This file is part of limb                           https://lila.oss/limb
 * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <limb/djbunix.h>

int
sa_realpathat(stralloc *sa, int bfd, const char *file)
{
    int ret = -1;
    int cwd = -1;

    if (bfd != AT_FDCWD) {
        cwd = open_read(".");
        if (cwd < 0) return -1;

        if (fd_chdir(bfd) < 0) {
            fd_close(cwd);
            return -1;
        }
    }

    if (!sa_realpath(sa, file))
        ret = 0;

    if (cwd >= 0) {
        fd_chdir(cwd);
        fd_close(cwd);
    }

    return ret;
}