Go on, we're listening...
(What's "the input of execvp command"? How do you want to
"break the string", and where?)
-Arthur
i want to break the string like "ls -al | grep abc into individual
word "ls" "-al" "|" "grep" "abc" and put them in array like
char* arg_list[]=
{
"ls",
"-al",
"|",
"grep",
"abc"
NULL
};
a variable program will be the first element of the array
arg_list. the two variable will be use as the parameter of
procedure spawn that will execute the command entered by user.
int spawn(char* program, char** arg_list)[/QUOTE]
Thanks for the specification.
The program you want is not complicated, it just requires good
technique.
You are going to have to manage memory in order to solve this in
the most general way. In other words, if you want a solution that
will work for an arbitrary number of arguments and for arbitrary
length arguments, you need to manage memory. If you want a
solution without the drawbacks of strtok, you'll need to invent
your own simple algorithm to parse the data.
Consequently, a nice general solution, will take me too long to
write. ;-)
A simple solution using strtok and a limited number of args,
which obliterates the command is quite simple.
#include <stdio.h>
#include <string.h>
#define MAX_ARGS 10
int spawn(char *program, char **arg_list)
{
int i;
printf("spawning %s:\t", program);
i = 0;
for (i = 0; arg_list
!= 0; ++i) {
printf("%s, ", arg_list);
}
return i;
}
int main(void)
{
char test[] = "ls -al | grep abc";
char *arg_list[MAX_ARGS+1];
int i;
char *p;
p = strtok(test, " \t");
for (i = 0; i < MAX_ARGS; ++i) {
arg_list = p;
p = strtok(NULL, " \t");
}
arg_list = 0;
spawn(arg_list[0], arg_list);
puts("Finis.");
return 0;
}
But I encourage you to try to do better.