Diff markup
001 001
002 002
003 003
004 004
005 005
006 006
007 007
008 008
009 009
010 010
011 011
012 012
013 013
014 014
015 015
016 016
017 017
018 018
019 #include "crt.h" 019 #include "crt.h"
>> 020 #include "string.h"
>> 021 #include "stdarg.h"
>> 022
020 #include "libc.h" 023 #include "libc.h"
021 024
>> 025 void exit (int status)
>> 026 {
>> 027 _sos_exit(status);
>> 028 }
>> 029
022 030
023 void * mmap(void *start, size_t length, int pr !! 031 pid_t fork(void)
024 const char *path, loff_t offset) <<
025 { 032 {
026 !! 033 return _sos_fork();
027 if (offset < 0) !! 034 }
028 return NULL; <<
029 035
030 if (0 != _sos_mmap(& start, length, prot, fl <<
031 return NULL; <<
032 036
033 return start; !! 037 int exec(const char *progname)
>> 038 {
>> 039 return _sos_exec(progname);
>> 040 }
>> 041
>> 042
>> 043 int sleep(unsigned int seconds)
>> 044 {
>> 045 return nanosleep(seconds, 0);
>> 046 }
>> 047
>> 048
>> 049 int nanosleep(unsigned long int sec,
>> 050 unsigned long int nanosec)
>> 051 {
>> 052 return _sos_nanosleep(sec, nanosec);
>> 053 }
>> 054
>> 055
>> 056
>> 057 int munmap(void * start, size_t length)
>> 058 {
>> 059 return _sos_munmap(start, length);
034 } 060 }
035 061
036 062
037 void * mremap(void * old_addr, size_t old_len, 063 void * mremap(void * old_addr, size_t old_len,
038 size_t new_len, 064 size_t new_len,
039 unsigned long flags) 065 unsigned long flags)
040 { 066 {
041 void * new_uaddr = old_addr; 067 void * new_uaddr = old_addr;
042 if (0 != _sos_mresize(old_addr, old_len, & n 068 if (0 != _sos_mresize(old_addr, old_len, & new_uaddr, new_len, flags))
043 return NULL; 069 return NULL;
044 070
045 return new_uaddr; 071 return new_uaddr;
046 } 072 }
047 073
048 074
>> 075 int mprotect(const void *addr, size_t len, int prot)
>> 076 {
>> 077 return _sos_mprotect(addr, len, prot);
>> 078 }
>> 079
>> 080
>> 081 int msync(void *start, size_t length, int flags)
>> 082 {
>> 083 return _sos_msync(start, length, flags);
>> 084 }
>> 085
>> 086
049 087
050 088
051 089
052 090
053 static void * kernel_heap_top = NULL; 091 static void * kernel_heap_top = NULL;
054 int brk(void *end_data_seg) 092 int brk(void *end_data_seg)
055 { 093 {
056 if (! end_data_seg) 094 if (! end_data_seg)
057 return -1; 095 return -1;
058 096
059 kernel_heap_top = _sos_brk(end_data_seg); 097 kernel_heap_top = _sos_brk(end_data_seg);
060 return 0; 098 return 0;
061 } 099 }
062 100
063 101
064 void *sbrk(ptrdiff_t increment) 102 void *sbrk(ptrdiff_t increment)
065 { 103 {
066 if (! kernel_heap_top) 104 if (! kernel_heap_top)
067 kernel_heap_top = _sos_brk(0); 105 kernel_heap_top = _sos_brk(0);
068 106
069 kernel_heap_top = _sos_brk(kernel_heap_top + 107 kernel_heap_top = _sos_brk(kernel_heap_top + increment);
070 return kernel_heap_top; 108 return kernel_heap_top;
071 } 109 }
072 110
073 111
074 void * calloc (size_t nmemb, size_t size) 112 void * calloc (size_t nmemb, size_t size)
075 { 113 {
076 return malloc(nmemb * size); 114 return malloc(nmemb * size);
077 } 115 }
078 116
079 117
080 118
081 119
082 120
083 121
084 122
085 static void * malloc_heap_top = NULL; 123 static void * malloc_heap_top = NULL;
086 void * malloc (size_t size) 124 void * malloc (size_t size)
087 { 125 {
088 void * retval; 126 void * retval;
089 127
090 if (size <= 0) 128 if (size <= 0)
091 return NULL; 129 return NULL;
092 130
093 131
094 size = ((size-1) & ~3) + 4; 132 size = ((size-1) & ~3) + 4;
095 133
096 if (! kernel_heap_top) 134 if (! kernel_heap_top)
097 kernel_heap_top = _sos_brk(0); 135 kernel_heap_top = _sos_brk(0);
098 136
099 if (! malloc_heap_top) 137 if (! malloc_heap_top)
100 malloc_heap_top = kernel_heap_top; 138 malloc_heap_top = kernel_heap_top;
101 139
102 retval = malloc_heap_top; 140 retval = malloc_heap_top;
103 malloc_heap_top += size; 141 malloc_heap_top += size;
104 142
>> 143 _sos_brk(malloc_heap_top);
105 return retval; 144 return retval;
106 } 145 }
>> 146
>> 147
>> 148 void free(void *ptr)
>> 149 {
>> 150
>> 151 }
>> 152
>> 153
>> 154
>> 155 int mount(const char *source, const char *target,
>> 156 const char *filesystemtype, unsigned long mountflags,
>> 157 const char *data)
>> 158 {
>> 159 return _sos_mount(source, target, filesystemtype, mountflags, data);
>> 160 }
>> 161
>> 162
>> 163 int umount(const char *target)
>> 164 {
>> 165 return _sos_umount(target);
>> 166 }
>> 167
>> 168
>> 169 void sync(void)
>> 170 {
>> 171 return _sos_sync();
>> 172 }
>> 173
>> 174
>> 175 int statvfs(const char *path, struct statvfs *buf)
>> 176 {
>> 177 return _sos_statvfs(path, buf);
>> 178 }
>> 179
>> 180
>> 181 int open(const char *pathname, int flags, ...)
>> 182 {
>> 183 va_list ap;
>> 184 unsigned int mode = 0;
>> 185
>> 186 va_start(ap, flags);
>> 187 if (flags & O_CREAT)
>> 188 mode = va_arg(ap, unsigned int);
>> 189 va_end(ap);
>> 190
>> 191 return _sos_open(pathname, flags, mode);
>> 192 }
>> 193
>> 194
>> 195 int close(int fd)
>> 196 {
>> 197 return _sos_close(fd);
>> 198 }
>> 199
>> 200
>> 201 int read(int fd, char * buf, size_t len)
>> 202 {
>> 203 int retval = _sos_read(fd, buf, & len);
>> 204 if (retval < 0)
>> 205 return retval;
>> 206 return len;
>> 207 }
>> 208
>> 209
>> 210 int write(int fd, const char * buf, size_t len)
>> 211 {
>> 212 int retval = _sos_write(fd, buf, & len);
>> 213 if (retval < 0)
>> 214 return retval;
>> 215 return len;
>> 216 }
>> 217
>> 218
>> 219 off_t lseek(int fd, off_t offset, int whence)
>> 220 {
>> 221 loff_t result = offset;
>> 222 int retval = _sos_seek64(fd, & result, whence);
>> 223 if (retval < 0)
>> 224 return retval;
>> 225 return result;
>> 226 }
>> 227
>> 228
>> 229 loff_t lseek64(int fd, loff_t offset, int whence)
>> 230 {
>> 231 loff_t result = offset;
>> 232 int retval = _sos_seek64(fd, & result, whence);
>> 233 if (retval < 0)
>> 234 return retval;
>> 235 return result;
>> 236 }
>> 237
>> 238
>> 239 void * mmap(void *start, size_t length, int prot , int flags,
>> 240 int fd, loff_t offset)
>> 241 {
>> 242
>> 243 if (offset < 0)
>> 244 return NULL;
>> 245
>> 246 if (0 != _sos_fmmap(& start, length, prot, flags, fd, offset))
>> 247 return NULL;
>> 248
>> 249 return start;
>> 250 }
>> 251
>> 252
>> 253 int ftruncate(int fd, off_t length)
>> 254 {
>> 255 return _sos_ftruncate64(fd, length);
>> 256 }
>> 257
>> 258
>> 259 int ftruncate64(int fd, loff_t length)
>> 260 {
>> 261 return _sos_ftruncate64(fd, length);
>> 262 }
>> 263
>> 264
>> 265 int fcntl(int fd, int cmd, int arg)
>> 266 {
>> 267 return _sos_fcntl(fd, cmd, arg);
>> 268 }
>> 269
>> 270
>> 271 int ioctl(int fd, int cmd, int arg)
>> 272 {
>> 273 return _sos_ioctl(fd, cmd, arg);
>> 274 }
>> 275
>> 276
>> 277 int creat(const char *pathname, mode_t mode)
>> 278 {
>> 279 return _sos_creat(pathname, mode);
>> 280 }
>> 281
>> 282 int link (const char *oldpath, const char *newpath)
>> 283 {
>> 284 return _sos_link(oldpath, newpath);
>> 285 }
>> 286
>> 287
>> 288 int unlink(const char *pathname)
>> 289 {
>> 290 return _sos_unlink(pathname);
>> 291 }
>> 292
>> 293
>> 294 int rename(const char *oldpath, const char *newpath)
>> 295 {
>> 296 return _sos_rename(oldpath, newpath);
>> 297 }
>> 298
>> 299
>> 300 int symlink(const char *target, const char *path)
>> 301 {
>> 302 return _sos_symlink(target, path);
>> 303 }
>> 304
>> 305
>> 306 int mknod(const char *pathname, mode_t mode,
>> 307 int type,
>> 308 unsigned int major, unsigned minor)
>> 309 {
>> 310 if (type == S_IFREG)
>> 311 return creat(pathname, mode);
>> 312
>> 313 return _sos_mknod(pathname, mode, type,
>> 314 major, minor);
>> 315 }
>> 316
>> 317
>> 318 int mkdir(const char *pathname, mode_t mode)
>> 319 {
>> 320 return _sos_mkdir(pathname, mode);
>> 321 }
>> 322
>> 323
>> 324 int rmdir(const char *pathname)
>> 325 {
>> 326 return _sos_rmdir(pathname);
>> 327 }
>> 328
>> 329
>> 330 int chmod(const char *path, mode_t mode)
>> 331 {
>> 332 return _sos_chmod(path, mode);
>> 333 }
>> 334
>> 335
>> 336 struct sos_DIR_struct
>> 337 {
>> 338 int fd;
>> 339 struct dirent dirent;
>> 340 };
>> 341
>> 342
>> 343 DIR *opendir(const char *name)
>> 344 {
>> 345 DIR * result = malloc(sizeof(DIR));
>> 346 if (! result)
>> 347 return NULL;
>> 348
>> 349 result->fd = _sos_open(name, O_DIRECTORY | O_RDONLY, 0);
>> 350 return result;
>> 351 }
>> 352
>> 353
>> 354 int dirfd(const DIR * dir)
>> 355 {
>> 356 if (dir)
>> 357 return dir->fd;
>> 358 return -1;
>> 359 }
>> 360
>> 361
>> 362 struct dirent *readdir(DIR *dir)
>> 363 {
>> 364 int retval = _sos_readdir(dir->fd, & dir->dirent);
>> 365 if (retval < 0)
>> 366 return NULL;
>> 367 return & dir->dirent;
>> 368 }
>> 369
>> 370
>> 371 int closedir(DIR *dir)
>> 372 {
>> 373 close(dir->fd);
>> 374 free(dir);
>> 375 return 0;
>> 376 }
>> 377
>> 378
>> 379 int stat(const char *file_name, struct stat *buf)
>> 380 {
>> 381 return _sos_stat(file_name, TRUE, buf);
>> 382 }
>> 383
>> 384
>> 385 int lstat(const char *file_name, struct stat *buf)
>> 386 {
>> 387 return _sos_stat(file_name, FALSE, buf);
>> 388 }
>> 389
>> 390
>> 391 int chroot(const char *path)
>> 392 {
>> 393 return _sos_chroot(path);
>> 394 }
>> 395
>> 396
>> 397 int chdir(const char *path)
>> 398 {
>> 399 return _sos_chdir(path);
>> 400 }
>> 401
>> 402 int fchdir(int fd)
>> 403 {
>> 404 return _sos_fchdir(fd);
>> 405 }
>> 406
>> 407
>> 408
>> 409 int printf (const char *format, ...)
>> 410 {
>> 411 char buff[4096];
>> 412 va_list ap;
>> 413
>> 414 va_start(ap, format);
>> 415 vsnprintf(buff, sizeof(buff), format, ap);
>> 416 va_end(ap);
>> 417
>> 418 return write (1, buff, strlen(buff));
>> 419 }
>> 420
>> 421