Skip to content

A cross-platform C library to get env info such as exe path, user name, and os version

License

Notifications You must be signed in to change notification settings

matyalatte/c-env-utils

Repository files navigation

c-env-utils

License: MIT

c-env-utils is a cross-platform C library to get environment information such as executable path, user name, and OS version.

Documentation

Most of the functions are here.
c-env-utils: include/env_utils.h File Reference

c-env-utils only supports utf8 strings.
You should use env_utils_windows.h if you want to use utf16 strings on Windows.
c-env-utils: include/env_utils_windows.h File Reference

Platforms

c-env-utils supports most of the desktop operating systems.

  • Windows
  • macOS
  • Linux
  • BSD
  • Haiku
  • Solaris
  • Other unix variants (maybe)

Example

#include <stdio.h>
#include "env_utils.h"

int main(void) {
    // Executable path
    char *exe_path = envuGetExecutablePath();
    printf("Exe: %s\n", exe_path);
    envuFree(exe_path);

    // Working directory
    char *cwd = envuGetCwd();
    printf("CWD: %s\n", cwd);
    envuFree(cwd);

    // Username
    char *username = envuGetUsername();
    printf("User: %s\n", username);
    envuFree(username);

    // OS
    char *prodname = envuGetOSProductName();
    printf("OS: %s\n", prodname);
    envuFree(prodname);

    // Parse the PATH variable
    int count;
    char **paths = envuGetEnvPaths(&count);
    printf("%s", "PATH:\n");
    for (char **p = paths; p < paths + count; p++) {
        printf("  %s\n", *p);
    }
    envuFreeEnvPaths(paths);
    return 0;
}

Building

Requirements

Build Whole Project

meson setup build
meson compile -C build
meson test -C build

Build Library Only

meson setup build -Dcli=false -Dtests=false
meson compile -C build

Build as Subproject

You don't need to clone the git repo if you build your project with meson.
Save the following text as subprojects/env_utils.wrap.

[wrap-git]
url = https://github.com/matyalatte/c-env-utils.git
revision = head
depth = 1

[provide]
env_utils = env_utils_dep

Then, you can use c-env-utils in your meson project.

env_utils_dep = dependency('env_utils', fallback : ['env_utils', 'env_utils_dep'])
executable('your_exe_name', ['your_code.cpp'], dependencies : [env_utils_dep])
meson setup build -Denv_utils:cli=false -Denv_utils:tests=false
meson compile -C build

Projects Which Use c-env-utils

  • Tuw: A tiny GUI wrapper for command-line tools.

About

A cross-platform C library to get env info such as exe path, user name, and os version

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project