| ... | ... | @@ -1509,6 +1509,13 @@ test "openSelfExe" { |
| 1509 | 1509 | |
| 1510 | 1510 | pub const SelfExePathError = os.ReadLinkError || os.SysCtlError; |
| 1511 | 1511 | |
| 1512 | /// `selfExePath` except allocates the result on the heap. |
| 1513 | /// Caller owns returned memory. |
| 1514 | pub fn selfExePathAlloc(allocator: *Allocator) ![]u8 { |
| 1515 | var buf: [MAX_PATH_BYTES]u8 = undefined; |
| 1516 | return mem.dupe(allocator, u8, try selfExePath(&buf)); |
| 1517 | } |
| 1518 | |
| 1512 | 1519 | /// Get the path to the current executable. |
| 1513 | 1520 | /// If you only need the directory, use selfExeDirPath. |
| 1514 | 1521 | /// If you only want an open file handle, use openSelfExe. |
| ... | ... | @@ -1568,16 +1575,6 @@ pub fn selfExeDirPathAlloc(allocator: *Allocator) ![]u8 { |
| 1568 | 1575 | /// Get the directory path that contains the current executable. |
| 1569 | 1576 | /// Returned value is a slice of out_buffer. |
| 1570 | 1577 | pub fn selfExeDirPath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]const u8 { |
| 1571 | | if (builtin.os.tag == .linux) { |
| 1572 | | // If the currently executing binary has been deleted, |
| 1573 | | // the file path looks something like `/a/b/c/exe (deleted)` |
| 1574 | | // This path cannot be opened, but it's valid for determining the directory |
| 1575 | | // the executable was in when it was run. |
| 1576 | | const full_exe_path = try os.readlinkC("/proc/self/exe", out_buffer); |
| 1577 | | // Assume that /proc/self/exe has an absolute path, and therefore dirname |
| 1578 | | // will not return null. |
| 1579 | | return path.dirname(full_exe_path).?; |
| 1580 | | } |
| 1581 | 1578 | const self_exe_path = try selfExePath(out_buffer); |
| 1582 | 1579 | // Assume that the OS APIs return absolute paths, and therefore dirname |
| 1583 | 1580 | // will not return null. |