diff options
Diffstat (limited to 'src/main.c')
| -rwxr-xr-x | src/main.c | 58 |
1 files changed, 45 insertions, 13 deletions
@@ -2,43 +2,75 @@ #include <stdlib.h> #include <unistd.h> +#include "helper_functions.c" #include "lsh_main_func.c" #include "lsh_builtins.c" typedef struct{ char *prompt; + char *pre_prompt; } SHELL_OB; void lsh_loop(SHELL_OB *shell_obj) { - char *line; - char **args; - int status; + char *line; + char **args; + int status; - do { - printf("%s",shell_obj->prompt); - line = lsh_read_line(); - args = lsh_split_line(line); - status = lsh_execute(args); + do { + //get pwd + char *cwd= getenv("PWD"); + char *pwd=basename(cwd); + snprintf(shell_obj->prompt,sizeof(char) * 1024,"%s %s> ",shell_obj->pre_prompt,pwd); + free(pwd); + printf("%s",shell_obj->prompt); + line = lsh_read_line(); + args = lsh_split_line(line); + status = lsh_execute(args); - free(line); - free(args); - } while (status); + free(line); + free(args); + } while (status); } int main(int argc, char **argv){ + //set sh level + char *shlvl = getenv("SHLVL"); + if(shlvl != NULL){ + int shlvli = atoi(shlvl); + shlvli++; + sprintf(shlvl,"%d",shlvli); + setenv("SHLVL",shlvl,1); + } + + //Set The prompt SHELL_OB shell_obj; shell_obj.prompt = (char *)malloc(sizeof(char) * 1024); + if (!shell_obj.prompt){ + fprintf(stderr, "lsh: allocation error\n"); + exit(EXIT_FAILURE); + } + shell_obj.pre_prompt = (char *)malloc(sizeof(char) * 1024); + if (!shell_obj.pre_prompt){ + fprintf(stderr, "lsh: allocation error\n"); + exit(EXIT_FAILURE); + } //get the hostname char *hostname = (char *)malloc(sizeof(char) * 1024); + if (!hostname) { + fprintf(stderr, "lsh: allocation error\n"); + exit(EXIT_FAILURE); + } gethostname(hostname,sizeof(char) * 1024); - snprintf(shell_obj.prompt,sizeof(char) * 1024,"%s@%s> ",getenv("USER"),hostname); + //set the pre_prompt + snprintf(shell_obj.pre_prompt,sizeof(char) * 1024,"%s@%s",getenv("USER"),hostname); + free(hostname); lsh_loop(&shell_obj); - free(hostname); free(shell_obj.prompt); + free(shell_obj.pre_prompt); return EXIT_SUCCESS; } |
