Minor hacks really.
[egate.git] / util / gip.c
1 /* Gateway kernel
2  * Europagate, 1995
3  *
4  * $Log: gip.c,v $
5  * Revision 1.3  1995/04/20 15:12:42  adam
6  * Minor hacks really.
7  *
8  * Revision 1.2  1995/03/27  12:52:18  adam
9  * A little more verbose in marc dump.
10  *
11  * Revision 1.1  1995/03/27  08:24:58  adam
12  * New module gip: Gateway IPc module.
13  * New module gw-db: Gateway hash-db module (user information table).
14  *
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <assert.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <sys/file.h>
24 #include <sys/stat.h>
25 #include <errno.h>
26
27 #include <gip.h>
28
29 GIP gip_initialize (const char *name)
30 {
31     GIP gip = malloc (sizeof(*gip));
32
33     if (!gip)
34         return NULL;
35     if (!(gip->name = malloc (strlen(name)+1)))
36     {
37         free (gip);
38         return NULL;
39     }
40     strcpy (gip->name, name);
41     gip->ret = mknod (gip->name, S_IFIFO|0666, 0);
42     gip->errno = errno;
43     gip->rfd = gip->wfd = -1;
44     return gip;
45 }
46
47 int gip_destroy (GIP gip)
48 {
49     assert (gip);
50
51     free (gip->name);
52     free (gip);
53     return 0;
54 }
55
56 int gip_infileno (GIP gip)
57 {
58     return gip->rfd;
59 }
60
61 int gip_errno (GIP gip)
62 {
63     return gip->errno;
64 }
65
66 int gip_read (GIP gip, char *buf, size_t count)
67 {
68     int r, no = 0;
69     while (no < count)
70     {
71         r = read (gip->rfd, buf+no, count-no);
72         if (r == -1)
73         {
74             gip->errno = errno;
75             return -1;
76         }
77         no += r;
78     }
79     return 0;
80 }
81
82 int gip_write (GIP gip, const char *buf, size_t count)
83 {
84     int r, no = 0;
85     while (no < count)
86     {
87         r = write (gip->wfd, buf+no, count-no);
88         if (r == -1)
89         {
90             gip->errno = errno;
91             return -1;
92         }
93         no += r;
94     }
95     return 0;
96 }
97
98 int gip_wline (GIP gip, const char *buf)
99 {
100     return gip_write (gip, buf, strlen(buf));
101 }
102