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

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