aboutsummaryrefslogtreecommitdiff
path: root/src/path.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/path.cc')
-rw-r--r--src/path.cc55
1 files changed, 31 insertions, 24 deletions
diff --git a/src/path.cc b/src/path.cc
index 6feabee..74e8a37 100644
--- a/src/path.cc
+++ b/src/path.cc
@@ -13,40 +13,47 @@ string find_cmd(const char * s)
string find_cmd(const string &s)
{
- if (s.size() == 0) return "";
- string unified_s = path_unifier(s);
- if (access(unified_s.c_str(), F_OK) == 0)
- return unified_s;
-
- string path = getenv("PATH");
- size_t i = 0, j = 0, l = path.size();
- while (true)
- {
- for (; j < l; j++)
- if (path[j] == ':')
- break;
-
- string cmd = path.substr(i, j-i) + '/' +s;
- if (access(cmd.c_str(), F_OK) == 0)
- return cmd;
-
- if (j == l)
- return "/";
-
- j++;
- i=j;
-
- }
+ string unified = path_unifier(s, true);
+ LOG << unified << endl;
+ return unified;
}
string path_unifier(const string &s, bool is_cmd)
{
if (s.size() == 0) return "";
+
if (s[0] == '/') return s;
if (s[0] == '.')
{
return get_current_dir() + s.substr(1);
}
+
+ // single word, use path
+ if (is_cmd && (s.find("/") == s.size()))
+ {
+ string path = getenv("PATH");
+ size_t i = 0, j = 0, l = path.size();
+ while (true)
+ {
+ for (; j < l; j++)
+ if (path[j] == ':')
+ break;
+
+ string cmd = path.substr(i, j-i) + '/' +s;
+ if (access(cmd.c_str(), F_OK) == 0)
+ return cmd;
+
+ if (j == l)
+ return "";
+
+ j++;
+ i=j;
+
+ }
+ }
+
+ return get_current_dir() + "/" + s;
+
}
int set_current_dir(const string &s)