Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/time.h>
- #include <time.h>
- #include <sys/resource.h>
- #define READ_BUFFER_SIZE 1024
- #define SPLIT_BUFFER_SIZE 100
- #define extras " \t\r\a\n"
- char *command_esp[100];
- char **basic_shell_splitcommand(char *command)
- {
- int buff_size = SPLIT_BUFFER_SIZE;
- int pos = 0;
- char **words = malloc(buff_size * sizeof(char*));
- char *word;
- if(!words)
- {
- fprintf(stderr, "basic_shell: Memory allocation error\n");
- exit(EXIT_FAILURE);
- }
- word = strtok(command,extras);
- while(word != NULL)
- {
- words[pos] = word;
- pos++;
- if(pos >=buff_size)
- {
- buff_size+= SPLIT_BUFFER_SIZE;
- words = realloc(words, buff_size* sizeof(char*));
- if(!words)
- {
- fprintf(stderr, "basic_shell: Memory allocation error!\n");
- exit(EXIT_FAILURE);
- }
- }
- word = strtok(NULL, extras);
- }
- words[pos] = NULL;
- return words;
- }
- char *basic_shell_readcommand()
- {
- int buff_size = READ_BUFFER_SIZE;
- char *allocate = malloc(buff_size* sizeof(char));
- int pos = 0;
- if(!allocate)
- {
- fprintf(stderr, "basic_shell: Memory allocation error!\n");
- exit(EXIT_FAILURE);
- }
- int n;
- while(1)
- {
- n = getchar();
- if(n == EOF || n == '\n')
- {
- allocate[pos] = '\0';
- return allocate;
- }
- allocate[pos] = n;
- pos++;
- if(pos >=buff_size)
- {
- buff_size+= READ_BUFFER_SIZE;
- allocate = realloc(allocate, buff_size* sizeof(char));
- if(!allocate)
- {
- fprintf(stderr, "basic_shell: Memory allocation error!\n");
- exit(EXIT_FAILURE);
- }
- }
- }
- }
- int basic_shell_execute(char **words)
- {
- if(strcmp(words[0], "cd") == 0) // executes cd command
- {
- chdir(words[1]);
- return 1;
- }
- else if(strcmp(words[0], "exit") == 0) // executes exit command
- {
- return 0;
- }
- else if(strcmp(words[0], "help") == 0) // executes help command
- {
- printf("basic_shell : v1.0.\nBasic shell commands will work in this shell.\n And also shell builtins like cd, exit, help and echo is valid here.\nTo know more about this commands type man command_name and press enter.\n\n\n");
- return 1;
- }
- struct rusage R;
- struct timeval UserTime, SystemTime;
- struct timeval StartTime, EndTime;
- gettimeofday(&StartTime, NULL);
- int p= 0;
- pid_t pid;
- pid = fork();
- if(pid == 0)
- {
- printf("hello");
- if(execvp(words[0], words)==-1)
- printf("Invalid command!\n");
- exit(EXIT_FAILURE);
- }
- if(pid <0)
- printf("Unexpected error occured!\n");
- if(pid>0)
- {
- wait(&p);
- getrusage(RUSAGE_CHILDREN, &R);
- UserTime = R.ru_utime;
- SystemTime = R.ru_stime;
- gettimeofday(&EndTime, NULL);
- printf("\n\tCPU time consumed by User: %ld ms\n",UserTime.tv_sec*1000+UserTime.tv_usec/1000);
- printf("\tCPU time consumed by System: %ld ms\n",SystemTime.tv_sec*1000+SystemTime.tv_usec/1000);
- printf("\tElapsed wall clock time: %ld ms\n",(EndTime.tv_sec-StartTime.tv_sec)*1000 + (EndTime.tv_usec-StartTime.tv_usec)/1000 );
- printf("\tThe number of times the process was preempted involuntarily: %ld\n",R.ru_nivcsw);
- printf("\tThe number of times the process was preempted voluntarily: %ld\n",R.ru_nvcsw);
- printf("\tNumber of page faults(Hard): %ld\n",R.ru_majflt);
- printf("\tNumber of page faults(Soft): %ld\n",R.ru_minflt);
- }
- return 1;
- }
- void basic_shell_start(int command_given)
- {
- int flag;
- char *command;
- char **words;
- if(command_given)
- {
- words = command_esp;
- flag = basic_shell_execute(words);
- int i = 0;
- while(words[i]!=NULL)
- {
- i++;
- }
- basic_shell_start(0);
- }
- else
- {
- do
- {
- printf("-> ");
- command = basic_shell_readcommand();
- words = basic_shell_splitcommand(command);
- flag = basic_shell_execute(words);
- int i = 0;
- while(words[i]!=NULL)
- {
- i++;
- }
- }while(flag);
- }
- }
- int main(int argc, char *argv[])
- {
- if(argc == 1)
- basic_shell_start(0);
- else
- {
- int i = 0, j = 0;
- for(i = 1; i<argc;i++)
- command_esp[j++] = argv[i];
- basic_shell_start(1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement