Skip to content
Draft
158 changes: 158 additions & 0 deletions alias.c
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to check your line 16 well...

Also, I'd like to believe memory is correctly handled in your code logic.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, in your addAlias() function, you keep adding to the beginning of the list which will influence how the list of aliases is printed out when the command alias is issued without arguments. Utilize the tail pointer, it also gives you an O(1) runtime.

Currently, your code logic does not account for when alias is issued without arguments. Look into that, it's supposed to print the list of aliases in that case.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare how sh prints the aliases, it doesn't include the alias keyword in the output. Handle the errors properly with the right error codes and messages. Consult with sh.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have concerns with the alias functions...

Could you share a screenshot of the result after testing this function?
Also, share the file that you used for the testing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright boss.

Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include "liteshell.h"

/**
* initAliasList - Initializer Function
*
* Description: This function initializes the alias linked list
*
* @aliasList: a pointer to already initialized linked list
*
* Return: (alias_t *) pointer to initialized aliasList on Success,
* NULL on Failure
*/

alias_t *initAliasList(alias_t *aliasList)
{
if (aliasList != NULL)
return (aliasList);

aliasList = (alias_t *) malloc(sizeof(alias_t));
if (aliasList == NULL)
return (NULL);

aliasList->head = NULL;
aliasList->tail = NULL;
aliasList->size = 0;
return (aliasList);
}

/**
* addAlias - Auxilliary Function
*
* Description: This function adds a new alias node to the alias linked list
*
* @aliasList: pointer to the alias list
*
* @name: name member of alias node to be added
*
* @value: value member of alias node to be added
*
* Return: 0 on Success, 1 on Failure
*/

int addAlias(alias_t *aliasList, const char *name, const char *value)
{
alias_node *newAlias;

newAlias = (alias_node *) malloc(sizeof(alias_node));
if (newAlias == NULL)
{
dprintf(2, "Failed to malloc\n");
return (1);
}

newAlias->name = strdup(name);
newAlias->value = strdup(value);
newAlias->next = NULL;

if (aliasList->head == NULL)
aliasList->head = newAlias;
else
aliasList->tail->next = newAlias;

aliasList->tail = newAlias;
aliasList->size++;
return (0);
}

/**
* findAlias - Auxilliary Function
*
* Description: This function locates a specific alias node in the linked list
* based on its name
*
* @aliasList: pointer to alias list
*
* @name: Name of the alias node to be found
*
* Return: (alias_node *) pointer to alias node whose name member
* matches name on Success, NULL on Failure
*/

alias_node *findAlias(alias_t *aliasList, const char *name)
{
alias_node *currAlias = aliasList->head;

while (currAlias)
{
if (strcmp(currAlias->name, name) == 0)
return (currAlias);
currAlias = currAlias->next;
}
return (NULL);
}

/**
* printAlias - Auxilliary Function
*
* Description: This is a function that prints the members of an alias node in
* the aliasList
*
* @alias: alias node to be printed
*
* @name: name member of the alias node
*
* Return: 0 on Success, 1 on Failure
*/

int printAlias(alias_node *alias, const char *name)
{
if (!alias)
{
printf("alias: %s not found\n", name);
return (1);
}
printf("%s='%s'\n", alias->name, alias->value);
return (0);
}

/**
* removeAlias - Auxilliary Function
*
* Description: This is the function that removes an alias node from an
* aliasList
*
* @aliasList: pointer to the alias list
*
* @name: name member of the alias node
*
* Return: 0 on Success, 1 on Failurw
*/

int removeAlias(alias_t *aliasList, const char *name)
{
alias_node *currAlias, *prevAlias;

if (!aliasList)
return (1);

currAlias = aliasList->head;
prevAlias = NULL;

while (currAlias)
{
if (strcmp(currAlias->name, name) == 0)
{
if (prevAlias == NULL)
aliasList->head = currAlias->next;
else
prevAlias->next = currAlias->next;

freeAlias(currAlias);
aliasList->size--;
}
prevAlias = currAlias;
currAlias = currAlias->next;
}
return (0);
}
163 changes: 163 additions & 0 deletions alias_handler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#include "liteshell.h"

/**
* tokenizeAliasArguments - Tokenizer Function
*
* Description: This function tokenizes the argument (string) based on the "="
* delimeter
*
* @argument: String argument to be tokenized
*
* Return: (char **) tokens array on Success, EXIT on Failure
*/

char **tokenizeAliasArguments(char *argument)
{
char **tokens, *equalToSign;
size_t len_name, len_value;

tokens = (char **) malloc(sizeof(char *) * 2);
if (!tokens)
{
dprintf(2, "Failed to allocate memory for tokens\n");
exit(EXIT_FAILURE);
}
tokens[0] = NULL;
tokens[1] = NULL;
equalToSign = strchr(argument, '=');
if (equalToSign)
{
len_name = equalToSign - argument;
tokens[0] = (char *) malloc(len_name + 1);
if (!tokens[0])
{
dprintf(2, "Failed to allocate memory for name token\n");
exit(EXIT_FAILURE);
}
strncpy(tokens[0], argument, len_name);
tokens[0][len_name] = '\0';
len_value = strlen(equalToSign + 1);
tokens[1] = (char *) malloc(len_value + 1);
if (!tokens[1])
{
dprintf(2, "Failed to allocate memory for value token\n");
exit(EXIT_FAILURE);
}
strncpy(tokens[1], equalToSign + 1, len_value);
tokens[1][len_value] = '\0';
}
else
{
tokens[0] = strdup(argument);
tokens[1] = NULL;
}
return (tokens);
}

/**
* handleAlias - Socket Function
*
* Description: This function acts as an interface between the user of the
* alias feature and the alias handler functions. An aliasList must have been
* initialized before using this function.
*
* @aliasList: pointer to an alias linked list
*
* @arguments: arguments for the alias command
*
* Return: 0 on Success, 1 on Failure
*/

int handleAlias(alias_t *aliasList, char **arguments)
{
int i;
char *name, *value;

if (!arguments[0])
return (printAliasList(aliasList));

for (i = 0; arguments[i]; ++i)
handleAliasCommands(aliasList, arguments[i]);
/** will handle returns */
}

/**
* handleAliasCommands - Helper Function
*
* Description: This function utilizes a tokenizer to separate the alias
* arguments into an array DS of size 2 containing the name and value pairs.
* It also calls other auxilliary functions and passes on their return values
* to handleALias()
*
* @aliasList: pointer to the alias linked list
*
* @argument: arguments for alias command
*
* Return: 0 on Success, 1 on Failure
*/

int handleAliasCommands(alias_t *aliasList, char *argument)
{
char **aliasTokens, *name, *value;
alias_node *alias;

aliasTokens = tokenizeAliasArguments(argument);

name = aliasTokens[0];
value = aliasTokens[1];

if (value == NULL)
{
alias = findAlias(aliasList, name);
return (printAlias(alias, name));
}
else
{
alias = findAlias(aliasList, name);
if (alias == NULL)
return (addAlias(aliasList, name, value));
free(alias->value);
alias->value = strdup(value);
}
return (0);
}

/**
* main - Entry Point
*
* Description: This is a test suit interface for users of the alias command
*
* Return: Always 0 on Success
*/

int main(void)
{
alias_t *aliasList = NULL;
/**
* Run the strings in this array as arguments to the alias command and
* compare with result from sh
*/
char *arguments[] = {
"pot",
"al=alias",
"ll=lampholder",
"lorem=ipsum",
"al",
"lorem",
"al=back-it-up",
NULL
};
/**char *arguments[] = {NULL};*/

aliasList = initAliasList(aliasList);

handleAlias(aliasList, arguments);
printAliasList(aliasList);
removeAlias(aliasList, "lorem");
printAliasList(aliasList);

freeAliasList(aliasList);
printf("See alias_handler.c ;)\n");

return (0);
}
Loading