This is continuation of the code Part 2 CC program 2 Handlin

This is continuation of the code.

Part 2. C/C++ program 2 Handling File Redirection input, output, output append) 4 Design and implement CC++ program (myfie.c) in loop: (1) to prompt for a command, (2) to read a command 5 from keyboard as you type), (3) to echo the command, (4) to fork a child process to process command while the 6 parent process is waiting for child process to complete this part, and then (5) the parent is back to the command 7 prompt for next command. 9 The commands are: read, write, and append, and exit. The following tasks (that is, test cases) are to be 10 implemented. 11 12 Taskl. When the command is exit\", then the program terminates with exito0) 13 14 Task2. The \"read\" command will do a file-redirection for a file to be read and to be written to a temporary file. For 15 example, read hellac\" will read a file \"hellac\" and write it to a temporary file (\"temp.txt\") 16 17 Task3. The \"write\" command will do a file-redirection for a file (\"temp.txt\") to be read and written to a file. For 18 example, \"write outi.c\" will read the save temp file \"temp.txt\" and write it to the file (\"outl.c\"). 19 20 Task4. The \"append\" command has a file-redirection for a file (temp.txt) to be appended to a file. For example, 21 \"append out2.c\" will read the save temp file \"temp.txt\" and write it to the file (out2.c\"). 22 23 Task5. You may have two commands with a pipe. For example, \"read

Solution

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include<string.h>
#include <stdlib.h>
#include<fcntl.h>
#include <sys/utsname.h>
#include<signal.h>
typedef struct process //struct for storing jobs names and pids
{
   int pid;
   char name[100];
}process;
process jobs[100];
int ultimate=0;
pid_t pid;
int cpid;
int k=0;

char input[1024],name[1024];
char *argv1[100];
int flag=1;
int ctr=0,fgh=0;
char bgprocesses[100][100];
void change(char *input, char **argv) //parsing given arguments to a new set of arguments to access efficiently
{
   int i;
   ctr=0;
   while (*input!=\'\\0\') //do till entire input is not null
   {   
       while(*input==\' \'||*input==\'\ \'||*input==\'\\t\' || *input==\'\\0\')//making spaces null
       {
           *input=\'\\0\';
           input++;
       }
       *argv++=input; //storing arguments in a new
       ctr++;
       while(*input!=\'\\0\'&& *input!=\' \'&& *input!= \'\\t\' && *input!= \'\ \') //discarding any empty
           input++;   
   }
   char s[1];
   s[0]=\'&\';
   s[1]=\'\\0\';
   if(strcmp(*(--argv),\"&\")==0)//identifying background processes
   {
       *argv=\'\\0\';
       flag=0;
   }
   else
   {
       ultimate=1;
       *(++argv)=\'\\0\';
       flag=1;
   }
}

void execute(char **argv)
{
   int status;
   pid=fork();
   cpid=pid;
   int i,t,l,s;
   if(flag==1) //executing foreground
   {
       if(pid<0)
       {   
           write(1,\"ERROR: forking child process failed\ \",36);
           exit(1);
       }
       else if(pid== 0)
       {
           if (execvp(*argv, argv) < 0) //executing command
           {   
               write(1,\"ERROR: exec failed\ \",21);
               exit(1);
           }
       }
       else
       {   
           waitpid(cpid,&status,WUNTRACED);//parent waits for child with cpid and also if cpid has stopped
           if(WIFSTOPPED(status))//returns positive if child has stopped
           {
               strcpy(jobs[k].name,argv[0]);
               jobs[k].pid=pid;
               k++;
           }
       }
   }
   else //executing background processes
   {
       if(fgh==1)
       {
           k=0;
           fgh=0;}
       strcpy(jobs[k].name,argv[0]);
       jobs[k].pid=pid;
       k++;

       if(pid<0)
       {   
           write(1,\"ERROR: forking child process failed\ \",36);
           exit(1);
       }
       else if(pid== 0)
       {

           if (execvp(*argv, argv) < 0)
           {   
               write(1,\"ERROR: exec failed\ \",21);
               exit(1);
           }
       }

   }
}
int main()
{
   if(signal(SIGUSR1,handler)==SIG_ERR)
       write(1,\"\ can\'t catch SIGUSR1\ \",25);
   if (signal(SIGKILL,handler)==SIG_ERR)

   {//   printf(\"\ can\'t catch SIGKILL\ \");
   }
   if (signal(SIGSTOP,handler)==SIG_ERR)
   {       //   printf(\"\ can\'t catch SIGSTOP\ \");
   }
   if (signal(SIGINT,handler)==SIG_ERR)
       write(1,\"\ can\'t catch SIGSTOP\ \",25);
   if(signal(SIGTSTP,handler)==SIG_ERR)
       write(1,\"cant catch SIGSTP\ \",25);
   char te[100];
   uid_t uid;
   char *username;
   username=getlogin(); //stores username

   struct utsname myuname; //stores attributes related to system
   char system[100];
   if(uname(&myuname) == -1)
       write(1,\"uname call failed!\ \",20);
   else
       strcpy(system,myuname.sysname);//storing sustem name
   strcat(username,\"@\");
   strcat(username,system); //command prompt
   getcwd(te,100);
   char *argv[100];
   int status;
   int t,i,l,s2;
   int oripid=getpid(); //pid of shell program
   char tf[100];
   char path[100];
   char printex[100];
   tf[0]=\'~\';
   tf[1]=\'\\0\';

   while(1)
   {   
       t=waitpid(-1,&status,WNOHANG);//waits for child to terminate but returns immediately if no child is terminated...if child is terminated its pid is returned
       char d[100];
       while(t>0)
       {
           /*if(WIFSIGNALED(status))
           {
           printf(\"%s exited with signal %d\ \" ,d,WTERMSIG(status))
           }
           else if(WIFSTOPPED(status))
           {
           printf(\"exited normally\ \");
           }*/

           for(i=0;i<k;i++) //removing from jobs queue when child terminated
               if(jobs[i].pid==t)
               {
                   strcpy(d,jobs[i].name);
                   break;
               }
           k--;
           for(l=i;l<k;l++)
           {
               strcpy(jobs[i].name,jobs[i+1].name);
               jobs[i].pid=jobs[i+1].pid;
           }
           char dest[10];
           sprintf(dest,\"%d\",t);
           //   printf(\"%s with pid=%d exited normally\ \",d,t);
           if(WIFSIGNALED(status))
           {
               printf(\"%s exited with signal %d\ \" ,d,WTERMSIG(status));
           }
           else if(WIFSTOPPED(status))
           {
               write(1,d,strlen(d));
               write(1,\" with pid=\",10);
               write(1,dest,strlen(dest));
               write(1,\" exited normally\ \",17);
           //   t=waitpid(-1,&status,WNOHANG);
           }
               t=waitpid(-1,&status,WNOHANG);

       }
       //printf(\"<%s:%s>\",username,tf); //printing command prompt
       write(1,\"<\",1);
       write(1,username,strlen(username));
       write(1,\":\",1);
       write(1,tf,strlen(tf));
       write(1,\">\",1);

       gets(input);
       if(strlen(input)>0)
       {
           change(input, argv);   
           if (strcmp(argv[0], \"quit\") == 0) //quitting the program
               exit(0);
           else if(strcmp(argv[0],\"cd\")==0)
           {
               if(argv[1]!=NULL&&strcmp(argv[1],\"..\")==0)//going 1 level back
               {
                   char fgh[100];
                   getcwd(fgh,100);
                   chdir(\"..\");
                   getcwd(tf,100);
               }
               else if(argv[1]==NULL) //if only cd then change to user
               {
                   chdir(\"/home/aayush\");
                   getcwd(tf,100);
               }
               else
               {
                   if(access(argv[1],(int) F_OK)==0) //checking for existence of directory
                   {  
                       getcwd(te,100);
                       strcat(te,\"/\");
                       strcat(te,argv[1]);
                       int h=chdir(te);
                       if(h==0)
                       {
                           getcwd(tf,100);
                       }
                       else
                           write(1,\"its a file and not a directory\ \",35);
                   }
                   else
                       write(1,\"no such dir\ \",13);
               }

           }
          
           else if(strcmp(argv[0],\"pinfo\")==0&&argv[1]!=NULL) //pinfo of particular id
           {
               int ff=0;
               int o=atoi(argv[1]);
               for(i=0;i<k;i++)
               {  
                   if(jobs[i].pid==o)
                       ff=1;
               }

               //               if(o>=k+1)
               if(ff==0)
                   write(1,\"no such pid exists\ \",19);
               else
               {

                   //   int temp=jobs[o-1].pid;
                   char g[100];
                   strcpy(g,\"/proc/\");
                   char buffer[15];
                   char *st=NULL;
                   size_t n=0;
                   //       sprintf(buffer,\"%d\",temp);

                   //strcat(g,buffer);
                   strcat(g,argv[1]);
                   for(i=0;i<100;i++)
                       printex[i]=\'\\0\';
                   strcpy(path,g);
                   strcat(path,\"/exe\");
                   readlink(path,printex,sizeof(printex));
                   int len=strlen(printex);
                   printex[len]=\'\\0\';
                   strcat(g,\"/status\");
                   FILE *f=fopen(g,\"r\");
                   getline(&st,&n,f);
                   write(1,st,strlen(st));
                   getline(&st,&n,f);
                   write(1,st,strlen(st));
                   getline(&st,&n,f);
                   getline(&st,&n,f);
                   write(1,st,strlen(st));
                   while(strncmp(\"VmSize\",st,6)!=0)
                       getline(&st,&n,f);
                   write(1,st,strlen(st));
                   fclose(f);
                   write(1,\"Executable path--\",17);
                   write(1,printex,strlen(printex));
                   write(1,\"\ \",1);
               }
           }
           else
               execute(argv);   
       }
   }
   return 0;
}

This is continuation of the code. Part 2. C/C++ program 2 Handling File Redirection input, output, output append) 4 Design and implement CC++ program (myfie.c)
This is continuation of the code. Part 2. C/C++ program 2 Handling File Redirection input, output, output append) 4 Design and implement CC++ program (myfie.c)
This is continuation of the code. Part 2. C/C++ program 2 Handling File Redirection input, output, output append) 4 Design and implement CC++ program (myfie.c)
This is continuation of the code. Part 2. C/C++ program 2 Handling File Redirection input, output, output append) 4 Design and implement CC++ program (myfie.c)
This is continuation of the code. Part 2. C/C++ program 2 Handling File Redirection input, output, output append) 4 Design and implement CC++ program (myfie.c)
This is continuation of the code. Part 2. C/C++ program 2 Handling File Redirection input, output, output append) 4 Design and implement CC++ program (myfie.c)

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site