-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys_file.c
135 lines (117 loc) · 2.81 KB
/
sys_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// System calls for fs
#include "defs.h"
#include "param.h"
#include "file.h"
extern struct proc *cp;
int argfd(uint32_t n, uint32_t *pfd, struct file **pf)
{
int fd;
struct file *f;
if (argint(n, &fd) < 0) {
kprintf("argfd: cannot get argument",0);
bbrk();
}
if (fd < 0 || fd > NFILE) {
kprintf("argfd: got invalid fd",0);
bbrk();
}
if (pfd)
pfd = fd;
if (pf)
pf = f;
return 0;
}
// sys_write(fd, pointer to char buffer, number of chars)
int sys_write(void)
{
int n;
struct file *f;
char *p;
n = argptr(1, &p, 1);
fb_put_char(*p);
//fb_write(p);
/*
if ( argfd(0,0,f) < 0 || argint(2, &n) || argptr(1, &p, n) )
PANIC("sys_write: BAD arguments!");
return filewrite(f, p , n);
*/
return 0;
}
// number read(fd, buffer, count)
int sys_read(void)
{
struct file *f;
char *buf;
int n;
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &buf, n) < 0)
PANIC("sys_read: error reading arguments.");
return consoleread(0, buf, 100);//fileread(f, buf, n);*/
}
// Allocate a fd to a caller process in the ptable entry
// to indicate it has an open file
fd_t fdalloc(struct file *f)
{
int fd;
for(fd = 0; fd < NOFILE; fd++){
if(cp->ofile[fd]==0){
cp->ofile[fd] = f;
return fd;
}
}
}
/*
The open system call is the first step a process must take to access the data in a
file. The syntax for the open system call is fd = open(pathname, flags, modes);
where pathname is a file name, flags indicate the type of open (such as for reading or writing),
and modes give the file permissions if the file is being created. The open system cal] returns an
integer' called the user file descripto
*/
//fd = open(pathname, omode);
fd_t sys_open(void)
{
char *path;
int omode, fd;
struct file *f;
struct inode *ip;
if (argstr(0, &path) == 0 || argint(1, &omode) == 0)
return -1;
if ((ip = namei(path)) == 0)
PANIC("sys_open: file not found");
if ((f = filealloc()) == 0)
PANIC("sys_open: can't allocate file table entry");
if ((fd = fdalloc(f)) < 0)
PANIC("sys_open: can't allocate file descriptor");
f->type = FD_INODE;
f->ip = ip;
f->off = 0;
f->readable = !(omode & O_WRONLY);
f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
return fd;
}
int sys_exec(void)
{
char *argv[MAXARG], *path, *buf;
int i;
uint32_t uargv, uarg;
if(argstr(0, &path) < 0 || argint(1, (int*)&uargv) < 0 )
PANIC("sys_exec: not finding arguments");
memset(argv, 0, sizeof(argv));
for(i = 0;;i++){
if(i >= NELEM(argv))
PANIC("sys_exec: too many arguments");
if(fetchint(uargv+4*i, (int*)&uarg) < 0 )
PANIC("sys_exec: fetchint");
if(uarg == 0 ) {
argv[i] = 0;
break;
}
if(fetchstr(uarg, &argv[i]) < 0)
PANIC("sys_exec: fetchstr");
//kprintf("sys_exec: argv[i] %d >", i);
fb_write(argv[i]);
}
//fb_write(">>>>>>>>");
//fb_write(path);
//fb_write("<<<<<<<<");
return exec(path, argv);
}