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

                   
                   
                    
                     

                  
                    
                         
 
 
 
                

                                     
 
                                                           
                                                                 

                      
                          
                       
                             
 
                                
                                                         
                                                         
                     
         
 



                                                    

                                                     

                                     
 





                                                              


                                                    
 
                                                  

                                     
 



                                                            
                                                               
                        
                



                                                              

                                     
         

              
                           
                                   
     
 
                              
                         
 
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/wait.h>

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



int main(void) {
    chdir(".");
    setvbuf(stdout, NULL, _IONBF, 0);

    char const *const original_wd = get_current_dir_name();
    char const *prompt = relative_path(original_wd, original_wd);
    bool done = false;
    while (!done) {
        char *line = NULL;
        size_t cap = 0;
        __ssize_t length = 0;

        printf("%s > ", prompt);
        if ((length = getline(&line, &cap, stdin)) < 0) {
            fprintf(stderr, "Failed to read from STDIN");
            exit(-1);
        }

        if (strspn(line, " \n\t") == strlen(line)) {
            continue;
        }

        line[length - 1] = '\0'; // cut the line feed

        process * processes = NULL;
        parse_line(line, &processes);

        if (strcmp(processes[0].argv[0], "cd") == 0) {

            if(arrayLen(processes) != 1) {
                perror("Can't chain cd with other processes");
            }
            if (arrayLen(processes[0].argv) != 3) {
                fprintf(stderr, "usage: cd <path>");
                goto clean;
            }

            int ret = chdir(processes[0].argv[1]);
            if (ret)
                printf("[%i] ", ret);

            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(processes[0].argv[1], "exit") == 0) {
            done = true;
        } else {
            int ret;
            for (size_t i = 0; i < arrayLen(processes); ++i) {
                  ret = exec_command(processes[i], 0);
            }
            if (ret)
                printf("[%i] ", ret);
        }

        clean:
        free((void *)line);
        free_processes(&processes);
    }

    free((void *)original_wd);
    free((void *)prompt);
}