Home | History | Annotate | Line # | Download | only in ipc
ip_send.c revision 1.2
      1 /*	$NetBSD: ip_send.c,v 1.2 2013/11/22 15:52:05 christos Exp $	*/
      2 /*-
      3  * Copyright (c) 1996
      4  *	Keith Bostic.  All rights reserved.
      5  *
      6  * See the LICENSE file for redistribution information.
      7  */
      8 
      9 #include "config.h"
     10 
     11 #ifndef lint
     12 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 ";
     13 #endif /* not lint */
     14 
     15 #include <sys/types.h>
     16 #include <sys/queue.h>
     17 
     18 #include <bitstring.h>
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 #include <string.h>
     22 #include <unistd.h>
     23 
     24 #include "../common/common.h"
     25 #include "ip.h"
     26 
     27 /*
     28  * vi_send --
     29  *	Construct and send an IP buffer.
     30  *
     31  * PUBLIC: int vi_send __P((int, const char *, IP_BUF *));
     32  */
     33 int
     34 vi_send(int ofd, const char *fmt, IP_BUF *ipbp)
     35 {
     36 	static char *bp;
     37 	static size_t blen;
     38 	size_t off;
     39 	u_int32_t ilen;
     40 	size_t nlen;
     41 	int n, nw;
     42 	char *p;
     43 
     44 	/*
     45 	 * Have not created the channel to vi yet?  -- RAZ
     46 	 *
     47 	 * XXX
     48 	 * How is that possible!?!?
     49 	 *
     50 	 * We'll soon find out.
     51 	 */
     52 	if (ofd == 0) {
     53 		fprintf(stderr, "No channel\n");
     54 		abort();
     55 	}
     56 
     57 	if (blen == 0 && (bp = malloc(blen = 512)) == NULL)
     58 		return (1);
     59 
     60 	p = bp;
     61 	nlen = 0;
     62 	*p++ = ipbp->code;
     63 	nlen += IPO_CODE_LEN;
     64 
     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;
     83 				}
     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;
     97 				}
     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;
    107 				}
    108 				break;
    109 			}
    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);
    114 }
    115