S
Sreekanth
Hi all,
I have implemented a timing out version of fgets function call. I am
pasting the entire code below. I have following doubts:
1. The code which I have written does it follow standard C programming
conventions. ( I am pretty familar with java styles but not with c :-(
).
2. In order to read character by character from stdin , I have made use
of termios.h function calls is it as standard portable way of doing it
or is there any other better way. I have tested it with freebsd 6.0 it
seems to work fine. Btw I got the code to do it from net
(http://www.macdonald.egate.net/CompSci/hkeyinp3.html)
3. Now is my code thread-safe?
Thanks for being so patient and please help me inorder to be a
efficient/better c-programmer.
----
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <setjmp.h>
#include <string.h>
#include <termios.h>
jmp_buf mybuf;
static void
sig_alrm(int signo)
{
longjmp(mybuf, 1);
return;
}
static struct termios stored_settings;
void
set_keypress(void)
{
struct termios new_settings;
tcgetattr(0, &stored_settings);
new_settings = stored_settings;
//setting buffer to 1 byte
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &new_settings);
return;
}
void
reset_keypress(void)
{
tcsetattr(0, TCSANOW, &stored_settings);
return;
}
char *
tgets(char *str, int size, FILE * stream, int nsec)
{
char *mytempchar;
int i = 0;
int ch;
mytempchar = (char *)malloc(sizeof(char) * size);
if (fileno(stream) == fileno(stdin))
set_keypress();
if (setjmp(mybuf) == 1)
goto done;
signal(SIGALRM,sig_alrm);
alarm(nsec);
while (i <= size - 1) {
ch = getc(stream);
if (ch != EOF) {
if (ch != '\n') {
mytempchar = (char)ch;
i = i + 1;
} else
break;
}
}
done:
if (i == 0)
mytempchar = NULL;
else
mytempchar = 0;
//printf("The gotten string is %s\n", mytempchar);
str = mytempchar;
reset_keypress();
return mytempchar;
}
int
main(void)
{
char *mystring;
int maxlen = 256;
FILE *myfile;
myfile = fopen("gen.txt","r");
mystring = (char *)malloc(sizeof(char) * maxlen);
//printf("Enter the Input String :::: ");
//mystring = tgets(mystring,maxlen,stdin,5);
mystring = tgets(mystring, maxlen,myfile , 5);
printf("%s\n", mystring);
return 0;
}
I have implemented a timing out version of fgets function call. I am
pasting the entire code below. I have following doubts:
1. The code which I have written does it follow standard C programming
conventions. ( I am pretty familar with java styles but not with c :-(
).
2. In order to read character by character from stdin , I have made use
of termios.h function calls is it as standard portable way of doing it
or is there any other better way. I have tested it with freebsd 6.0 it
seems to work fine. Btw I got the code to do it from net
(http://www.macdonald.egate.net/CompSci/hkeyinp3.html)
3. Now is my code thread-safe?
Thanks for being so patient and please help me inorder to be a
efficient/better c-programmer.
----
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <setjmp.h>
#include <string.h>
#include <termios.h>
jmp_buf mybuf;
static void
sig_alrm(int signo)
{
longjmp(mybuf, 1);
return;
}
static struct termios stored_settings;
void
set_keypress(void)
{
struct termios new_settings;
tcgetattr(0, &stored_settings);
new_settings = stored_settings;
//setting buffer to 1 byte
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VMIN] = 1;
tcsetattr(0, TCSANOW, &new_settings);
return;
}
void
reset_keypress(void)
{
tcsetattr(0, TCSANOW, &stored_settings);
return;
}
char *
tgets(char *str, int size, FILE * stream, int nsec)
{
char *mytempchar;
int i = 0;
int ch;
mytempchar = (char *)malloc(sizeof(char) * size);
if (fileno(stream) == fileno(stdin))
set_keypress();
if (setjmp(mybuf) == 1)
goto done;
signal(SIGALRM,sig_alrm);
alarm(nsec);
while (i <= size - 1) {
ch = getc(stream);
if (ch != EOF) {
if (ch != '\n') {
mytempchar = (char)ch;
i = i + 1;
} else
break;
}
}
done:
if (i == 0)
mytempchar = NULL;
else
mytempchar = 0;
//printf("The gotten string is %s\n", mytempchar);
str = mytempchar;
reset_keypress();
return mytempchar;
}
int
main(void)
{
char *mystring;
int maxlen = 256;
FILE *myfile;
myfile = fopen("gen.txt","r");
mystring = (char *)malloc(sizeof(char) * maxlen);
//printf("Enter the Input String :::: ");
//mystring = tgets(mystring,maxlen,stdin,5);
mystring = tgets(mystring, maxlen,myfile , 5);
printf("%s\n", mystring);
return 0;
}