Hide message when the Vim version is too old to run the plugin.
[profile.git] / answerback.c
1 /* answerback.c version 0.1
2  * Written by John Newbigin <jn@it.swin.edu.au>
3  * GPL Version 2
4  * Compile with 'gcc -Wall -o answerback answerback.c'
5  * */
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <termios.h>
9 #include <signal.h>
10 #include <string.h>
11 int main(int argc, char **argv)
12 {
13   char *name = "ANSWERBACK";
14   char code = 5;
15   char buffer[16];
16   int r;
17   sigset_t sig, sigsave;
18   struct termios term, termsave;
19   FILE *fp;
20   if(argc == 2)
21   {
22     name = argv[1];
23   }
24   if((fp = fopen(ctermid(NULL), "r+")) == NULL)
25   {
26     perror("open");
27     return 0;
28   }
29   setbuf(fp, NULL);
30   sigemptyset(&sig);
31   sigaddset(&sig, SIGINT);
32   sigaddset(&sig, SIGTSTP);
33   sigprocmask(SIG_BLOCK, &sig, &sigsave);
34   tcgetattr(fileno(fp), &termsave);
35   term = termsave;
36   term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON);
37   term.c_cc[VMIN] = 0;
38   term.c_cc[VTIME] = 10;
39   tcsetattr(fileno(fp), TCSAFLUSH, &term);
40   write(fileno(fp), &code, 1);
41   memset(buffer, 0, sizeof(buffer));
42   r = read(fileno(fp), buffer, sizeof(buffer) - 1);
43   tcsetattr(fileno(fp), TCSAFLUSH, &termsave);
44   sigprocmask(SIG_SETMASK, &sigsave, NULL);
45   fclose(fp);
46   if(r > 0)
47   {
48     printf("%s='%s'\n", name, buffer);
49   }
50   return 0;
51 }