-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dadf0ed
Showing
24 changed files
with
2,497 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
simpleshell |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* interactive - returns true if shell is interactive mode | ||
* @info: struct address | ||
* | ||
* Return: 1 if interactive mode, 0 otherwise | ||
*/ | ||
int interactive(info_t *info) | ||
{ | ||
return (isatty(STDIN_FILENO) && info->readfd <= 2); | ||
} | ||
|
||
/** | ||
* is_delim - checks if character is a delimeter | ||
* @c: the char to check | ||
* @delim: the delimeter string | ||
* Return: 1 if true, 0 if false | ||
*/ | ||
int is_delim(char c, char *delim) | ||
{ | ||
while (*delim) | ||
if (*delim++ == c) | ||
return (1); | ||
return (0); | ||
} | ||
|
||
/** | ||
*_isalpha - checks for alphabetic character | ||
*@c: The character to input | ||
*Return: 1 if c is alphabetic, 0 otherwise | ||
*/ | ||
|
||
int _isalpha(int c) | ||
{ | ||
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) | ||
return (1); | ||
else | ||
return (0); | ||
} | ||
|
||
/** | ||
*_atoi - converts a string to an integer | ||
*@s: the string to be converted | ||
*Return: 0 if no numbers in string, converted number otherwise | ||
*/ | ||
|
||
int _atoi(char *s) | ||
{ | ||
int i, sign = 1, flag = 0, output; | ||
unsigned int result = 0; | ||
|
||
for (i = 0; s[i] != '\0' && flag != 2; i++) | ||
{ | ||
if (s[i] == '-') | ||
sign *= -1; | ||
|
||
if (s[i] >= '0' && s[i] <= '9') | ||
{ | ||
flag = 1; | ||
result *= 10; | ||
result += (s[i] - '0'); | ||
} | ||
else if (flag == 1) | ||
flag = 2; | ||
} | ||
|
||
if (sign == -1) | ||
output = -result; | ||
else | ||
output = result; | ||
|
||
return (output); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include "shell.h" | ||
|
||
/** | ||
* _myexit - exits the shell | ||
* @info: Structure containing potential arguments. Used to maintain | ||
* constant function prototype. | ||
* Return: exits with a given exit status | ||
* (0) if info.argv[0] != "exit" | ||
*/ | ||
int _myexit(info_t *info) | ||
{ | ||
int exitcheck; | ||
|
||
if (info->argv[1]) /* If there is an exit arguement */ | ||
{ | ||
exitcheck = _erratoi(info->argv[1]); | ||
if (exitcheck == -1) | ||
{ | ||
info->status = 2; | ||
print_error(info, "Illegal number: "); | ||
_eputs(info->argv[1]); | ||
_eputchar('\n'); | ||
return (1); | ||
} | ||
info->err_num = _erratoi(info->argv[1]); | ||
return (-2); | ||
} | ||
info->err_num = -1; | ||
return (-2); | ||
} | ||
|
||
/** | ||
* _mycd - changes the current directory of the process | ||
* @info: Structure containing potential arguments. Used to maintain | ||
* constant function prototype. | ||
* Return: Always 0 | ||
*/ | ||
int _mycd(info_t *info) | ||
{ | ||
char *s, *dir, buffer[1024]; | ||
int chdir_ret; | ||
|
||
s = getcwd(buffer, 1024); | ||
if (!s) | ||
_puts("TODO: >>getcwd failure emsg here<<\n"); | ||
if (!info->argv[1]) | ||
{ | ||
dir = _getenv(info, "HOME="); | ||
if (!dir) | ||
chdir_ret = /* TODO: what should this be? */ | ||
chdir((dir = _getenv(info, "PWD=")) ? dir : "/"); | ||
else | ||
chdir_ret = chdir(dir); | ||
} | ||
else if (_strcmp(info->argv[1], "-") == 0) | ||
{ | ||
if (!_getenv(info, "OLDPWD=")) | ||
{ | ||
_puts(s); | ||
_putchar('\n'); | ||
return (1); | ||
} | ||
_puts(_getenv(info, "OLDPWD=")), _putchar('\n'); | ||
chdir_ret = /* TODO: what should this be? */ | ||
chdir((dir = _getenv(info, "OLDPWD=")) ? dir : "/"); | ||
} | ||
else | ||
chdir_ret = chdir(info->argv[1]); | ||
if (chdir_ret == -1) | ||
{ | ||
print_error(info, "can't cd to "); | ||
_eputs(info->argv[1]), _eputchar('\n'); | ||
} | ||
else | ||
{ | ||
_setenv(info, "OLDPWD", _getenv(info, "PWD=")); | ||
_setenv(info, "PWD", getcwd(buffer, 1024)); | ||
} | ||
return (0); | ||
} | ||
|
||
/** | ||
* _myhelp - changes the current directory of the process | ||
* @info: Structure containing potential arguments. Used to maintain | ||
* constant function prototype. | ||
* Return: Always 0 | ||
*/ | ||
int _myhelp(info_t *info) | ||
{ | ||
char **arg_array; | ||
|
||
arg_array = info->argv; | ||
_puts("help call works. Function not yet implemented \n"); | ||
if (0) | ||
_puts(*arg_array); /* temp att_unused workaround */ | ||
return (0); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#include "shell.h" | ||
|
||
|
||
/** | ||
* _myhistory - displays the history list, one command by line, preceded | ||
* with line numbers, starting at 0. | ||
* @info: Structure containing potential arguments. Used to maintain | ||
* constant function prototype. | ||
* Return: Always 0 | ||
*/ | ||
int _myhistory(info_t *info) | ||
{ | ||
print_list(info->history); | ||
return (0); | ||
} | ||
|
||
/** | ||
* unset_alias - sets alias to string | ||
* @info: parameter struct | ||
* @str: the string alias | ||
* | ||
* Return: Always 0 on success, 1 on error | ||
*/ | ||
int unset_alias(info_t *info, char *str) | ||
{ | ||
char *p, c; | ||
int ret; | ||
|
||
p = _strchr(str, '='); | ||
if (!p) | ||
return (1); | ||
c = *p; | ||
*p = 0; | ||
ret = delete_node_at_index(&(info->alias), | ||
get_node_index(info->alias, node_starts_with(info->alias, str, -1))); | ||
*p = c; | ||
return (ret); | ||
} | ||
|
||
/** | ||
* set_alias - sets alias to string | ||
* @info: parameter struct | ||
* @str: the string alias | ||
* | ||
* Return: Always 0 on success, 1 on error | ||
*/ | ||
int set_alias(info_t *info, char *str) | ||
{ | ||
char *p; | ||
|
||
p = _strchr(str, '='); | ||
if (!p) | ||
return (1); | ||
if (!*++p) | ||
return (unset_alias(info, str)); | ||
|
||
unset_alias(info, str); | ||
return (add_node_end(&(info->alias), str, 0) == NULL); | ||
} | ||
|
||
/** | ||
* print_alias - prints an alias string | ||
* @node: the alias node | ||
* | ||
* Return: Always 0 on success, 1 on error | ||
*/ | ||
int print_alias(list_t *node) | ||
{ | ||
char *p = NULL, *a = NULL; | ||
|
||
if (node) | ||
{ | ||
p = _strchr(node->str, '='); | ||
for (a = node->str; a <= p; a++) | ||
_putchar(*a); | ||
_putchar('\''); | ||
_puts(p + 1); | ||
_puts("'\n"); | ||
return (0); | ||
} | ||
return (1); | ||
} | ||
|
||
/** | ||
* _myalias - mimics the alias builtin (man alias) | ||
* @info: Structure containing potential arguments. Used to maintain | ||
* constant function prototype. | ||
* Return: Always 0 | ||
*/ | ||
int _myalias(info_t *info) | ||
{ | ||
int i = 0; | ||
char *p = NULL; | ||
list_t *node = NULL; | ||
|
||
if (info->argc == 1) | ||
{ | ||
node = info->alias; | ||
while (node) | ||
{ | ||
print_alias(node); | ||
node = node->next; | ||
} | ||
return (0); | ||
} | ||
for (i = 1; info->argv[i]; i++) | ||
{ | ||
p = _strchr(info->argv[i], '='); | ||
if (p) | ||
set_alias(info, info->argv[i]); | ||
else | ||
print_alias(node_starts_with(info->alias, info->argv[i], '=')); | ||
} | ||
|
||
return (0); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include "shell.h" | ||
|
||
|
||
/** | ||
* _myenv - prints the current environment | ||
* @info: Structure containing potential arguments. Used to maintain | ||
* constant function prototype. | ||
* Return: Always 0 | ||
*/ | ||
int _myenv(info_t *info) | ||
{ | ||
print_list_str(info->env); | ||
return (0); | ||
} | ||
|
||
/** | ||
* _getenv - gets the value of an environ variable | ||
* @info: Structure containing potential arguments. Used to maintain | ||
* @name: env var name | ||
* | ||
* Return: the value | ||
*/ | ||
char *_getenv(info_t *info, const char *name) | ||
{ | ||
list_t *node = info->env; | ||
char *p; | ||
|
||
while (node) | ||
{ | ||
p = starts_with(node->str, name); | ||
if (p && *p) | ||
return (p); | ||
node = node->next; | ||
} | ||
return (NULL); | ||
} | ||
|
||
/** | ||
* _mysetenv - Initialize a new environment variable, | ||
* or modify an existing one | ||
* @info: Structure containing potential arguments. Used to maintain | ||
* constant function prototype. | ||
* Return: Always 0 | ||
*/ | ||
int _mysetenv(info_t *info) | ||
{ | ||
if (info->argc != 3) | ||
{ | ||
_eputs("Incorrect number of arguements\n"); | ||
return (1); | ||
} | ||
if (_setenv(info, info->argv[1], info->argv[2])) | ||
return (0); | ||
return (1); | ||
} | ||
|
||
/** | ||
* _myunsetenv - Remove an environment variable | ||
* @info: Structure containing potential arguments. Used to maintain | ||
* constant function prototype. | ||
* Return: Always 0 | ||
*/ | ||
int _myunsetenv(info_t *info) | ||
{ | ||
int i; | ||
|
||
if (info->argc == 1) | ||
{ | ||
_eputs("Too few arguements.\n"); | ||
return (1); | ||
} | ||
for (i = 1; i <= info->argc; i++) | ||
_unsetenv(info, info->argv[i]); | ||
|
||
return (0); | ||
} | ||
|
||
/** | ||
* populate_env_list - populates env linked list | ||
* @info: Structure containing potential arguments. Used to maintain | ||
* constant function prototype. | ||
* Return: Always 0 | ||
*/ | ||
int populate_env_list(info_t *info) | ||
{ | ||
list_t *node = NULL; | ||
size_t i; | ||
|
||
for (i = 0; environ[i]; i++) | ||
add_node_end(&node, environ[i], 0); | ||
info->env = node; | ||
return (0); | ||
} | ||
|
Oops, something went wrong.