8265d49326d64424b8d663674a4b9d67ade8adb7
[egate.git] / util / gip.c
1 /* Gateway kernel
2  * Europagate, 1995
3  *
4  * $Log: gip.c,v $
5  * Revision 1.6  1995/05/02 15:26:52  adam
6  * EINTR obvserved on reads and writes.
7  *
8  * Revision 1.5  1995/05/01  16:27:28  adam
9  * Various improvements. Close-on-exec and close on failure on either
10  * read or write FIFO.
11  *
12  * Revision 1.4  1995/04/21  16:38:07  adam
13  * Added more debugging logs.
14  *
15  * Revision 1.3  1995/04/20  15:12:42  adam
16  * Minor hacks really.
17  *
18  * Revision 1.2  1995/03/27  12:52:18  adam
19  * A little more verbose in marc dump.
20  *
21  * Revision 1.1  1995/03/27  08:24:58  adam
22  * New module gip: Gateway IPc module.
23  * New module gw-db: Gateway hash-db module (user information table).
24  *
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <sys/file.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36
37 #include <gw-log.h>
38
39 #include <gip.h>
40
41 GIP gip_initialize (const char *name)
42 {
43     GIP gip = malloc (sizeof(*gip));
44
45     if (!gip)
46         return NULL;
47     if (!(gip->name = malloc (strlen(name)+1)))
48     {
49         free (gip);
50         return NULL;
51     }
52     strcpy (gip->name, name);
53     gip->ret = mknod (gip->name, S_IFIFO|0666, 0);
54     gip->errno = errno;
55     gip->rfd = gip->wfd = -1;
56     gw_log (GW_LOG_DEBUG, "gip", "Creating %s", gip->name);
57     return gip;
58 }
59
60 int gip_destroy (GIP gip)
61 {
62     assert (gip);
63
64     free (gip->name);
65     free (gip);
66     return 0;
67 }
68
69 int gip_infileno (GIP gip)
70 {
71     return gip->rfd;
72 }
73
74 int gip_outfileno (GIP gip)
75 {
76     return gip->wfd;
77 }
78
79 int gip_errno (GIP gip)
80 {
81     return gip->errno;
82 }
83
84 int gip_read (GIP gip, char *buf, size_t count)
85 {
86     int r, no = 0;
87     while (no < count)
88     {
89         while (1)
90         {
91             r = read (gip->rfd, buf+no, count-no);
92             if (r != -1)
93                 break;
94             if (errno != EINTR)
95             {
96                 gip->errno = errno;
97                 return -1;
98             }
99         }
100         no += r;
101     }
102     return 0;
103 }
104
105 int gip_write (GIP gip, const char *buf, size_t count)
106 {
107     int r, no = 0;
108     while (no < count)
109     {
110         while (1)
111         {
112             r = write (gip->wfd, buf+no, count-no);
113             if (r != -1)
114                 break;
115             if (errno != EINTR)
116             {
117                 gip->errno = errno;
118                 return -1;
119             }
120         }
121         no += r;
122     }
123     return 0;
124 }
125
126 int gip_wline (GIP gip, const char *buf)
127 {
128     return gip_write (gip, buf, strlen(buf));
129 }
130