Skip to content
Snippets Groups Projects
Commit e9cab772 authored by Joey Le's avatar Joey Le
Browse files

HOPEFULLY THE BUGS ARE FIXED

parent 0e5f2a1f
Branches
No related tags found
No related merge requests found
......@@ -33,15 +33,14 @@
* memset(), strcmp(), strcpy(), strtok(), strlen(), strchr()
*/
int build_cmd_list(char *cmd_line, command_list_t *clist)
{
int build_cmd_list(char *cmd_line, command_list_t *clist) {
if (cmd_line == NULL) {
return ERR_CMD_OR_ARGS_TOO_BIG;
}
//This code here is for trimming the lines
// Trim leading and trailing spaces
char *trimmed_line = strdup(cmd_line);
if (trimmed_line == NULL) {
return ERR_CMD_OR_ARGS_TOO_BIG;
}
......@@ -52,67 +51,38 @@ int build_cmd_list(char *cmd_line, command_list_t *clist)
trimmed_line[--len] = '\0';
}
clist->commands = (command_t *)malloc(CMD_MAX * sizeof(command_t));
if (clist->commands == NULL) {
free(trimmed_line);
return ERR_CMD_OR_ARGS_TOO_BIG;
}
int command_count = 0;
char *command = strtok(trimmed_line, PIPE_STRING);
//STEPS
//Split the command into even more parts
//The first one will be the exectuble
//and if they're more than they will be the arguements
//Populate it using strcpy()
while (command != NULL && command_count < CMD_MAX) {
memset(&clist->commands[command_count], 0, sizeof(command_t));
char *command_and_arguments = strtok(command, SPACE_CHAR);
if (command_and_arguments != NULL) {
if (strlen(command_and_arguments) >= EXE_MAX) {
free(trimmed_line);
free(clist->commands);
return ERR_CMD_OR_ARGS_TOO_BIG;
}
strcpy(clist->commands[command_count].exe, command_and_arguments);
int argument_count = 0;
while ((command_and_arguments = strtok(NULL, SPACE_CHAR)) != NULL) {
if (strlen(command_and_arguments) >= ARG_MAX) {
free(trimmed_line);
free(clist->commands);
return ERR_CMD_OR_ARGS_TOO_BIG;
}
// Copy the argument
strcpy(clist->commands[command_count].args[argument_count], command_and_arguments);
argument_count++;
}
// Null-terminate the arguments list
clist->commands[command_count].args[argument_count] = NULL;
clist->commands[command_count].args[argument_count] = NULL; // Terminate arguments list
}
// Move to the next command
command_count++;
command = strtok(NULL, PIPE_STRING);
}
if (command != NULL) {
free(trimmed_line);
free(clist->commands);
return ERR_TOO_MANY_COMMANDS;
}
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment