Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/wait.h>
- void register_child_signal(){
- }
- void execute_command(char** args){
- int pid;
- int status;
- pid = fork();
- if(pid == 0){
- execvp(args[0], args);
- printf("Error! Command not found\n");
- exit(0);
- }
- else if(pid < 0){
- printf("Error forking!\n");
- }
- else{
- do{
- waitpid(pid, &status, 0);
- }
- while(!WIFEXITED(status) && !WIFSIGNALED(status));
- }
- }
- void execute_shell_builtin(char** command_array){
- if(!strcmp("cd", command_array[0])){
- }
- else if(!strcmp("echo", command_array[0])){
- }
- else if(!strcmp("export", command_array[0])){
- }
- }
- int evaluate_command(char** command_array){
- if(!strcmp("cd", command_array[0]) || !strcmp("echo", command_array[0]) || !strcmp("export", command_array[0])){
- return 0;
- }
- else if(!strcmp("exit", command_array[0])){
- return -1;
- }
- else{
- return 1;
- }
- }
- char** parse_command(char input[]){
- static char* strings[100];
- int index = 0;
- char* token = strtok(input, " \n"); // split string by space and newline
- while (token != NULL){
- strings[index++] = token ;
- token = strtok(NULL, " \n");
- }
- strings[index] = NULL;
- return strings;
- }
- void shell_loop(){
- int status = 1;
- do {
- printf("%s", "$ ");
- char input_command[100] ;
- fgets(input_command, 100, stdin);
- char** command_array = parse_command(input_command); // parse array of char to array of strings seperated by space and ended with null
- int type = evaluate_command(command_array); // 0: shell builtin, 1: executable or error, -1: exit
- switch (type) {
- case 0:
- execute_shell_builtin(command_array);
- break;
- case 1:
- execute_command(command_array);
- break;
- default:
- status = 0;
- break;
- }
- }
- while(status);
- }
- void setup_environment(){
- /*cd(Current_Working_Directory)*/
- }
- void on_child_exit(){
- /*reap_child_zombie()
- write_to_log_file("Child terminated")*/
- }
- void parent_main(){
- /*register_child_signal(on_child_exit());*/
- setup_environment();
- shell_loop();
- }
- int main() {
- shell_loop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement