Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -989,12 +989,29 @@ int os_self_exe_path(Buf *out_path) {
}

#elif defined(ZIG_OS_DARWIN)
// How long is the executable's path?
uint32_t u32_len = 0;
int ret1 = _NSGetExecutablePath(nullptr, &u32_len);
assert(ret1 != 0);
buf_resize(out_path, u32_len);
int ret2 = _NSGetExecutablePath(buf_ptr(out_path), &u32_len);

// Make a buffer having room for the temp path.
Copy link
Contributor

@bnoordhuis bnoordhuis Jun 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might just be me but I personally don't really like comments that just say what the code on the line below does.

As a rule of thumb, comments should explain what won't be obvious from just looking at the code, not restate the code in prose.

edit: hadn't noticed this has been merged between starting and finishing the review.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The justification is that it makes code faster to read.

Buf *tmp = buf_alloc_fixed(u32_len);

// Fill the executable path.
int ret2 = _NSGetExecutablePath(buf_ptr(tmp), &u32_len);
assert(ret2 == 0);

// Resolve the real path from that.
buf_resize(out_path, PATH_MAX);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not relevant to Zig but in libuv we allocate 2*PATH_MAX to work around a bug in old versions of Apple's libc where the expanded path is sometimes > PATH_MAX.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll throw that in

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

char *real_path = realpath(buf_ptr(tmp), buf_ptr(out_path));
assert(real_path == buf_ptr(out_path));

// Deallocate our scratch space.
buf_deinit(tmp);

// Resize out_path for the correct length.
buf_resize(out_path, strlen(buf_ptr(out_path)));

return 0;
#elif defined(ZIG_OS_LINUX)
buf_resize(out_path, 256);
Expand Down