Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m working on a c++ project, where we are writing our own simple shell. Right

ID: 3668812 • Letter: I

Question

I'm working on a c++ project, where we are writing our own simple shell. Right now, I have code to take the input, tokenize it, and format it to go into the execlp() command. Right now, I have that formatted as a string (this is a separate file, im just trying to get this one piece working before bringing it back to my other code). But when I pass the string into execlp(), im getting an error, saying "no matchign function for call to execlp". Not sure where to go from here. I've tried converting it to c_str(), but no luck.

testing.cc-/Volumes/Macintosh HD/Dropbox Dropbox SimpleShell.cc testing.cc testing2.cc untitled untitled untitled 24 string to_tokenize; 25 int i=0; 26 27 string command_to_execute; 28 29 int main(int argc, char argv) string commands [5] { /ls", ''ls", "-1", "-a"); 31 32 for(int i-0; i

Explanation / Answer

int execlp(const char *file, const char *arg, ...);

The function requires const char* and you are passing char*.

Replace line 41, 42 with const char* cstr_cte = command_to_execute.c_str();

Two arguments are required for execlp

This will work.

#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;

int main(int argc, char **argv) {
   string commandToExecute = "ls";
   string option1 = "-a";

// for more than one option option1 = "-a -l"

   const char *command = commandToExecute.c_str();
   const char *option = option1.c_str();
  
   execlp(command, option, (char*) NULL);
   return 0;
}

You should be go to go. Tested.