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

I\'m having trouble wrapping my head around this one. Here\'s what I\'m supposed

ID: 3566510 • Letter: I

Question

I'm having trouble wrapping my head around this one. Here's what I'm supposed to do -

I've created a shell in C++, as I'll show you. Now I have to add pipes and redirects to it. I'm having trouble figuring it out. Here's what I've got so far:

#include <iostream>
#include <string>
#include <cstddef>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <general.h>

using namespace std;

const int MAX_ARGS=64;
bool isBlank( string cmd );
void parseArgs( string cmd, char * argv[ ] );

int main()
{
   string line;

   pid_t pid;

   int open(char *filename, int how);
   int close(int fd);
   int creat(char *filename, mode_t mode);
   int pipe(int array[2]);
   int dup2(int oldfd, int newfd);

   char * argv[MAX_ARGS];

   while(true){
       cout<<"$ ";
       getline( cin,line );

       if( isBlank( line ) )
           continue;

       if( line == "exit" )
           exit(EXIT_SUCCESS);
  
   pid = fork ();

   if (pid==0)
   {
       parseArgs( line,argv );
       execvp(argv[0],argv );

       cout << "ERROR. command "<< argv[0] << "NOT FOUND." <<endl;
       exit (EXIT_FAILURE);
   }
   else
   {
       wait (NULL);
  
   }
}
return 0;
}  

bool isblank( string cmd )
{
   size_t pos=cmd.find_first_not_of(" ");
   return pos==string::npos;
}

bool contains ( string cmd, char c)
{
   size_t pos = cmd.find(c);
   return pos !=string::npos;
}

void split(string str, char c, string &str1, string &str2)
{
   size_t pos=str.find(c);
   str1=str.substr(0, pos);
   pos=str.find_first_not_of(" ",pos+1);
   str2=str.substr(pos);
}

void parseArgs( string cmd, char*argv[ ] )
{
   int argc=0;
   size_t start=0;
   size_t end=0;
   string arg;

   while (end != string::npos && argc<MAX_ARGS-1)
   {
       start=cmd.find_first_not_of(" ", end);
       if (start==string::npos)
           break;
       end=cmd.find_first_of( " ", start );
       arg=cmd.substr( start,end-start);
       argv[argc]=new char[arg.length( ) +1];
       strcpy(argv[argc],arg.c_str());
       argc++;
   }
   argv[argc]=NULL;
}

Explanation / Answer

#include <iostream>
#include <string>
#include <cstddef>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <cctype>

using namespace std;

const int MAX_ARGS=64;
bool isblank( string cmd );
void parseArgs( string cmd, char * argv[ ] );

int main()
{
    string line;

   pid_t pid;

   int open(char *filename, int how);
    int close(int fd);
    int creat(char *filename, mode_t mode);
    int pipe(int array[2]);
    int dup2(int oldfd, int newfd);

   char * argv[MAX_ARGS];

   while(true){
        cout<<"$ ";
        getline( cin,line );

       if( isblank( line ) )
            continue;

       if( line == "exit" )
            exit(EXIT_SUCCESS);
  
    pid = fork ();

   if (pid==0)
    {
        parseArgs( line,argv );
        execvp(argv[0],argv );

       cout << "ERROR. command "<< argv[0] << "NOT FOUND." <<endl;
        exit (EXIT_FAILURE);
    }
    else
    {
        wait (NULL);
  
    }
}
return 0;
}

bool isblank( string cmd )
{
    size_t pos=cmd.find_first_not_of(" ");
    return pos==string::npos;
}

bool contains ( string cmd, char c)
{
    size_t pos = cmd.find(c);
    return pos !=string::npos;
}

void split(string str, char c, string &str1, string &str2)
{
    size_t pos=str.find(c);
    str1=str.substr(0, pos);
    pos=str.find_first_not_of(" ",pos+1);
    str2=str.substr(pos);
}

void parseArgs( string cmd, char*argv[ ] )
{
    int argc=0;
    size_t start=0;
    size_t end=0;
    string arg;

   while (end != string::npos && argc<MAX_ARGS-1)
    {
        start=cmd.find_first_not_of(" ", end);
        if (start==string::npos)
            break;
        end=cmd.find_first_of( " ", start );
        arg=cmd.substr( start,end-start);
        argv[argc]=new char[arg.length( ) +1];
        strcpy(argv[argc],arg.c_str());
        argc++;
    }
    argv[argc]=NULL;
}

Hi Try this

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote