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

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