A program to edit compile and run any program

Here is my program to edit a c program using vi editor, compile it and run it. I have used a child process which will call vi editor. The parent process will wait for the termination of the child. If the termination status is 0, parent will fork another child which will call gcc program to compile the file. Again if the compilation is successful and the return status is 0, the parent will fork another child to run the program.




#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main(int argc,char *argv[])
{
pid_t p1,p2;
int status;
char ofilename[20];
char pathname[20];
if (argv<2)
{
printf("give the file name\n");
exit(1);
}


p1 = fork();
if (!p1)/*child*/
{
printf("Editing the file.........\n");
if (execlp("vi","vi",argv[1],NULL)==-1)
{
perror("execl error. not able to start vi");
exit(1);
}
}
wait(&status);
if(WIFEXITED(status) &&WEXITSTATUS(m)==0)
{
p2 = fork();
if (p2==0)
{
printf("Compiling the file %s.........\n",argv[1]);
strncpy(ofilename,argv[1],strlen(argv[1])-2);
ofilename[strlen(argv[1])-1]='\0';
if (execlp("gcc","gcc",argv[1],"-o",ofilename,NULL)==-1)
{
perror("execl error. not able to start gcc");
exit(1);
}
}
else
{
wait(&status);
if(WIFEXITED(status) &&WEXITSTATUS(m)==0)
{
strcpy(pathname,"./");
strcat(pathname,ofilename);
if (execl(pathname,ofilename,NULL)==-1)
{
perror("execl error. not able to run");
printf("%s",ofilename);
exit(1);

}
}
}
}
}

Here the parent processes are waiting for the children to exit so that, you will compile the program after it has been edited and run after it has been compiled. Also if the compilation is not successful, WEXITSTATUS of the execed gcc will be non-zero and hence your program will not try to run this new program.

Note: You can add a feature such that after unsuccessful compilation, the program goes back to editor again



















Comments

Popular Posts