diff options
| author | iceyrazor <iceyrazor@mailfence.com> | 2026-02-09 23:48:22 -0600 |
|---|---|---|
| committer | iceyrazor <iceyrazor@mailfence.com> | 2026-02-09 23:48:22 -0600 |
| commit | 42abbc8f06fb56761502824b8085516299535534 (patch) | |
| tree | 70fc16f235b3b5346774dc098cac2c8dcf60549d /src/helper_functions.c | |
| parent | f1f01cbbf9681ee311a69db40e832c630950cb53 (diff) | |
- readme. todo
- update pwd
- update SHLVL
- basename pwd in prompt
- updating pwd
Diffstat (limited to 'src/helper_functions.c')
| -rw-r--r-- | src/helper_functions.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/helper_functions.c b/src/helper_functions.c index 9427b87..5c81f15 100644 --- a/src/helper_functions.c +++ b/src/helper_functions.c @@ -1,6 +1,8 @@ #ifndef shell_helper_functions #define shell_helper_functions +#include <stdio.h> +#include <stdlib.h> #include <string.h> #include <unistd.h> @@ -51,4 +53,38 @@ char *str_replace(char *orig, char *rep, char *with) { return result; } +char *basename(char *path_in) +{ + char *token = NULL; + 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); + } + strcpy(basePath, path_in); + + token = strtok(basePath, "/"); + while (token != NULL) { + lastToken = token; + token = strtok(NULL, "/"); + } + + if (lastToken == NULL){ + free(basePath); + return NULL; + } + + char *ret_str = (char*)malloc(sizeof(char)*strlen(lastToken)+1); + if (!ret_str) { + fprintf(stderr, "lsh: allocation error\n"); + exit(EXIT_FAILURE); + } + strcpy(ret_str,lastToken); + free(basePath); + return ret_str; +} + #endif |
