*** empty log message ***
[m17n/m17n-test.git] / fifotest1.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <ctype.h>
8
9 #define PIPE_IN "/tmp/mimdemo-in"
10 #define PIPE_OUT "/tmp/mimdemo-out"
11
12 int
13 main ()
14 {
15   FILE *in, *out;
16   mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
17   int c;
18
19   umask (0);
20   if (mkfifo (PIPE_IN, mode) < 0 && errno != EEXIST)
21     {
22       fprintf (stderr, "Failed to make a named pipe.\n");
23       exit (1);
24     }
25
26   if (mkfifo (PIPE_OUT, mode) < 0 && errno != EEXIST)
27     {
28       fprintf (stderr, "Failed to make a named pipe.\n");
29       exit (1);
30     }
31
32   do {
33     printf ("accepting...");
34     fflush (stdout);
35     in = fopen (PIPE_IN, "r");
36     printf ("\nconnecting...");
37     fflush (stdout);
38     out = fopen (PIPE_OUT, "w");
39     printf ("done\n");
40     if (! in || ! out)
41       {
42         fprintf (stderr, "Failed to open a named pipe.\n");
43         exit (1);
44       }
45     while ((c = getc (in)) != EOF && c != '!')
46       {
47         putchar (c);
48         putc (toupper (c), out);
49         fflush (out);
50       }
51     fclose (in);
52     fclose (out);
53   } while (c != '!'); 
54
55   unlink (PIPE_IN);
56   unlink (PIPE_OUT);
57   exit (0);
58 }