Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/syscall/write.c
2093 views
1
#include <types.h>
2
#include <lib.h>
3
#include <copyinout.h>
4
#include <kern/errno.h>
5
#include <file.h>
6
#include <filedesc.h>
7
#include <kern/iovec.h>
8
#include <kern/fcntl.h>
9
#include <uio.h>
10
#include <proc.h>
11
#include <current.h>
12
#include <syscall.h>
13
14
int
15
sys_write( int fd, userptr_t ubuf, size_t ulen, int *retval ) {
16
int err = 0;
17
struct file *f = NULL;
18
struct proc *p = NULL;
19
char kbuf[ulen];
20
struct uio wuio;
21
struct iovec iov;
22
23
KASSERT( curthread != NULL );
24
KASSERT( curthread->td_proc != NULL );
25
26
p = curthread->td_proc;
27
28
//attempt to get a hold of the file
29
err = file_get( p, fd, &f );
30
if( err )
31
return err;
32
33
//make sure it is not readony
34
if( f->f_oflags & O_RDONLY )
35
return EBADF;
36
37
//copy the data from userland into kernel
38
err = copyin( ubuf, kbuf, sizeof( kbuf ) );
39
if( err ) {
40
F_UNLOCK( f );
41
return err;
42
}
43
44
//prepare the iovec/uio
45
uio_kinit(
46
&iov,
47
&wuio,
48
kbuf,
49
sizeof( kbuf ),
50
f->f_offset,
51
UIO_WRITE
52
);
53
54
//perform the IO
55
err = VOP_WRITE( f->f_vnode, &wuio );
56
if( err ) {
57
F_UNLOCK( f );
58
return err;
59
}
60
61
//update the offset
62
f->f_offset = wuio.uio_offset;
63
F_UNLOCK( f );
64
65
//number of bytes written
66
*retval = ulen - wuio.uio_resid;
67
return 0;
68
}
69
70