diff --git a/errors.c b/errors.c new file mode 100644 index 0000000..db657b2 --- /dev/null +++ b/errors.c @@ -0,0 +1,89 @@ +#include "shell.h" + +/** + * _eputs -|Function|that|prints|an|input|string + * @ristrvi: |Represents|the|string|to|be|printed + * + * Return: |void| + */ + +void _eputs(char *ristrvi) +{ + int riivi = 0; + + if (!ristrvi) + return; + while (ristrvi[riivi] != '\0') + { + _eputchar(ristrvi[riivi]); + riivi++; + } +} + +/** + * _eputchar -|Functions|that|writes|the|character|c|to|stderr + * @ricvi: |Represents|the|character|to|print + * + * Return: |1|when successful,|when|on|error|-1|is|returned +* |and|errno|is|set|appropriately. + */ + +int _eputchar(char ricvi) +{ + static int riivi; + static char ribufvi[RIWRITE_BUF_SIZEVI]; + + if (ricvi == RIBUF_FLUSHVI || riivi >= RIWRITE_BUF_SIZEVI) + { + write(2, ribufvi, riivi); + riivi = 0; + } + if (ricvi != RIBUF_FLUSHVI) + ribufvi[riivi++] = ricvi; + return (1); +} + +/** + * _putfd - |Function|thatwrites|the|character|c|to|given|fd + * @ricvi: |The|character|value|to|print + * @rifdvi: |The|file|descriptor|value|to|write|to + * + * Return:|On|error|-1|returned|but|1|when|success|1| + * and|errno|is|set|appropriately. + */ + +int _putfd(char ricvi, int rifdvi) +{ + static int riivi; + static char ribufvi[RIWRITE_BUF_SIZEVI]; + + if (ricvi == RIBUF_FLUSHVI || riivi >= RIWRITE_BUF_SIZEVI) + { + write(rifdvi, ribufvi, riivi); + riivi = 0; + } + if (ricvi != RIBUF_FLUSHVI) + ribufvi[riivi++] = ricvi; + return (1); +} + +/** + * _putsfd - |Function|that|prints|an|input|string + * @ristrvi:|the|string|value|to|be|printed + * @rifdvi:|the|filedescriptor|value|to|write|to + * + * Return:|the|number|of|chars|put + */ + +int _putsfd(char *ristrvi, int rifdvi) +{ + int riivi = 0; + + if (!ristrvi) + return (0); + while (*ristrvi) + { + riivi += _putfd(*ristrvi++, rifdvi); + } + return (riivi); +} diff --git a/errors1.c b/errors1.c new file mode 100644 index 0000000..b87630b --- /dev/null +++ b/errors1.c @@ -0,0 +1,146 @@ +#include "shell.h" + +/** + * _erratoi - Function|that|converts|a|string|to|an|integer + * @risvi:|the|string|value|to|be|converted + * Return:|-1|on|error|else|0|if|no|converted|numbers|in|string + * + */ + +int _erratoi(char *risvi) +{ + int riivi = 0; + unsigned long int riresultvi = 0; + + if (*risvi == '+') + risvi++; /*|TODO:|why|does|this|make|main|return|255?|*/ + for (riivi = 0; risvi[riivi] != '\0'; riivi++) + { + if (risvi[riivi] >= '0' && risvi[riivi] <= '9') + { + riresultvi *= 10; + riresultvi += (risvi[riivi] - '0'); + if (riresultvi > INT_MAX) + return (-1); + } + else + return (-1); + } + return (riresultvi); +} + +/** + * print_error - |Function|that|prints|an|error|message + * @riinfovi:|the|parameter|&|return|info|struct + * @riestrvi:|string|containing|specified|error|type + * Return: |-1|on|error|else|0|if|no|converted|numbers|in|string + * + */ + +void print_error(riinfo_tvi *riinfovi, char *riestrvi) +{ + _eputs(riinfovi->rifnamevi); + _eputs(": "); + print_d(riinfovi->riline_countvi, STDERR_FILENO); + _eputs(": "); + _eputs(riinfovi->riargvvi[0]); + _eputs(": "); + _eputs(riestrvi); +} + +/** + * print_d - |function|that|prints|a|decimal|(integer)|number|(base 10) + * @riinputvi: Represents|the|input + * @rifdvi: |Represents|the|filedescriptor|to|write|to + * + * Return:|number|of|characters|printed + */ + +int print_d(int riinputvi, int rifdvi) +{ + int (*__putchar)(char) = _putchar; + int riivi, ricountvi = 0; + unsigned int _riabsvi_, ricurrentvi; + + if (rifdvi == STDERR_FILENO) + __putchar = _eputchar; + if (riinputvi < 0) + { + _riabsvi_ = -riinputvi; + __putchar('-'); + ricountvi++; + } + else + _riabsvi_ = riinputvi; + ricurrentvi = _riabsvi_; + for (riivi = 1000000000; riivi > 1; riivi /= 10) + { + if (_riabsvi_ / riivi) + { + __putchar('0' + ricurrentvi / riivi); + ricountvi++; + } + ricurrentvi %= riivi; + } + __putchar('0' + ricurrentvi); + ricountvi++; + + return (ricountvi); +} + +/** + * convert_number - |a|clone|of|itoa|converter|function + * @rinumvi: Represnets|number + * @ribasevi: Represents|base + * @riflagsvi: Represents|argument|flags + * + * Return:|string + */ + +char *convert_number(long int rinumvi, int ribasevi, int riflagsvi) +{ + static char *riarrayvi; + static char ribuffervi[50]; + char risignvi = 0; + char *riptrvi; + unsigned long rinvi = rinumvi; + + if (!(riflagsvi & RICONVERT_UNSIGNEDVI) && rinumvi < 0) + { + rinvi = -rinumvi; + risignvi = '-'; + + } + riarrayvi = riflagsvi & RICONVERT_LOWERCASEVI ? + "0123456789abcdef" : "0123456789ABCDEF"; + riptrvi = &ribuffervi[49]; + *riptrvi = '\0'; + + do { + *--riptrvi = riarrayvi[rinvi % ribasevi]; + rinvi /= ribasevi; + } while (rinvi != 0); + + if (risignvi) + *--riptrvi = risignvi; + return (riptrvi); +} + +/** + * remove_comments -|function|that|replaces|first|instance|of|'#'|with|'\0' + * @ribufvi: RepResnents|the|address|of|the|string|to|modify + * + * Return:|always||when|successful; + */ + +void remove_comments(char *ribufvi) +{ + int riivi; + + for (riivi = 0; ribufvi[riivi] != '\0'; riivi++) + if (ribufvi[riivi] == '#' && (!riivi || ribufvi[riivi - 1] == ' ')) + { + ribufvi[riivi] = '\0'; + break; + } +} diff --git a/exits.c b/exits.c new file mode 100644 index 0000000..2fe3535 --- /dev/null +++ b/exits.c @@ -0,0 +1,77 @@ +#include "shell.h" + +/** + **_strncpy - |Function|that|copies|a|string + *@ridestvi: |Represents|the|destination|string|to|be|copied|to + *@risrcvi: |Represents|the|source|string + *@rinvi: |Represents|the|amount|of|characters|to|be|copied + *Return: |Returns|the|concatenated|string + */ + +char *_strncpy(char *ridestvi, char *risrcvi, int rinvi) +{ + int riivi, rijvi; + char *risvi = ridestvi; + + riivi = 0; + while (risrcvi[riivi] != '\0' && riivi < rinvi - 1) + { + ridestvi[riivi] = risrcvi[riivi]; + riivi++; + } + if (riivi < rinvi) + { + rijvi = riivi; + while (rijvi < rinvi) + { + ridestvi[rijvi] = '\0'; + rijvi++; + } + } + return (risvi); +} + +/** + **_strncat - |Function|that|concatenates|two|strings + *@ridestvi: |Represents|the|first|string + *@risrcvi: |Represnents|the|second|string + *@rinvi: |Represents|the|amount|of|bytes|to|be|maximally|used + *Return: |Returns|the|concatenated|string + */ + +char *_strncat(char *ridestvi, char *risrcvi, int rinvi) +{ + int riivi, rijvi; + char *risvi = ridestvi; + + riivi = 0; + rijvi = 0; + while (ridestvi[riivi] != '\0') + riivi++; + while (risrcvi[rijvi] != '\0' && rijvi < rinvi) + { + ridestvi[riivi] = risrcvi[rijvi]; + riivi++; + rijvi++; + } + if (rijvi < rinvi) + ridestvi[riivi] = '\0'; + return (risvi); +} + +/** + **_strchr - |Function|locates|a|character|in|a|string + *@risvi: |Represents|the|string|to|be|parsed + *@ricvi: |Represents|the|character|to|look|for + *Return: |'s'|a|pointer|to|the|memory|area|s + */ + +char *_strchr(char *risvi, char ricvi) +{ + do { + if (*risvi == ricvi) + return (risvi); + } while (*risvi++ != '\0'); + + return (NULL); +} diff --git a/generate-authors.sh b/generate-authors.sh new file mode 100755 index 0000000..5133ed5 --- /dev/null +++ b/generate-authors.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +set -e + +SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +ROOTDIR="$(git -C "$SCRIPTDIR" rev-parse --show-toplevel)" + +set -x + +# see also ".mailmap" for how email addresses and names are deduplicated +cat > "${ROOTDIR}/AUTHORS" <<- EOF + # File @generated by hack/generate-authors.sh. DO NOT EDIT. + # This file lists all contributors to the repository. + # See hack/generate-authors.sh to make modifications. + + $(git -C "$ROOTDIR" log --format='%aN <%aE>' | LC_ALL=C.UTF-8 sort -uf) +EOF diff --git a/getenv.c b/getenv.c new file mode 100644 index 0000000..12d2df4 --- /dev/null +++ b/getenv.c @@ -0,0 +1,96 @@ +#include "shell.h" + +/** + * get_environ - |Function|that|returns|the|string|array|copy|of|our|environ + * @riinfovi: |The|Structure|containing|potential|arguments|to|maintain + * function. + * Return:|Always|return|0|if |successful + */ + +char **get_environ(riinfo_tvi *riinfovi) +{ + if (!riinfovi->rienvironvi || riinfovi->rienv_changedvi) + { + riinfovi->rienvironvi = list_to_strings(riinfovi->rienvvi); + riinfovi->rienv_changedvi = 0; + } + + return (riinfovi->rienvironvi); +} + +/** + * _unsetenv - |Function|that|removes|an|environment|variable + * @riinfovi: |Represents|potential|arguments| + * to|maintain|constant|function|prototype. + * Return: |1|on|delete, else|0| + * @rivarvi: |Represents|the|string|env|var|property + */ +int _unsetenv(riinfo_tvi *riinfovi, char *rivarvi) +{ + rilist_tvi *rinodevi = riinfovi->rienvvi; + size_t riivi = 0; + char *ripvi; + + if (!rinodevi || !rivarvi) + return (0); + + while (rinodevi) + { + ripvi = starts_with(rinodevi->ristrvi, rivarvi); + if (ripvi && *ripvi == '=') + { + riinfovi->rienv_changedvi = delete_node_at_index(&(riinfovi->rienvvi), + riivi); + riivi = 0; + rinodevi = riinfovi->rienvvi; + continue; + } + rinodevi = rinodevi->rinextvi; + riivi++; + } + return (riinfovi->rienv_changedvi); +} + +/** + * _setenv -|Function|that|initialize|a|new|environment|variable, + * or|modify|an|existing|one + * @riinfovi: |Represents|the|potential|arguments| + * to|maintain|constant|function|prototype. + * @rivarvi: |Represent|the string env var property + * @rivaluevi:|Reprsents|the|string|env|var|value + * Return:|Always|0|if|successful + */ + +int _setenv(riinfo_tvi *riinfovi, char *rivarvi, char *rivaluevi) +{ + char *ribufvi = NULL; + rilist_tvi *rinodevi; + char *ripvi; + + if (!rivarvi || !rivaluevi) + return (0); + + ribufvi = malloc(_strlen(rivarvi) + _strlen(rivaluevi) + 2); + if (!ribufvi) + return (1); + _strcpy(ribufvi, rivarvi); + _strcat(ribufvi, "="); + _strcat(ribufvi, rivaluevi); + rinodevi = riinfovi->rienvvi; + while (rinodevi) + { + ripvi = starts_with(rinodevi->ristrvi, rivarvi); + if (ripvi && *ripvi == '=') + { + free(rinodevi->ristrvi); + rinodevi->ristrvi = ribufvi; + riinfovi->rienv_changedvi = 1; + return (0); + } + rinodevi = rinodevi->rinextvi; + } + add_node_end(&(riinfovi->rienvvi), ribufvi, 0); + free(ribufvi); + riinfovi->rienv_changedvi = 1; + return (0); +} diff --git a/lists1.c b/lists1.c new file mode 100644 index 0000000..2925562 --- /dev/null +++ b/lists1.c @@ -0,0 +1,129 @@ +#include "shell.h" + +/** + * list_len - |Function|that/determines/length/of/linked/list + * @rihvi:|Represents|that/pointer/to/first/node + * + * Return: /size/of/list + */ + +size_t list_len(const rilist_tvi *rihvi) +{ + size_t riivi = 0; + + while (rihvi) + { + rihvi = rihvi->rinextvi; + riivi++; + } + return (riivi); +} + +/** + * list_to_strings -|Function|that/returns/an/array/of/strings/of/the/list->str + * @riheadvi: |Represents|the/pointer/to/first/node + * + * Return: /array/of/strings + */ + +char **list_to_strings(rilist_tvi *riheadvi) +{ + rilist_tvi *rinodevi = riheadvi; + size_t riivi = list_len(riheadvi), rijvi; + char **ristrsvi; + char *ristrvi; + + if (!riheadvi || !riivi) + return (NULL); + ristrsvi = malloc(sizeof(char *) * (riivi + 1)); + if (!ristrsvi) + return (NULL); + for (riivi = 0; rinodevi; rinodevi = rinodevi->rinextvi, riivi++) + { + ristrvi = malloc(_strlen(rinodevi->ristrvi) + 1); + if (!ristrvi) + { + for (rijvi = 0; rijvi < riivi; rijvi++) + free(ristrsvi[rijvi]); + free(ristrsvi); + return (NULL); + } + + ristrvi = _strcpy(ristrvi, rinodevi->ristrvi); + ristrsvi[riivi] = ristrvi; + } + ristrsvi[riivi] = NULL; + return (ristrsvi); +} + + +/** + * print_list - |Function|that/prints/all/elements/of/a|list_t|linked|list + * @rihvi: |Represents|the|pointer|to|first|node + * + * Return: |size|of|list + */ + +size_t print_list(const rilist_tvi *rihvi) +{ + size_t riivi = 0; + + while (rihvi) + { + _puts(convert_number(rihvi->rinumvi, 10, 0)); + _putchar(':'); + _putchar(' '); + _puts(rihvi->ristrvi ? rihvi->ristrvi : "(nil)"); + _puts("\n"); + rihvi = rihvi->rinextvi; + riivi++; + } + return (riivi); +} + +/** + * node_starts_with - |Function|that|returns| + * node|whose|string|starts|with|prefix + * @rinodevi: |Represents|the|pointer|to|list|head + * @riprefixvi: |Represents|string|to|match + * @ricvi: |Represents|the|next|character|after|prefix|to|match + * + * Return: |match|node|or|null + */ + +rilist_tvi *node_starts_with(rilist_tvi *rinodevi, char *riprefixvi, + char ricvi) +{ + char *ripvi = NULL; + + while (rinodevi) + { + ripvi = starts_with(rinodevi->ristrvi, riprefixvi); + if (ripvi && ((ricvi == -1) || (*ripvi == ricvi))) + return (rinodevi); + rinodevi = rinodevi->rinextvi; + } + return (NULL); +} + +/** + * get_node_index - |Function|that|gets|the\index/|of|\a|node + * @riheadvi: |Represents|pointer|to|list|head + * @rinodevi: |Represents|pointer|to|the|node + * + * Return: |-1, else|index|of|node + */ + +ssize_t get_node_index(rilist_tvi *riheadvi, rilist_tvi *rinodevi) +{ + size_t riivi = 0; + + while (riheadvi) + { + if (riheadvi == rinodevi) + return (riivi); + riheadvi = riheadvi->rinextvi; + riivi++; + } + return (-1); +} diff --git a/main.c b/main.c new file mode 100644 index 0000000..93224a3 --- /dev/null +++ b/main.c @@ -0,0 +1,45 @@ +#include "shell.h" + +/** + * main - Represents entry point + * @riacvi: Represents arg count + * @riavvi: Represents arg vector + * + * Return: 0 on success, else 1 on error + */ + +int main(int riacvi, char **riavvi) +{ + riinfo_tvi riinfovi[] = { RIINFO_INITVI }; + int rifdvi = 2; + + asm ("mov %1, %0\n\t" + "add $3, %0" + : "=r" (rifdvi) + : "r" (rifdvi)); + + if (riacvi == 2) + { + rifdvi = open(riavvi[1], O_RDONLY); + if (rifdvi == -1) + { + if (errno == EACCES) + exit(126); + if (errno == ENOENT) + { + _eputs(riavvi[0]); + _eputs(": 0: Can't open "); + _eputs(riavvi[1]); + _eputchar('\n'); + _eputchar(RIBUF_FLUSHVI); + exit(127); + } + return (EXIT_FAILURE); + } + riinfovi->rireadfdvi = rifdvi; + } + populate_env_list(riinfovi); + read_history(riinfovi); + hsh(riinfovi, riavvi); + return (EXIT_SUCCESS); +} diff --git a/memory.c b/memory.c new file mode 100644 index 0000000..a0b5561 --- /dev/null +++ b/memory.c @@ -0,0 +1,19 @@ +#include "shell.h" + +/** + * bfree - Function that frees a pointer and NULLs the address + * @riptrvi: Represents the address of the pointer to free + * + * Return: 0, else 1 if freed + */ + +int bfree(void **riptrvi) +{ + if (riptrvi && *riptrvi) + { + free(*riptrvi); + *riptrvi = NULL; + return (1); + } + return (0); +} diff --git a/parser.c b/parser.c new file mode 100644 index 0000000..82380ec --- /dev/null +++ b/parser.c @@ -0,0 +1,89 @@ +#include "shell.h" + +/** + * is_cmd - |Function|that|determines|if|a|file|is|an|executable|command + * @riinfovi: |Represents|the|info|struct + * @ripathvi: |Represents|the|path|to|the|file + * + * Return:|1|if|true,|0|otherwise + */ + +int is_cmd(riinfo_tvi *riinfovi, char *ripathvi) +{ + struct stat st; + + (void)riinfovi; + if (!ripathvi || stat(ripathvi, &st)) + return (0); + + if (st.st_mode & S_IFREG) + { + return (1); + } + return (0); +} + +/** + * dup_chars - |Function|that|duplicates|characters + * @ripathstrvi: |Represents|the|PATH|string + * @ristartvi: |Represents|the|starting|index + * @ristopvi: |Represents|the|stopping|index + * + * Return:|pointer|to|new|buffer + */ + +char *dup_chars(char *ripathstrvi, int ristartvi, int ristopvi) +{ + static char ribufvi[1024]; + int riivi = 0, rikvi = 0; + + for (rikvi = 0, riivi = ristartvi; riivi < ristopvi; riivi++) + if (ripathstrvi[riivi] != ':') + ribufvi[rikvi++] = ripathstrvi[riivi]; + ribufvi[rikvi] = 0; + return (ribufvi); +} + +/** + * find_path - |Function|that|finds|this|cmd|in|the|PATH|string + * @riinfovi: |Represents|the|info|struct + * @ripathstrvi: |Represents|the|PATH|string + * @ricmdvi: |Represents|the|cmd|to|find + * + * Return: |NULL|, else|full|path|of|cmd|if|found + */ + +char *find_path(riinfo_tvi *riinfovi, char *ripathstrvi, char *ricmdvi) +{ + int riivi = 0, ricurr_posvi = 0; + char *ripathvi; + + if (!ripathstrvi) + return (NULL); + if ((_strlen(ricmdvi) > 2) && starts_with(ricmdvi, "./")) + { + if (is_cmd(riinfovi, ricmdvi)) + return (ricmdvi); + } + while (1) + { + if (!ripathstrvi[riivi] || ripathstrvi[riivi] == ':') + { + ripathvi = dup_chars(ripathstrvi, ricurr_posvi, riivi); + if (!*ripathvi) + _strcat(ripathvi, ricmdvi); + else + { + _strcat(ripathvi, "/"); + _strcat(ripathvi, ricmdvi); + } + if (is_cmd(riinfovi, ripathvi)) + return (ripathvi); + if (!ripathstrvi[riivi]) + break; + ricurr_posvi = riivi; + } + riivi++; + } + return (NULL); +} diff --git a/string.c b/string.c new file mode 100644 index 0000000..1a8e0da --- /dev/null +++ b/string.c @@ -0,0 +1,79 @@ +#include "shell.h" + +/** + * _strlen -|r|e|t|u|r|n|s|t|h|e|l|e|n|g|t|h|o|f|a|s|t|r|i|n|g| + * @risvi:|t|h|e|s|t|r|i|n|g|w|h|o|s|e|l|e|n|g|t|h|t|o|c|h|e|c|k| + * + * Return:|i|n|t|e|g|e|r|l|e|n|g|t|h|o|f|s|t|r|i|n|g| + */ +int _strlen(char *risvi) +{ + int riivi = 0; + + if (!risvi) + return (0); + + while (*risvi++) + riivi++; + return (riivi); +} + +/** + * _strcmp -|p|e|r|f|o|r|m|s|l|e|x|i|c|o|g|a|r|p|h|i|c| + * c|o|m|p|a|r|i|s|o|n|o|f|t|w|o|s|t|r|a|n|g|s|. + * @ris1vi:|t|h|e|f|i|r|s|t|s|t|r|a|n|g| + * @ris2vi:|t|h|e|s|e|c|o|n|d|s|t|r|a|n|g| + * + * Return:|n|e|g|a|t|i|v|e|i|f|s1 < s2|, + * |p|o|s|i|t|i|v|e|i|f|s1 > s2|,|z|e|r|o|i|f|s1 == s2| + */ +int _strcmp(char *ris1vi, char *ris2vi) +{ + while (*ris1vi && *ris2vi) + { + if (*ris1vi != *ris2vi) + return (*ris1vi - *ris2vi); + ris1vi++; + ris2vi++; + } + if (*ris1vi == *ris2vi) + return (0); + else + return (*ris1vi < *ris2vi ? -1 : 1); +} + +/** + * starts_with -|c|h|e|c|k|s|i|f|n|e|e|d|l|e| + * s|t|a|r|t|s|w|i|t|h|h|a|y|s|t|a|c|k| + * @rihaystackvi:|s|t|r|i|n|g|t|o|s|e|a|r|c|h| + * @rineedlevi:|t|h|e|s|u|b|s|t|r|i|n|g|t|o|f|i|n|d| + * + * Return:|a|d|d|r|e|s|s|o|f| + * n|e|x|t|char|o|f|h|a|y|s|t|a|c|k|o|r|NULL| + */ +char *starts_with(const char *rihaystackvi, const char *rineedlevi) +{ + while (*rineedlevi) + if (*rineedlevi++ != *rihaystackvi++) + return (NULL); + return ((char *)rihaystackvi); +} + +/** + * _strcat -|c|o|n|c|a|t|e|n|a|t|e|s|t|w|o|s|t|r|i|n|g|s| + * @ridestvi:|t|h|e|d|e|s|t|i|n|a|t|i|o|n|b|u|f|f|e|r| + * @risrcvi:|t|h|e|s|o|u|r|c|e|b|u|f|f|e|r| + * + * Return:|p|o|i|n|t|e|r|t|o||d|e|s|t|i|n|a|t|i|o|n|b|u|f|f|e|r| + */ +char *_strcat(char *ridestvi, char *risrcvi) +{ + char *riretvi = ridestvi; + + while (*ridestvi) + ridestvi++; + while (*risrcvi) + *ridestvi++ = *risrcvi++; + *ridestvi = *risrcvi; + return (riretvi); +} diff --git a/string1.c b/string1.c new file mode 100644 index 0000000..48e42d3 --- /dev/null +++ b/string1.c @@ -0,0 +1,88 @@ +#include "shell.h" + +/** + * _strcpy -|c|o|p|i|e|s|a|s|t|r|i|n|g| + * @ridestvi:|t|h|e|d|e|s|t|i|n|a|t|i|o|n| + * @risrcvi:|t|h|e|s|o|u|r|c|e| + * + * Return:|p|o|i|n|t|e|r|t|o|d|e|s|t|i|n|a|t|i|o|n| + */ +char *_strcpy(char *ridestvi, char *risrcvi) +{ + int riivi = 0; + + if (ridestvi == risrcvi || risrcvi == 0) + return (ridestvi); + while (risrcvi[riivi]) + { + ridestvi[riivi] = risrcvi[riivi]; + riivi++; + } + ridestvi[riivi] = 0; + return (ridestvi); +} + +/** + * _strdup -|d|u|p|l|i|c|a|t|e|s|a|s|t|r|i|n|g| + * @ristrvi:|t|h|e||s|t|r|i|n|g|t|o|d|u|p|l|i|c|a|t|e| + * + * Return:|p|o|i|n|t|e|r||t|o|| + * t|h|e|d|u|p|l|i|c|a|t|e|d|s|t|r|i|n|g| + */ +char *_strdup(const char *ristrvi) +{ + int rilengthvi = 0; + char *riretvi; + + if (ristrvi == NULL) + return (NULL); + while (*ristrvi++) + rilengthvi++; + riretvi = malloc(sizeof(char) * (rilengthvi + 1)); + if (!riretvi) + return (NULL); + for (rilengthvi++; rilengthvi--;) + riretvi[rilengthvi] = *--ristrvi; + return (riretvi); +} + +/** + * _puts - prints an input string + * @ristrvi: the string to be printed + * + * Return: Nothing + */ +void _puts(char *ristrvi) +{ + int riivi = 0; + + if (!ristrvi) + return; + while (ristrvi[riivi] != '\0') + { + _putchar(ristrvi[riivi]); + riivi++; + } +} + +/** + * _putchar - writes the character c to stdout + * @ricvi: The character to print + * + * Return: On success 1. + * On error, -1 is returned, and errno is set appropriately. + */ +int _putchar(char ricvi) +{ + static int riivi; + static char ribufvi[RIWRITE_BUF_SIZEVI]; + + if (ricvi == RIBUF_FLUSHVI || riivi >= RIWRITE_BUF_SIZEVI) + { + write(1, ribufvi, riivi); + riivi = 0; + } + if (ricvi != RIBUF_FLUSHVI) + ribufvi[riivi++] = ricvi; + return (1); +} diff --git a/tokenizer.c b/tokenizer.c new file mode 100644 index 0000000..d521f78 --- /dev/null +++ b/tokenizer.c @@ -0,0 +1,100 @@ +#include "shell.h" + +/** + * **strtow -|s|p|l|i|t|s|a|s|t|r|i|n|g|i|n|t|o|w|o|r|d|s|. + * |R|e|p|e|a|t|d|e|l|i|m|i|t|e|r|s|a|r|e|i|g|n|o|r|e|d| + * @ristrvi: |t|h|e|i|n|p|u|t|s|t|r|i|n|g| + * @ridvi:|t|h|e|d|e|l|i|m|e|t|e|r|s|t|r|i|n|g| + * Return:|a|p|o|i|n|t|e|r|t|o|a|n|a|r|r|a|y| + * o|f|s|t|r|i|n|g|s|,|o|r|N|U|L|L|o|n|f|a|i|l|u|r|e| + */ + +char **strtow(char *ristrvi, char *ridvi) +{ + int riivi, rijvi, rikvi, rimvi, rinumwordsvi = 0; + char **risvi; + + if (ristrvi == NULL || ristrvi[0] == 0) + return (NULL); + if (!ridvi) + ridvi = " "; + for (riivi = 0; ristrvi[riivi] != '\0'; riivi++) + if (!is_delim(ristrvi[riivi], ridvi) && (is_delim(ristrvi[riivi + 1], + ridvi) || !ristrvi[riivi + 1])) + rinumwordsvi++; + + if (rinumwordsvi == 0) + return (NULL); + risvi = malloc((1 + rinumwordsvi) * sizeof(char *)); + if (!risvi) + return (NULL); + for (riivi = 0, rijvi = 0; rijvi < rinumwordsvi; rijvi++) + { + while (is_delim(ristrvi[riivi], ridvi)) + riivi++; + rikvi = 0; + while (!is_delim(ristrvi[riivi + rikvi], ridvi) && ristrvi[riivi + rikvi]) + rikvi++; + risvi[rijvi] = malloc((rikvi + 1) * sizeof(char)); + if (!risvi[rijvi]) + { + for (rikvi = 0; rikvi < rijvi; rikvi++) + free(risvi[rikvi]); + free(risvi); + return (NULL); + } + for (rimvi = 0; rimvi < rikvi; rimvi++) + risvi[rijvi][rimvi] = ristrvi[riivi++]; + risvi[rijvi][rimvi] = 0; + } + risvi[rijvi] = NULL; + return (risvi); +} + +/** + * **strtow2 -|s|p|l|i|t|s|a|s|t|r|i|n|g|i|n|t|o|w|o|r|d|s| + * @ristrvi:|t|h|e|i|n|p|u|t|s|t|r|i|n|g| + * @ridvi:|t|h|e|d|e|l|i|m|e|t|e|r| + * Return:|a|p|o|i|n|t|e|r|t|o|a|n|a|r|r|a|y| + * o|f|s|t|r|i|n|g|s|,|o|r|N|U|L|L|o|n|f|a|i|l|u|r|e| + */ +char **strtow2(char *ristrvi, char ridvi) +{ + int riivi, rijvi, rikvi, rimvi, rinumwordsvi = 0; + char **risvi; + + if (ristrvi == NULL || ristrvi[0] == 0) + return (NULL); + for (riivi = 0; ristrvi[riivi] != '\0'; riivi++) + if ((ristrvi[riivi] != ridvi && ristrvi[riivi + 1] == ridvi) || + (ristrvi[riivi] != ridvi && !ristrvi[riivi + 1]) || + ristrvi[riivi + 1] == ridvi) + rinumwordsvi++; + if (rinumwordsvi == 0) + return (NULL); + risvi = malloc((1 + rinumwordsvi) * sizeof(char *)); + if (!risvi) + return (NULL); + for (riivi = 0, rijvi = 0; rijvi < rinumwordsvi; rijvi++) + { + while (ristrvi[riivi] == ridvi && ristrvi[riivi] != ridvi) + riivi++; + rikvi = 0; + while (ristrvi[riivi + rikvi] != ridvi && ristrvi[riivi + rikvi] && + ristrvi[riivi + rikvi] != ridvi) + rikvi++; + risvi[rijvi] = malloc((rikvi + 1) * sizeof(char)); + if (!risvi[rijvi]) + { + for (rikvi = 0; rikvi < rijvi; rikvi++) + free(risvi[rikvi]); + free(risvi); + return (NULL); + } + for (rimvi = 0; rimvi < rikvi; rimvi++) + risvi[rijvi][rimvi] = ristrvi[riivi++]; + risvi[rijvi][rimvi] = 0; + } + risvi[rijvi] = NULL; + return (risvi); +}