1

I know my title may be confusing, but bear with me. I'm new to signals.

I have a program that acts like the cat or more unix commands, but it has a little twist. It prints out the first x lines and then allows the user to press Ctrl-/ to print out the next x lines. I know Ctrl-/ is by default used to stop the process, but I want to override it.

I want to do something along the lines of

if (user_enters = signal(SIGQUIT, sig_function)) then print the next x lines

I'm not sure how to conditionally check if the user entered the Ctrl-/ signal

right now my code forces the user to enter n to go to the next x lines, like so...

        scanf(" %c", &next);
        if (next == 'n'){
          //loop +4 times and only print the last 4 lines
          for (i = 0; i < 4; i++){
            printf("%s", buf);
            if (i < 3){
              if (fgets(buf, sizeof(buf), file) == NULL){
                fclose(file);
                return 0;
              }
            }
          }

is there a way to do this?

Kendall Weihe
  • 2,021
  • 4
  • 27
  • 53
  • signals catching must be explictly handled by calling `signal()` (or better `sigaction()`). – hexasoft Nov 12 '15 at 18:27
  • right but how can I conditionally check if the signal has been inputted? should I put the conditional block inside the function called by `signal(sig, function)` ? – Kendall Weihe Nov 12 '15 at 19:02
  • nevermind, I can see the correct method here http://stackoverflow.com/questions/4217037/catch-ctrl-c-in-c – Kendall Weihe Nov 12 '15 at 19:05
  • yes, you have a function that's called by the signal. Depend to you then to deal with that information (as it breaks the execution flow you may have to deal with global flags). – hexasoft Nov 12 '15 at 19:07

0 Answers0