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 8) and /userland/libc.c (Article 9.5)


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 "libc.h"                              !! 020 #include "string.h"
021 #include "stdarg.h"                               021 #include "stdarg.h"
022                                                   022 
                                                   >> 023 #include "libc.h"
023                                                   024 
024 void * fakemmap(void *start, size_t length, in !! 025 void exit (int status)
025                 const char *path, loff_t offse << 
026 {                                                 026 {
027   /* At kernel side, offset is considered posi !! 027   _sos_exit(status);
028   if (offset < 0)                              !! 028 }
029     return NULL;                               << 
030                                                   029 
031   if (0 != _sos_fakemmap(& start, length, prot << 
032     return NULL;                               << 
033                                                   030 
034   return start;                                !! 031 pid_t fork(void)
                                                   >> 032 {
                                                   >> 033   return _sos_fork();
                                                   >> 034 }
                                                   >> 035 
                                                   >> 036 
                                                   >> 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);
035 }                                                 060 }
036                                                   061 
037                                                   062 
038 void * mremap(void * old_addr, size_t old_len,    063 void * mremap(void * old_addr, size_t old_len,
039               size_t new_len,                     064               size_t new_len,
040               unsigned long flags)                065               unsigned long flags)
041 {                                                 066 {
042   void * new_uaddr = old_addr;                    067   void * new_uaddr = old_addr;
043   if (0 != _sos_mresize(old_addr, old_len, & n    068   if (0 != _sos_mresize(old_addr, old_len, & new_uaddr, new_len, flags))
044     return NULL;                                  069     return NULL;
045                                                   070 
046   return new_uaddr;                               071   return new_uaddr;
047 }                                                 072 }
048                                                   073 
049                                                   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 
050 /**                                               087 /**
051  * The presence of this global variable withou    088  * The presence of this global variable without any protected access
052  * to it explains why the "brk/sbrk" functions    089  * to it explains why the "brk/sbrk" functions below are MT-unsafe !
053  */                                               090  */
054 static void * kernel_heap_top = NULL;             091 static void * kernel_heap_top = NULL;
055 int brk(void *end_data_seg)                       092 int brk(void *end_data_seg)
056 {                                                 093 {
057   if (! end_data_seg)                             094   if (! end_data_seg)
058     return -1;                                    095     return -1;
059                                                   096 
060   kernel_heap_top = _sos_brk(end_data_seg);       097   kernel_heap_top = _sos_brk(end_data_seg);
061   return 0;                                       098   return 0;
062 }                                                 099 }
063                                                   100 
064                                                   101 
065 void *sbrk(ptrdiff_t increment)                   102 void *sbrk(ptrdiff_t increment)
066 {                                                 103 {
067   if (! kernel_heap_top)                          104   if (! kernel_heap_top)
068     kernel_heap_top = _sos_brk(0);                105     kernel_heap_top = _sos_brk(0);
069                                                   106 
070   kernel_heap_top = _sos_brk(kernel_heap_top +    107   kernel_heap_top = _sos_brk(kernel_heap_top + increment);
071   return kernel_heap_top;                         108   return kernel_heap_top;
072 }                                                 109 }
073                                                   110 
074                                                   111 
075 void * calloc (size_t nmemb, size_t size)         112 void * calloc (size_t nmemb, size_t size)
076 {                                                 113 {
077   return malloc(nmemb * size);                    114   return malloc(nmemb * size);
078 }                                                 115 }
079                                                   116 
080                                                   117 
081 /**                                               118 /**
082  * The presence of this global variable withou    119  * The presence of this global variable without any protected access
083  * to it explains why the "malloc/calloc" func    120  * to it explains why the "malloc/calloc" functions below are
084  * MT-unsafe !                                    121  * MT-unsafe !
085  */                                               122  */
086 static void * malloc_heap_top = NULL;             123 static void * malloc_heap_top = NULL;
087 void * malloc (size_t size)                       124 void * malloc (size_t size)
088 {                                                 125 {
089   void * retval;                                  126   void * retval;
090                                                   127 
091   if (size <= 0)                                  128   if (size <= 0)
092     return NULL;                                  129     return NULL;
093                                                   130 
094   /* Align on a 4B boundary */                    131   /* Align on a 4B boundary */
095   size = ((size-1) & ~3) + 4;                     132   size = ((size-1) & ~3) + 4;
096                                                   133 
097   if (! kernel_heap_top)                          134   if (! kernel_heap_top)
098     kernel_heap_top = _sos_brk(0);                135     kernel_heap_top = _sos_brk(0);
099                                                   136 
100   if (! malloc_heap_top)                          137   if (! malloc_heap_top)
101     malloc_heap_top = kernel_heap_top;            138     malloc_heap_top = kernel_heap_top;
102                                                   139 
103   retval = malloc_heap_top;                       140   retval = malloc_heap_top;
104   malloc_heap_top += size;                        141   malloc_heap_top += size;
105                                                   142 
106   _sos_brk(malloc_heap_top);                      143   _sos_brk(malloc_heap_top);
107   return retval;                                  144   return retval;
108 }                                                 145 }
109                                                   146 
110                                                   147 
111 void free(void *ptr)                              148 void free(void *ptr)
112 {                                                 149 {
113   //  bochs_printf("Free ignored (not implemen    150   //  bochs_printf("Free ignored (not implemented yet)\n");
114 }                                                 151 }
115                                                   152 
116                                                   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 
117 int open(const char *pathname, int flags, /* m    181 int open(const char *pathname, int flags, /* mode_t mode */...)
118 {                                                 182 {
119   va_list ap;                                     183   va_list ap;
120   unsigned int mode = 0;                          184   unsigned int mode = 0;
121                                                   185  
122   va_start(ap, flags);                            186   va_start(ap, flags);
123   if (flags & O_CREAT)                            187   if (flags & O_CREAT)
124     mode = va_arg(ap, unsigned int);              188     mode = va_arg(ap, unsigned int);
125   va_end(ap);                                     189   va_end(ap);
126                                                   190  
127   return _sos_open(pathname, flags, mode);        191   return _sos_open(pathname, flags, mode);
128 }                                                 192 }
129                                                   193 
130                                                   194 
                                                   >> 195 int close(int fd)
                                                   >> 196 {
                                                   >> 197   return _sos_close(fd);
                                                   >> 198 }
                                                   >> 199 
                                                   >> 200 
131 int read(int fd, char * buf, size_t len)          201 int read(int fd, char * buf, size_t len)
132 {                                                 202 {
133   int retval = _sos_read(fd, buf, & len);         203   int retval = _sos_read(fd, buf, & len);
134   if (retval < 0)                                 204   if (retval < 0)
135     return retval;                                205     return retval;
136   return len;                                     206   return len;
137 }                                                 207 }
138                                                   208 
139                                                   209 
140 int write(int fd, const char * buf, size_t len    210 int write(int fd, const char * buf, size_t len)
141 {                                                 211 {
142   int retval = _sos_write(fd, buf, & len);        212   int retval = _sos_write(fd, buf, & len);
143   if (retval < 0)                                 213   if (retval < 0)
144     return retval;                                214     return retval;
145   return len;                                     215   return len;
146 }                                                 216 }
147                                                   217 
148                                                   218 
149 off_t lseek(int fd, off_t offset, int whence)     219 off_t lseek(int fd, off_t offset, int whence)
150 {                                                 220 {
151   loff_t result = offset;                         221   loff_t result = offset;
152   int retval = _sos_seek64(fd, & result, whenc    222   int retval = _sos_seek64(fd, & result, whence);
153   if (retval < 0)                                 223   if (retval < 0)
154     return retval;                                224     return retval;
155   return result;                                  225   return result;
156 }                                                 226 }
157                                                   227 
158                                                   228 
159 loff_t lseek64(int fd, loff_t offset, int when    229 loff_t lseek64(int fd, loff_t offset, int whence)
160 {                                                 230 {
161   loff_t result = offset;                         231   loff_t result = offset;
162   int retval = _sos_seek64(fd, & result, whenc    232   int retval = _sos_seek64(fd, & result, whence);
163   if (retval < 0)                                 233   if (retval < 0)
164     return retval;                                234     return retval;
165   return result;                                  235   return result;
166 }                                                 236 }
167                                                   237 
168                                                   238 
169 void * mmap(void *start, size_t length, int pr    239 void * mmap(void *start, size_t length, int prot , int flags,
170             int fd, loff_t offset)                240             int fd, loff_t offset)
171 {                                                 241 {
172   /* At kernel side, offset is considered posi    242   /* At kernel side, offset is considered positive */
173   if (offset < 0)                                 243   if (offset < 0)
174     return NULL;                                  244     return NULL;
175                                                   245 
176   if (0 != _sos_fmmap(& start, length, prot, f    246   if (0 != _sos_fmmap(& start, length, prot, flags, fd, offset))
177     return NULL;                                  247     return NULL;
178                                                   248 
179   return start;                                   249   return start;
180 }                                                 250 }
181                                                   251 
182                                                   252 
183 int ftruncate(int fd, off_t length)               253 int ftruncate(int fd, off_t length)
184 {                                                 254 {
185   return _sos_ftruncate64(fd, length);            255   return _sos_ftruncate64(fd, length);
186 }                                                 256 }
187                                                   257 
188                                                   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 
189 struct sos_DIR_struct                             336 struct sos_DIR_struct
190 {                                                 337 {
191   int fd;                                         338   int fd;
192   struct dirent dirent;                           339   struct dirent dirent;
193 };                                                340 };
194                                                   341 
195                                                   342 
196 DIR *opendir(const char *name)                    343 DIR *opendir(const char *name)
197 {                                                 344 {
198   DIR * result = malloc(sizeof(DIR));             345   DIR * result = malloc(sizeof(DIR));
199   if (! result)                                   346   if (! result)
200     return NULL;                                  347     return NULL;
201                                                   348 
202   result->fd = _sos_open(name, O_DIRECTORY | O    349   result->fd = _sos_open(name, O_DIRECTORY | O_RDONLY, 0);
203   return result;                                  350   return result;
204 }                                                 351 }
205                                                   352 
206                                                   353 
207 int dirfd(const DIR * dir)                        354 int dirfd(const DIR * dir)
208 {                                                 355 {
209   if (dir)                                        356   if (dir)
210     return dir->fd;                               357     return dir->fd;
211   return -1;                                      358   return -1;
212 }                                                 359 }
213                                                   360 
214                                                   361 
215 struct dirent *readdir(DIR *dir)                  362 struct dirent *readdir(DIR *dir)
216 {                                                 363 {
217   int retval = _sos_readdir(dir->fd, & dir->di    364   int retval = _sos_readdir(dir->fd, & dir->dirent);
218   if (retval < 0)                                 365   if (retval < 0)
219     return NULL;                                  366     return NULL;
220   return & dir->dirent;                           367   return & dir->dirent;
221 }                                                 368 }
222                                                   369 
223                                                   370 
224 int closedir(DIR *dir)                            371 int closedir(DIR *dir)
225 {                                                 372 {
226   close(dir->fd);                                 373   close(dir->fd);
227   free(dir);                                      374   free(dir);
228   return 0;                                       375   return 0;
229 }                                                 376 }
230                                                   377 
231                                                   378 
232 int stat(const char *file_name, struct stat *b    379 int stat(const char *file_name, struct stat *buf)
233 {                                                 380 {
234   return _sos_stat(file_name, TRUE, buf);         381   return _sos_stat(file_name, TRUE, buf);
235 }                                                 382 }
236                                                   383 
237                                                   384 
238 int lstat(const char *file_name, struct stat *    385 int lstat(const char *file_name, struct stat *buf)
239 {                                                 386 {
240   return _sos_stat(file_name, FALSE, buf);        387   return _sos_stat(file_name, FALSE, buf);
241 }                                                 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 
                                                      

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