Saturday, 15 August 2015

Struggling with Creating a Shell in C -



Struggling with Creating a Shell in C -

i've been struggling implement basic shell. understand basic concept , i've gotten far receiving seg-fault. need implement shell doing:

prompt parse input create argv[] fork kid -> execvp() parent -> wait/waitpid()

the code have listed below , cannot life of me figure out i'm doing wrong i've looked @ numerous other places. initial thought has strtok() haven't been able pinpoint it.

int main(int argc, char *argv[]) { pid_t pid; char input[100]; char* p[20]; int numofargs; char* s; while(1){ printf("simpleshell"); fgets(input, 100, stdin); s = strtok(input, " "); while(p[numofargs] != null){ p[numofargs] = strtok (null, " "); numofargs++; } pid = fork(); if (pid == 0){ execvp(p[0],p); perror("exec failure "); } else waitpid(); } exit(0); }

notice p array of pointers char. uninitialized start, meaning each entry p[n] (i.e. each of p's 20 elements 0 <= n < 20) garbage. yet first thing test see if 1 of items null. that's crashtastic. also, haven't initialized numofargs, element of p accessing? knows. crashtastic. start fixing things.

c

No comments:

Post a Comment