Hi
I am trying to get a line from the user and execute the command.Here are sme parts of my program
Code:
/* A structure for command and parameters */
typedef struct Command_t {
char *name;
int argc;
char *argv[];
} Command;
......
......
/* Parses the command line and stores the result into a Command structure */
/* This function is taken from the Gary Nutt's Operating Systems book */
int parseCommand(char *cLine, Command *cmd)
{
int argc;
char **clPtr;
/* Initialization */
clPtr=&cLine; /* cLine is the command line */
argc = 0;
cmd->argv[argc]=(char *)malloc(MAX_ARG_LEN);
/* Fill argv */
while ((cmd->argv[argc]=strsep(clPtr,WHITESPACE)) != NULL)
{
cmd->argv[++argc]=(char *) malloc(MAX_ARG_LEN);
}
//cmd->argv[argc]=(char *)NULL;
/* Set the command name and argc */
cmd->argc= argc-1;
cmd->name= (char *)malloc(sizeof(cmd->argv[0]));
strcpy(cmd->name,cmd->argv[0]);
return 1;
}
......
......
/*Runs a command */
int runCommand(char *command_str,Command cmd)
{
int status;
pid_t pid;
pid=fork();
if (pid<0)
{
fprintf(stderr,"Cannot create child process!\n");
exit(1);
}
else if (pid==0)
{
if (execv(command_str,cmd.argv)==-1)
fprintf(stderr,"Cannot execute command!\n");
}
else
while (wait(&status) != pid);
return status;
}
....
....
Well. When I entered a command like "ls -l",my program just excutes /bin/ls not with the paramters.
What is wrong?