Xv6 with picoc & Linkage editor
v1.0
The project delineate mutual cohesion between c library, linkage editor ( linker), interpreter and operating system by porting the same on xv6 kernel
|
00001 #include "./include/stdio.h" 00002 #include "types.h" 00003 #include "user.h" 00004 #include "ctype.h" 00005 00006 static FILE _io_stdout = { 00007 .desc = 1, 00008 }; 00009 FILE *stdout = &_io_stdout; 00010 static FILE _io_stdin = { 00011 .desc = 0, 00012 }; 00013 FILE *stdin = &_io_stdin; 00014 static FILE _io_stderr = { 00015 .desc = 0, 00016 }; 00017 FILE *stderr = &_io_stderr; 00018 00019 int checkmode(char *mode, int *seek){ 00020 int f; 00021 for (;;) { 00022 switch (*mode) { 00023 case 0: return f; 00024 case 'b': break; 00025 case 'r': f=O_RDONLY; break; 00026 case 'w': f=O_WRONLY|O_CREATE; break; 00027 case 'a': f=O_WRONLY|O_CREATE; *seek = 1; break; 00028 case '+': f|=O_RDWR; break; 00029 } 00030 ++mode; 00031 } 00032 } 00033 00034 FILE *fopen(char *path,char* mode){ 00035 int fd ; 00036 FILE *fp; 00037 fp = (FILE *)malloc(sizeof(FILE)); 00038 00039 if(fp == (void *)0); 00040 // printf("malloc problem\n"); 00041 00042 int m,seekset = 0; 00043 m = checkmode(mode,&seekset ); 00044 fd = open(path, m); 00045 00046 if(seekset) 00047 lseek(fd,0,2); 00048 00049 fp->desc = fd; 00050 return fp; 00051 } 00052 00053 00054 00055 int fread(void *ptr, size_t size, size_t nmemb, FILE *stream) { 00056 if(stream == (void*)0) 00057 return 0; 00058 int res; 00059 /*printf("fd = %d\n",stream->desc );*/ 00060 res = read(stream->desc,ptr,size*nmemb); 00061 return res; 00062 } 00063 00064 00065 int fwrite(void *ptr, size_t size , size_t nmemb, FILE *stream){ 00066 if(stream == (void*)0) 00067 return 0; 00068 // if STDIO is buffered implementation should be added its simple direct implememtation 00069 int res; 00070 res = write(stream->desc, ptr, size*nmemb); 00071 return res/size; 00072 } 00073 00074 00075 int fclose(FILE *fp){ 00076 int ret; 00077 if(fp == (void*)0) 00078 return 1; 00079 ret = close(fp->desc); 00080 free(fp); 00081 return ret; 00082 // exit(1); 00083 } 00084 00085 int fseek(FILE *stream, int off, int whence){ 00086 return lseek(stream->desc, off, whence); 00087 } 00088 00089 int fputs(char *s,FILE *stream){ 00090 return fwrite(s,strlen(s),1,stream); 00091 }