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