kern_subr.c revision 1.9 1 1.9 cgd /*
2 1.9 cgd * Copyright (c) 1982, 1986, 1991 Regents of the University of California.
3 1.9 cgd * All rights reserved.
4 1.9 cgd * (c) UNIX System Laboratories, Inc.
5 1.9 cgd * All or some portions of this file are derived from material licensed
6 1.9 cgd * to the University of California by American Telephone and Telegraph
7 1.9 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 1.9 cgd * the permission of UNIX System Laboratories, Inc.
9 1.9 cgd *
10 1.9 cgd * Redistribution and use in source and binary forms, with or without
11 1.9 cgd * modification, are permitted provided that the following conditions
12 1.9 cgd * are met:
13 1.9 cgd * 1. Redistributions of source code must retain the above copyright
14 1.9 cgd * notice, this list of conditions and the following disclaimer.
15 1.9 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.9 cgd * notice, this list of conditions and the following disclaimer in the
17 1.9 cgd * documentation and/or other materials provided with the distribution.
18 1.9 cgd * 3. All advertising materials mentioning features or use of this software
19 1.9 cgd * must display the following acknowledgement:
20 1.9 cgd * This product includes software developed by the University of
21 1.9 cgd * California, Berkeley and its contributors.
22 1.9 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.9 cgd * may be used to endorse or promote products derived from this software
24 1.9 cgd * without specific prior written permission.
25 1.9 cgd *
26 1.9 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.9 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.9 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.9 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.9 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.9 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.9 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.9 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.9 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.9 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.9 cgd * SUCH DAMAGE.
37 1.9 cgd *
38 1.9 cgd * from: @(#)kern_subr.c 7.7 (Berkeley) 4/15/91
39 1.9 cgd * $Id: kern_subr.c,v 1.9 1994/05/17 04:22:00 cgd Exp $
40 1.9 cgd */
41 1.9 cgd
42 1.9 cgd #include <sys/param.h>
43 1.9 cgd #include <sys/systm.h>
44 1.9 cgd #include <sys/proc.h>
45 1.9 cgd
46 1.9 cgd int
47 1.9 cgd uiomove(cp, n, uio)
48 1.9 cgd register caddr_t cp;
49 1.9 cgd register int n;
50 1.9 cgd register struct uio *uio;
51 1.9 cgd {
52 1.9 cgd register struct iovec *iov;
53 1.9 cgd u_int cnt;
54 1.9 cgd int error = 0;
55 1.9 cgd
56 1.9 cgd
57 1.9 cgd #ifdef DIAGNOSTIC
58 1.9 cgd if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
59 1.9 cgd panic("uiomove: mode");
60 1.9 cgd if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
61 1.9 cgd panic("uiomove proc");
62 1.9 cgd #endif
63 1.9 cgd while (n > 0 && uio->uio_resid) {
64 1.9 cgd iov = uio->uio_iov;
65 1.9 cgd cnt = iov->iov_len;
66 1.9 cgd if (cnt == 0) {
67 1.9 cgd uio->uio_iov++;
68 1.9 cgd uio->uio_iovcnt--;
69 1.9 cgd continue;
70 1.9 cgd }
71 1.9 cgd if (cnt > n)
72 1.9 cgd cnt = n;
73 1.9 cgd switch (uio->uio_segflg) {
74 1.9 cgd
75 1.9 cgd case UIO_USERSPACE:
76 1.9 cgd case UIO_USERISPACE:
77 1.9 cgd if (uio->uio_rw == UIO_READ)
78 1.9 cgd error = copyout(cp, iov->iov_base, cnt);
79 1.9 cgd else
80 1.9 cgd error = copyin(iov->iov_base, cp, cnt);
81 1.9 cgd if (error)
82 1.9 cgd return (error);
83 1.9 cgd break;
84 1.9 cgd
85 1.9 cgd case UIO_SYSSPACE:
86 1.9 cgd if (uio->uio_rw == UIO_READ)
87 1.9 cgd bcopy((caddr_t)cp, iov->iov_base, cnt);
88 1.9 cgd else
89 1.9 cgd bcopy(iov->iov_base, (caddr_t)cp, cnt);
90 1.9 cgd break;
91 1.9 cgd }
92 1.9 cgd iov->iov_base += cnt;
93 1.9 cgd iov->iov_len -= cnt;
94 1.9 cgd uio->uio_resid -= cnt;
95 1.9 cgd uio->uio_offset += cnt;
96 1.9 cgd cp += cnt;
97 1.9 cgd n -= cnt;
98 1.9 cgd }
99 1.9 cgd return (error);
100 1.9 cgd }
101 1.9 cgd
102 1.9 cgd int
103 1.9 cgd uioapply(func, arg1, arg2, uio)
104 1.9 cgd int (*func)();
105 1.9 cgd int arg1, arg2;
106 1.9 cgd register struct uio *uio;
107 1.9 cgd {
108 1.9 cgd register struct iovec *iov;
109 1.9 cgd u_int cnt, cnt1;
110 1.9 cgd int error = 0;
111 1.9 cgd
112 1.9 cgd
113 1.9 cgd /*#ifdef DIAGNOSTIC*/
114 1.9 cgd if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
115 1.9 cgd panic("uioapply: mode");
116 1.9 cgd if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
117 1.9 cgd panic("uioapply proc");
118 1.9 cgd /*#endif*/
119 1.9 cgd while (uio->uio_resid) {
120 1.9 cgd iov = uio->uio_iov;
121 1.9 cgd cnt = iov->iov_len;
122 1.9 cgd if (cnt == 0) {
123 1.9 cgd uio->uio_iov++;
124 1.9 cgd uio->uio_iovcnt--;
125 1.9 cgd continue;
126 1.9 cgd }
127 1.9 cgd cnt1 = cnt;
128 1.9 cgd error = (*func)(arg1, arg2, uio->uio_offset, uio->uio_rw,
129 1.9 cgd iov->iov_base, &cnt1, uio->uio_procp);
130 1.9 cgd cnt -= cnt1;
131 1.9 cgd iov->iov_base += cnt;
132 1.9 cgd iov->iov_len -= cnt;
133 1.9 cgd uio->uio_resid -= cnt;
134 1.9 cgd uio->uio_offset += cnt;
135 1.9 cgd if (error || cnt1)
136 1.9 cgd return (error);
137 1.9 cgd }
138 1.9 cgd return (0);
139 1.9 cgd }
140 1.9 cgd
141 1.9 cgd /*
142 1.9 cgd * Give next character to user as result of read.
143 1.9 cgd */
144 1.9 cgd int
145 1.9 cgd ureadc(c, uio)
146 1.9 cgd register int c;
147 1.9 cgd register struct uio *uio;
148 1.9 cgd {
149 1.9 cgd register struct iovec *iov;
150 1.9 cgd
151 1.9 cgd if (uio->uio_resid <= 0)
152 1.9 cgd panic("ureadc: uio_resid == 0");
153 1.9 cgd
154 1.9 cgd again:
155 1.9 cgd if (uio->uio_iovcnt == 0)
156 1.9 cgd panic("ureadc: uio_iovcnt == 0");
157 1.9 cgd
158 1.9 cgd iov = uio->uio_iov;
159 1.9 cgd if (iov->iov_len <= 0) {
160 1.9 cgd uio->uio_iovcnt--;
161 1.9 cgd uio->uio_iov++;
162 1.9 cgd goto again;
163 1.9 cgd }
164 1.9 cgd switch (uio->uio_segflg) {
165 1.9 cgd
166 1.9 cgd case UIO_USERSPACE:
167 1.9 cgd if (subyte(iov->iov_base, c) < 0)
168 1.9 cgd return (EFAULT);
169 1.9 cgd break;
170 1.9 cgd
171 1.9 cgd case UIO_SYSSPACE:
172 1.9 cgd *iov->iov_base = c;
173 1.9 cgd break;
174 1.9 cgd
175 1.9 cgd case UIO_USERISPACE:
176 1.9 cgd if (suibyte(iov->iov_base, c) < 0)
177 1.9 cgd return (EFAULT);
178 1.9 cgd break;
179 1.9 cgd }
180 1.9 cgd iov->iov_base++;
181 1.9 cgd iov->iov_len--;
182 1.9 cgd uio->uio_resid--;
183 1.9 cgd uio->uio_offset++;
184 1.9 cgd return (0);
185 1.9 cgd }
186 1.9 cgd
187 1.9 cgd #ifndef lint /* unused except by ct.c, other oddities XXX */
188 1.9 cgd /*
189 1.9 cgd * Get next character written in by user from uio.
190 1.9 cgd */
191 1.9 cgd int
192 1.9 cgd uwritec(uio)
193 1.9 cgd struct uio *uio;
194 1.9 cgd {
195 1.9 cgd register struct iovec *iov;
196 1.9 cgd register int c;
197 1.9 cgd
198 1.9 cgd if (uio->uio_resid <= 0)
199 1.9 cgd return (-1);
200 1.9 cgd again:
201 1.9 cgd if (uio->uio_iovcnt <= 0)
202 1.9 cgd panic("uwritec");
203 1.9 cgd iov = uio->uio_iov;
204 1.9 cgd if (iov->iov_len == 0) {
205 1.9 cgd uio->uio_iov++;
206 1.9 cgd if (--uio->uio_iovcnt == 0)
207 1.9 cgd return (-1);
208 1.9 cgd goto again;
209 1.9 cgd }
210 1.9 cgd switch (uio->uio_segflg) {
211 1.9 cgd
212 1.9 cgd case UIO_USERSPACE:
213 1.9 cgd c = fubyte(iov->iov_base);
214 1.9 cgd break;
215 1.9 cgd
216 1.9 cgd case UIO_SYSSPACE:
217 1.9 cgd c = *(u_char *) iov->iov_base;
218 1.9 cgd break;
219 1.9 cgd
220 1.9 cgd case UIO_USERISPACE:
221 1.9 cgd c = fuibyte(iov->iov_base);
222 1.9 cgd break;
223 1.9 cgd }
224 1.9 cgd if (c < 0)
225 1.9 cgd return (-1);
226 1.9 cgd iov->iov_base++;
227 1.9 cgd iov->iov_len--;
228 1.9 cgd uio->uio_resid--;
229 1.9 cgd uio->uio_offset++;
230 1.9 cgd return (c);
231 1.9 cgd }
232 1.9 cgd #endif /* notdef */
233