I'm building a shell for an Operating Systems class that must use exec() or one of its variants to execute external commands. Currently, I'm using execlp(command,command_parameters, (char *) NULL)
. This runs the command just fine (e.g. ls
returns a standard directory listing), but doesn't seem to parse any of the parameters (e.g. running mkdir hello
throws an error "hello: missing operand... Try 'hello --help' for more information). What am I missing?
else // Try to handle an external command
{
char *command_parameters = malloc(sizeof(raw_command)-sizeof(comman开发者_Go百科d));
strcpy(command_parameters, raw_command+strlen(command)+1);
pmesg(1, "Command is %s.\n", command);
pmesg(1, "The command parameters are %s.\n", command_parameters);
pid_t pid = fork();
pmesg(1, "Process forked. ID = %i. \n", pid);
int status;
if (fork < 0)
{
printf("Could not fork a process to complete the external command.\n");
exit(EXIT_FAILURE);
}
if (pid == 0) // This is the child process
{
pmesg(1, "This is the child process, running execlp.\n");
if (execlp(command, command_parameters, (char *) NULL) < 0)
{
printf("Could not execute the external command.\n");
exit(EXIT_FAILURE);
}
else { pmesg(1, "Executed the child process.\n"); }
}
else {while(wait(&status) != pid); } // Wait for the child to finish executing
pmesg(1, "The child has finished executing.\n");
}
(pmesg
is a debug tag that prints the statement given a certain debug level).
Thanks!
A couple of issues here:
execlp( const char *file, const char *arg, ...)
expects the arguments to be split up and passed separately, not as one big string.- The first arg (after
const char *file
) is by convention, the name of the executable you're running, which gets put intoargv[0]
in the called program. The first parameter, thus, will need to go after that.
e.g.:
execlp( command, command, arg1, arg2, ..., (char *)NULL );
With what you have, doing it like:
execlp( command, command, command_parameters, (char *)NULL );
will probably, as-is, take care of your issue with "mkdir", "hello"
, but you're still not splitting the command_parameters
string up, so it won't work without modification for commands with more than one argument.
EDIT: P.S. Your line
if (fork < 0)
should be
if (pid < 0)
精彩评论