[C] fgets problem with SIGINT singlal!!!


 
Thread Tools Search this Thread
Top Forums Programming [C] fgets problem with SIGINT singlal!!!
# 1  
Old 01-01-2009
[C] fgets problem with SIGINT singlal!!!

Hi all,

I have this method to read a string from a STDIN:

PHP Code:
void readLine(charinputBuffer){

    
fgets (inputBufferMAX_LINEstdin);
    
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:

PHP Code:
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
# 2  
Old 01-02-2009
Code:
#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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Always pass SIGINT in ksh

The following command will run and wait for input from the user. /usr/sap/SAP/webdisp/wdispmon pf=/usr/sap/SAP/webdisp/profile What I would like to do is (in one command): - Add the above line to a ksh script - Receive the output - and send a SIGINT I have seen many posts on how to... (3 Replies)
Discussion started by: sapsid
3 Replies

2. UNIX for Dummies Questions & Answers

SIGINT issue

May i know what are the possible causes for SIGINT other than ctrl-c? Thanks (17 Replies)
Discussion started by: pandeesh
17 Replies

3. Programming

fgets problems newline

hello, i'm trying to write a C-program that reads a file line by line. (and searches each line for a given string) This file is an special ASCII-database-file, with a lot of entries. I checked the line with most length, and it was about 4000 characters. With google i found several... (4 Replies)
Discussion started by: p1cm1n
4 Replies

4. Programming

fgets problems

I've been having trouble with reading past the end-of-file in C. Can anyone find my stupid mistake? This is the minimal code needed to cause the error for me: FILE *f = fopen(name, "r"); if (!f) return; pari_sp ltop = avma; char line; while(fgets(line, 1100, f) != NULL) printf(".");... (23 Replies)
Discussion started by: CRGreathouse
23 Replies

5. Programming

why multiple SIGINT raises when i hit C-c

hi, in my application, i have set up to capture SIGINT and execute a handler.the problem is whenever i hit C-c, multiple SIGINT are sent to the application.I have blocked the SIGINT right after catching the first one but it is unsuccessful.Here is what i do : jmp_buf main_loop; int... (1 Reply)
Discussion started by: Sedighzadeh
1 Replies

6. Shell Programming and Scripting

Intercepting SIGINT in a bash script

I've written a bash script which captures video with DVgrab. Because of the nature of the tapes that I am digitizing, sometimes I want to quit capturing before the time that I set for DVgrab. When this is the case I press Ctrl-c and DVgrab exits cleanly, my problem is that there is additional... (5 Replies)
Discussion started by: Starcast
5 Replies

7. Programming

Question about NULL Character & fgets()

Assume client send the message " Hello ", i get output such as Sent mesg: hello Bytes Sent to Client: 6 bytes_received = recv(clientSockD, data, MAX_DATA, 0); if(bytes_received) { send(clientSockD, data, bytes_received, 0); data = '\0';... (2 Replies)
Discussion started by: f.ben.isaac
2 Replies

8. Programming

Problem with handling SIGINT

For a program I am designing, which involves handling the keyboard input Ctrl^c (SIGINT), it is taking ages for the program to actually recognise and perform the corresponding action whenever I run it and hit Ctrl^C at the CL. I have to do at least 3 Ctrl^Cs before the program will actually... (3 Replies)
Discussion started by: JamesGoh
3 Replies

9. Programming

Problem with fgets and rewind function ..

Hello Friends, I got stuck with fgets () & rewind() function .. Please need help.. Actually I am doing a like, The function should read lines from a txt file until the function is called.. If the data from the txt file ends then it goes to the top and then again when the function is called... (1 Reply)
Discussion started by: user_prady
1 Replies

10. Programming

fgets()

does anyone knows how to accept a command from a user.. i was wondering to use fgets(), but got no idea how to start it... (4 Replies)
Discussion started by: skanky
4 Replies
Login or Register to Ask a Question