nckernel  0.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
open.c
Go to the documentation of this file.
1 #include <slibc.h>
2 #include <stdint.h>
3 #include <sys/types.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <stdarg.h>
7 #include <stddef.h>
8 #include <stdlib.h>
9 #include <fcntl.h>
10 #include <stropts.h>
11 #include <pthread.h>
12 #include <string.h>
13 #include <unistd.h>
14 
15 #include <object.h>
16 #include <list.h>
17 #include <vfs.h>
18 #include <thread.h>
19 
20 #include <debug.h>
21 
22 int open(const char *path, int mode, ...)
23 {
24  struct file_object_info info;
25  struct thread *tcb;
26  register int fd;
27  int ret;
28 
29  tcb = pthread_self();
30  if (!tcb || !tcb->file_ctx) {
31  printf("Invalid TCB\n");
32  return -EFAULT;
33  }
34 
35  fd = 0;
36  while (fd < MAX_ENTRY && tcb->file_ctx->table[fd].ctx) {
37  fd++;
38  }
39 
40  if (fd == MAX_ENTRY) {
41  printf("No more slot\n");
42  return -ENOSPC;
43  }
44 
45  info.size = BUFSIZ;
46  info.path = path;
47  info.mode = mode;
48 
49  ret = object_create(&tcb->file_ctx->table[fd].object, &info);
50  if (ret < 0) {
51  return ret;
52  }
53 
54  return fd;
55 }
56 
57 off_t lseek(int fd, off_t offset, int whence)
58 {
59  struct entry *ctx;
60  struct ninfo *ninfo;
61  struct thread *tcb;
62 
63  tcb = pthread_self();
64  if (!tcb || !tcb->file_ctx) {
65  return -EFAULT;
66  }
67 
68  ctx = tcb->file_ctx->table[fd].ctx;
69  if (!ctx) {
70  return -EINVAL;
71  }
72 
73  ninfo = ctx->ninfo;
74  if (!ninfo) {
75  return -EINVAL;
76  }
77 
78  if (!ninfo->nops.lseek) {
79  return -EPERM;
80  }
81 
82  return ninfo->nops.lseek(ninfo, &ctx->nctx, offset, whence);
83 }
84 
85 ssize_t read(int fd, void *buf, size_t size)
86 {
87  struct entry *ctx;
88  struct ninfo *ninfo;
89  struct thread *tcb;
90 
91  tcb = pthread_self();
92  if (!tcb || !tcb->file_ctx) {
93  return -EFAULT;
94  }
95 
96  ctx = tcb->file_ctx->table[fd].ctx;
97  if (!ctx) {
98  return -EINVAL;
99  }
100 
101  ninfo = ctx->ninfo;
102  if (!ninfo) {
103  return -EINVAL;
104  }
105 
106  if (!ninfo->nops.read) {
107  return -EPERM;
108  }
109 
110  return ninfo->nops.read(ninfo, &ctx->nctx, buf, size);
111 }
112 
113 ssize_t write(int fd, const void *buf, size_t size)
114 {
115  struct entry *ctx;
116  struct ninfo *ninfo;
117  struct thread *tcb;
118 
119  tcb = pthread_self();
120  if (!tcb || !tcb->file_ctx) {
121  return -EFAULT;
122  }
123 
124  ctx = tcb->file_ctx->table[fd].ctx;
125  if (!ctx) {
126  return -EINVAL;
127  }
128 
129  ninfo = ctx->ninfo;
130  if (!ninfo) {
131  return -EINVAL;
132  }
133 
134  if (!ninfo->nops.write) {
135  return -EPERM;
136  }
137 
138  return ninfo->nops.write(ninfo, &ctx->nctx, buf, size);
139 }
140 
141 int close(int fd)
142 {
143  struct thread *tcb;
144  int ret = 0;
145 
146  tcb = pthread_self();
147  if (!tcb || !tcb->file_ctx) {
148  return -EFAULT;
149  }
150 
151  object_destroy(&tcb->file_ctx->table[fd].object);
152  return ret;
153 }
154 
155 int ioctl(int fd, int request, ...)
156 {
157  struct entry *ctx;
158  struct ninfo *ninfo;
159  struct thread *tcb;
160  va_list ap;
161  int ret = -EPERM;
162 
163  tcb = pthread_self();
164  if (!tcb || !tcb->file_ctx) {
165  return -EFAULT;
166  }
167 
168  ctx = tcb->file_ctx->table[fd].ctx;
169  if (!ctx) {
170  return -EINVAL;
171  }
172 
173  ninfo = ctx->ninfo;
174  if (!ninfo) {
175  return -EINVAL;
176  }
177 
178  if (ninfo->nops.ioctl) {
179  va_start(ap, request);
180  ret = ninfo->nops.ioctl(ninfo, &ctx->nctx, request, ap);
181  va_end(ap);
182  }
183 
184  return ret;
185 }
186 
187 /* End of a file */