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