-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
mingw: Add implementation of realpath() #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <stdlib.h> | ||
#include <errno.h> | ||
#include <io.h> | ||
|
||
#ifndef R_OK | ||
#define R_OK 4 | ||
#endif | ||
|
||
// Make sure a path only has forward slashes. | ||
char *to_unix_path(char *p) { | ||
if (p != NULL) { | ||
char *pp = p; | ||
while (*pp != 0) { | ||
if (*pp == '\\') | ||
*pp = '/'; | ||
++pp; | ||
} | ||
} | ||
return p; | ||
} | ||
|
||
// Implement realpath() using _fullpath and make it use the same error codes as realpath() on unix. | ||
// Also have it return a path with forward slashes only as some code relies on this, | ||
// but _fullpath() returns backward slashes no matter what. | ||
char *realpath(const char *path, char *resolved_path) { | ||
char *ret = NULL; | ||
if (path == NULL) { | ||
errno = EINVAL; | ||
} else if (access(path, R_OK) == 0) { | ||
ret = resolved_path; | ||
if (ret == NULL) | ||
ret = malloc(_MAX_PATH); | ||
if (ret == NULL) { | ||
errno = ENOMEM; | ||
} else { | ||
ret = _fullpath(ret, path, _MAX_PATH); | ||
if (ret == NULL) | ||
errno = EIO; | ||
} | ||
} | ||
return to_unix_path(ret); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
extern char *realpath(const char *path, char *resolved_path); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO wouldn't
be slightly cleaner here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If is always preferred to if/else, unless if gets less efficient than if/else.
The cleanest here IMHO would be avoiding introducing "ret" var and just using resolved_path which is there anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, I just wondered if having to do one less assignment in the
resolved_path == NULL
case might be more efficient. But optimising low-level C code isn't my area of expertise ;-)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did that first but that ended up with multiple returns statements - I am fine with that, but I had the idea most uPy sources didn't use it. Checked again though, and I was wrong, it's used in multiple places..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lurch : jumps are the most inefficient on average, and to be avoided. Simple assignments is 1 cycle almost on each CPU.
@stinos: Sure, returns, break, continue are used well and make it much easier to reason about control flow than ladders and ladders of if/else's or same ladders of conditional expressions in if/while.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't realise an if/else jump was more expensive than just an if jump.