From 1eb83090ac4b5a31b352a843c85304df9af1f3e0 Mon Sep 17 00:00:00 2001 From: iceyrazor Date: Fri, 27 Feb 2026 19:53:33 -0600 Subject: - changed make - malloc with error function --- src/helper_functions.c | 22 ++++++++++++---------- src/lsh_builtins.c | 7 ++----- src/lsh_main_func.c | 34 +++++++++++++--------------------- src/main.c | 18 +++--------------- 4 files changed, 30 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/helper_functions.c b/src/helper_functions.c index 5c81f15..c1931fd 100644 --- a/src/helper_functions.c +++ b/src/helper_functions.c @@ -1,11 +1,21 @@ #ifndef shell_helper_functions #define shell_helper_functions +#include #include #include #include #include +void *malloce(size_t size){ + void* mal=malloc(size); + if(!mal){ + fprintf(stderr, "lsh: allocation error\n"); + exit(EXIT_FAILURE); + } + return mal; +} + // You must free the result if result is non-NULL. char *str_replace(char *orig, char *rep, char *with) { char *result; // the return string @@ -59,11 +69,7 @@ char *basename(char *path_in) char *lastToken = NULL; char *basePath; - basePath = (char *)malloc(sizeof(char) * (strlen(path_in) + 1)); - if (!basePath) { - fprintf(stderr, "lsh: allocatin error\n"); - exit(EXIT_FAILURE); - } + basePath = (char *)malloce(sizeof(char) * (strlen(path_in) + 1)); strcpy(basePath, path_in); token = strtok(basePath, "/"); @@ -77,11 +83,7 @@ char *basename(char *path_in) return NULL; } - char *ret_str = (char*)malloc(sizeof(char)*strlen(lastToken)+1); - if (!ret_str) { - fprintf(stderr, "lsh: allocation error\n"); - exit(EXIT_FAILURE); - } + char *ret_str = (char*)malloce(sizeof(char)*strlen(lastToken)+1); strcpy(ret_str,lastToken); free(basePath); return ret_str; diff --git a/src/lsh_builtins.c b/src/lsh_builtins.c index 81fc2b5..0986cfe 100644 --- a/src/lsh_builtins.c +++ b/src/lsh_builtins.c @@ -2,6 +2,7 @@ #define shell_builtins #include "lsh_main_func.c" +#include "helper_functions.c" #include #include #include @@ -48,11 +49,7 @@ int lsh_cd(char **args) if (err != 0) { perror("lsh"); } - char *cwd=(char*)malloc(sizeof(char)*4096); - if (!cwd) { - fprintf(stderr, "lsh: allocation error\n"); - exit(EXIT_FAILURE); - } + char *cwd=(char*)malloce(sizeof(char)*4096); getcwd(cwd,sizeof(char*)*4096); setenv("PWD",cwd,1); free(cwd); diff --git a/src/lsh_main_func.c b/src/lsh_main_func.c index 74e7881..8a3aade 100644 --- a/src/lsh_main_func.c +++ b/src/lsh_main_func.c @@ -8,19 +8,16 @@ #include #include +#include "helper_functions.c" + #define LSH_RL_BUFSIZE 1024 char *lsh_read_line(void) { int bufsize = LSH_RL_BUFSIZE; int position = 0; - char *buffer = malloc(sizeof(char) * bufsize); + char *buffer = malloce(sizeof(char) * bufsize); int c; - if (!buffer) { - fprintf(stderr, "lsh: allocation error\n"); - exit(EXIT_FAILURE); - } - while (1) { // Read a character c = getchar(); @@ -47,7 +44,7 @@ char *lsh_read_line(void) } char *handle_vars(char *token){ - char *ret_token=(char*)malloc(sizeof(char)*strlen(token)+1); + char *ret_token=(char*)malloce(sizeof(char)*strlen(token)+1); strcpy(ret_token,token); if (token[0]=='~'){ @@ -55,24 +52,24 @@ char *handle_vars(char *token){ if(!getenv("HOME")){ fprintf(stderr,"lsh: you aint got no home. da hell\n"); free(ret_token); - ret_token=(char*)malloc(sizeof(char)*strlen(token)+1); + ret_token=(char*)malloce(sizeof(char)*strlen(token)+1); strcpy(ret_token,token); return ret_token; } free(ret_token); - ret_token=(char*)malloc(sizeof(char)*strlen(token)+strlen(getenv("HOME"))+1); + ret_token=(char*)malloce(sizeof(char)*strlen(token)+strlen(getenv("HOME"))+1); strcpy(ret_token,getenv("HOME")); strcat(ret_token,token); } - char *tmp_token=(char*)malloc(sizeof(char)*strlen(ret_token)+1); + char *tmp_token=(char*)malloce(sizeof(char)*strlen(ret_token)+1); strcpy(tmp_token,ret_token); int i=0; while (tmp_token[i]!='\0'){ if(tmp_token[i]=='$'){ int j=0; while(tmp_token[i+j]!=' ' && tmp_token[i+j]) j++; - char *varname=(char*)malloc(sizeof(char)*j+1); + char *varname=(char*)malloce(sizeof(char)*j+1); varname[j]='\0'; strncpy(varname,&tmp_token[i+1],j); varname[j-1]='\0'; @@ -84,7 +81,7 @@ char *handle_vars(char *token){ free(ret_token); int tmp_len=strlen(tmp_token); - ret_token=(char*)malloc(sizeof(char)*(strlen(tmp_token)-strlen(varname))+strlen(env)+1); + ret_token=(char*)malloce(sizeof(char)*(strlen(tmp_token)-strlen(varname))+strlen(env)+1); tmp_token[i]='\0'; tmp_token[i+j]='\0'; @@ -96,7 +93,7 @@ char *handle_vars(char *token){ } free(tmp_token); - tmp_token=(char*)malloc(sizeof(char)*strlen(ret_token)+1); + tmp_token=(char*)malloce(sizeof(char)*strlen(ret_token)+1); strcpy(tmp_token,ret_token); free(varname); @@ -176,8 +173,8 @@ char * strtok_quotes(char *token){ char *pop_quotes(char *token){ int ch_i=0; - char *right=(char*)malloc(sizeof(char)*strlen(token)+1); - char *ret_token=(char*)malloc(sizeof(char)*strlen(token)+1); + char *right=(char*)malloce(sizeof(char)*strlen(token)+1); + char *ret_token=(char*)malloce(sizeof(char)*strlen(token)+1); strcpy(ret_token,token); while(ch_i