c-env-utils
C library to get environment information
Loading...
Searching...
No Matches
env_utils.h File Reference
#include <stdlib.h>
#include <stdint.h>

Go to the source code of this file.

Macros

#define _ENVU_EXTERN   __attribute__((visibility("default"))) extern
 
#define _ENVU_ENUM(s)   typedef unsigned int s; enum
 
#define ENVU_VERSION   "0.3.0"
 
#define ENVU_VERSION_INT   300
 

Functions

_ENVU_EXTERN const char * envuGetVersion (void)
 Gets the version of c-env-utils.
 
_ENVU_EXTERN int envuGetVersionAsInt (void)
 Gets the version of c-env-utils as an integer.
 
_ENVU_EXTERN void envuFree (void *p)
 Frees the memory of a string allocated by c-env-utils.
 
_ENVU_EXTERN char * envuGetExecutablePath (void)
 Gets the path to the executing binary.
 
_ENVU_EXTERN int envuFileExists (const char *path)
 Returns if the specified path is a regular file for not.
 
_ENVU_EXTERN int envuPathExists (const char *path)
 Returns if the specified path exists for not.
 
_ENVU_EXTERN char * envuGetFullPath (const char *path)
 Gets a full path of the specified path.
 
_ENVU_EXTERN char * envuGetRealPath (const char *path)
 Gets a real path of the specified path.
 
_ENVU_EXTERN char * envuGetDirectory (const char *path)
 Gets a parent directory of the specified path.
 
_ENVU_EXTERN char * envuGetExecutableDir (void)
 Gets the directory of the executing binary.
 
_ENVU_EXTERN char * envuGetCwd (void)
 Gets the current working directory.
 
_ENVU_EXTERN int envuSetCwd (const char *path)
 Sets the current working directory.
 
_ENVU_EXTERN char * envuGetEnv (const char *name)
 Gets a value of an environment variable.
 
_ENVU_EXTERN int envuSetEnv (const char *name, const char *value)
 Sets an environment variable.
 
_ENVU_EXTERN char * envuGetHome (void)
 Gets user's home directory.
 
_ENVU_EXTERN char * envuGetUsername (void)
 Gets user name.
 
_ENVU_EXTERN char * envuGetOS (void)
 Gets the name of running OS.
 
_ENVU_EXTERN char * envuGetOSVersion (void)
 Gets the version of running OS.
 
_ENVU_EXTERN char * envuGetOSProductName (void)
 Gets the product name and its version of running OS.
 
_ENVU_EXTERN char ** envuGetEnvPaths (int *path_count)
 Gets the environment paths from the PATH variable.
 
_ENVU_EXTERN char ** envuParseEnvPaths (const char *env_path, int *path_count)
 Parses the PATH variable.
 
_ENVU_EXTERN void envuFreeEnvPaths (char **paths)
 Frees the memory of an array allocated by envuGetEnvPaths() or envuParseEnvPaths().
 

Macro Definition Documentation

◆ _ENVU_ENUM

#define _ENVU_ENUM (   s)    typedef unsigned int s; enum

◆ _ENVU_EXTERN

#define _ENVU_EXTERN   __attribute__((visibility("default"))) extern

◆ ENVU_VERSION

#define ENVU_VERSION   "0.3.0"

◆ ENVU_VERSION_INT

#define ENVU_VERSION_INT   300

Function Documentation

◆ envuFileExists()

_ENVU_EXTERN int envuFileExists ( const char *  path)

Returns if the specified path is a regular file for not.

Parameters
pathA path.
Returns
If the specified path is a regular file or not.

◆ envuFree()

_ENVU_EXTERN void envuFree ( void *  p)

Frees the memory of a string allocated by c-env-utils.

Parameters
pA pointer to a string.

◆ envuFreeEnvPaths()

_ENVU_EXTERN void envuFreeEnvPaths ( char **  paths)

Frees the memory of an array allocated by envuGetEnvPaths() or envuParseEnvPaths().

Parameters
pathsA null-terminated array of strings.

◆ envuGetCwd()

_ENVU_EXTERN char * envuGetCwd ( void  )

Gets the current working directory.

Note
Strings that are returned from this method should be freed with envuFree().
Returns
A string that represents the current working directory. Or a null pointer if failed.

◆ envuGetDirectory()

_ENVU_EXTERN char * envuGetDirectory ( const char *  path)

Gets a parent directory of the specified path.

Note
Strings that are returned from this method should be freed with envuFree().
Parameters
pathA path.
Returns
A parent directory of the specified path. Or a null pointer if failed.

◆ envuGetEnv()

_ENVU_EXTERN char * envuGetEnv ( const char *  name)

Gets a value of an environment variable.

Note
Strings that are returned from this method should be freed with envuFree().
Parameters
nameA name of an environment variable.
Returns
A value of an environment variable. Or a null pointer if failed.

◆ envuGetEnvPaths()

_ENVU_EXTERN char ** envuGetEnvPaths ( int *  path_count)

Gets the environment paths from the PATH variable.

Note
Arrays that are returned from this method should be freed with envuFreeEnvPaths().
Parameters
path_countThe number of paths will be stored here if it's not a null pointer.
Returns
A null-terminated array of strings. Or a null pointer if failed.

◆ envuGetExecutableDir()

_ENVU_EXTERN char * envuGetExecutableDir ( void  )

Gets the directory of the executing binary.

Note
Strings that are returned from this method should be freed with envuFree().
Returns
A string that represents the directory of the executing binary. Or a null pointer if failed.

◆ envuGetExecutablePath()

_ENVU_EXTERN char * envuGetExecutablePath ( void  )

Gets the path to the executing binary.

Note
Strings that are returned from this method should be freed with envuFree().
Returns
A string that represents the path to the executing binary. Or a null pointer if failed.

◆ envuGetFullPath()

_ENVU_EXTERN char * envuGetFullPath ( const char *  path)

Gets a full path of the specified path.

It resolves dot segments but ignores symlinks.

Note
Strings that are returned from this method should be freed with envuFree().
Parameters
pathA path.
Returns
A full path of the specified path. Or a null pointer if failed.

◆ envuGetHome()

_ENVU_EXTERN char * envuGetHome ( void  )

Gets user's home directory.

Note
Strings that are returned from this method should be freed with envuFree().
Returns
A string that represents user's home directory. Or a null pointer if failed.

◆ envuGetOS()

_ENVU_EXTERN char * envuGetOS ( void  )

Gets the name of running OS.

e.g. "Windows" for Windows, "Darwin" for macOS, and "Linux" for Linux distros. You can see more examples here.

Note
Strings that are returned from this method should be freed with envuFree().
Returns
A string that represents running OS. Or a null pointer if failed.

◆ envuGetOSProductName()

_ENVU_EXTERN char * envuGetOSProductName ( void  )

Gets the product name and its version of running OS.

e.g. "Microsoft Windows 10 Home", "Mac OS X 10.15.5", and "Ubuntu 20.04 LTS". You can see more examples here.

Warning
Note that the return value can be disguised by users. Your program may execute unexpected codes if you use conditional branching by the return value.
Note
Strings that are returned from this method should be freed with envuFree().
Returns
A string that represents the product name and its version of running OS. Or a null pointer if failed.

◆ envuGetOSVersion()

_ENVU_EXTERN char * envuGetOSVersion ( void  )

Gets the version of running OS.

You can see examples of return values here.

Note
Strings that are returned from this method should be freed with envuFree().
Returns
A string that represents the version of running OS. Or a null pointer if failed.

◆ envuGetRealPath()

_ENVU_EXTERN char * envuGetRealPath ( const char *  path)

Gets a real path of the specified path.

It can resolve symlinks but it fails if the specified path does not exist.

Warning
This function can NOT resolve symlinks on Windows.
Note
Strings that are returned from this method should be freed with envuFree().
Parameters
pathA path.
Returns
A real path of the specified path. Or a null pointer if failed.

◆ envuGetUsername()

_ENVU_EXTERN char * envuGetUsername ( void  )

Gets user name.

Note
Strings that are returned from this method should be freed with envuFree().
Returns
A string that represents user name. Or a null pointer if failed.

◆ envuGetVersion()

_ENVU_EXTERN const char * envuGetVersion ( void  )

Gets the version of c-env-utils.

It should be of the form x.x.x.

Returns
A string that represents the version.

◆ envuGetVersionAsInt()

_ENVU_EXTERN int envuGetVersionAsInt ( void  )

Gets the version of c-env-utils as an integer.

The value should be major * 100 + minor * 100 + patch. If revGetVersion() == "1.2.3" then revGetVersionAsInt() == 10203.

Returns
An integer that represents the version.

◆ envuParseEnvPaths()

_ENVU_EXTERN char ** envuParseEnvPaths ( const char *  env_path,
int *  path_count 
)

Parses the PATH variable.

Its paths should be separated by semicolons on Windows, or by colons on other platforms.

Note
Arrays that are returned from this method should be freed with envuFreeEnvPaths().
Parameters
env_pathThe value of the PATH variable.
path_countThe number of paths will be stored here if it's not a null pointer.
Returns
A null-terminated array of strings. Or a null pointer if failed.

◆ envuPathExists()

_ENVU_EXTERN int envuPathExists ( const char *  path)

Returns if the specified path exists for not.

Parameters
pathA path.
Returns
If the specified path exists or not.

◆ envuSetCwd()

_ENVU_EXTERN int envuSetCwd ( const char *  path)

Sets the current working directory.

Parameters
pathA string that represents a directory.
Returns
0 if successful. -1 indicates failure.

◆ envuSetEnv()

_ENVU_EXTERN int envuSetEnv ( const char *  name,
const char *  value 
)

Sets an environment variable.

Note
On Windows, empty strings will be treated as null pointers.
Parameters
nameA name of an environment variable.
valueA value of an environment variable. Or a null pointer to remove the variable.
Returns
0 if successful. -1 indicates failure.