Skip to content
Snippets Groups Projects
Commit 13f880f1 authored by Vanshika Mohan Bongade's avatar Vanshika Mohan Bongade
Browse files

Upload New File

parent 0fbf1a4f
Branches
No related tags found
No related merge requests found
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "dshlib.h"
int build_cmd_list(char *cmd_line, command_list_t *clist)
{
if (strlen(cmd_line) == 0)
{
return WARN_NO_CMDS; // Empty input
}
// Initialize command list
clist->num = 0;
memset(clist->commands, 0, sizeof(clist->commands));
char *saveptr; // For strtok_r (reentrant version of strtok)
char *token = strtok_r(cmd_line, PIPE_STRING, &saveptr);
while (token != NULL)
{
if (clist->num >= CMD_MAX)
{
return ERR_TOO_MANY_COMMANDS; // Too many commands
}
// Trim leading spaces
while (isspace((unsigned char)*token))
token++;
// Trim trailing spaces
char *end = token + strlen(token) - 1;
while (end > token && isspace((unsigned char)*end))
end--;
*(end + 1) = '\0';
// Extract command and arguments separately
char *cmd_name = strtok(token, " "); // First word is the command
char *cmd_args = strtok(NULL, ""); // Rest are arguments
if (cmd_name != NULL)
{
strncpy(clist->commands[clist->num].exe, cmd_name, EXE_MAX - 1);
if (cmd_args != NULL)
{
strncpy(clist->commands[clist->num].args, cmd_args, ARG_MAX - 1);
}
clist->num++;
}
token = strtok_r(NULL, PIPE_STRING, &saveptr); // Move to next command
}
return OK;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment