From 7ff2c76673e80b638a3bda438862eec7a1cd8056 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vin=C3=ADcius=20R=2E=20Miguel?= Date: Wed, 3 Jun 2020 14:36:36 -0300 Subject: [PATCH] Simple commands working ok now --- Sources/opsys.cpp | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/Sources/opsys.cpp b/Sources/opsys.cpp index 2a80c98..d52f8c5 100644 --- a/Sources/opsys.cpp +++ b/Sources/opsys.cpp @@ -3,15 +3,6 @@ OpSys OS; // Global OpSys variable. Should be accessible to all modules. bool sigint = false; -//template -//int execvps(const char* file, const char* const (&argv)[N]) -//{ -// cerr << "Here\n"; -// assert((N > 0) && (argv[N - 1] == nullptr)); -// return execvp(file, const_cast(argv)); -//} - - void signal_handler(int s) { if (s==SIGINT) @@ -58,30 +49,42 @@ void OpSys::change_dir(vector command) } } -short int OpSys::simple_command(vector tokens) +std::vector make_argv( std::vectorconst& in ) { - pid_t pid; // valor para teste pai/filho + std::vector out; + out.reserve( in.size() + 1 ); -// std::vector argv(tokens.size() + 1); + for (const auto& s : in) { + out.push_back( s.data() ); + } + out.push_back(NULL); + out.shrink_to_fit(); -// std::transform(tokens.begin(), tokens.end(), argv.begin(), -// [](std::string arg) { -// return &(arg)[0]; -// }); + return out; +} - string command = ""; - for (unsigned int i = 1; i tokens) +{ + pid_t pid; + + int status; + if(is_verbose) { - command += tokens[i-1]; + cerr << "execvp(\"" << tokens[0] << "\",\""; + for(unsigned int i = 0; i < tokens.size()-1; i++) + cerr << tokens[i] << ' '; + cerr << tokens.back(); + cerr << "\");\n"; } - string bincmd = "/bin/" + command; - int status; + + pid = fork(); if (pid == 0) { -// execvp(argv[0], argv.data()); - execlp(bincmd.c_str(), command.c_str(), NULL); + //printf("execlp(%s, %s, NULL);", bincmd.c_str(), command.c_str()); + execvp(tokens[0].c_str(), const_cast(make_argv(tokens).data())); cerr << tokens[0] << ": comando não encontrado\n"; + exit(0); // TODO: exit with cleanup } else if (pid < 0) { -- 2.11.4.GIT