Go to the documentation of this file.00001 #include <grass/gis.h>
00002 #include <unistd.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <signal.h>
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 static int ctrlz = 0;
00035 static void catch_ctrlz(int);
00036 static void catch_int(int);
00037
00038
00039 int G_gets(char *buf)
00040 {
00041 #ifdef SIGTSTP
00042 RETSIGTYPE(*sigtstp) ();
00043 int tty;
00044 #endif
00045 char p[128];
00046 char *eof;
00047
00048 ctrlz = 0;
00049 #ifdef SIGTSTP
00050 if ((tty = isatty(0))) {
00051 sigtstp = signal(SIGTSTP, catch_ctrlz);
00052 if (sigtstp != (RETSIGTYPE(*)())SIG_DFL)
00053 signal(SIGTSTP, sigtstp);
00054 }
00055 #endif
00056 eof = fgets(p, 100, stdin);
00057
00058 if (strlen(p) > 1 && p[strlen(p) - 1] == '\n' && p[strlen(p) - 2] == '\r')
00059 p[strlen(p) - 2] = '\0';
00060 else
00061 p[strlen(p) - 1] = '\0';
00062
00063 strcpy(buf, p);
00064
00065 #ifdef SIGTSTP
00066 if (tty)
00067 signal(SIGTSTP, sigtstp);
00068 #endif
00069 if (eof)
00070 return 1;
00071 if (ctrlz)
00072 return 0;
00073
00074 exit(EXIT_SUCCESS);
00075 }
00076
00077 static void catch_ctrlz(int n)
00078 {
00079 #ifdef __MINGW32__
00080 G_warning("catch_ctrlz: ignored Ctrl-z");
00081 #else
00082
00083 RETSIGTYPE(*sigint) ();
00084
00085
00086 ctrlz = 1;
00087 signal(n, SIG_DFL);
00088 kill(0, n);
00089
00090
00091 sigint = signal(SIGINT, catch_int);
00092 kill(getpid(), SIGINT);
00093 signal(SIGINT, sigint);
00094 #endif
00095 }
00096
00097 static void catch_int(int n)
00098 {
00099 }