Hi all,
I have this method to read a string from a STDIN:
void readLine(char* inputBuffer){
fgets (inputBuffer, MAX_LINE, stdin);
fflush(stdin);
/* remove '\n' char from string */
if(strlen(inputBuffer) != 0)
inputBuffer[strlen(inputBuffer)-1] = '\0';
}
All work fine but if i catch SIGINT signal (CTRL+C) with this method:
void handle_SIGNINT(){
/* here i don't want exit with program!!! */
}
i would like that "fgets's wait" was released when there is a signal....now to realese it i need press ENTER KEY!!!, how can i do in automate?
can i "simulate" the ENTER key pressed writing a particular ASCII char into STDIN???
Thanks a lot in advance, Martin
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void readLine(char *inputBuffer)
{
char *p=NULL;
*inputbuffer=0x0;
if(fgets (inputBuffer, MAX_LINE, stdin)==NULL)
{
if(feof(stdin)) return;
if(errno == EINTR)
{
*readLine=0x0; /* or do what you want to a partial read */
return;
}
/* other errors */
perror("Fatal error on stdin");
exit(1);
}
/* fflush(stdin) is NOT a defined operation
if this actually works it is "programming by accident"
*/
fflush(stdin);
/* remove '\n' char from string */
/* if the string entered is greater than MAX_LINE
then you are whacking off valid characters
if(strlen(inputBuffer) != 0)
inputBuffer[strlen(inputBuffer)-1] = '\0';
I am assuming MAX_LINE is defined by you and is not some system value
try this instead:
*/
p=strchr(inputBuffer, '\n');
if(p!=NULL)
*p=0x0;
}
fgets handles SIGINT all by itself - don't need to call a signal handler for SIGINT or leave the SIGINT handler as it is by adding code as above.