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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include "gis.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
/**********************************************************
* G_gets (buf)
* char *buf buffer to receive data
*
* does a gets() from stdin. exits upon EOF.
* if stdin is a tty (ie, not a pipe or redirected)
* then ctrl-z is detected
*
* returns
* 1 read ok
* 0 ctrl-z entered. calling routine should re-print a prompt
* and call G_gets() again
*
* note: This is very useful for allowing a program to
* reprompt after the program is restarted after
* being stopped with a ctrl-Z.
*
* sample use:
* do {
* fprintf (stderr, "Enter some input: ") ;
* } while ( ! G_gets(buff) ) ;
*
* If the user suspends the process at this prompt G_gets will return
* "0" causing the reprompting.
***********************************************************/
static int ctrlz = 0;
static void catch_ctrlz (int);
static void catch_int (int);
int G_gets (char *buf)
{
void (*sigtstp)();
int tty;
char p[128];
char *eof;
ctrlz = 0;
#ifdef SIGTSTP
if (tty = isatty(0))
{
sigtstp = signal (SIGTSTP, catch_ctrlz);
if (sigtstp != (void (*)()) SIG_DFL)
signal (SIGTSTP, sigtstp);
}
#endif
eof = fgets(p,100,stdin);
/* strip the EOL character */
p[strlen(p)-1]='\0';
/* buf could be any length. Any overflow will occur here. */
strcpy(buf,p);
#ifdef SIGTSTP
if (tty)
signal (SIGTSTP, sigtstp);
#endif
if (eof)
return 1;
if (ctrlz)
return 0;
exit(1);
}
static void catch_ctrlz (int n)
{
#ifdef __MINGW32__
G_warning ( "catch_ctrlz: ignored Ctrl-z" );
#else
void (*sigint)();
/* having caught ctrlz - effect a ctrl-z using kill */
ctrlz = 1;
signal (n, SIG_DFL);
kill (0, n);
/* for berkley systems, ctrlz will not cause eof on read */
sigint = signal (SIGINT, catch_int);
kill (getpid(), SIGINT);
signal (SIGINT, sigint);
#endif
}
static void catch_int (int n)
{
}
|