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 "types.h" 00002 #include "stat.h" 00003 #include "fcntl.h" 00004 #include "user.h" 00005 #include "x86.h" 00006 #define STRALIGN(x) (((unsigned long)x&7)?8-((unsigned long)x&7):0) 00007 #define UNALIGNED(x,y) (((unsigned long)x & (sizeof (unsigned long)-1)) ^ ((unsigned long)y & (sizeof (unsigned long)-1))) 00008 char * 00009 strncpy(char *dest,char *src, int n) 00010 { 00011 int i; 00012 00013 for (i = 0; i < n && src[i] != '\0'; i++) 00014 dest[i] = src[i]; 00015 for ( ; i < n; i++) 00016 dest[i] = '\0'; 00017 00018 return dest; 00019 } 00020 char* 00021 strcpy(char *s, char *t) 00022 { 00023 char *os; 00024 00025 os = s; 00026 while((*s++ = *t++) != 0) 00027 ; 00028 return os; 00029 } 00030 00031 char* 00032 strcat(char *s, char *t) 00033 { 00034 char *os; 00035 os = s; 00036 while(*s++ != 0) 00037 ; 00038 s--; 00039 while((*s++ = *t++) != 0){ 00040 } 00041 *s = 0; 00042 return os; 00043 } 00044 int 00045 strcmp(const char *p, const char *q) 00046 { 00047 while(*p && *p == *q) 00048 p++, q++; 00049 if( ( (uchar)*p - (uchar)*q ) == 0 ) 00050 return 0; 00051 return ( (( uchar)*p - (uchar)*q ) > 0 ? 1 : -1); 00052 } 00053 int 00054 strncmp(const char *s1, const char *s2,int n) 00055 {/* 00056 while(*p && *p == *q && n>0) 00057 p++, q++,n--; 00058 if( ( (uchar)*p - (uchar)*q ) == 0 ) 00059 return 0; 00060 return ( (( uchar)*p - (uchar)*q ) > 0 ? 1 : -1); 00061 */ 00062 register const unsigned char* a=(const unsigned char*)s1; 00063 register const unsigned char* b=(const unsigned char*)s2; 00064 register const unsigned char* fini=a+n; 00065 while (a!=fini) { 00066 register int res=*a-*b; 00067 if (res) return res; 00068 if (!*a) return 0; 00069 ++a; ++b; 00070 } 00071 return 0; 00072 } 00073 int 00074 memcmp(const void *s1, const void *s2,int n) 00075 {/* 00076 while(*p && *p == *q && n>0) 00077 p++, q++,n--; 00078 if( ( (uchar)*p - (uchar)*q ) == 0 ) 00079 return 0; 00080 return ( (( uchar)*p - (uchar)*q ) > 0 ? 1 : -1); 00081 */ 00082 register const unsigned char* a=(const unsigned char*)s1; 00083 register const unsigned char* b=(const unsigned char*)s2; 00084 register const unsigned char* fini=a+n; 00085 while (a!=fini) { 00086 register int res=*a-*b; 00087 if (res) return res; 00088 if (!*a) return 0; 00089 ++a; ++b; 00090 } 00091 return 0; 00092 } 00093 uint 00094 strlen(char *s) 00095 { 00096 int n; 00097 00098 for(n = 0; s[n]; n++) 00099 ; 00100 return n; 00101 } 00102 00103 void* 00104 memset(void *dst, int c, uint n) 00105 { 00106 stosb(dst, c, n); 00107 return dst; 00108 } 00109 00110 char* 00111 strchr(const char *s, char c) 00112 { 00113 for(; *s; s++) 00114 if(*s == c) 00115 return (char*)s; 00116 return 0; 00117 } 00118 00119 char* 00120 gets(char *buf, int max) 00121 { 00122 int i, cc; 00123 char c; 00124 00125 for(i=0; i+1 < max; ){ 00126 cc = read(0, &c, 1); 00127 if(cc < 1) 00128 break; 00129 buf[i++] = c; 00130 if(c == '\n' || c == '\r') 00131 break; 00132 } 00133 buf[i] = '\0'; 00134 return buf; 00135 } 00136 00137 int 00138 stat(char *n, struct stat *st) 00139 { 00140 int fd; 00141 int r; 00142 00143 fd = open(n, O_RDONLY); 00144 if(fd < 0) 00145 return -1; 00146 r = fstat(fd, st); 00147 r = st->size; 00148 close(fd); 00149 return r; 00150 } 00151 int 00152 fsstat(int fd, struct stat *st) 00153 { 00154 int r; 00155 00156 if(fd < 0) 00157 return -1; 00158 r = fstat(fd, st); 00159 r = st->size; 00160 // close(fd); 00161 return r; 00162 } 00163 int 00164 atoi(const char *s) 00165 { 00166 int n; 00167 00168 n = 0; 00169 while('0' <= *s && *s <= '9') 00170 n = n*10 + *s++ - '0'; 00171 return n; 00172 } 00173 /*void* 00174 memcpy(void *vdst, void *vsrc, int n){ 00175 return memmove(vdst,vsrc,n); 00176 }*/ 00177 void * 00178 memcpy (void *dst, void *src, int n) 00179 { 00180 void *res = dst; 00181 unsigned char *c1, *c2; 00182 c1 = (unsigned char *) dst; 00183 c2 = (unsigned char *) src; 00184 while (n--) *c1++ = *c2++; 00185 return (res); 00186 /* 00187 int tmp; 00188 unsigned long *lx1 = NULL; 00189 const unsigned long *lx2 = NULL; 00190 00191 if (!UNALIGNED(dst, src) && n > sizeof(unsigned long)) { 00192 00193 if ((tmp = STRALIGN(dst))) { 00194 c1 = (unsigned char *) dst; 00195 c2 = (unsigned char *) src; 00196 while (tmp-- && n--) 00197 *c1++ = *c2++; 00198 if (n == (int) - 1) 00199 return (res); 00200 dst = c1; 00201 src = c2; 00202 } 00203 00204 lx1 = (unsigned long *) dst; 00205 lx2 = (unsigned long *) src; 00206 00207 for (; n >= sizeof(unsigned long); n -= sizeof(unsigned long)) 00208 *lx1++ = *lx2++; 00209 } 00210 00211 if (n) { 00212 c1 = (unsigned char *) (lx1?lx1:dst); 00213 c2 = (unsigned char *) (lx1?lx2:src); 00214 while (n--) 00215 *c1++ = *c2++; 00216 } 00217 00218 return (res); 00219 */ 00220 } 00221 void* 00222 memmove(void *vdst, void *vsrc, int n) 00223 { 00224 char *dst, *src; 00225 00226 dst = vdst; 00227 src = vsrc; 00228 while(n-- > 0) 00229 *dst++ = *src++; 00230 return vdst; 00231 } 00232 /* 00233 double 00234 pow(double mant, double expo){ 00235 unsigned int e; 00236 long double ret; 00237 00238 if ( mant == 0. ) { 00239 if ( expo > 0. ) 00240 return 0.; 00241 else if ( expo == 0. ) 00242 return 1.; 00243 else 00244 return 1./mant; 00245 } 00246 00247 if ( expo == (int) (e = (int) expo) ) { 00248 00249 if ( (int)e < 0 ) { 00250 e = -e; 00251 mant = 1./mant; 00252 } 00253 00254 ret = 1.; 00255 00256 while (1) { 00257 if ( e & 1 ) 00258 ret *= mant; 00259 if ( (e >>= 1) == 0 ) 00260 break; 00261 mant *= mant; 00262 } 00263 return ret; 00264 } 00265 return exp ( log (mant) * expo ); 00266 }*/