SimpleOS

LXR

Navigation



Site hébergé par : enix

The LXR Cross Referencer for SOS

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Article:1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] [ 6.5 ] [ 7 ] [ 7.5 ] [ 8 ] [ 9 ] [ 9.5 ]

Diff markup

Differences between /userland/libc.c (Article 7.5) and /userland/libc.c (Article 9)


001 /* Copyright (C) 2005 David Decotigny             001 /* Copyright (C) 2005 David Decotigny
002                                                   002 
003    This program is free software; you can redi    003    This program is free software; you can redistribute it and/or
004    modify it under the terms of the GNU Genera    004    modify it under the terms of the GNU General Public License
005    as published by the Free Software Foundatio    005    as published by the Free Software Foundation; either version 2
006    of the License, or (at your option) any lat    006    of the License, or (at your option) any later version.
007                                                   007    
008    This program is distributed in the hope tha    008    This program is distributed in the hope that it will be useful,
009    but WITHOUT ANY WARRANTY; without even the     009    but WITHOUT ANY WARRANTY; without even the implied warranty of
010    MERCHANTABILITY or FITNESS FOR A PARTICULAR    010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
011    GNU General Public License for more details    011    GNU General Public License for more details.
012                                                   012    
013    You should have received a copy of the GNU     013    You should have received a copy of the GNU General Public License
014    along with this program; if not, write to t    014    along with this program; if not, write to the Free Software
015    Foundation, Inc., 59 Temple Place - Suite 3    015    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
016    USA.                                           016    USA. 
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   /* At kernel side, offset is considered posi !! 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 
049 /**                                               081 /**
050  * The presence of this global variable withou    082  * The presence of this global variable without any protected access
051  * to it explains why the "brk/sbrk" functions    083  * to it explains why the "brk/sbrk" functions below are MT-unsafe !
052  */                                               084  */
053 static void * kernel_heap_top = NULL;             085 static void * kernel_heap_top = NULL;
054 int brk(void *end_data_seg)                       086 int brk(void *end_data_seg)
055 {                                                 087 {
056   if (! end_data_seg)                             088   if (! end_data_seg)
057     return -1;                                    089     return -1;
058                                                   090 
059   kernel_heap_top = _sos_brk(end_data_seg);       091   kernel_heap_top = _sos_brk(end_data_seg);
060   return 0;                                       092   return 0;
061 }                                                 093 }
062                                                   094 
063                                                   095 
064 void *sbrk(ptrdiff_t increment)                   096 void *sbrk(ptrdiff_t increment)
065 {                                                 097 {
066   if (! kernel_heap_top)                          098   if (! kernel_heap_top)
067     kernel_heap_top = _sos_brk(0);                099     kernel_heap_top = _sos_brk(0);
068                                                   100 
069   kernel_heap_top = _sos_brk(kernel_heap_top +    101   kernel_heap_top = _sos_brk(kernel_heap_top + increment);
070   return kernel_heap_top;                         102   return kernel_heap_top;
071 }                                                 103 }
072                                                   104 
073                                                   105 
074 void * calloc (size_t nmemb, size_t size)         106 void * calloc (size_t nmemb, size_t size)
075 {                                                 107 {
076   return malloc(nmemb * size);                    108   return malloc(nmemb * size);
077 }                                                 109 }
078                                                   110 
079                                                   111 
080 /**                                               112 /**
081  * The presence of this global variable withou    113  * The presence of this global variable without any protected access
082  * to it explains why the "malloc/calloc" func    114  * to it explains why the "malloc/calloc" functions below are
083  * MT-unsafe !                                    115  * MT-unsafe !
084  */                                               116  */
085 static void * malloc_heap_top = NULL;             117 static void * malloc_heap_top = NULL;
086 void * malloc (size_t size)                       118 void * malloc (size_t size)
087 {                                                 119 {
088   void * retval;                                  120   void * retval;
089                                                   121 
090   if (size <= 0)                                  122   if (size <= 0)
091     return NULL;                                  123     return NULL;
092                                                   124 
093   /* Align on a 4B boundary */                    125   /* Align on a 4B boundary */
094   size = ((size-1) & ~3) + 4;                     126   size = ((size-1) & ~3) + 4;
095                                                   127 
096   if (! kernel_heap_top)                          128   if (! kernel_heap_top)
097     kernel_heap_top = _sos_brk(0);                129     kernel_heap_top = _sos_brk(0);
098                                                   130 
099   if (! malloc_heap_top)                          131   if (! malloc_heap_top)
100     malloc_heap_top = kernel_heap_top;            132     malloc_heap_top = kernel_heap_top;
101                                                   133 
102   retval = malloc_heap_top;                       134   retval = malloc_heap_top;
103   malloc_heap_top += size;                        135   malloc_heap_top += size;
104                                                   136 
                                                   >> 137   _sos_brk(malloc_heap_top);
105   return retval;                                  138   return retval;
106 }                                                 139 }
                                                   >> 140 
                                                   >> 141 
                                                   >> 142 void free(void *ptr)
                                                   >> 143 {
                                                   >> 144   //  bochs_printf("Free ignored (not implemented yet)\n");
                                                   >> 145 }
                                                   >> 146 
                                                   >> 147 
                                                   >> 148 
                                                   >> 149 int mount(const char *source, const char *target,
                                                   >> 150           const char *filesystemtype, unsigned long mountflags,
                                                   >> 151           const char *data)
                                                   >> 152 {
                                                   >> 153   return _sos_mount(source, target, filesystemtype, mountflags, data);
                                                   >> 154 }
                                                   >> 155 
                                                   >> 156 
                                                   >> 157 int umount(const char *target)
                                                   >> 158 {
                                                   >> 159   return _sos_umount(target);
                                                   >> 160 }
                                                   >> 161 
                                                   >> 162 
                                                   >> 163 void sync(void)
                                                   >> 164 {
                                                   >> 165   return _sos_sync();
                                                   >> 166 }
                                                   >> 167 
                                                   >> 168 
                                                   >> 169 int statvfs(const char *path, struct statvfs *buf)
                                                   >> 170 {
                                                   >> 171   return _sos_statvfs(path, buf);
                                                   >> 172 }
                                                   >> 173 
                                                   >> 174 
                                                   >> 175 int open(const char *pathname, int flags, /* mode_t mode */...)
                                                   >> 176 {
                                                   >> 177   va_list ap;
                                                   >> 178   unsigned int mode = 0;
                                                   >> 179  
                                                   >> 180   va_start(ap, flags);
                                                   >> 181   if (flags & O_CREAT)
                                                   >> 182     mode = va_arg(ap, unsigned int);
                                                   >> 183   va_end(ap);
                                                   >> 184  
                                                   >> 185   return _sos_open(pathname, flags, mode);
                                                   >> 186 }
                                                   >> 187 
                                                   >> 188 
                                                   >> 189 int close(int fd)
                                                   >> 190 {
                                                   >> 191   return _sos_close(fd);
                                                   >> 192 }
                                                   >> 193 
                                                   >> 194 
                                                   >> 195 int read(int fd, char * buf, size_t len)
                                                   >> 196 {
                                                   >> 197   int retval = _sos_read(fd, buf, & len);
                                                   >> 198   if (retval < 0)
                                                   >> 199     return retval;
                                                   >> 200   return len;
                                                   >> 201 }
                                                   >> 202 
                                                   >> 203 
                                                   >> 204 int write(int fd, const char * buf, size_t len)
                                                   >> 205 {
                                                   >> 206   int retval = _sos_write(fd, buf, & len);
                                                   >> 207   if (retval < 0)
                                                   >> 208     return retval;
                                                   >> 209   return len;
                                                   >> 210 }
                                                   >> 211 
                                                   >> 212 
                                                   >> 213 off_t lseek(int fd, off_t offset, int whence)
                                                   >> 214 {
                                                   >> 215   loff_t result = offset;
                                                   >> 216   int retval = _sos_seek64(fd, & result, whence);
                                                   >> 217   if (retval < 0)
                                                   >> 218     return retval;
                                                   >> 219   return result;
                                                   >> 220 }
                                                   >> 221 
                                                   >> 222 
                                                   >> 223 loff_t lseek64(int fd, loff_t offset, int whence)
                                                   >> 224 {
                                                   >> 225   loff_t result = offset;
                                                   >> 226   int retval = _sos_seek64(fd, & result, whence);
                                                   >> 227   if (retval < 0)
                                                   >> 228     return retval;
                                                   >> 229   return result;
                                                   >> 230 }
                                                   >> 231 
                                                   >> 232 
                                                   >> 233 void * mmap(void *start, size_t length, int prot , int flags,
                                                   >> 234             int fd, loff_t offset)
                                                   >> 235 {
                                                   >> 236   /* At kernel side, offset is considered positive */
                                                   >> 237   if (offset < 0)
                                                   >> 238     return NULL;
                                                   >> 239 
                                                   >> 240   if (0 != _sos_fmmap(& start, length, prot, flags, fd, offset))
                                                   >> 241     return NULL;
                                                   >> 242 
                                                   >> 243   return start;
                                                   >> 244 }
                                                   >> 245 
                                                   >> 246 
                                                   >> 247 int ftruncate(int fd, off_t length)
                                                   >> 248 {
                                                   >> 249   return _sos_ftruncate64(fd, length);
                                                   >> 250 }
                                                   >> 251 
                                                   >> 252 
                                                   >> 253 int ftruncate64(int fd, loff_t length)
                                                   >> 254 {
                                                   >> 255   return _sos_ftruncate64(fd, length);
                                                   >> 256 }
                                                   >> 257 
                                                   >> 258 
                                                   >> 259 int fcntl(int fd, int cmd, int arg)
                                                   >> 260 {
                                                   >> 261   return _sos_fcntl(fd, cmd, arg);
                                                   >> 262 }
                                                   >> 263 
                                                   >> 264 
                                                   >> 265 int ioctl(int fd, int cmd, int arg)
                                                   >> 266 {
                                                   >> 267   return _sos_ioctl(fd, cmd, arg);
                                                   >> 268 }
                                                   >> 269 
                                                   >> 270 
                                                   >> 271 int creat(const char *pathname, mode_t mode)
                                                   >> 272 {
                                                   >> 273   return _sos_creat(pathname, mode);
                                                   >> 274 }
                                                   >> 275 
                                                   >> 276 int link (const char *oldpath, const char *newpath)
                                                   >> 277 {
                                                   >> 278   return _sos_link(oldpath, newpath);
                                                   >> 279 }
                                                   >> 280 
                                                   >> 281 
                                                   >> 282 int unlink(const char *pathname)
                                                   >> 283 {
                                                   >> 284   return _sos_unlink(pathname);
                                                   >> 285 }
                                                   >> 286 
                                                   >> 287 
                                                   >> 288 int rename(const char *oldpath, const char *newpath)
                                                   >> 289 {
                                                   >> 290   return _sos_rename(oldpath, newpath);
                                                   >> 291 }
                                                   >> 292 
                                                   >> 293 
                                                   >> 294 int symlink(const char *target, const char *path)
                                                   >> 295 {
                                                   >> 296   return _sos_symlink(target, path);
                                                   >> 297 }
                                                   >> 298 
                                                   >> 299 
                                                   >> 300 int mknod(const char *pathname, mode_t mode,
                                                   >> 301           int type,
                                                   >> 302           unsigned int major, unsigned minor)
                                                   >> 303 {
                                                   >> 304   if (type == S_IFREG)
                                                   >> 305     return creat(pathname, mode);
                                                   >> 306   
                                                   >> 307   return _sos_mknod(pathname, mode, type,
                                                   >> 308                     major, minor);
                                                   >> 309 }
                                                   >> 310 
                                                   >> 311 
                                                   >> 312 int mkdir(const char *pathname, mode_t mode)
                                                   >> 313 {
                                                   >> 314   return _sos_mkdir(pathname, mode);
                                                   >> 315 }
                                                   >> 316 
                                                   >> 317 
                                                   >> 318 int rmdir(const char *pathname)
                                                   >> 319 {
                                                   >> 320   return _sos_rmdir(pathname);
                                                   >> 321 }
                                                   >> 322 
                                                   >> 323 
                                                   >> 324 int chmod(const char *path, mode_t mode)
                                                   >> 325 {
                                                   >> 326   return _sos_chmod(path, mode);
                                                   >> 327 }
                                                   >> 328 
                                                   >> 329 
                                                   >> 330 struct sos_DIR_struct
                                                   >> 331 {
                                                   >> 332   int fd;
                                                   >> 333   struct dirent dirent;
                                                   >> 334 };
                                                   >> 335 
                                                   >> 336 
                                                   >> 337 DIR *opendir(const char *name)
                                                   >> 338 {
                                                   >> 339   DIR * result = malloc(sizeof(DIR));
                                                   >> 340   if (! result)
                                                   >> 341     return NULL;
                                                   >> 342 
                                                   >> 343   result->fd = _sos_open(name, O_DIRECTORY | O_RDONLY, 0);
                                                   >> 344   return result;
                                                   >> 345 }
                                                   >> 346 
                                                   >> 347 
                                                   >> 348 int dirfd(const DIR * dir)
                                                   >> 349 {
                                                   >> 350   if (dir)
                                                   >> 351     return dir->fd;
                                                   >> 352   return -1;
                                                   >> 353 }
                                                   >> 354 
                                                   >> 355 
                                                   >> 356 struct dirent *readdir(DIR *dir)
                                                   >> 357 {
                                                   >> 358   int retval = _sos_readdir(dir->fd, & dir->dirent);
                                                   >> 359   if (retval < 0)
                                                   >> 360     return NULL;
                                                   >> 361   return & dir->dirent;
                                                   >> 362 }
                                                   >> 363 
                                                   >> 364 
                                                   >> 365 int closedir(DIR *dir)
                                                   >> 366 {
                                                   >> 367   close(dir->fd);
                                                   >> 368   free(dir);
                                                   >> 369   return 0;
                                                   >> 370 }
                                                   >> 371 
                                                   >> 372 
                                                   >> 373 int stat(const char *file_name, struct stat *buf)
                                                   >> 374 {
                                                   >> 375   return _sos_stat(file_name, TRUE, buf);
                                                   >> 376 }
                                                   >> 377 
                                                   >> 378 
                                                   >> 379 int lstat(const char *file_name, struct stat *buf)
                                                   >> 380 {
                                                   >> 381   return _sos_stat(file_name, FALSE, buf);
                                                   >> 382 }
                                                   >> 383 
                                                   >> 384 
                                                   >> 385 int chroot(const char *path)
                                                   >> 386 {
                                                   >> 387   return _sos_chroot(path);
                                                   >> 388 }
                                                   >> 389 
                                                   >> 390 
                                                   >> 391 int chdir(const char *path)
                                                   >> 392 {
                                                   >> 393   return _sos_chdir(path);
                                                   >> 394 }
                                                   >> 395 
                                                   >> 396 int fchdir(int fd)
                                                   >> 397 {
                                                   >> 398   return _sos_fchdir(fd);
                                                   >> 399 }
                                                   >> 400 
                                                   >> 401 
                                                   >> 402 
                                                   >> 403 int printf (const char *format, ...)
                                                   >> 404 {
                                                   >> 405   char buff[4096];
                                                   >> 406   va_list ap;
                                                   >> 407 
                                                   >> 408   va_start(ap, format);
                                                   >> 409   vsnprintf(buff, sizeof(buff), format, ap);
                                                   >> 410   va_end(ap);
                                                   >> 411 
                                                   >> 412   return write (1, buff, strlen(buff));
                                                   >> 413 }
                                                   >> 414 
                                                   >> 415 
                                                      

source navigation ] diff markup ] identifier search ] general search ]