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/lsh_main_func.c | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) (limited to 'src/lsh_main_func.c') 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