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