I am trying to create a shell, and am having trouble trying to fill the *argv[] array with user input. I call my parse function, and store each word into a 2d array. I tried many different ways to store it directly into *argv[], but eventually decided to try using the 2d array. Is there anyway to fill *argv[] with pointers to the strings stored in the 2d array? Or fill *argv[] with pointers to strings without use of the 2d array?
Ive tried many other things but my current attempt is the while loop after calling the parse function.
int main() {
int status, fRedirect, i;
pid_t pid;
char s[STORAGE];
char p[MAXITEM][STORAGE];
char *argv[MAXITEM];
for(;;) {
printf("\2: ");
scanf("%s", s);
parse(s, p);
while(p[i] != '\0') {
args[i] = *p[i];
}
if(p[0] == EOF)
break;
if(p[0] == '\0')
continue;
pid = fork();
if(pid == 0) {
if(execvp(*p, p) == -1) {
printf("Execution failed!\n");
exit(9);
}
}
if(pid != 0) {
wait(-1, &status, 0);
}
else {
fRedirect = open("/dev/null", O_WRONLY);
dup2(fRedirect, STDOUT_FILENO);
}
}
/*killpg(getpid(), SIGTERM);*/
printf("p2 terminated. \n");
exit(0);
}
void parse(char *s, char p[][STORAGE]) {
int i, j = 0;
while(*s != EOF && *s != '&' && *s != '\n' && *s != ';' && *s != '\0') {
for(i=0; i < strlen(s); i++)
p[j][i] = s[i];
i = i+getword(s);
j++;
}
}