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