xref: /titanic_50/usr/src/cmd/ssh/libssh/common/proxy-io.c (revision 0e42dee69ed771bf604dd1789fca9d77b5bbe302)
1 /*
2  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 #include <stdio.h>
9 #include <unistd.h>
10 #include "proxy-io.h"
11 
12 int
13 proxy_read_write_loop(int readfd, int writefd)
14 {
15 	int	rbytes, bytes_to_write, bytes_written;
16 	char 	readbuf[BUFFER_SIZ];
17 	char	*ptr;
18 
19 	rbytes = read(readfd, readbuf, sizeof (readbuf));
20 
21 	if (rbytes > 0) {
22 		bytes_to_write = rbytes;
23 		ptr = readbuf;
24 		while (bytes_to_write > 0) {
25 			if ((bytes_written =
26 			    write(writefd, ptr, bytes_to_write)) < 0) {
27 				perror("write");
28 				return (0);
29 			}
30 			bytes_to_write -= bytes_written;
31 			ptr += bytes_written;
32 		}
33 	} else if (rbytes <= 0) {
34 		return (0);
35 	}
36 	/* Read and write successful */
37 	return (1);
38 }
39