Repository: fastfetch-cli/fastfetch
File: src/util/platform/FFPlatform_unix.c (C)

Code before:
    #elif defined(__APPLE__)
        int exePathLen = proc_pidpath((int) getpid(), exePath, sizeof(exePath));
    #elif defined(__FreeBSD__) || defined(__NetBSD__)
        size_t exePathLen = sizeof(exePath);
        if(sysctl(
            (int[]){CTL_KERN,
            #ifdef __FreeBSD__
                KERN_PROC, KERN_PROC_PATHNAME, (int) getpid()
            #else
                KERN_PROC_ARGS, (int) getpid(), KERN_PROC_PATHNAME
            #endif
            }, 4,
            exePath, &exePathLen,
            NULL, 0
        ) < 0)
            exePathLen = 0;
        else
            exePathLen--; // remove terminating NUL
    #elif defined(__OpenBSD__)
        size_t exePathLen = 0;
    #elif defined(__sun)
        ssize_t exePathLen = readlink("/proc/self/path/a.out", exePath, sizeof(exePath) - 1);
        if (exePathLen >= 0)
            exePath[exePathLen] = '\0';
    #elif defined(__HAIKU__)
        size_t exePathLen = 0; // TODO
    #endif
    if (exePathLen > 0)
    {
        ffStrbufEnsureFree(&platform->exePath, PATH_MAX);
        if (realpath(exePath, platform->exePath.chars))
            ffStrbufRecalculateLength(&platform->exePath);
        else
            ffStrbufSetNS(&platform->exePath, (uint32_t) exePathLen, exePath);
    }
}

static void platformPathAddEnv(FFlist* dirs, const char* env)
{
    const char* envValue = getenv(env);
    if(!ffStrSet(envValue))
        return;

    FF_STRBUF_AUTO_DESTROY value = ffStrbufCreateA(64);
    ffStrbufAppendS(&value, envValue);

    uint32_t startIndex = 0;
    while (startIndex < value.length)
    {
        uint32_t colonIndex = ffStrbufNextIndexC(&value, startIndex, ':');
        value.chars[colonIndex] = '\0';

Review comment (suggestion):
This can be implemented using `get_next_image_info` and searching for the `B_APP_IMAGE` (see https://www.haiku-os.org/legacy-docs/bebook/TheKernelKit_Images.html#get_next_image_info)

Code after:
        ssize_t exePathLen = readlink("/proc/self/exe", exePath, sizeof(exePath) - 1);
        if (exePathLen >= 0)
            exePath[exePathLen] = '\0';
    #elif defined(__APPLE__)
        int exePathLen = proc_pidpath((int) getpid(), exePath, sizeof(exePath));
    #elif defined(__FreeBSD__) || defined(__NetBSD__)
        size_t exePathLen = sizeof(exePath);
        if(sysctl(
            (int[]){CTL_KERN,
            #ifdef __FreeBSD__
                KERN_PROC, KERN_PROC_PATHNAME, (int) getpid()
            #else
                KERN_PROC_ARGS, (int) getpid(), KERN_PROC_PATHNAME
            #endif
            }, 4,
            exePath, &exePathLen,
            NULL, 0
        ) < 0)
            exePathLen = 0;
        else
            exePathLen--; // remove terminating NUL
    #elif defined(__OpenBSD__)
        size_t exePathLen = 0;
    #elif defined(__sun)
        ssize_t exePathLen = readlink("/proc/self/path/a.out", exePath, sizeof(exePath) - 1);
        if (exePathLen >= 0)
            exePath[exePathLen] = '\0';
    #elif defined(__HAIKU__)
        size_t exePathLen = 0;
        image_info info;
        int32 cookie = 0;

        while (get_next_image_info(B_CURRENT_TEAM, &cookie, &info) == B_OK) {
            if (info.type == B_APP_IMAGE) {
                exePathLen = strlcpy(exePath, info.name, PATH_MAX);
                break;
            }
        }
    #endif
    if (exePathLen > 0)
    {
        ffStrbufEnsureFree(&platform->exePath, PATH_MAX);
        if (realpath(exePath, platform->exePath.chars))
            ffStrbufRecalculateLength(&platform->exePath);
        else
            ffStrbufSetNS(&platform->exePath, (uint32_t) exePathLen, exePath);
    }
}

static void platformPathAddEnv(FFlist* dirs, const char* env)
{