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 "stat.h" 00004 #include "user.h" 00005 00006 static void 00007 putc(int fd, char c) 00008 { 00009 write(fd, &c, 1); 00010 } 00011 static void 00012 sprintint(char *s, int xx, int base, int sgn, int *curpointer) 00013 { 00014 static char digits[] = "0123456789ABCDEF"; 00015 char buf[16]; 00016 int i, neg, tmp; 00017 uint x; 00018 00019 neg = 0; 00020 if(sgn && xx < 0){ 00021 neg = 1; 00022 x = -xx; 00023 } else { 00024 x = xx; 00025 } 00026 00027 i = 0; 00028 do{ 00029 buf[i++] = digits[x % base]; 00030 }while((x /= base) != 0); 00031 if(neg) 00032 buf[i++] = '-'; 00033 tmp = *curpointer; 00034 *curpointer += i; 00035 while(--i >= 0) 00036 *(s + i+ tmp) = buf[i]; 00037 } 00038 00039 static void 00040 printint(int fd, int xx, int base, int sgn , int *cur) 00041 { 00042 static char digits[] = "0123456789ABCDEF"; 00043 char buf[16]; 00044 int i, neg; 00045 uint x; 00046 00047 neg = 0; 00048 if(sgn && xx < 0){ 00049 neg = 1; 00050 x = -xx; 00051 } else { 00052 x = xx; 00053 } 00054 00055 i = 0; 00056 do{ 00057 buf[i++] = digits[x % base]; 00058 }while((x /= base) != 0); 00059 if(neg) 00060 buf[i++] = '-'; 00061 *cur += i; 00062 00063 while(--i >= 0) 00064 putc(fd, buf[i]); 00065 } 00066 void 00067 sputc(char *str,char ch,int *stringPointer){ 00068 *(str + *stringPointer) = ch; 00069 (*stringPointer)++; 00070 } 00071 int snprintf(char *st,int n,char *fmt,...){ 00072 char *s; 00073 char str[512]; 00074 int c, i, state; 00075 uint *ap; 00076 state = 0; 00077 /* due to +1 it fetches the second argument*/ 00078 ap = (uint*)(void*)&fmt + 1; 00079 int stringPointer = 0; 00080 for(i = 0; fmt[i]; i++){ 00081 c = fmt[i] & 0xff; 00082 if(state == 0){ 00083 if(c == '%'){ 00084 state = '%'; 00085 } else { 00086 sputc(str, c,&stringPointer); 00087 } 00088 } else if(state == '%'){ 00089 if(c == 'd'){ 00090 sprintint(str, *ap, 10, 1,&stringPointer); 00091 ap++; 00092 } else if(c == 'x' || c == 'p'){ 00093 sprintint(str, *ap, 16, 0,&stringPointer); 00094 ap++; 00095 } else if(c == 's'){ 00096 s = (char*)*ap; 00097 ap++; 00098 if(s == 0) 00099 s = "(null)"; 00100 while(*s != 0){ 00101 sputc(str, *s,&stringPointer); 00102 s++; 00103 } 00104 } else if(c == 'c'){ 00105 sputc(str, *ap,&stringPointer); 00106 ap++; 00107 } else if(c == '%'){ 00108 sputc(str, c,&stringPointer); 00109 } else { 00110 // Unknown % sequence. Print it to draw attention. 00111 sputc(str, '%', &stringPointer); 00112 sputc(str, c , &stringPointer); 00113 } 00114 state = 0; 00115 } 00116 } 00117 i = 0; 00118 while(i < n-1 && i < stringPointer){ 00119 *(st+i) = str[i]; 00120 i++; 00121 } 00122 *(st+i) = '\0'; 00123 if(stringPointer > n ) 00124 return stringPointer - n; 00125 return n; 00126 } 00127 int 00128 printf(char *fmt, ...) 00129 { 00130 char *s; 00131 int c, i, state, stringPointer; 00132 uint *ap; 00133 stringPointer = 0; 00134 00135 state = 0; 00136 /* due to +1 it fetches the second argument*/ 00137 ap = (uint*)(void*)&fmt + 1; 00138 for(i = 0; fmt[i]; i++){ 00139 c = fmt[i] & 0xff; 00140 if(state == 0){ 00141 if(c == '%'){ 00142 state = '%'; 00143 } else { 00144 putc(1, c); 00145 stringPointer++ ; 00146 } 00147 } else if(state == '%'){ 00148 if(c == 'd'){ 00149 printint(1, *ap, 10, 1,&stringPointer); 00150 ap++; 00151 } else if(c == 'x' || c == 'p'){ 00152 printint(1, *ap, 16, 0,&stringPointer); 00153 ap++; 00154 } else if(c == 's'){ 00155 s = (char*)*ap; 00156 ap++; 00157 if(s == 0) 00158 s = "(null)"; 00159 while(*s != 0){ 00160 putc(1, *s); 00161 s++; 00162 stringPointer++ ; 00163 } 00164 } else if(c == 'c'){ 00165 putc(1, *ap); 00166 ap++; 00167 stringPointer++ ; 00168 } else if(c == '%'){ 00169 putc(1, c); 00170 stringPointer++ ; 00171 00172 } else { 00173 // Unknown % sequence. Print it to draw attention. 00174 putc(1, '%'); 00175 putc(1, c); 00176 stringPointer += 2; 00177 } 00178 state = 0; 00179 } 00180 } 00181 return stringPointer; 00182 00183 } 00184 // Print to the given fd. Only understands %d, %x, %p, %s. 00185 int sprintf(char *str,char *fmt, ...){ 00186 char *s; 00187 int c, i, state; 00188 uint *ap; 00189 state = 0; 00190 /* due to +1 it fetches the second argument*/ 00191 ap = (uint*)(void*)&fmt + 1; 00192 int stringPointer = 0; 00193 for(i = 0; fmt[i]; i++){ 00194 c = fmt[i] & 0xff; 00195 if(state == 0){ 00196 if(c == '%'){ 00197 state = '%'; 00198 } else { 00199 sputc(str, c,&stringPointer); 00200 } 00201 }else if(state == '%'){ 00202 if(c == 'd'){ 00203 sprintint(str, *ap, 10, 1,&stringPointer); 00204 ap++; 00205 } else if(c == 'x' || c == 'p'){ 00206 sprintint(str, *ap, 16, 0,&stringPointer); 00207 ap++; 00208 } else if(c == 's'){ 00209 s = (char*)*ap; 00210 ap++; 00211 if(s == 0) 00212 s = "(null)"; 00213 while(*s != 0){ 00214 sputc(str, *s,&stringPointer); 00215 s++; 00216 } 00217 } else if(c == 'c'){ 00218 sputc(str, *ap,&stringPointer); 00219 ap++; 00220 } else if(c == '%'){ 00221 sputc(str, c,&stringPointer); 00222 } else { 00223 // Unknown % sequence. Print it to draw attention. 00224 sputc(str, '%', &stringPointer); 00225 sputc(str, c , &stringPointer); 00226 } 00227 state = 0; 00228 } 00229 } 00230 return stringPointer; 00231 } 00232 int 00233 fprintf(FILE *stream,char *fmt, ...) 00234 { 00235 char *s; 00236 int c, i, state, stringPointer; 00237 uint *ap; 00238 stringPointer = 0; 00239 00240 state = 0; 00241 /* due to +1 it fetches the second argument*/ 00242 ap = (uint*)(void*)&fmt + 1; 00243 for(i = 0; fmt[i]; i++){ 00244 c = fmt[i] & 0xff; 00245 if(state == 0){ 00246 if(c == '%'){ 00247 state = '%'; 00248 } else { 00249 putc(stream->desc, c); 00250 stringPointer++ ; 00251 } 00252 } else if(state == '%'){ 00253 if(c == 'd'){ 00254 printint(stream->desc, *ap, 10, 1,&stringPointer); 00255 ap++; 00256 } else if(c == 'x' || c == 'p'){ 00257 printint(stream->desc, *ap, 16, 0,&stringPointer); 00258 ap++; 00259 } else if(c == 's'){ 00260 s = (char*)*ap; 00261 ap++; 00262 if(s == 0) 00263 s = "(null)"; 00264 while(*s != 0){ 00265 putc(stream->desc, *s); 00266 s++; 00267 stringPointer++ ; 00268 } 00269 } else if(c == 'c'){ 00270 putc(stream->desc, *ap); 00271 ap++; 00272 stringPointer++ ; 00273 } else if(c == '%'){ 00274 putc(1, c); 00275 stringPointer++ ; 00276 00277 } else { 00278 // Unknown % sequence. Print it to draw attention. 00279 putc(stream->desc, '%'); 00280 putc(stream->desc, c); 00281 stringPointer += 2; 00282 } 00283 state = 0; 00284 } 00285 } 00286 return stringPointer; 00287 00288 }