which command

This commit is contained in:
Kelvin Sherlock 2016-02-11 15:48:46 -05:00
parent 446e3e5e1e
commit 50f171d5c8
3 changed files with 37 additions and 0 deletions

View File

@ -12,6 +12,9 @@
#include <cstdio>
#include <cctype>
#include "cxx/string_splitter.h"
#include "cxx/filesystem.h"
//MAXPATHLEN
#include <sys/param.h>
@ -21,6 +24,9 @@ namespace ToolBox {
std::string UnixToMac(const std::string path);
}
namespace fs = filesystem;
namespace {
/*

View File

@ -16,6 +16,7 @@ int builtin_set(Environment &e, const std::vector<std::string> &, const fdmask &
int builtin_unset(Environment &e, const std::vector<std::string> &, const fdmask &);
int builtin_export(Environment &e, const std::vector<std::string> &, const fdmask &);
int builtin_unexport(Environment &e, const std::vector<std::string> &, const fdmask &);
int builtin_which(Environment &e, const std::vector<std::string> &, const fdmask &);
int builtin_evaluate(Environment &e, std::vector<token> &&, const fdmask &);

View File

@ -12,6 +12,8 @@
#include <cerrno>
#include <cstdlib>
#include "cxx/filesystem.h"
#include "cxx/string_splitter.h"
#include <unistd.h>
#include <sys/wait.h>
@ -19,6 +21,34 @@
extern volatile int control_c;
namespace fs = filesystem;
extern fs::path mpw_path();
namespace ToolBox {
std::string MacToUnix(const std::string path);
std::string UnixToMac(const std::string path);
}
fs::path which(const Environment &env, const std::string &name) {
std::error_code ec;
if (name.find_first_of("/:") != name.npos) {
// canonical?
fs::path p(name);
if (fs::exists(p, ec)) return name;
return "";
}
std::string s = env.get("commands");
for (string_splitter ss(s, ','); ss; ++ss) {
fs::path p(ToolBox::MacToUnix(*ss));
p /= name;
if (fs::exists(p, ec)) return p;
}
// error..
return "";
}
void launch_mpw(const Environment &env, const std::vector<std::string> &argv, const fdmask &fds) {