add db.1.85
[nvi.git] / ipc / ip_send.c
blob843ca86f60109a4f6a58b5a4c5f36be2d0040f82
1 /*-
2 * Copyright (c) 1996
3 * Keith Bostic. All rights reserved.
5 * See the LICENSE file for redistribution information.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "$Id: ip_send.c,v 8.10 2001/06/25 15:19:25 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:25 $";
12 #endif /* not lint */
14 #include <sys/types.h>
15 #include <sys/queue.h>
17 #include <bitstring.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
23 #include "../common/common.h"
24 #include "ip.h"
27 * vi_send --
28 * Construct and send an IP buffer.
30 * PUBLIC: int vi_send __P((int, char *, IP_BUF *));
32 int
33 vi_send(int ofd, char *fmt, IP_BUF *ipbp)
35 static char *bp;
36 static size_t blen;
37 size_t off;
38 u_int32_t ilen;
39 int nlen, n, nw;
40 char *p;
43 * Have not created the channel to vi yet? -- RAZ
45 * XXX
46 * How is that possible!?!?
48 * We'll soon find out.
50 if (ofd == 0) {
51 fprintf(stderr, "No channel\n");
52 abort();
55 if (blen == 0 && (bp = malloc(blen = 512)) == NULL)
56 return (1);
58 p = bp;
59 nlen = 0;
60 *p++ = ipbp->code;
61 nlen += IPO_CODE_LEN;
63 if (fmt != NULL)
64 for (; *fmt != '\0'; ++fmt)
65 switch (*fmt) {
66 case '1': /* Value #1. */
67 ilen = htonl(ipbp->val1);
68 goto value;
69 case '2': /* Value #2. */
70 ilen = htonl(ipbp->val2);
71 goto value;
72 case '3': /* Value #3. */
73 ilen = htonl(ipbp->val3);
74 value: nlen += IPO_INT_LEN;
75 if (nlen >= blen) {
76 blen = blen * 2 + nlen;
77 off = p - bp;
78 if ((bp = realloc(bp, blen)) == NULL)
79 return (1);
80 p = bp + off;
82 memcpy(p, &ilen, IPO_INT_LEN);
83 p += IPO_INT_LEN;
84 break;
85 case 'a': /* String #1. */
86 case 'b': /* String #2. */
87 ilen = *fmt == 'a' ? ipbp->len1 : ipbp->len2;
88 nlen += IPO_INT_LEN + ilen;
89 if (nlen >= blen) {
90 blen = blen * 2 + nlen;
91 off = p - bp;
92 if ((bp = realloc(bp, blen)) == NULL)
93 return (1);
94 p = bp + off;
96 ilen = htonl(ilen);
97 memcpy(p, &ilen, IPO_INT_LEN);
98 p += IPO_INT_LEN;
99 if (*fmt == 'a') {
100 memcpy(p, ipbp->str1, ipbp->len1);
101 p += ipbp->len1;
102 } else {
103 memcpy(p, ipbp->str2, ipbp->len2);
104 p += ipbp->len2;
106 break;
108 for (n = p - bp, p = bp; n > 0; n -= nw, p += nw)
109 if ((nw = write(ofd, p, n)) < 0)
110 return (1);
111 return (0);