diff options
| author | iceyrazor <iceyrazor@mailfence.com> | 2026-02-27 19:53:33 -0600 |
|---|---|---|
| committer | iceyrazor <iceyrazor@mailfence.com> | 2026-02-27 19:53:33 -0600 |
| commit | 1eb83090ac4b5a31b352a843c85304df9af1f3e0 (patch) | |
| tree | 5b78d7f1a7b8281f34c980630e5b9fbdf84833ce /src/helper_functions.c | |
| parent | 022a202e31157859bc0fac25479a4782514e50a9 (diff) | |
- changed make
- malloc with error function
Diffstat (limited to 'src/helper_functions.c')
| -rw-r--r-- | src/helper_functions.c | 22 |
1 files changed, 12 insertions, 10 deletions
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 <stddef.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> +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; |
