darwin-head-build-fixes-20020821
[openafs.git] / src / afs / afs_osi_uio.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include "../afs/param.h"
12
13 RCSID("$Header$");
14
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16 #include "../afs/afsincludes.h" /* Afs-based standard headers */
17 #include "../afs/afs_stats.h" /* statistics */
18 #include "../afs/afs_cbqueue.h"
19 #include "../afs/nfsclient.h"
20 #include "../afs/afs_osidnlc.h"
21
22
23 /*
24  * UIO routines
25  */
26
27 /* routine to make copy of uio structure in ainuio, using aoutvec for space */
28 int afsio_copy(struct uio *ainuio, struct uio *aoutuio, 
29         register struct iovec *aoutvec)
30 {
31     register int i;
32     register struct iovec *tvec;
33
34     AFS_STATCNT(afsio_copy);
35     if (ainuio->afsio_iovcnt > AFS_MAXIOVCNT) return EINVAL;
36     memcpy((char *)aoutuio, (char *)ainuio, sizeof(struct uio));
37     tvec = ainuio->afsio_iov;
38     aoutuio->afsio_iov = aoutvec;
39     for(i=0;i<ainuio->afsio_iovcnt;i++){
40         memcpy((char *)aoutvec, (char *)tvec, sizeof(struct iovec));
41         tvec++;     /* too many compiler bugs to do this as one expr */
42         aoutvec++;
43     }
44     return 0;
45 }
46
47 /* trim the uio structure to the specified size */
48 int afsio_trim(register struct uio *auio, register afs_int32 asize)
49 {
50     register int i;
51     register struct iovec *tv;
52
53     AFS_STATCNT(afsio_trim);
54     auio->afsio_resid = asize;
55     tv = auio->afsio_iov;
56     /* It isn't clear that multiple iovecs work ok (hasn't been tested!) */
57     for(i=0;;i++,tv++) {
58         if (i >= auio->afsio_iovcnt || asize <= 0) {
59             /* we're done */
60             auio->afsio_iovcnt = i;
61             break;
62         }
63         if (tv->iov_len <= asize)
64             /* entire iovec is included */
65             asize -= tv->iov_len;   /* this many fewer bytes */
66         else {
67             /* this is the last one */
68             tv->iov_len = asize;
69             auio->afsio_iovcnt = i+1;
70             break;
71         }
72     }
73     return 0;
74 }
75
76 /* skip asize bytes in the current uio structure */
77 int afsio_skip(register struct uio *auio, register afs_int32 asize)
78 {
79     register struct iovec *tv;  /* pointer to current iovec */
80     register int cnt;
81
82     AFS_STATCNT(afsio_skip);
83    /* It isn't guaranteed that multiple iovecs work ok (hasn't been tested!) */
84     while (asize > 0 && auio->afsio_resid) {
85         tv = auio->afsio_iov;
86         cnt = tv->iov_len;
87         if (cnt == 0) {
88             auio->afsio_iov++;
89             auio->afsio_iovcnt--;
90             continue;
91         }
92         if (cnt > asize)
93             cnt = asize;
94         tv->iov_base = (char *)(tv->iov_base) + cnt;
95         tv->iov_len -= cnt;
96         auio->uio_resid -= cnt;
97         auio->uio_offset += cnt;
98         asize -= cnt;
99     }
100     return 0;
101 }
102