aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/eval.h1
-rw-r--r--include/path.h16
-rw-r--r--src/eval.cc3
-rw-r--r--src/main.cc8
-rw-r--r--src/path.cc42
5 files changed, 68 insertions, 2 deletions
diff --git a/include/eval.h b/include/eval.h
index bd3912c..a6157e0 100644
--- a/include/eval.h
+++ b/include/eval.h
@@ -18,6 +18,7 @@
#include "job.h"
#include "ground.h"
#include "parse.h"
+#include "path.h"
namespace ov4
{
diff --git a/include/path.h b/include/path.h
new file mode 100644
index 0000000..ca0e052
--- /dev/null
+++ b/include/path.h
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <unistd.h>
+#include <optional>
+#include <string>
+#include <unistd.h>
+#include <iterator>
+
+
+namespace ov4
+{
+
+std::string find_cmd(char* s);
+std::string find_cmd(std::string s);
+
+}
diff --git a/src/eval.cc b/src/eval.cc
index 369e032..4350367 100644
--- a/src/eval.cc
+++ b/src/eval.cc
@@ -43,12 +43,13 @@ void eval(char *cmdline)
block_all(&prev);
pid_t pid = fork();
+ const char* cmd = find_cmd(argv[0]).c_str();
if (pid == 0) // child
{
sigprocmask(SIG_SETMASK, &prev, nullptr);
setpgid(0, 0);
- execve(argv[0], argv, environ);
+ execve(cmd, argv, environ);
atomic_print(cmdline, true);
atomic_print(": Command not found\n");
diff --git a/src/main.cc b/src/main.cc
index 5a2bd71..1042cf8 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -22,6 +22,7 @@
#include "handler.h"
#include "ground.h"
#include "eval.h"
+#include "path.h"
using namespace std;
using namespace ov4;
@@ -47,13 +48,18 @@ handler_t *Signal(int signum, handler_t *handler);
// built-in commands
void builtin_command_quit();
-
+void test()
+{
+ cout << find_cmd("ls") << endl;
+ exit(0);
+}
/*
* main - The shell's main routine
*/
int main(int argc, char **argv)
{
+ //test();
init();
char c;
char cmdline[MAXLINE];
diff --git a/src/path.cc b/src/path.cc
new file mode 100644
index 0000000..519fd20
--- /dev/null
+++ b/src/path.cc
@@ -0,0 +1,42 @@
+#include "path.h"
+
+namespace ov4
+{
+
+using namespace std;
+
+string find_cmd(char* s)
+{
+ string tmp = s;
+ return find_cmd(tmp);
+}
+
+string find_cmd(string s)
+{
+ if (s.size() == 0) return "/";
+ if (s[0] == '/') return 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;
+ cout << cmd << endl;
+ if (access(cmd.c_str(), F_OK) == 0)
+ return cmd;
+
+ if (j == l)
+ return "/";
+
+ j++;
+ i=j;
+
+ }
+}
+
+
+}