Keeping input before CTRL-C interrupts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

void INThandler(int);             

struct sigaction act;

char data[128];

int  main(void)
{ 
   act.sa_handler = INThandler;
   sigemptyset(&act.sa_mask);
   act.sa_flags = 0;
   sigaction(SIGINT, &act, NULL);      
 
   int n;

   while((n=read(0, data, 128)) > 0)
   {
   if(data[n] = '\n') break;
   }
   
   data[n-1] ='\0';
   
   printf("%s\n", data);
   
   return 0;
}

void  INThandler(int sig)
{
  printf("\n%s\n", data);   
  printf("\nSIGNAL HANDLER CALLED\n");
}


With this program I can handle CTRL-C, but I lose whatever I input before it.
I have to use read() to do this, but I have no idea how save the input before the interrupt. Any ideas?
Line 26: Use the comparison operator '==' instead of the assignment operator '='.
Topic archived. No new replies allowed.