Stubbed out some more libc functions.
[planlOS.git] / programs / libc / files.c
bloba1390d889e042b938e0e1dce3634e9a7ac4b020e
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28 #include <sys/select.h>
29 #include <planlos/syscalls.h>
31 int open(const char *path, int oflag, ...)
33 int status = -1;
34 asm volatile("int $0x80" : "=b"(status): "a"(SYS_OPEN), "b"(path), "c"(oflag));
35 return status;
38 int creat(const char *path, mode_t mode)
40 return open(path, O_CREAT | O_TRUNC | O_WRONLY, mode);
43 ssize_t write(int fildes, const void *buf, size_t nbyte)
45 int status = -1;
46 asm volatile("int $0x80" : "=b"(status): "a"(SYS_WRITE), "b"(fildes), "c"(buf), "d"(nbyte));
47 return status;
50 ssize_t read(int fildes, void *buf, size_t nbyte)
52 int status = -1;
53 asm volatile("int $0x80" : "=b"(status): "a"(SYS_READ), "b"(fildes), "c"(buf), "d"(nbyte));
54 return status;
57 int close(int fildes)
59 int status = -1;
60 asm volatile("int $0x80" : "=b"(status): "a"(SYS_CLOSE), "b"(fildes));
61 return status;
64 off_t lseek(int fildes, off_t offset, int whence)
66 int position = -1;
67 asm volatile("int $0x80" : "=b"(position): "a"(SYS_SEEK), "b"(fildes), "c"(offset), "d"(whence));
68 return position;
71 int mknod(const char *path, mode_t mode, dev_t dev)
73 int status = -1;
74 asm volatile("int $0x80" : "=b"(status): "a"(SYS_MKNOD), "b"(path), "c"(mode));
75 return status;
77 int mkdir(const char *path, mode_t mode)
79 return mknod(path, mode | S_IFDIR, 0);
82 struct DIR
84 int fd;
85 struct dirent dirent;
88 #define O_DIR 0x1000
90 DIR *opendir(const char *path)
92 int fd = open(path, O_RDONLY | O_DIR);
93 if (fd == -1) return 0;
94 DIR *dir = malloc(sizeof(DIR));
95 dir->fd = fd;
96 return dir;
98 int closedir(DIR *dir)
100 close(dir->fd);
101 free(dir);
102 return 0;
104 struct dirent *readdir(DIR *dir)
106 if (!dir) return 0;
107 if (read(dir->fd, dir->dirent.d_name, NAME_MAX + 1) <= 0) return 0;
108 return &dir->dirent;
110 void rewinddir(DIR *dir)
112 seekdir(dir, 0);
114 void seekdir(DIR *dir, long index)
116 if (dir)
118 lseek(dir->fd, index, SEEK_SET);
121 long telldir(DIR *dir)
123 if (!dir) return -1;
124 return lseek(dir->fd, 0, SEEK_CUR);
127 int ioctl(int fd, int request, ...)
129 int param = *(&request + 1);
130 int status;
131 asm volatile("int $0x80" : "=b"(status): "a"(SYS_IOCTL), "b"(fd), "c"(request), "d"(param));
132 return status;
135 int unlink(const char *filename)
137 return -1;
140 int pipe(int fd[2])
142 int in;
143 int out;
144 asm volatile("int $0x80" : "=b"(in), "=c"(out): "a"(SYS_PIPE));
145 if (in == -1)
146 return -1;
147 fd[1] = in;
148 fd[0] = out;
149 return 0;
152 int fcntl(int fd, int cmd, ...)
154 uintptr_t param = *(uintptr_t*)(&cmd + 1);
155 int status;
156 asm volatile("int $0x80" : "=b"(status): "a"(SYS_FCNTL), "b"(fd), "c"(cmd), "d"(param));
157 return status;
160 int stat(const char *path, struct stat *buf)
162 return -1;
165 void FD_CLR(int fd, fd_set *fdset)
168 int FD_ISSET(int fd, fd_set *fdset)
170 return 0;
172 void FD_SET(int fd, fd_set *fdset)
175 void FD_ZERO(fd_set *fdset)
179 /*int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds,
180 const struct timespec *timeout, const sigset_t *sigmask)
183 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds,
184 struct timeval *timeout)
186 return -1;