Misplaced, could you please help me out with this... obviously I am having some trouble with this but it's been such a long time since I've done C. Anyways, I want to read from the command line of a shell I am making and parse each individual word accordingly into each "word" into the argv array.
I have two problems: Number one, I am not sure if I am iterating correctly to place each value in each element of the argv. Number two, I am getting a seg fault when I am running the program below. Although of course, if we fix problem 1. number 2 should be no sweat.
Code:
...
#define BUFFSIZE 128
#define ARRAYSIZE 4
...
typedef struct command_struct {
char name[BUFFSIZE];
char *argv[ARRAYSIZE];
int argc;
} command;
...
command *cmds[ARRAYSIZE];
...
Code:
void parse (char cmdline[], command *cmds)
{
/* create a pointer to the command line */
char * cmdline_ptr;
cmdline_ptr = cmdline;
char * cmd_name;
cmd_name = cmds->name;
char * cmd_argv;
cmd_argv = cmds->argv[ARRAYSIZE];
while (*cmdline_ptr != '\0')
/* while the cmdline_ptr is not equal to NULL */
{
/* write values to cmd->name and cmd->argv */
while ((*cmdline_ptr != ' ') || (*cmdline_ptr != '\t')){
*cmd_name++ = *cmdline_ptr;
*cmd_argv++ = *cmdline_ptr++;
}
}
}