Simple functions for paths, files, environment variables, processes, and system information.
Free functions & types
Types, constants & data
enum class CFileType { Missing, File, Directory, Symlink, Other };
Functions
cstr cJoinPath(std::initializer_list<cstr> parts);
cstr cJoinPath(const cstr& first, const cstr& second);
template<class... Parts> requires(sizeof...(Parts) > 0) cstr cJoinPath(const cstr& first, const cstr& second, const Parts&... rest);
Joins path components with filesystem path semantics; an absolute component can replace the preceding path.
cstr cAbsolutePath(const cstr& path, const cstr& base = "");
Resolves a relative path against base, or the current directory when base is empty.
Normalizes . and .. lexically without requiring the path to exist.
cstr cRelativePath(const cstr& path, const cstr& base = "");
Returns a lexical path from base to path, making both absolute first. An empty base uses the current directory; symbolic links are not resolved.
Resolves the existing path, including symbolic links.
Expands supported environment syntax and a leading home-directory marker.
bool cHasEnv(const cstr& name);
Tests whether the process environment contains the variable, including one set to an empty string.
Returns the environment value. A missing variable gives an empty string, or the explicit fallback in the two-argument overload.
void cUnsetEnv(const cstr& name);
Removes a variable from the current process environment. It does not change the parent shell’s environment.
cstr cExpandEnvs(const cstr& text, bool strict = true);
Expands $NAME, ${NAME}, or $(NAME) once. A backslash escapes a dollar. Missing variables throw unless strict=false, which leaves them intact.
void cStrToFile(const cstr& text, const cstr& path);
Writes the string’s bytes, replacing any existing file contents. Embedded NUL bytes are preserved.
void cAppendToFile(const cstr& text, const cstr& path);
Appends the string’s bytes, creating the file if necessary. No separator or newline is added automatically.
Reads the complete file into an owning CBuffer. The returned bytes remain valid independently of the file.
Reads lines into a vector, removing LF and any immediately preceding CR. Blank lines are retained, and a final unterminated line is included; use cForEachLine() to avoid loading all lines at once.
void cForEachLine(const cstr& path, const std::function<bool(const cstr&)>& visit);
Visits lines without LF or a preceding CR; return false to stop reading.
void cSaveAtomic(const cstr& text, const cstr& path);
void cSaveAtomic(const CBuffer& buffer, const cstr& path);
Writes through a temporary sibling and atomically replaces the destination. The parent directory must exist.
bool cCreateDirs(const cstr& path);
Creates missing directories throughout the path. Returns true if creation occurred, or false if the directory already existed.
void cCopyFile(const cstr& source, const cstr& destination, bool overwrite = false);
Copies a file’s contents to the destination. An existing destination is rejected unless overwrite is true; use cCopyTree() for directories.
void cCopyTree(const cstr& source, const cstr& destination, bool overwrite = false);
Copies directory contents into the destination. Merges existing directories and preserves symlinks without following them; overwrite applies to leaf entries.
bool cRemove(const cstr& path);
Removes a file or empty directory; returns false when it does not exist.
uint64_t cRemoveTree(const cstr& path);
Removes an entire tree and returns the number of removed entries.
CFileInfo cFileInfo(const cstr& path, bool followSymlinks = false);
Returns metadata for a link itself unless followSymlinks=true. writeTime is Unix seconds and permissions holds POSIX permission bits.
bool cIsSymlink(const cstr& path);
Tests the directory entry itself for a symbolic link, including a link whose target is missing.
Returns the target text stored in a symbolic link. A relative target remains relative to the directory containing that link.
void cCreateSymlink(const cstr& target, const cstr& path);
Creates a link at path containing the supplied target. The target may be relative and need not already exist.
Visits entries below the root; false stops the entire walk. When following links, each physical directory is traversed at most once.
Matches basenames with *, ?, and bracket patterns; returns sorted paths.
Returns the current user’s home directory. Uses an absolute HOME environment value when available, otherwise the account’s home directory made absolute.
Returns the resolved system temporary-directory path. This locates the shared temporary directory; use cCreateTempDir() to create a new owned directory.
Returns the resolved path of the running executable, including symbolic-link resolution.
cstr cConfigDir(const cstr& application = "");
Returns the user configuration location, optionally with one application-name component appended. Uses ~/Library/Application Support on macOS and an absolute XDG_CONFIG_HOME or ~/.config elsewhere; the directory is not created.
cstr cCacheDir(const cstr& application = "");
Returns the user cache location, optionally with one application-name component appended. Uses ~/Library/Caches on macOS and an absolute XDG_CACHE_HOME or ~/.cache elsewhere; the directory is not created.
cstr cDataDir(const cstr& application = "");
Returns the user data location, optionally with one application-name component appended. Uses ~/Library/Application Support on macOS and an absolute XDG_DATA_HOME or ~/.local/share elsewhere; the directory is not created.
Creates an owned file with mode 0600. An empty directory selects cTempDir().
CTempDir cCreateTempDir(const cstr& dir = "", const cstr& prefix = "catalyst-");
Creates an owned directory with mode 0700. An empty directory selects cTempDir().
Executes an argument vector directly, supplies stdin, and captures both output streams. Nonzero exit status is returned; launch and I/O failures throw.
Runs a shell command, supplies stdin, and captures stdout and stderr. Shell quoting and expansion apply.
Searches for an executable and returns an empty string if none is found.
inline double cMonotonicNow();
Returns monotonic seconds for measuring intervals; the origin has no calendar meaning.
inline double cElapsed(double start);
Returns seconds elapsed since a cMonotonicNow() reading.
Returns the operating system’s complete hostname. Unlike the legacy cHost() helper, it preserves punctuation and domain components.
Returns filesystem capacity, free bytes, and bytes available to the current user for the given path. Reserved filesystem space can make available smaller than free.
Returns a snapshot of this process’s resident and virtual memory sizes in bytes. These measure different aspects of memory use and need not track one another.
uint64_t cAvailableMemory();
Estimates system memory available for use, including reclaimable memory where the platform reports it. This is a changing snapshot, not a guarantee that an allocation will succeed.
Returns the initial alphanumeric part of the hostname. Use cHostName() for the full hostname.
inline void cSetEnv(const cstr& name, const cstr& value, bool redefine = true);
Sets a variable in the process environment. With redefine=false, an existing value is retained; changes do not update the parent shell.
inline size_t cProcessId();
Returns the current process ID.
inline size_t cSystemMemory();
Returns the system’s physical memory size in bytes using the reported page count and page size.
inline void cReplaceEnvs(cstr& s);
Legacy in-place expansion of $(NAME). A missing variable is left in place with a warning.
inline bool cExists(const cstr& path);
Tests whether the path resolves to an existing filesystem object. Follows symbolic links, so a dangling link reports false.
inline bool cIsDir(const cstr& path);
Tests whether the path resolves to a directory, following symbolic links.
inline bool cIsFile(const cstr& path);
Tests whether the path resolves to a regular file, following symbolic links.
inline cstr cBasename(const cstr& path);
Returns the final path component, including its extension.
inline cstr cFilename(const cstr& path);
Returns the final component without its extension.
inline cstr cParentDir(const cstr& path);
Returns the lexical parent portion of a path. A bare filename has an empty parent; this does not inspect the filesystem.
inline cstr cExtension(const cstr& path);
Returns the final filename extension without its leading dot, or an empty string if none exists.
inline double cWriteTime(const cstr& path);
Returns the last modification time as approximate Unix seconds, converted from the filesystem clock.
inline cstr cCurrentDir();
Returns the process’s current working directory.
Returns the names of all immediate directory entries, including hidden entries and subdirectories. Results are not sorted.
inline size_t cFileSize(const cstr& path);
Returns a regular file’s size in bytes. This does not recursively total directory contents.
inline bool cReadable(const cstr& path);
Tests read access using the current process credentials. It is a preliminary snapshot; a later open can still fail.
inline bool cWritable(const cstr& path);
Tests write access using the current process credentials. It does not create the path or ensure a later write will succeed.
inline bool cExecutable(const cstr& path);
Tests execute access using the current process credentials. On directories, this tests search permission rather than identifying a runnable program.
inline cstr cTempPath(const cstr& path, const cstr& suffix = "");
Produces a temporary-looking path string; it does not create or reserve the path.
Returns Unix wall-clock seconds.
inline uint64_t cTicks();
Returns the high-resolution clock’s native tick count; do not assume seconds or nanoseconds.
template<class R, class P> inline void cSleep(const std::chrono::duration<R, P>& dt);
inline void cSleep(double dt);
inline void cSleep();
Blocks the current thread for the supplied duration. The numeric overload uses seconds; scheduling may delay the thread beyond the requested interval.
inline cstr cFileToStr(const cstr& path);
Reads the complete file into a string, preserving its bytes and line endings. Throws CError when the file cannot be opened or read.
inline void cCreateDir(const cstr& path);
Creates one directory and throws if creation fails, including when it already exists. cCreateDirs() creates missing parents and tolerates existing directories.
inline void cRename(const cstr& oldPath, const cstr& newPath);
Renames or moves a filesystem entry using the platform’s rename semantics. Cross-filesystem moves can fail; this is not a copy-and-delete operation.
inline size_t cThreadCount();
Returns the hardware concurrency hint, which can be zero if unavailable. This is not a count of currently running process threads.
Produces a clock-derived printable identifier; uniqueness is not guaranteed across processes.
Prints the process ID and sleeps for 30 seconds.
inline void cSaveBinary(char* buf, size_t bytes, const cstr& path);
Writes exactly the supplied byte range to a file, replacing existing contents. The caller retains ownership of the source memory.