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

📄 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 "string.h"#include "stdarg.h"#include "libc.h"void exit (int status){  _sos_exit(status);}pid_t fork(void){  return _sos_fork();}int exec(const char *progname){  return _sos_exec(progname);}int sleep(unsigned int seconds){  return nanosleep(seconds, 0);}int nanosleep(unsigned long int sec,	      unsigned long int nanosec){  return _sos_nanosleep(sec, nanosec);}int munmap(void * start, size_t length){  return _sos_munmap(start, length);}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;}int mprotect(const void *addr, size_t len, int prot){  return _sos_mprotect(addr, len, prot);}/** * 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 mount(const char *source, const char *target,	  const char *filesystemtype, unsigned long mountflags,	  const char *data){  return _sos_mount(source, target, filesystemtype, mountflags, data);}int umount(const char *target){  return _sos_umount(target);}void sync(void){  return _sos_sync();}int statvfs(const char *path, struct statvfs *buf){  return _sos_statvfs(path, buf);}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 close(int fd){  return _sos_close(fd);}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);}int ftruncate64(int fd, loff_t length){  return _sos_ftruncate64(fd, length);}int fcntl(int fd, int cmd, int arg){  return _sos_fcntl(fd, cmd, arg);}int ioctl(int fd, int cmd, int arg){  return _sos_ioctl(fd, cmd, arg);}int creat(const char *pathname, mode_t mode){  return _sos_creat(pathname, mode);}int link (const char *oldpath, const char *newpath){  return _sos_link(oldpath, newpath);}int unlink(const char *pathname){  return _sos_unlink(pathname);}int rename(const char *oldpath, const char *newpath){  return _sos_rename(oldpath, newpath);}int symlink(const char *target, const char *path){  return _sos_symlink(target, path);}int mknod(const char *pathname, mode_t mode,	  int type,	  unsigned int major, unsigned minor){  if (type == S_IFREG)    return creat(pathname, mode);    return _sos_mknod(pathname, mode, type,		    major, minor);}int mkdir(const char *pathname, mode_t mode){  return _sos_mkdir(pathname, mode);}int rmdir(const char *pathname){  return _sos_rmdir(pathname);}int chmod(const char *path, mode_t mode){  return _sos_chmod(path, mode);}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);}int chroot(const char *path){  return _sos_chroot(path);}int chdir(const char *path){  return _sos_chdir(path);}int fchdir(int fd){  return _sos_fchdir(fd);}int printf (const char *format, ...){  char buff[4096];  va_list ap;  va_start(ap, format);  vsnprintf(buff, sizeof(buff), format, ap);  va_end(ap);  return write (1, buff, strlen(buff));}

⌨️ 快捷键说明

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