diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-12-23 00:29:39 -0500 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-12-23 00:57:56 -0500 |
| commit | 39c7bd24e4f768b23074b8634ac637b175b7639f (patch) | |
| tree | e3454d346c9d37abb9bb56847d3743eeb6cfa4e8 /std/os | |
| parent | 760b307e8a8fcbb31fc1f2abb170ef7399aa917e (diff) | |
| download | zig-39c7bd24e4f768b23074b8634ac637b175b7639f.tar.gz zig-39c7bd24e4f768b23074b8634ac637b175b7639f.zip | |
port most of main.cpp to self hosted compiler
Diffstat (limited to 'std/os')
| -rw-r--r-- | std/os/index.zig | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/std/os/index.zig b/std/os/index.zig index 09109c3242..8e79eda40b 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -1543,6 +1543,39 @@ pub fn openSelfExe() -> %io.File { } } +/// Get the directory path that contains the current executable. +/// Caller owns returned memory. +pub fn selfExeDirPath(allocator: &mem.Allocator) -> %[]u8 { + switch (builtin.os) { + Os.linux => { + // If the currently executing binary has been deleted, + // the file path looks something like `/a/b/c/exe (deleted)` + // This path cannot be opened, but it's valid for determining the directory + // the executable was in when it was run. + const full_exe_path = %return readLink(allocator, "/proc/self/exe"); + %defer allocator.free(full_exe_path); + const dir = path.dirname(full_exe_path); + return allocator.shrink(u8, full_exe_path, dir.len); + }, + Os.windows => { + @panic("TODO windows std.os.selfExeDirPath"); + //buf_resize(out_path, 256); + //for (;;) { + // DWORD copied_amt = GetModuleFileName(nullptr, buf_ptr(out_path), buf_len(out_path)); + // if (copied_amt <= 0) { + // return ErrorFileNotFound; + // } + // if (copied_amt < buf_len(out_path)) { + // buf_resize(out_path, copied_amt); + // return 0; + // } + // buf_resize(out_path, buf_len(out_path) * 2); + //} + }, + else => @compileError("unimplemented: std.os.selfExeDirPath for " ++ @tagName(builtin.os)), + } +} + pub fn isTty(handle: FileHandle) -> bool { if (is_windows) { return windows_util.windowsIsTty(handle); |
