001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019 #include "crt.h"
020 #include "libc.h"
021 #include "stdarg.h"
022
023
024 void * fakemmap(void *start, size_t length, int prot , int flags,
025 const char *path, loff_t offset)
026 {
027
028 if (offset < 0)
029 return NULL;
030
031 if (0 != _sos_fakemmap(& start, length, prot, flags, path, offset))
032 return NULL;
033
034 return start;
035 }
036
037
038 void * mremap(void * old_addr, size_t old_len,
039 size_t new_len,
040 unsigned long flags)
041 {
042 void * new_uaddr = old_addr;
043 if (0 != _sos_mresize(old_addr, old_len, & new_uaddr, new_len, flags))
044 return NULL;
045
046 return new_uaddr;
047 }
048
049
050
051
052
053
054 static void * kernel_heap_top = NULL;
055 int brk(void *end_data_seg)
056 {
057 if (! end_data_seg)
058 return -1;
059
060 kernel_heap_top = _sos_brk(end_data_seg);
061 return 0;
062 }
063
064
065 void *sbrk(ptrdiff_t increment)
066 {
067 if (! kernel_heap_top)
068 kernel_heap_top = _sos_brk(0);
069
070 kernel_heap_top = _sos_brk(kernel_heap_top + increment);
071 return kernel_heap_top;
072 }
073
074
075 void * calloc (size_t nmemb, size_t size)
076 {
077 return malloc(nmemb * size);
078 }
079
080
081
082
083
084
085
086 static void * malloc_heap_top = NULL;
087 void * malloc (size_t size)
088 {
089 void * retval;
090
091 if (size <= 0)
092 return NULL;
093
094
095 size = ((size-1) & ~3) + 4;
096
097 if (! kernel_heap_top)
098 kernel_heap_top = _sos_brk(0);
099
100 if (! malloc_heap_top)
101 malloc_heap_top = kernel_heap_top;
102
103 retval = malloc_heap_top;
104 malloc_heap_top += size;
105
106 _sos_brk(malloc_heap_top);
107 return retval;
108 }
109
110
111 void free(void *ptr)
112 {
113
114 }
115
116
117 int open(const char *pathname, int flags, ...)
118 {
119 va_list ap;
120 unsigned int mode = 0;
121
122 va_start(ap, flags);
123 if (flags & O_CREAT)
124 mode = va_arg(ap, unsigned int);
125 va_end(ap);
126
127 return _sos_open(pathname, flags, mode);
128 }
129
130
131 int read(int fd, char * buf, size_t len)
132 {
133 int retval = _sos_read(fd, buf, & len);
134 if (retval < 0)
135 return retval;
136 return len;
137 }
138
139
140 int write(int fd, const char * buf, size_t len)
141 {
142 int retval = _sos_write(fd, buf, & len);
143 if (retval < 0)
144 return retval;
145 return len;
146 }
147
148
149 off_t lseek(int fd, off_t offset, int whence)
150 {
151 loff_t result = offset;
152 int retval = _sos_seek64(fd, & result, whence);
153 if (retval < 0)
154 return retval;
155 return result;
156 }
157
158
159 loff_t lseek64(int fd, loff_t offset, int whence)
160 {
161 loff_t result = offset;
162 int retval = _sos_seek64(fd, & result, whence);
163 if (retval < 0)
164 return retval;
165 return result;
166 }
167
168
169 void * mmap(void *start, size_t length, int prot , int flags,
170 int fd, loff_t offset)
171 {
172
173 if (offset < 0)
174 return NULL;
175
176 if (0 != _sos_fmmap(& start, length, prot, flags, fd, offset))
177 return NULL;
178
179 return start;
180 }
181
182
183 int ftruncate(int fd, off_t length)
184 {
185 return _sos_ftruncate64(fd, length);
186 }
187
188
189 struct sos_DIR_struct
190 {
191 int fd;
192 struct dirent dirent;
193 };
194
195
196 DIR *opendir(const char *name)
197 {
198 DIR * result = malloc(sizeof(DIR));
199 if (! result)
200 return NULL;
201
202 result->fd = _sos_open(name, O_DIRECTORY | O_RDONLY, 0);
203 return result;
204 }
205
206
207 int dirfd(const DIR * dir)
208 {
209 if (dir)
210 return dir->fd;
211 return -1;
212 }
213
214
215 struct dirent *readdir(DIR *dir)
216 {
217 int retval = _sos_readdir(dir->fd, & dir->dirent);
218 if (retval < 0)
219 return NULL;
220 return & dir->dirent;
221 }
222
223
224 int closedir(DIR *dir)
225 {
226 close(dir->fd);
227 free(dir);
228 return 0;
229 }
230
231
232 int stat(const char *file_name, struct stat *buf)
233 {
234 return _sos_stat(file_name, TRUE, buf);
235 }
236
237
238 int lstat(const char *file_name, struct stat *buf)
239 {
240 return _sos_stat(file_name, FALSE, buf);
241 }