aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-09-01 08:52:58 +0000
committer_ov4 <ov4@ov4.org>2026-09-01 08:52:58 +0000
commite91283fd2e449e17679bb0a911ea03f94cd1d759 (patch)
tree2ab688ad63c5f362bba928d749c2f9e619e86329
parent22ee3d7ca978cddf87c132e2271df0cb47b4a99e (diff)
better cd
-rw-r--r--include/path.h2
-rw-r--r--src/eval.cc5
-rw-r--r--src/path.cc55
3 files changed, 36 insertions, 26 deletions
diff --git a/include/path.h b/include/path.h
index e669ca8..c5f0d2b 100644
--- a/include/path.h
+++ b/include/path.h
@@ -7,6 +7,8 @@
#include <iterator>
#include <iostream>
+#include "all.h"
+
namespace ov4
{
diff --git a/src/eval.cc b/src/eval.cc
index 4f293c7..8785dd6 100644
--- a/src/eval.cc
+++ b/src/eval.cc
@@ -34,13 +34,14 @@ void eval(char *cmdline)
block_all(&prev);
pid_t pid = fork();
- const char* cmd = find_cmd(argv[0]).c_str();
+ string cmd = find_cmd(argv[0]);
+ const char* cmd_c = cmd.c_str();
if (pid == 0) // child
{
sigprocmask(SIG_SETMASK, &prev, nullptr);
setpgid(0, 0);
- execve(cmd, argv, environ);
+ execve(cmd_c, argv, environ);
atomic_print(cmdline, true);
atomic_print(": Command not found\n");
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)