c-env-utils
C library to get environment information
Loading...
Searching...
No Matches
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);
}
return 0;
}
_ENVU_EXTERN char * envuGetExecutablePath(void)
Gets the path to the executing binary.
_ENVU_EXTERN char ** envuGetEnvPaths(int *path_count)
Gets the environment paths from the PATH variable.
_ENVU_EXTERN char * envuGetCwd(void)
Gets the current working directory.
_ENVU_EXTERN void envuFree(void *p)
Frees the memory of a string allocated by c-env-utils.
_ENVU_EXTERN char * envuGetUsername(void)
Gets user name.
_ENVU_EXTERN void envuFreeEnvPaths(char **paths)
Frees the memory of an array allocated by envuGetEnvPaths() or envuParseEnvPaths().
_ENVU_EXTERN char * envuGetOSProductName(void)
Gets the product name and its version of running OS.

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.