Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @file shell.c */
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include "log.h"
- #include <sys/time.h>
- #include "signal.h"
- log_t *Log;
- extern char ** environ;
- char * str;
- int am;
- pid_t id2;
- void ignoreExit(int signal)
- {
- (void)signal;
- return;
- }
- void cleanZombie(int signal)
- {
- (void)signal;
- int status;
- while (waitpid((pid_t)(-1), &status, WNOHANG) > 0) {}
- }
- void printHistory()
- {
- int i = 0;
- while(i < Log -> counter)
- {
- printf("%s\n", (Log -> data)[i]);
- i++;
- }
- }
- void forkStuff(int ti, char * st)
- {
- int arg = 0;
- int counter = 0;
- while(1)
- {
- if(st[counter] == ' ')
- arg++;
- else if(st[counter] == '\0')
- {
- arg++;
- break;
- }
- counter++;
- }
- char ** argv = (char **) malloc((arg + 1)*sizeof(char*));
- char * delim = " ";
- char * first = strtok(st, delim);
- argv[0] = strdup(first);
- char * ptr;
- int g = 1;
- while ((ptr = strtok(NULL, delim)) != NULL) {
- argv[g] = strdup(ptr);
- g++;
- }
- argv[arg] = NULL;
- struct timeval start;
- struct timeval end;
- if(ti == 1)
- gettimeofday(&start, 0);
- id2 = fork();
- if(id2 == -1)
- exit(EXIT_FAILURE);
- else if(id2 == 0)
- {
- execvp(argv[0], argv);
- printf("%s: Command not found\n", argv[0]);
- int k = 0;
- while(k <= arg)
- {
- free(argv[k]);
- k++;
- }
- free(argv);
- log_destroy(Log);
- free(st);
- exit(0);
- }
- else
- {
- if(am == 0)
- {
- printf("Command executed by pid = %d\n", id2);
- int status;
- waitpid(id2, &status, 0);
- }
- else if(am == 1)
- {
- printf("Command executed by pid = %d\n", id2);
- signal(SIGCHLD, cleanZombie);
- }
- }
- if(ti == 1)
- {
- gettimeofday(&end, 0);
- long el = (end.tv_sec-start.tv_sec)*1000000 + end.tv_usec-start.tv_usec;
- printf("Execution took %ld microseconds.\n", el) ;
- }
- int k = 0;
- while(k <= arg)
- {
- free(argv[k]);
- k++;
- }
- free(argv);
- }
- void changeDirectory(char * s)
- {
- char * dir = (char *)malloc((strlen(s) - 1)*sizeof(char)) ;
- strcpy(dir, s + 3);
- int sd = chdir(dir);
- if(sd == -1)
- printf("%s: No such file or directory\n", dir);
- free(dir);
- }
- /**
- * * Starting point for shell.
- * */
- int main(int argc, char ** argv) {
- /**
- * * Analyze command line arguments
- * */
- Log = malloc(sizeof(log_t));
- log_init(Log);
- int tz = 0;
- if(argc > 1)
- {
- if(strcmp(argv[1], "-h") == 0)
- printf("Shell by vinhha2\n");
- else if(strcmp(argv[1], "=") == 0)
- {
- char ** ptr = environ;
- while(*ptr)
- {
- printf("%s\n", *ptr);
- ptr++;
- }
- }
- else if(strcmp(argv[1], "-f") == 0)
- {
- FILE* f = fopen(argv[2], "r");
- if(f != NULL)
- {
- char* str1 = NULL;
- size_t size1 = 0;
- while(!feof(f)){
- getline(&str1, &size1, f);
- system(str1);
- }
- free(str1);
- }
- fclose(f);
- }
- else if(strcmp(argv[1], "-t") == 0)
- tz = 1;
- }
- signal(SIGINT, ignoreExit);
- while(1)
- {
- /**
- * * Print a command prompt
- * */
- am = 0;
- int id = getpid();
- char wd[1024];
- getcwd(wd, sizeof(wd));
- printf("(pid=%d)%s$ ", id, wd);
- /**
- * * Read the commands
- * */
- str = NULL;
- size_t size = 0;
- getline(&str, &size, stdin);
- if(strcmp(str, "\n") == 0)
- {
- free(str);
- continue;
- }
- signal(SIGINT, ignoreExit);
- char * lol = strndup(str, strlen(str) - 1);
- free(str);
- str = strdup(lol);
- free(lol);
- if(strstr(str, "!") == NULL)
- log_push(Log, str);
- /**
- * * Print the PID of the process executing the command
- * */
- /**
- * * Decide which actions to take based on the command (exit, run program, etc.)
- * */
- if(strstr(str, "cd ") != NULL && str[0] == 'c')
- {
- changeDirectory(str);
- }
- else if(strcmp(str, "exit") == 0)
- {
- log_destroy(Log);
- free(str);
- exit(0);
- }
- else if(strcmp(str, "!#") == 0)
- {
- printHistory();
- }
- else if(str[0] == '!')
- {
- if(strlen(str) == 1)
- {
- free(str);
- printf("No Match\n");
- continue;
- }
- char * abc = str + 1;
- char * s = strdup(abc);
- char * argg = log_search(Log, s);
- if(argg == NULL)
- {
- printf("No Match\n");
- free(s);
- free(str);
- continue;
- }
- else
- {
- printf("%s matches %s\n", s, argg);
- free(str);
- str = strdup(argg);
- free(argg);
- free(s);
- if(strstr(str, "cd ") != NULL)
- {
- changeDirectory(str);
- }
- else if(strstr(str, "&") != NULL)
- {
- char * ok;
- if(strstr(str, " &") != NULL)
- ok = strndup(str, strlen(str) - 2);
- else
- ok = strndup(str, strlen(str) - 1);
- free(str);
- str = strdup(ok);
- free(ok);
- am = 1;
- forkStuff(tz, str);
- }
- else
- {
- forkStuff(tz, str);
- }
- }
- }
- else if(strstr(str, "&") != NULL)
- {
- char * ok;
- int k = 1;
- int abcd = 0;
- int le = strlen(str);
- while(abcd < le - 1)
- {
- if(str[abcd] == ' ')
- k++;
- else
- k = 1;
- lol++;
- abcd++;
- }
- ok = strndup(str, strlen(str) - k);
- free(str);
- str = strdup(ok);
- free(ok);
- am = 1;
- forkStuff(tz, str);
- }
- else
- {
- forkStuff(tz, str);
- }
- free(str);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement