Sync usage with man page.
[netbsd-mini2440.git] / dist / nvi / ipc / ip_send.c
blob10b50a662f78ecc7dc29e4dec3e14ebcb84898f2
1 /* $NetBSD$ */
3 /*-
4 * Copyright (c) 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 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";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include "../common/common.h"
26 #include "ip.h"
29 * vi_send --
30 * Construct and send an IP buffer.
32 * PUBLIC: int vi_send __P((int, char *, IP_BUF *));
34 int
35 vi_send(int ofd, char *fmt, IP_BUF *ipbp)
37 static char *bp;
38 static size_t blen;
39 size_t off;
40 u_int32_t ilen;
41 int nlen, n, nw;
42 char *p;
45 * Have not created the channel to vi yet? -- RAZ
47 * XXX
48 * How is that possible!?!?
50 * We'll soon find out.
52 if (ofd == 0) {
53 fprintf(stderr, "No channel\n");
54 abort();
57 if (blen == 0 && (bp = malloc(blen = 512)) == NULL)
58 return (1);
60 p = bp;
61 nlen = 0;
62 *p++ = ipbp->code;
63 nlen += IPO_CODE_LEN;
65 if (fmt != NULL)
66 for (; *fmt != '\0'; ++fmt)
67 switch (*fmt) {
68 case '1': /* Value #1. */
69 ilen = htonl(ipbp->val1);
70 goto value;
71 case '2': /* Value #2. */
72 ilen = htonl(ipbp->val2);
73 goto value;
74 case '3': /* Value #3. */
75 ilen = htonl(ipbp->val3);
76 value: nlen += IPO_INT_LEN;
77 if (nlen >= blen) {
78 blen = blen * 2 + nlen;
79 off = p - bp;
80 if ((bp = realloc(bp, blen)) == NULL)
81 return (1);
82 p = bp + off;
84 memcpy(p, &ilen, IPO_INT_LEN);
85 p += IPO_INT_LEN;
86 break;
87 case 'a': /* String #1. */
88 case 'b': /* String #2. */
89 ilen = *fmt == 'a' ? ipbp->len1 : ipbp->len2;
90 nlen += IPO_INT_LEN + ilen;
91 if (nlen >= blen) {
92 blen = blen * 2 + nlen;
93 off = p - bp;
94 if ((bp = realloc(bp, blen)) == NULL)
95 return (1);
96 p = bp + off;
98 ilen = htonl(ilen);
99 memcpy(p, &ilen, IPO_INT_LEN);
100 p += IPO_INT_LEN;
101 if (*fmt == 'a') {
102 memcpy(p, ipbp->str1, ipbp->len1);
103 p += ipbp->len1;
104 } else {
105 memcpy(p, ipbp->str2, ipbp->len2);
106 p += ipbp->len2;
108 break;
110 for (n = p - bp, p = bp; n > 0; n -= nw, p += nw)
111 if ((nw = write(ofd, p, n)) < 0)
112 return (1);
113 return (0);