Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-
- * main.c
- * Copyright (c) 1993-1999, Francisco Rosales <frosal@fi.upm.es>
- *
- * Minishell C source
- * Show how to use "obtain_order" input interface function
- *
- * THIS FILE IS TO BE MODIFIED
- */
- #include <stddef.h> /* NULL */
- #include <stdio.h> /* setbuf, printf */
- #include <unistd.h>
- #include <stdlib.h>
- #include <paths.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #define STDIN_FILENO 0
- #define STDOUT_FILENO 1
- #define STDERR_FILENO 2
- extern int obtain_order(); /* See parser.y for description */
- int old_in, old_out, old_err;
- // This function operates on redirection of STDIN, STDOUT, STDERR
- void redirection( char **fileC){
- int fp;
- //Check if we have request the STDIN
- if (fileC[0] != NULL) {
- //Open the file descriptor and open the file
- fp=open(fileC[0],O_RDONLY,0666);
- //Close the STDIN
- close(STDIN_FILENO);
- // I do a copy of STDIN on fp
- dup(fp);
- }
- //Check if we have request the STDUOT
- if (fileC[1] != NULL) {
- //Open the file descriptor and open and creat the file
- fp=open(fileC[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
- //Close the STDOUT
- close(STDOUT_FILENO);
- // I do a copy of STDOUT on fp
- dup(fp);
- }
- //Check if we have request the STDERR
- if (fileC[2] != NULL) {
- //Open the file descriptor and open and creat the file
- fp=open(fileC[2], O_CREAT | O_TRUNC | O_WRONLY, 0666);
- //Close the STDERR
- close(STDERR_FILENO);
- // I do a copy of STDERR on fp
- dup(fp);
- }
- }
- //With this function I copy the first position of standard INPUT, OUTPUT, ERROR
- void copyFileDescriptor()
- {
- old_in = dup(STDIN_FILENO);
- old_out = dup(STDOUT_FILENO);
- old_err = dup(STDERR_FILENO);
- }
- //Reposition the three standard in old position
- void setupFiledescriptor()
- {
- close(STDIN_FILENO);
- dup(old_in);
- close(STDOUT_FILENO);
- dup(old_out);
- close(STDERR_FILENO);
- dup(old_err);
- }
- void welcomeScreen()
- {
- printf("\n-------------------------------------------------\n");
- printf("\tWelcome to MINI-shell \n");
- printf("\tAuthor :Giovanni Formisano Francesco D'Alia\n");
- printf("-------------------------------------------------\n");
- printf("\n\n");
- }
- void shellPrompt(char *currentDirectory)
- {
- printf("%s :> ",getcwd(currentDirectory, 1024));
- }
- int main(void)
- {
- char* currentDirectory;
- char ***argvv;
- int argvc;
- char **argv;
- char *filev[3];
- int bg;
- int err;
- int ret,pid,status,pid2,pid3;
- int fp[2],fp2[2];
- setbuf(stdout, NULL); /* Unbuffered */
- setbuf(stdin, NULL);
- copyFileDescriptor();
- welcomeScreen();
- currentDirectory = (char*) calloc(1024, sizeof(char));
- while (1) {
- shellPrompt(currentDirectory);
- //fprintf(stderr, "%s", "msh> "); /* Prompt */
- ret = obtain_order(&argvv, filev, &bg);
- if (ret == 0) break; /* EOF */
- if (ret == -1) continue; /* Syntax error */
- argvc = ret - 1; /* Line */
- if (argvc == 0) continue; /* Empty line */
- //This variable args count how many command the user put on the shell
- int args =0;
- //This FOR count count how many command the user put on the shell
- for (argvc = 0; (argv = argvv[argvc]); argvc++)
- args++;
- for (argvc = 0; (argv = argvv[argvc]); argvc++) {
- // First of all I check how many command I have insert, in this case with only one command
- //I creat the fork to execution of that command while the father wait the child that it's finish
- if (args == 1) {
- redirection(filev);
- //Create a fork
- pid = fork();
- //Check the error
- if ( pid < 0 ){
- printf("error create fork!!!!");
- exit(1);
- }
- //This is child
- if ( pid == 0 ){
- //This command exe the command that find inside /bin/bash
- err = execvp((char *)argvv[argvc][0],argvv[argvc]);
- //Check the error of exevp
- if (err < 0)
- perror("errore execvp");
- exit(0);
- }else {
- //This is the father and than with check (!bg) the programm control if there is or no
- // the command in background
- if(!bg) {
- //Wait the termination of child
- wait(&status);
- }
- else {
- printf("[%d]\n",pid);
- }
- }
- }
- //Recall the function setupFileDescriptor to rearrange the STANDARD INPUT OUTPU ERR
- setupFiledescriptor();
- // First of all I check how many command I have insert, in this case with only two command
- //Than I create a pipe to connect the two process. To do this I must create two fork, because the idea is
- //one fork() for each process
- if (args == 2) {
- redirection(filev);
- //Create the first fork()
- pid = fork();
- //Check the error
- if ( pid < 0 ){
- printf("error create fork!!!!");
- exit(1);
- }
- if ( pid == 0 ){
- //Create a pipe for the connection and use two file descriptor
- pipe(fp);
- //I creat the second fork()
- pid2 = fork();
- if ( pid2 == 0 )
- {
- //Here close the file descriptor STDOUTPUT
- close(STDOUT_FILENO);
- //Redirection the STDOUT on OUTPUT of pipe
- dup2(fp[1],STDOUT_FILENO);
- //Close the INPUT of pipe because must close the file descriptor that pipe don't use
- close(fp[0]);
- //This command exe the command that find inside /bin/bash
- err = execvp((char *)argvv[argvc][0],argvv[argvc]);
- //Check the error of exevp
- if (err < 0)
- perror("errore execvp");
- exit(0);
- }
- //Wait the termination of child
- wait(&status);
- //Close the INPUT
- close(STDIN_FILENO);
- //Redirection the STDINPUT on INPUT of pipe
- dup2(fp[0],STDIN_FILENO);
- //Close the OUTPUT of pipe because must close the file descriptor that pipe don't use
- close(fp[1]);
- //This command exe the second commad that user insert
- err = execvp((char *)argvv[argvc+1][0],argvv[argvc+1]);
- //Check the error of exevp
- if (err < 0)
- perror("errore execvp");
- exit(0);
- }else{
- //This is the father and than with check (!bg) the programm control if there is or no
- // the command in background
- if(!bg) {
- //Wait the termination of child
- wait(&status);
- argvc++;
- }
- else {
- printf("[%d]\n",pid);
- }
- }
- }
- //Recall the function setupFileDescriptor to rearrange the STANDARD INPUT OUTPU ERR
- setupFiledescriptor();
- // First of all I check how many command I have insert, in this case with only three command
- //Than I create two pipe to connect three process. To do this I must create three fork, because the idea is
- //one fork() for each process.
- //The idea is the fork many inside exec the first command, the second fork exec the second command,
- // and the firs fork exec the last command.
- if (args == 3) {
- redirection(filev);
- //Create the first fork()
- pid = fork();
- //Check the error
- if ( pid < 0 ){
- printf("error create fork!!!!");
- exit(1);
- }
- if ( pid == 0 ){
- //Create a pipe for the connection and use two file descriptor
- pipe(fp);
- //Create the second fork()
- pid2 = fork();
- //Chech error of second fork()
- if ( pid2 < 0 ){
- printf("error create fork!!!!");
- exit(1);
- }
- if ( pid2 == 0 )
- {
- //I create the second pipe
- pipe(fp2);
- //I creat the third fork()
- pid3 = fork();
- //Check the error of third fork()
- if ( pid3 < 0 ){
- printf("error create fork!!!!");
- exit(1);
- }
- if ( pid3 == 0){
- //Close the INPUT
- close(fp2[0]);
- //Redirection the STDOUT on OUTPUT of pipe
- dup2(fp2[1],STDOUT_FILENO);
- //Close the INPUT of pipe because must close the file descriptor that pipe don't use
- close(fp2[1]);
- //This command exe the first command
- err = execvp((char *)argvv[argvc][0],argvv[argvc]);
- //Check error of execvp
- if (err < 0)
- perror("errore execvp");
- exit(0);
- }else {
- //Wait the termination of child
- wait(&status);
- //Close the INPUT
- close (fp2[1]);
- //With this two function dup there is the connection of second command with
- //third command exchanging the INPUT of one with OUTPUT of the other one
- dup2(fp2[0],STDIN_FILENO);
- dup2(fp[1],STDOUT_FILENO);
- ////Close the INPUT of pipe one because must close the file descriptor that pipe don't use
- close (fp[0]);
- //Close the OUTPUT of pipe two because must close the file descriptor that pipe don't use
- close(fp2[1]);
- //This command exe the second command
- err = execvp((char *)argvv[argvc+1][0],argvv[argvc+1]);
- //Check error of execvp
- if (err < 0)
- perror("errore execvp");
- exit(0);
- }
- }
- //Wait the termination of child
- wait(&status);
- //Close the STDIN
- close(STDIN_FILENO);
- //Redirection the STDINPUT on INPUT of pipe
- dup2(fp[0],STDIN_FILENO);
- //Close the OUTPUT of pipe two because must close the file descriptor
- close(fp[1]);
- //This command exe the third command
- err = execvp((char *)argvv[argvc+2][0],argvv[argvc+2]);
- if (err < 0)
- perror("errore execvp");
- exit(0);
- }else{
- //This is the father and than with check (!bg) the programm control if there is or no
- // the command in background
- if(!bg) {
- //Wait the termination of child
- wait(&status);
- argvc = argvc + 2;
- }
- else {
- printf("[%d]\n",pid);
- }
- }
- }
- //Recall the function setupFileDescriptor to rearrange the STANDARD INPUT OUTPU ERR
- setupFiledescriptor();
- }
- setupFiledescriptor();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement