From 5c5292c8aec3569e797d94fcdf9f64553a273603 Mon Sep 17 00:00:00 2001 From: Yunju Chen Date: Sat, 18 Nov 2017 22:34:31 +0000 Subject: [PATCH 1/6] Add exit with value function --- builtins.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/builtins.c b/builtins.c index 16265e7..69856ad 100644 --- a/builtins.c +++ b/builtins.c @@ -50,14 +50,32 @@ void env_b(__attribute__((unused))char *line) } /** - * exit_b - Exits the shell. After freeing allocated resources. + * exit_b - Exits the shell with value 0. After freeing allocated resources. + * If exit value provided, exit with value. If exit value is not integer, + * exit with 2 and print error message. * @line: A string representing the input from the user. */ void exit_b(char *line) { + int token_count, value; + char **argv; + const char *delim = "\n\t "; + + argv = token_interface(line, delim, token_count); + if (argv[1] == NULL) + value = 0; + else if (atoi(argv[1])) + value = atoi(argv[1]); + else + { + value = 2; + print_str("logout", 0); + printf("exit: %s: numeric argument required\n", argv[1]); + exit(value); + } free(line); print_str("logout", 0); - exit(1); + exit(value); } /** From 0042205286a06df1119bea12d3be4491a0c52d0a Mon Sep 17 00:00:00 2001 From: Yunju Chen Date: Sat, 18 Nov 2017 22:38:34 +0000 Subject: [PATCH 2/6] Add free(line) to else case --- builtins.c | 1 + 1 file changed, 1 insertion(+) diff --git a/builtins.c b/builtins.c index 69856ad..434d768 100644 --- a/builtins.c +++ b/builtins.c @@ -71,6 +71,7 @@ void exit_b(char *line) value = 2; print_str("logout", 0); printf("exit: %s: numeric argument required\n", argv[1]); + free(line); exit(value); } free(line); From ec0e6486ad740521e002429cb7e30d622ef53a9d Mon Sep 17 00:00:00 2001 From: Yunju Chen Date: Mon, 20 Nov 2017 19:01:58 +0000 Subject: [PATCH 3/6] Fix exit_b memory leak issue, add _atoi() to hblib and update header file --- builtins.c | 7 ++++--- olaf.h | 5 +++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/builtins.c b/builtins.c index 434d768..db89e24 100644 --- a/builtins.c +++ b/builtins.c @@ -57,15 +57,15 @@ void env_b(__attribute__((unused))char *line) */ void exit_b(char *line) { - int token_count, value; + int token_count = 0, value; char **argv; const char *delim = "\n\t "; argv = token_interface(line, delim, token_count); if (argv[1] == NULL) value = 0; - else if (atoi(argv[1])) - value = atoi(argv[1]); + else if (_atoi(argv[1])) + value = _atoi(argv[1]); else { value = 2; @@ -74,6 +74,7 @@ void exit_b(char *line) free(line); exit(value); } + double_free(argv); free(line); print_str("logout", 0); exit(value); diff --git a/olaf.h b/olaf.h index 2836480..8b22c08 100644 --- a/olaf.h +++ b/olaf.h @@ -53,5 +53,10 @@ void cd_b(char *directory); int _strcmp(char *s1, char *s2); char *_strdup(char *src); void print_str(char *str, int new_line); +int _atoi(char *s); +int length(char *s); +int exponent(int len); +int total(char *s); +int neg(char *s, int p); #endif From 7bbd6721b59069d46161e0b0a2615b6808e8e574 Mon Sep 17 00:00:00 2001 From: Yunju Chen Date: Mon, 20 Nov 2017 19:08:58 +0000 Subject: [PATCH 4/6] Add _atoi to hblib.c file --- hbtlib.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/hbtlib.c b/hbtlib.c index f258edf..7470fb2 100644 --- a/hbtlib.c +++ b/hbtlib.c @@ -57,3 +57,120 @@ void print_str(char *str, int new_line) if (new_line == 0) write(STDOUT_FILENO, "\n", 1); } + +int length(char *s); +int exponent(int len); +int total(char *s); +int neg(char *s, int p); + +/** + * _atoi - Converts numbers in a string to integers + * @s: string + * Return: String number converted to int + */ +int _atoi(char *s) +{ + int i; + int len; + int number; + unsigned int body; + int expt; + int total_len; + int negatives; + + len = length(s); + expt = exponent(len); + total_len = total(s); + body = 0; + for (i = total_len - len; s[i] != '\0'; i++) + { + number = (s[i] - 48) * expt; + body = number + body; + expt = expt / 10; + } + negatives = neg(s, total_len - len); + if (negatives % 2 != 0) + body = -body; + return (body); +} + +/** + * length - Calculates the length of the number + * @s: string + * Return: The length of the number + */ +int length(char *s) +{ + int i; + int len; + + for (i = 0, len = 0; s[i] != '\0'; i++) + { + if (s[i] >= 48 && s[i] <= 57) + { + len++; + if (s[i + 1] == 32) /* stopping for spaces*/ + break; + } + } + return (len); +} + +/** + * exponent - Calculates the exponent to multiply the number by + * @len: length of a number + * Return: The exponent of a number + */ +int exponent(int len) +{ + int expt; + + for (expt = 1; len - 1 > 0; len--) + { + expt = expt * 10; + } + return (expt); +} + +/** + * total - Calculates the total length of a string up to the numbers + * @s: string + * Return: Total len of a string + */ +int total(char *s) +{ + int i; + + for (i = 0; s[i] != '\0'; i++) + { + if (s[i] >= 48 && s[i] <= 57) + { + if (s[i + 1] < 48 || s[i + 1] > 57) + { + i++; + break; + } + } + } + return (i); +} + +/** + * neg - Calculates the total number of negatives + * @s: string + * @p: position were the number starts in the string + * Return: Total count of negatives + */ +int neg(char *s, int p) +{ + int i; + int counter; + + for (counter = 0, i = 0; i < p; i++) + { + if (s[i] == 45) + counter++; + } + return (counter); +} + From 6e104c4effb97f4e58b90f79a2c1c50f7c23edeb Mon Sep 17 00:00:00 2001 From: Yunju Chen Date: Mon, 20 Nov 2017 19:24:20 +0000 Subject: [PATCH 5/6] Move atoi out of hblib.c b/c Betty warning --- atoi.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ hbtlib.c | 117 ------------------------------------------------------- 2 files changed, 116 insertions(+), 117 deletions(-) create mode 100644 atoi.c diff --git a/atoi.c b/atoi.c new file mode 100644 index 0000000..286d62a --- /dev/null +++ b/atoi.c @@ -0,0 +1,116 @@ +int length(char *s); +int exponent(int len); +int total(char *s); +int neg(char *s, int p); + +/** + * _atoi - Converts numbers in a string to integers + * @s: string + * Return: String number converted to int + */ +int _atoi(char *s) +{ + int i; + int len; + int number; + unsigned int body; + int expt; + int total_len; + int negatives; + + len = length(s); + expt = exponent(len); + total_len = total(s); + body = 0; + for (i = total_len - len; s[i] != '\0'; i++) + { + number = (s[i] - 48) * expt; + body = number + body; + expt = expt / 10; + } + negatives = neg(s, total_len - len); + if (negatives % 2 != 0) + body = -body; + return (body); +} + +/** + * length - Calculates the length of the number + * @s: string + * Return: The length of the number + */ +int length(char *s) +{ + int i; + int len; + + for (i = 0, len = 0; s[i] != '\0'; i++) + { + if (s[i] >= 48 && s[i] <= 57) + { + len++; + if (s[i + 1] == 32) /* stopping for spaces*/ + break; + } + } + return (len); +} + +/** + * exponent - Calculates the exponent to multiply the number by + * @len: length of a number + * Return: The exponent of a number + */ +int exponent(int len) +{ + int expt; + + for (expt = 1; len - 1 > 0; len--) + { + expt = expt * 10; + } + return (expt); +} + +/** + * total - Calculates the total length of a string up to the numbers + * @s: string + * Return: Total len of a string + */ +int total(char *s) +{ + int i; + + for (i = 0; s[i] != '\0'; i++) + { + if (s[i] >= 48 && s[i] <= 57) + { + if (s[i + 1] < 48 || s[i + 1] > 57) + { + i++; + break; + } + } + } + return (i); +} + +/** + * neg - Calculates the total number of negatives + * @s: string + * @p: position were the number starts in the string + * Return: Total count of negatives + */ +int neg(char *s, int p) +{ + int i; + int counter; + + for (counter = 0, i = 0; i < p; i++) + { + if (s[i] == 45) + counter++; + } + return (counter); +} + diff --git a/hbtlib.c b/hbtlib.c index 7470fb2..f258edf 100644 --- a/hbtlib.c +++ b/hbtlib.c @@ -57,120 +57,3 @@ void print_str(char *str, int new_line) if (new_line == 0) write(STDOUT_FILENO, "\n", 1); } - -int length(char *s); -int exponent(int len); -int total(char *s); -int neg(char *s, int p); - -/** - * _atoi - Converts numbers in a string to integers - * @s: string - * Return: String number converted to int - */ -int _atoi(char *s) -{ - int i; - int len; - int number; - unsigned int body; - int expt; - int total_len; - int negatives; - - len = length(s); - expt = exponent(len); - total_len = total(s); - body = 0; - for (i = total_len - len; s[i] != '\0'; i++) - { - number = (s[i] - 48) * expt; - body = number + body; - expt = expt / 10; - } - negatives = neg(s, total_len - len); - if (negatives % 2 != 0) - body = -body; - return (body); -} - -/** - * length - Calculates the length of the number - * @s: string - * Return: The length of the number - */ -int length(char *s) -{ - int i; - int len; - - for (i = 0, len = 0; s[i] != '\0'; i++) - { - if (s[i] >= 48 && s[i] <= 57) - { - len++; - if (s[i + 1] == 32) /* stopping for spaces*/ - break; - } - } - return (len); -} - -/** - * exponent - Calculates the exponent to multiply the number by - * @len: length of a number - * Return: The exponent of a number - */ -int exponent(int len) -{ - int expt; - - for (expt = 1; len - 1 > 0; len--) - { - expt = expt * 10; - } - return (expt); -} - -/** - * total - Calculates the total length of a string up to the numbers - * @s: string - * Return: Total len of a string - */ -int total(char *s) -{ - int i; - - for (i = 0; s[i] != '\0'; i++) - { - if (s[i] >= 48 && s[i] <= 57) - { - if (s[i + 1] < 48 || s[i + 1] > 57) - { - i++; - break; - } - } - } - return (i); -} - -/** - * neg - Calculates the total number of negatives - * @s: string - * @p: position were the number starts in the string - * Return: Total count of negatives - */ -int neg(char *s, int p) -{ - int i; - int counter; - - for (counter = 0, i = 0; i < p; i++) - { - if (s[i] == 45) - counter++; - } - return (counter); -} - From 63bd9ae1d57afa2c701461974b59eb0563bf7f56 Mon Sep 17 00:00:00 2001 From: Yunju Chen Date: Mon, 20 Nov 2017 23:55:16 +0000 Subject: [PATCH 6/6] Add another double_free() to exit_b --- builtins.c | 1 + 1 file changed, 1 insertion(+) diff --git a/builtins.c b/builtins.c index db89e24..7c6e50a 100644 --- a/builtins.c +++ b/builtins.c @@ -71,6 +71,7 @@ void exit_b(char *line) value = 2; print_str("logout", 0); printf("exit: %s: numeric argument required\n", argv[1]); + double_free(argv); free(line); exit(value); }