⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 libc.c

📁 Simple Operating Systems (简称SOS)是一个可以运行在X86平台上(包括QEMU
💻 C
字号:
/* Copyright (C) 2005 David Decotigny   This program is free software; you can redistribute it and/or   modify it under the terms of the GNU General Public License   as published by the Free Software Foundation; either version 2   of the License, or (at your option) any later version.      This program is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.      You should have received a copy of the GNU General Public License   along with this program; if not, write to the Free Software   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,   USA. */#include "crt.h"#include "libc.h"#include "stdarg.h"void * fakemmap(void *start, size_t length, int prot , int flags,		const char *path, loff_t offset){  /* At kernel side, offset is considered positive */  if (offset < 0)    return NULL;  if (0 != _sos_fakemmap(& start, length, prot, flags, path, offset))    return NULL;  return start;}void * mremap(void * old_addr, size_t old_len,	      size_t new_len,	      unsigned long flags){  void * new_uaddr = old_addr;  if (0 != _sos_mresize(old_addr, old_len, & new_uaddr, new_len, flags))    return NULL;  return new_uaddr;}/** * The presence of this global variable without any protected access * to it explains why the "brk/sbrk" functions below are MT-unsafe ! */static void * kernel_heap_top = NULL;int brk(void *end_data_seg){  if (! end_data_seg)    return -1;  kernel_heap_top = _sos_brk(end_data_seg);  return 0;}void *sbrk(ptrdiff_t increment){  if (! kernel_heap_top)    kernel_heap_top = _sos_brk(0);  kernel_heap_top = _sos_brk(kernel_heap_top + increment);  return kernel_heap_top;}void * calloc (size_t nmemb, size_t size){  return malloc(nmemb * size);}/** * The presence of this global variable without any protected access * to it explains why the "malloc/calloc" functions below are * MT-unsafe ! */static void * malloc_heap_top = NULL;void * malloc (size_t size){  void * retval;  if (size <= 0)    return NULL;  /* Align on a 4B boundary */  size = ((size-1) & ~3) + 4;  if (! kernel_heap_top)    kernel_heap_top = _sos_brk(0);  if (! malloc_heap_top)    malloc_heap_top = kernel_heap_top;  retval = malloc_heap_top;  malloc_heap_top += size;  _sos_brk(malloc_heap_top);  return retval;}void free(void *ptr){  //  bochs_printf("Free ignored (not implemented yet)\n");}int open(const char *pathname, int flags, /* mode_t mode */...){  va_list ap;  unsigned int mode = 0;   va_start(ap, flags);  if (flags & O_CREAT)    mode = va_arg(ap, unsigned int);  va_end(ap);   return _sos_open(pathname, flags, mode);}int read(int fd, char * buf, size_t len){  int retval = _sos_read(fd, buf, & len);  if (retval < 0)    return retval;  return len;}int write(int fd, const char * buf, size_t len){  int retval = _sos_write(fd, buf, & len);  if (retval < 0)    return retval;  return len;}off_t lseek(int fd, off_t offset, int whence){  loff_t result = offset;  int retval = _sos_seek64(fd, & result, whence);  if (retval < 0)    return retval;  return result;}loff_t lseek64(int fd, loff_t offset, int whence){  loff_t result = offset;  int retval = _sos_seek64(fd, & result, whence);  if (retval < 0)    return retval;  return result;}void * mmap(void *start, size_t length, int prot , int flags,	    int fd, loff_t offset){  /* At kernel side, offset is considered positive */  if (offset < 0)    return NULL;  if (0 != _sos_fmmap(& start, length, prot, flags, fd, offset))    return NULL;  return start;}int ftruncate(int fd, off_t length){  return _sos_ftruncate64(fd, length);}struct sos_DIR_struct{  int fd;  struct dirent dirent;};DIR *opendir(const char *name){  DIR * result = malloc(sizeof(DIR));  if (! result)    return NULL;  result->fd = _sos_open(name, O_DIRECTORY | O_RDONLY, 0);  return result;}int dirfd(const DIR * dir){  if (dir)    return dir->fd;  return -1;}struct dirent *readdir(DIR *dir){  int retval = _sos_readdir(dir->fd, & dir->dirent);  if (retval < 0)    return NULL;  return & dir->dirent;}int closedir(DIR *dir){  close(dir->fd);  free(dir);  return 0;}int stat(const char *file_name, struct stat *buf){  return _sos_stat(file_name, TRUE, buf);}int lstat(const char *file_name, struct stat *buf){  return _sos_stat(file_name, FALSE, buf);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -