diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-05-03 21:01:34 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-05-03 21:01:34 +0300 |
commit | fb9ca7c3aaed3080399fd0cde659699879002eb0 (patch) | |
tree | cf50437aacd54ba43b8f9d98899af2aaa8559319 /windows/realpath.c | |
parent | b4bb3fdb9cfcb76fb5b64e0a031a70c807ac7395 (diff) | |
parent | 4cd21deebca0e75b77d62d8d748aa66453532d9b (diff) | |
download | micropython-fb9ca7c3aaed3080399fd0cde659699879002eb0.tar.gz micropython-fb9ca7c3aaed3080399fd0cde659699879002eb0.zip |
Merge pull request #554 from stinos/mingw-realpath
mingw: Add implementation of realpath()
Diffstat (limited to 'windows/realpath.c')
-rw-r--r-- | windows/realpath.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/windows/realpath.c b/windows/realpath.c new file mode 100644 index 0000000000..550298a7d5 --- /dev/null +++ b/windows/realpath.c @@ -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); +} |