Writing my own shell - trouble with handling ctrl+c (SIGINT) correctly
<p>I am writing my own simple shell. One thing which I need to do is to control the SIGINT signal by remaining in the shell and just printing the prompt on a fresh line when ctrl+c is pressed. Currently, I have been able to handle the signal and the shell simply prints <code>^C</code> after the prompt. However, the cursor remains on the same line. What I would like to do instead is have the shell print <code>^C</code> after the prompt, move onto the next line and then print a fresh prompt.</p>

<p>I have found <a href="https://stackoverflow.com/questions/20109424/handling-ctrl-c-in-dummy-shell">this question</a>, which addresses the exact same issue. The problem with mine is that my <code>main</code> calls another function where the prompt loop is run. I have tried many different ways to try to implement the solution given on the link above, in both the main and the prompt loop function, but all with no luck. Here is my code so far:</p>

<p><strong>Main.c</strong></p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;signal.h&gt;
#include "MyShell.h"

void ctrl_C_handler();

int main(int argc, char **argv) {

    signal(SIGINT, ctrl_C_handler);
    my_shell_loop();

    return EXIT_SUCCESS;
}

void ctrl_C_handler() { 
    //Catches the SIGINT signal fine without anything happening in this function
    //I cannot figure out how to have MyShell print a fresh prompt on a new line 
    //after ctrl+C is pressed
}
</code></pre>

<p><strong>MyShellLoop.c</strong></p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;time.h&gt;
#include "MyShell.h"

char *get_time();

void my_shell_loop() {
    char *line;
    char **args;
    int status;

    char *prompt = (char *) malloc(17);

    do {
        strcpy(prompt, get_time());
        strcat(prompt, " # ");
        printf("%s", prompt);
        line = read_command();
        args = split_command(line);
        status = execute_command(args);

        free(line);
        free(args);
    } while (status);

    free(prompt);
}
</code></pre>

<p><strong>EDIT</strong></p>

<p>Using:</p>

<pre><code>void ctrl_C_handler() { 
    signal(SIGINT, ctrl_C_handler);
    printf("\n");
    my_shell_loop();
}
</code></pre>

<p>acts as desired the first time ctrl+c is pressed, but then acts as it did before for any further times it is pressed.</p>