argsAlloc
Caller must call argsFree on result. On Windows, the result is encoded as WTF-8. On other platforms, the result is an opaque sequence of bytes with no particular encoding.
Function parameters
Parameters
Type definitions in this namespace
Types
- ArgIteratorWindows
- Iterator that implements the Windows command-line parsing algorithm.
- ArgIteratorGeneralOptions
- Optional parameters for `ArgIteratorGeneral`
- ArgIterator
- Cross-platform command line argument iterator.
Functions in this namespace
Functions
- getCwd
- The result is a slice of `out_buffer`, from index `0`.
- getCwdAlloc
- Caller must free the returned memory.
- getEnvMap
- Returns a snapshot of the environment variables of the current process.
- getEnvVarOwned
- Caller must free returned memory.
- hasEnvVarConstant
- On Windows, `key` must be valid WTF-8.
- hasNonEmptyEnvVarConstant
- On Windows, `key` must be valid WTF-8.
- parseEnvVarInt
- Parses an environment variable as an integer.
- hasEnvVar
- On Windows, if `key` is not valid [WTF-8](https://simonsapin.github.io/wtf-8/),
- hasNonEmptyEnvVar
- On Windows, if `key` is not valid [WTF-8](https://simonsapin.github.io/wtf-8/),
- getenvW
- Windows-only.
- ArgIteratorGeneral
- A general Iterator to parse a string into a set of arguments
- args
- Holds the command-line arguments, with the program name as the first entry.
- argsWithAllocator
- You must deinitialize iterator's internal buffers by calling `deinit` when done.
- argsAlloc
- Caller must call argsFree on result.
- getUserInfo
- POSIX function which gets a uid from username.
- posixGetUserInfo
- TODO this reads /etc/passwd.
- execv
- Replaces the current process image with the executed process.
- execve
- Replaces the current process image with the executed process.
- totalSystemMemory
- Returns the total system memory, in bytes as a u64.
- cleanExit
- Indicate that we are now terminating with a successful exit code.
- raiseFileDescriptorLimit
- Raise the open file descriptor limit.
- createEnvironFromMap
- Creates a null-delimited environment variable block in the format
- createEnvironFromExisting
- Creates a null-delimited environment variable block in the format
- createWindowsEnvBlock
- Caller must free result.
- fatal
- Logs an error and then terminates the process with exit code 1.
Error sets in this namespace
Error Sets
Tells whether calling the `execv` or `execve` functions will be a compile error.
Values
Source
Implementation
pub fn argsAlloc(allocator: Allocator) ![][:0]u8 {
// TODO refactor to only make 1 allocation.
var it = try argsWithAllocator(allocator);
defer it.deinit();
var contents = std.array_list.Managed(u8).init(allocator);
defer contents.deinit();
var slice_list = std.array_list.Managed(usize).init(allocator);
defer slice_list.deinit();
while (it.next()) |arg| {
try contents.appendSlice(arg[0 .. arg.len + 1]);
try slice_list.append(arg.len);
}
const contents_slice = contents.items;
const slice_sizes = slice_list.items;
const slice_list_bytes = try math.mul(usize, @sizeOf([]u8), slice_sizes.len);
const total_bytes = try math.add(usize, slice_list_bytes, contents_slice.len);
const buf = try allocator.alignedAlloc(u8, .of([]u8), total_bytes);
errdefer allocator.free(buf);
const result_slice_list = mem.bytesAsSlice([:0]u8, buf[0..slice_list_bytes]);
const result_contents = buf[slice_list_bytes..];
@memcpy(result_contents[0..contents_slice.len], contents_slice);
var contents_index: usize = 0;
for (slice_sizes, 0..) |len, i| {
const new_index = contents_index + len;
result_slice_list[i] = result_contents[contents_index..new_index :0];
contents_index = new_index + 1;
}
return result_slice_list;
}