summaryrefslogblamecommitdiffstats
path: root/02_exercise/shell.c
blob: 46b169a55bff8610edbc3efcb74452ae17b78cff (plain) (tree)
1
2
3
4
5
6
7
8
9
                  


                   
                   

                   
                    
                     

                      
                   

                  
                         

                                                                         
 

                                                   
 
                
 
                                                           

                                                                 


                             
                             
 
                                

                                                            
                     
         


                                            



                                                            
                                                    
                    

                                  




                                                         
                         


                                             
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <assert.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "array.h"
#include "prompt_utils.h"

void print_prompt(char const *const original_wd, char const *current_wd);

// Returns the return code of the executed programm
int exec_command(char *command);

int main(void) {

    char const *const original_wd = get_current_dir_name();
    char const *prompt = relative_path(original_wd, original_wd);
    printf("%s \n", original_wd);
    while (true) {
        char *command = NULL;
        size_t cap = 0;
        __ssize_t length = 0;

        printf("%s > ", prompt);
        if ((length = getline(&command, &cap, stdin)) < 0) {
            fprintf(stderr, "Failed to read from STDIN");
            exit(-1);
        }
        if (strstr(command, "cd") != NULL) {
            printf("Changing dirs \n");
            chdir("beispiele");
            free((void *)prompt);
            char const *current_wd = get_current_dir_name();
            prompt = relative_path(original_wd, current_wd);
            free((void *)current_wd);
        } else if (strcmp(command, "exit\n") == 0) {
            exit(0);
        } else {
            exec_command(command);
        }
        free((void *)command);
    }
    // Gotta cast to void otherwise we discard qualifiers
    free((void *)original_wd);
    free((void *)prompt);
}

int exec_command(char *command) { return 0; }