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