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