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