std::ffi::c::env (v0.2.2)

Raw C bindings for environment access.

Latest

Overview

std::ffi::c::env exposes C environment functions such as getenv, setenv, unsetenv, and clearenv.

Prefer std::env for application code; use this layer when you need the raw C symbols directly.

Source: thrustc/std/v0.2.2/ffi/c/env.thrust

Public Signatures

Exported declarations for this module snapshot.

fn getenv(name: CString)         CString @public @extern("getenv")        @convention("C");
fn secureGetenv(name: CString)   CString @public @extern("secure_getenv") @convention("C");
fn setenv(name: CString, value: CString, overwrite: s32) s32 @public @extern("setenv") @convention("C");
fn unsetenv(name: CString)       s32     @public @extern("unsetenv")      @convention("C");
fn clearenv()                    s32     @public @extern("clearenv")      @convention("C");
fn putenv(assignment: CString)   s32     @public @extern("putenv")        @convention("C");

Behavior and Use

These functions call directly into the platform C library environment helpers.

Prefer std::env unless you specifically need the raw C symbols.

Examples

import std::ffi::c::env as c;

// Keep raw getenv behind a small helper so the rest of the program does not
// have to deal with null C pointers directly.
fn pathOrDefault() CString @public {
    var value: CString = c::getenv("PATH");

    if (value as ptr[char]) == nullptr {
        return "/usr/bin";
    }

    return value;
}
import std::ffi::c::env as c;

// This writes process-wide state. Prefer std::env in application code unless
// the raw C behavior is exactly what you need.
fn enableLocalMode() bool @public {
    return c::setenv("THRUST_MODE", "local", 1) == 0;
}
import std::ffi::c::env as c;

// A tiny cleanup wrapper keeps the raw unsetenv result visible to callers.
fn clearLocalMode() s32 @public {
    return c::unsetenv("THRUST_MODE");
}

Notes

Back to std index