EINTR obvserved on reads and writes.
[egate.git] / util / lgets.c
1 /* Utility: Read line from FIFO
2  * Europagate, 1995
3  *
4  * $Log: lgets.c,v $
5  * Revision 1.2  1995/05/02 15:26:52  adam
6  * EINTR obvserved on reads and writes.
7  *
8  * Revision 1.1  1995/05/01  12:43:58  adam
9  * lgets function moved from kernel to util.
10  *
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <assert.h>
16 #include <ctype.h>
17 #include <unistd.h>
18
19 #include <gw-log.h>
20
21 #include <lgets.h>
22
23 int lgets (char *buf, int max, int fd)
24 {
25     int r, no = 0;
26
27     --max;
28     while (no <= max)
29     {
30         while (1)
31         {
32             if ((r=read (fd, buf+no, 1)) == 1)
33                 break;
34             if (r == -1 && errno == EINTR)
35                 continue;
36             if (r == -1)
37                 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "lgets", "read fail");
38             else
39                 gw_log (GW_LOG_WARN, "lgets", "read eof");
40             buf[no] = '\0';
41             return 0;
42         }
43         if (buf[no] == 1)
44             return 0;     
45         if (buf[no++] == '\n')
46             break;
47     }
48     buf[no] = '\0';     
49     return 1;
50 }
51