Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <assert.h>
- #include <signal.h> //for kill
- #include <fcntl.h>
- int bg_flag=0;
- int py_flag=0;
- int arg_count=0;
- void execute(char **argv)
- {
- printf("%d \n",bg_flag);
- int status;
- int pid = fork();
- if (pid <0) //fork error has happened
- {
- perror("Can't fork a child process\n");
- exit(EXIT_FAILURE);
- }
- if (pid==0) //It's the child process and can run his task
- {
- if (py_flag==1)
- {
- int i;
- char *new_argv[arg_count];
- new_argv[0]="/usr/bin/python";
- for (i=1; i<=arg_count;i++)
- {
- new_argv[i]=argv[i-1];http://pastebin.com/SMJkgSJH bg job working, python multi-opt working
- printf("%s new argv[%d] is \n",new_argv[i],i);
- }
- new_argv[arg_count+1]=0;
- //new_argv[1]=argv[0];
- //new_argv[2]=0;
- execvp(new_argv[0],new_argv);
- }
- printf("%d \n", bg_flag);
- if (bg_flag==1)
- {
- printf("bg \n");
- execvp(argv[0],argv);
- }
- else
- {
- execvp(argv[0],argv);
- perror("error");
- }
- }
- else //pid>0 it's the parent and should wait for the child
- {
- if (bg_flag==1)
- {
- }
- else
- wait(NULL);
- }
- }
- int main (int argc, char **argv)
- {
- FILE *bfd=NULL; //batch file descriptor
- char cwd[100]; //current working directory
- int batch_mode=0;
- char input[512];
- char *args[512];
- char **next = args;
- char *temp;
- getcwd(cwd,sizeof(cwd));
- if (argv[1]!=0)
- {
- batch_mode=1;
- if((bfd = fopen(argv[1], "r")) == NULL)
- {
- printf("Error Opening File.\n");
- exit(1);
- }
- }
- chdir(cwd);
- if (batch_mode)
- {
- while( fgets(input, sizeof(input), bfd) != NULL )
- {
- arg_count=0;
- write(STDOUT_FILENO,input, strlen(input));
- input[strlen(input) - 1] = 0;
- temp = strtok(input, " ");
- while(temp != NULL)
- {
- *next++ = temp;
- temp = strtok(NULL, " ");
- arg_count++;
- }
- *next = NULL;
- next=args;
- execute(args);
- }
- fclose(bfd);
- }
- else //in shell mode
- while (1)
- {
- {
- arg_count=0;
- printf("mysh> ");
- fgets(input,512,stdin);
- input[strlen(input) - 1] = 0;
- temp = strtok(input, " ");
- while(temp != NULL)
- {
- //printf("%s\n", temp);
- *next++ = temp;
- temp = strtok(NULL, " ");
- arg_count++;
- }
- *next = NULL;
- next=args;
- if (strstr(args[0],".py") !=NULL) //is our first argument a python program?
- py_flag=1; //if yes set the py_flag
- if (strcmp(args[0], "exit")==0) //is our first argument "exit" command?
- exit(0);
- if (strcmp(args[0], "cd")==0) //is our first argument "cd" command?
- {
- if ((!args[1]) || (strcmp(args[1],"~")==0)) //is the second argument for "cd" command NULL or ~?
- {
- chdir(getenv("HOME")); //change the current directory to home
- getcwd(cwd,sizeof(cwd));
- }
- else
- {
- chdir(args[1]); //otherwise change the current directory to the given directory
- getcwd(cwd,sizeof(cwd));
- }
- }
- if (strcmp(args[0],"wait")==0)
- wait(NULL);
- if (strstr(args[arg_count-1],"&")!=NULL)
- {
- printf("%s", args[arg_count-1]);
- bg_flag=1;
- args[arg_count-1]=NULL;
- execute(args);
- printf("bg_flag is %d \n",bg_flag);
- }
- else
- {
- execute(args);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement