1*bf21cd93STycho Nightingale /*
2*bf21cd93STycho Nightingale  * This file and its contents are supplied under the terms of the
3*bf21cd93STycho Nightingale  * Common Development and Distribution License ("CDDL"), version 1.0.
4*bf21cd93STycho Nightingale  * You may only use this file in accordance with the terms of version
5*bf21cd93STycho Nightingale  * 1.0 of the CDDL.
6*bf21cd93STycho Nightingale  *
7*bf21cd93STycho Nightingale  * A full copy of the text of the CDDL should have accompanied this
8*bf21cd93STycho Nightingale  * source.  A copy of the CDDL is also available via the Internet at
9*bf21cd93STycho Nightingale  * http://www.illumos.org/license/CDDL.
10*bf21cd93STycho Nightingale  */
11*bf21cd93STycho Nightingale 
12*bf21cd93STycho Nightingale /*
13*bf21cd93STycho Nightingale  * Copyright 2013 Pluribus Networks Inc.
14*bf21cd93STycho Nightingale  */
15*bf21cd93STycho Nightingale 
16*bf21cd93STycho Nightingale #include <sys/uio.h>
17*bf21cd93STycho Nightingale 
18*bf21cd93STycho Nightingale #include <termios.h>
19*bf21cd93STycho Nightingale #include <unistd.h>
20*bf21cd93STycho Nightingale 
21*bf21cd93STycho Nightingale /*
22*bf21cd93STycho Nightingale  * Make a pre-existing termios structure into "raw" mode: character-at-a-time
23*bf21cd93STycho Nightingale  * mode with no characters interpreted, 8-bit data path.
24*bf21cd93STycho Nightingale  */
25*bf21cd93STycho Nightingale void
26*bf21cd93STycho Nightingale cfmakeraw(struct termios *t)
27*bf21cd93STycho Nightingale {
28*bf21cd93STycho Nightingale 	t->c_iflag &= ~(IMAXBEL|IXOFF|INPCK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IGNPAR);
29*bf21cd93STycho Nightingale 	t->c_iflag |= IGNBRK;
30*bf21cd93STycho Nightingale 	t->c_oflag &= ~OPOST;
31*bf21cd93STycho Nightingale 	t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN|NOFLSH|TOSTOP |PENDIN);
32*bf21cd93STycho Nightingale 	t->c_cflag &= ~(CSIZE|PARENB);
33*bf21cd93STycho Nightingale 	t->c_cflag |= CS8|CREAD;
34*bf21cd93STycho Nightingale 	t->c_cc[VMIN] = 1;
35*bf21cd93STycho Nightingale 	t->c_cc[VTIME] = 0;
36*bf21cd93STycho Nightingale }
37*bf21cd93STycho Nightingale 
38*bf21cd93STycho Nightingale ssize_t
39*bf21cd93STycho Nightingale preadv(int d, const struct iovec *iov, int iovcnt, off_t offset)
40*bf21cd93STycho Nightingale {
41*bf21cd93STycho Nightingale 	off_t		old_offset;
42*bf21cd93STycho Nightingale 	ssize_t		n;
43*bf21cd93STycho Nightingale 
44*bf21cd93STycho Nightingale 	old_offset = lseek(d, (off_t)0, SEEK_CUR);
45*bf21cd93STycho Nightingale 	if (old_offset == -1)
46*bf21cd93STycho Nightingale 		return (-1);
47*bf21cd93STycho Nightingale 
48*bf21cd93STycho Nightingale 	offset = lseek(d, offset, SEEK_SET);
49*bf21cd93STycho Nightingale 	if (offset == -1)
50*bf21cd93STycho Nightingale 		return (-1);
51*bf21cd93STycho Nightingale 
52*bf21cd93STycho Nightingale 	n = readv(d, iov, iovcnt);
53*bf21cd93STycho Nightingale 	if (n == -1)
54*bf21cd93STycho Nightingale 		return (-1);
55*bf21cd93STycho Nightingale 
56*bf21cd93STycho Nightingale 	offset = lseek(d, old_offset, SEEK_SET);
57*bf21cd93STycho Nightingale 	if (offset == -1)
58*bf21cd93STycho Nightingale 		return (-1);
59*bf21cd93STycho Nightingale 
60*bf21cd93STycho Nightingale 	return (n);
61*bf21cd93STycho Nightingale }
62*bf21cd93STycho Nightingale 
63*bf21cd93STycho Nightingale ssize_t
64*bf21cd93STycho Nightingale pwritev(int d, const struct iovec *iov, int iovcnt, off_t offset)
65*bf21cd93STycho Nightingale {
66*bf21cd93STycho Nightingale 	off_t		old_offset;
67*bf21cd93STycho Nightingale 	ssize_t		n;
68*bf21cd93STycho Nightingale 
69*bf21cd93STycho Nightingale 	old_offset = lseek(d, (off_t)0, SEEK_CUR);
70*bf21cd93STycho Nightingale 	if (old_offset == -1)
71*bf21cd93STycho Nightingale 		return (-1);
72*bf21cd93STycho Nightingale 
73*bf21cd93STycho Nightingale 	offset = lseek(d, offset, SEEK_SET);
74*bf21cd93STycho Nightingale 	if (offset == -1)
75*bf21cd93STycho Nightingale 		return (-1);
76*bf21cd93STycho Nightingale 
77*bf21cd93STycho Nightingale 	n = writev(d, iov, iovcnt);
78*bf21cd93STycho Nightingale 	if (n == -1)
79*bf21cd93STycho Nightingale 		return (-1);
80*bf21cd93STycho Nightingale 
81*bf21cd93STycho Nightingale 	offset = lseek(d, old_offset, SEEK_SET);
82*bf21cd93STycho Nightingale 	if (offset == -1)
83*bf21cd93STycho Nightingale 		return (-1);
84*bf21cd93STycho Nightingale 
85*bf21cd93STycho Nightingale 	return (n);
86*bf21cd93STycho Nightingale }
87