mylsh

my own shell, based on brenns10's
git clone https://github.com/jennydoe/mylsh.git
Log | Files | Refs | Feed | README

commit 90a661b66031dfe95ac4cf9df784531dd328d686
parent 9b79ea1e04f86726b3db46fe11f774ade224a864
Author: Jenny Doe <tng@soykaf.me>
Date:   Wed, 20 Mar 2019 13:17:30 +0100

fixed: better handling of memory issues and fixed incorrect types

Diffstat:
Mlsh.c | 33+++++++++++++++++++++------------
1 file changed, 21 insertions(+), 12 deletions(-)

diff --git a/lsh.c b/lsh.c @@ -22,6 +22,12 @@ #include <wchar.h> #include <locale.h> +enum lsh_error { + LSH_EXECUTE_OOM, + LSH_READ_LINE_OOM, + LSH_SPLIT_LINE_OOM +}; + const wchar_t prompt[] = L"λ "; wchar_t *lsh_read_line(void); @@ -87,11 +93,11 @@ lsh_execute(wchar_t * *words) * hint would be appreciated: mailto tng@soykaf.me */ size_t l = 1 + sizeof(char) * wcslen(words[1]); - char **v = &words[1]; + wchar_t **v = &words[1]; char *t = malloc(l); if (t == NULL) { fprintf(stderr, "out of memory\n"); - exit(1); + return (LSH_EXECUTE_OOM); } wcsrtombs(&t[0], v, l, NULL); @@ -112,11 +118,11 @@ lsh_execute(wchar_t * *words) } else { int l = 2 + 2 * sizeof(char) * wcslen(words[1]); - char **v = &words[1]; + wchar_t **v = &words[1]; char *t = malloc(l); if (t == NULL) { fprintf(stderr, "out of memory\n"); - exit(1); + return (LSH_EXECUTE_OOM); } wcsrtombs(&t[0], v, l, NULL); @@ -154,7 +160,7 @@ lsh_read_line(void) wchar_t *line = malloc(sizeof(wchar_t) * 1); if (line == NULL) { fprintf(stderr, "out of memory\n"); - exit(1); + return NULL; } if (isatty(fileno(stdin))) @@ -166,11 +172,13 @@ lsh_read_line(void) if (pos >= bufsize) { bufsize += 1; - line = realloc(line, sizeof(wchar_t) * bufsize); - if (line == NULL) { + wchar_t *tmp = realloc(line, sizeof(wchar_t) * bufsize); + if (tmp == NULL) { fprintf(stderr, "out of memory\n"); - exit(1); + free(line); + return NULL; } + line = tmp; } @@ -189,7 +197,7 @@ lsh_split_line(wchar_t * line) wchar_t **words = malloc(sizeof(wchar_t *) * 10); if (words == NULL) { fprintf(stderr, "out of memory\n"); - exit(1); + return NULL; } wchar_t *word; @@ -206,12 +214,13 @@ lsh_split_line(wchar_t * line) ++pos; if (pos >= bufsize) { bufsize += 10; - words = realloc(words, sizeof(wchar_t *) * - bufsize); + wchar_t **tmp = realloc(words, sizeof(wchar_t *) * bufsize); if (words == NULL) { fprintf(stderr, "out of memory\n"); - exit(1); + free(words); + return NULL; } + words = tmp; } word = wcstok(NULL, s, &dummy);