Mercurial > sos > sos
comparison src/std.c @ 94:191e99dffd6c
Moved some std.h functions to string.h, worked on a simple version of sprintf
sprintf is a very simple version of the C Standard Library version. It should
work for doubles, ints, and strings.
I also added the strlen function.
author | Jonathan Pevarnek <pevarnj@gmail.com> |
---|---|
date | Sat, 14 May 2011 12:54:29 -0400 |
parents | 480f5685b3c2 |
children | d8f21e4a75e3 |
comparison
equal
deleted
inserted
replaced
93:effa07f3c157 | 94:191e99dffd6c |
---|---|
1 #include <std.h> | 1 #include <std.h> |
2 #include <string.h> | |
2 | 3 |
3 void init_all(u64 __memsize) | 4 void init_all(u64 __memsize) |
4 { | 5 { |
5 init_io_int(); | 6 init_io_int(); |
6 init_console(); | 7 init_console(); |
120 putline(a, (b - a > CON_LEN)?CON_LEN:(b - a)); | 121 putline(a, (b - a > CON_LEN)?CON_LEN:(b - a)); |
121 a += CON_LEN; | 122 a += CON_LEN; |
122 } while(a < b); | 123 } while(a < b); |
123 } | 124 } |
124 | 125 |
125 void strcpy(char *dest, const char *src) | |
126 { | |
127 while((*dest++ = *src++)); | |
128 } | |
129 | |
130 int strcmp(const char *a, const char *b) | |
131 { | |
132 while(1) { | |
133 if(*a - *b) return *a - *b; | |
134 if(*a == '\0') return 0; | |
135 a++; | |
136 b++; | |
137 } | |
138 return -1; | |
139 } | |
140 | |
141 char* sGet(char *a, unsigned int n) | 126 char* sGet(char *a, unsigned int n) |
142 { | 127 { |
143 int length = getline(a, n); | 128 int length = getline(a, n); |
144 a[(length < n)?length:n - 1] = '\0'; | 129 a[(length < n)?length:n - 1] = '\0'; |
145 return a; | 130 return a; |
146 } | 131 } |
147 | |
148 char* append(char *dest, char *src) | |
149 { | |
150 char *ret = dest; | |
151 while(*dest&& *++dest); //get to null in first string | |
152 while((*dest++ = *src++)); | |
153 return ret; | |
154 } | |
155 | |
156 void* memcpy(void *destination, const void *source, size_t num) | |
157 { | |
158 int i; | |
159 const u8 *from = source; | |
160 u8 *to = destination; | |
161 for(i = 0; i < num; i++) | |
162 to[i] = from[i]; | |
163 return destination; | |
164 } | |
165 | |
166 | |
167 | 132 |
168 //DYNAMIC MEMORY | 133 //DYNAMIC MEMORY |
169 | 134 |
170 static Header base; | 135 static Header base; |
171 static Header *allocp = NULL; //the location of the last known free block | 136 static Header *allocp = NULL; //the location of the last known free block |
177 allocp->next = (void*)HEAP_START; | 142 allocp->next = (void*)HEAP_START; |
178 allocp->next->next = &base; | 143 allocp->next->next = &base; |
179 allocp->next->size = memSize/sizeof(blockUnit); | 144 allocp->next->size = memSize/sizeof(blockUnit); |
180 if((sizeof(blockUnit)%sizeof(u64))) { | 145 if((sizeof(blockUnit)%sizeof(u64))) { |
181 sPrint("WARNING: MEMORY NOT 8-BYTE ALIGNED\n"); | 146 sPrint("WARNING: MEMORY NOT 8-BYTE ALIGNED\n"); |
182 char foo[40]; | |
183 sPrint(append("MEMORY BLOCK SIZE IS: ", append(itoa(sizeof(Header), foo, 10), "\n"))); | |
184 } | 147 } |
185 } | 148 } |
186 | 149 |
187 void* malloc(size_t size) | 150 void* malloc(size_t size) |
188 { | 151 { |