IO redirection in C shell Please implement input output red

I/O redirection in C shell

Please implement input / output redirection in the code posted below. Here are the specs:

Your shell will need to support file redirection. Use the same syntax as defined in the Bash shell: a single \'>\' implies that one needs to redirect the standard output of the program being started to the referenced file while a single \'<\' implies the same with standard input. The double \'>>\' implies that standard output will append to an existing file rather than create a new file (similar behavior is required for the \'<<\' operator and standard input). You do not need to implement support for the Bash pipeline operator \'|\'.

Before calling exec to begin execution, the child process may have to close stdin (file desriptor 0) and/or stdout (file descriptor 0), open the corresponding file and use the dup2 system call to make it the appropriate file descriptor. Don\'t forget to use the close system call to close the old file descriptor.

#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

int change_dir(char* args[])
{
   if (args[1] == NULL)
   {
       chdir(getenv(\"HOME\"));
       return 1;
   }
  
   else
   {
       if (chdir(args[1]) == -1)
       {
           cout << \"No such directory\" << args[1];
           return -1;
       }
   }
   return 0;
}

int helper_func(char* args[])
{
   cout << \"Builtin commands so far are; cd, and help.\ \";
  
   return 0;
}

int main()
{
   while(true)
   {
       cout << \"$ \";
       char cmd[128];
       cin.getline(cmd,128);
      
       vector <char*> args;
       char *line = strtok(cmd, \" \");
       char *tmp = line;
       while (tmp != NULL)
       {
           args.push_back(tmp);
           tmp = strtok(NULL, \" \");
       }
      
       char** argv = new char*[args.size() + 1];
       for (int i = 0; i < args.size(); i++)
           argv[i] = args[i];
      
       argv[args.size()] = NULL;
      
      
       if (strcmp(cmd, \"exit\") == 0)
       {
           {
               exit(0);
           }
       }
      
       //else if (strcmp(args[0], \"cd\") == 0) change_dir(args);
      
       if (strcmp(cmd, \"cd\") == 0)
       {
           change_dir(argv);
       }
      
       if (strcmp(cmd, \"help\") == 0)
       {
           helper_func(argv);
       }
      
       else
       {
           pid_t pid;
           pid_t wpid;
           int status;
           pid = fork();
           if (pid == 0)
           {
               if (execvp(args[0], argv) == -1)
               {
                   perror(\"cmd\");
               }
               exit(EXIT_FAILURE);
           }
           else if (pid < 0)
           {
               perror(\"cmd\");
           }
           {
               do {
                   wpid = waitpid(pid, &status, WUNTRACED);
               } while (!WIFEXITED(status) && !WIFSIGNALED(status));
           }
       }
   }
   signal(SIGINT, SIG_IGN);
   signal(SIGTERM, SIG_DFL);
  
   return 0;
}

Solution

#include <stdio.h>

int main ()
{
FILE *file_in, *file_out;
int cmd=0;

printf(\"Unix INPUT/OUTPUT Redirection \ \");
printf(\"----------------------------- \ \");
printf(\"(1) cmd \'>\' - redirect the standard output of program to new file \ \");
printf(\"(2) cmd \'<\' - redirect the standard input of program to new file \ \");
printf(\"(3) cmd \'>>\' - append the standard output of program to file \ \");
printf(\"(4) cmd \'<<\' - append the standard output of program to file \ \");
printf(\"Enter option : \");

scanf(\"%d\", &cmd);

switch(cmd)
{
    case 1: file_out = freopen(\"file_out.txt\", \"w\", stdout);
                printf(\"This text is redirected to file_out.txt\ \");
                    fclose(file_out);
                    break;
    case 2: file_in = freopen(\"file_in.txt\", \"w\", stdin);
                printf(\"This text is redirected to file_in.txt\ \");
                    fclose(file_in);
                    break;
    case 3: file_out = freopen(\"file_out.txt\", \"a\", stdout);
                printf(\"This text is appended to file_out.txt\ \");
                    fclose(file_out);
                    break;                   
    case 4: file_in = freopen(\"file_in.txt\", \"a\", stdin);
                printf(\"This text is appended to file_in.txt\ \");
                    fclose(file_in);
                    break;
}

return(0);
}

I/O redirection in C shell Please implement input / output redirection in the code posted below. Here are the specs: Your shell will need to support file redire
I/O redirection in C shell Please implement input / output redirection in the code posted below. Here are the specs: Your shell will need to support file redire
I/O redirection in C shell Please implement input / output redirection in the code posted below. Here are the specs: Your shell will need to support file redire

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site