Note that there are some explanatory texts on larger screens.

plurals
  1. POProgram strangely returning to the top of the loop
    primarykey
    data
    text
    <p>I would like to preface this question by saying I'm new to C and therefore terrible at it, so I apologize in advance for any blatant errors or bad style. Also, I'm not sure how to introduce the problem before showing you my code, so here it is:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; int main() { int MAX_INPUT_SIZE = 200; volatile int running = 1; while (running) { char input[MAX_INPUT_SIZE]; char *tokens[100]; const char *cmds[] = { "wait", "pwd", "cd", "exit" }; char *cmdargs[100]; printf("shell&gt; "); fgets(input, MAX_INPUT_SIZE, stdin); // remove newline character at end int nl = strlen(input) - 1; if (input[nl] == '\n') { input[nl] = '\0'; } // tokenize input string, put each token into an array char *space; space = strtok(input, " "); tokens[0] = space; int i = 1; while (space != NULL) { space = strtok(NULL, " "); tokens[i] = space; ++i; } // copy tokens after first one into string int noargscheck; if (tokens[1] != NULL) { noargscheck = 0; strcpy((char *)cmdargs, tokens[1]); for (i = 2; tokens[i] != NULL; i++) { strcat((char *)cmdargs, " "); strcat((char *)cmdargs, tokens[i]); } } else { noargscheck = 1; } // compare tokens[0] to list of internal commands int isInternal = -1; for (i = 0; i &lt; 4; i++) { if (strcmp(tokens[0], cmds[i]) == 0) { isInternal = i; } } // internal commands char wd[200]; if (isInternal != -1) { switch (isInternal) { case 0: // wait break; case 1: // pwd if (getcwd(wd, sizeof(wd)) == NULL) { perror("getcwd() error!"); } else { printf("%s\n", wd); } break; case 2: // cd if (noargscheck) { chdir("/home"); } else if (chdir((const char *)cmdargs) != 0) { perror("cd failed"); } break; case 3: // exit exit(1); break; } } else { // external commands pid_t child_pid; switch (child_pid = fork()) { case -1: perror("Fork failed"); return 1; case 0: // child printf("\nHERE\n"); // for debugging execvp(tokens[0], cmdargs); break; } } } } </code></pre> <p>When I run this code with the input <code>echo hello world</code>, the program successfully enters the <code>case 0</code> case in the second switch statement that starts with <code>switch (child_pid=fork())</code> but the output, which is unexpected, is as follows:</p> <p>OUTPUT: (including one line that shows my input at the prompt)</p> <p><code>shell&gt; echo hello world</code> (my input)</p> <p><code>shell&gt;</code> (this is the part I don't understand)</p> <p><code>HERE</code></p> <p><code>shell&gt;</code> (the program now waits here at the prompt for the next user input)</p> <p>I cannot figure out why the extra <code>shell&gt;</code> prompt is printing. Can anybody see the problem?</p> <p>EDIT: fixed the first parameter of execvp. Changed from <code>"echo"</code> (which was there because I'm silly) to <code>tokens[0]</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload