fork download
  1. ////////////////////////////// small_libc.c /////////////////////////////////////
  2.  
  3.  
  4. #include "small_libc_conf.h"
  5. #define NULL 0x0
  6.  
  7. #if _USE_STRCPY_
  8. char *strcpy(char *dst, const char *src) {
  9. char *dstSave=dst;
  10. int c;
  11.  
  12. do {
  13. c = *dst++ = *src++;
  14. } while(c);
  15.  
  16. return dstSave;
  17. }
  18. #endif
  19.  
  20. #if _USE_STRNCPY_
  21. char *strncpy(char *dst, const char *src, int count) {
  22. int c = 1;
  23. char *dstSave = dst;
  24.  
  25. while(count-- > 0 && c)
  26. c = *dst++ = *src++;
  27.  
  28. *dst = 0;
  29. return dstSave;
  30. }
  31. #endif
  32.  
  33.  
  34. #if _USE_STRCAT_
  35. char *strcat(char *dst, const char *src) {
  36. int c;
  37. char *dstSave=dst;
  38.  
  39. while(*dst)
  40. ++dst;
  41.  
  42. do {
  43. c = *dst++ = *src++;
  44. } while(c);
  45.  
  46. return dstSave;
  47. }
  48. #endif
  49.  
  50.  
  51. #if _USE_STRNCAT_
  52. char *strncat(char *dst, const char *src, int count) {
  53. int c = 1;
  54. char *dstSave = dst;
  55.  
  56. while(*dst && --count > 0)
  57. ++dst;
  58. while(--count > 0 && c)
  59. c = *dst++ = *src++;
  60. *dst = 0;
  61. return dstSave;
  62. }
  63. #endif
  64.  
  65.  
  66. #if _USE_STRCMP_
  67. int strcmp(const char *string1, const char *string2) {
  68. int diff, c;
  69. for(;;) {
  70. diff = *string1++ - (c = *string2++);
  71.  
  72. if(diff) return diff;
  73. if(c == 0) return 0;
  74. }
  75. }
  76. #endif
  77.  
  78.  
  79. #if _USE_STRNCMP_
  80. int strncmp(const char *string1, const char *string2, int count) {
  81. int diff, c;
  82. while(count-- > 0) {
  83. diff = *string1++ - (c = *string2++);
  84.  
  85. if(diff) return diff;
  86. if(c == 0) return 0;
  87. }
  88. return 0;
  89. }
  90. #endif
  91.  
  92.  
  93. #if _USE_STRSTR_
  94. char *strstr(const char *string, const char *find) {
  95. int i;
  96. for(;;) {
  97. for(i = 0; string[i] == find[i] && find[i]; ++i) ;
  98.  
  99. if(find[i] == 0) return (char*)string;
  100. if(*string++ == 0) return NULL;
  101. }
  102. }
  103. #endif
  104.  
  105.  
  106. #if _USE_STRLEN_
  107. int strlen(const char *string) {
  108. const char *base=string;
  109. while(*string++) ;
  110. return string - base - 1;
  111. }
  112. #endif
  113.  
  114.  
  115. #if _USE_MEMCPY_
  116. void *memcpy(void *dst, const void *src, unsigned long bytes) {
  117. if(((unsigned long)dst | (unsigned long)src | bytes) & 3) {
  118. unsigned char *Dst = (unsigned char*)dst, *Src = (unsigned char*)src;
  119. while((int)bytes-- > 0)
  120. *Dst++ = *Src++;
  121. } else {
  122. unsigned long *Dst32 = (unsigned long*)dst, *Src32 = (unsigned long*)src;
  123. bytes >>= 2;
  124. while((int)bytes-- > 0)
  125. *Dst32++ = *Src32++;
  126. }
  127.  
  128. return dst;
  129. }
  130. #endif
  131.  
  132.  
  133. #if _USE_MEMMOVE_
  134. void *memmove(void *dst, const void *src, unsigned long bytes) {
  135. unsigned char *Dst = (unsigned char*)dst;
  136. unsigned char *Src = (unsigned char*)src;
  137.  
  138. if(Dst < Src) {
  139. while((int)bytes-- > 0)
  140. *Dst++ = *Src++;
  141. } else {
  142. Dst += bytes;
  143. Src += bytes;
  144. while((int)bytes-- > 0)
  145. *--Dst = *--Src;
  146. }
  147. return dst;
  148. }
  149. #endif
  150.  
  151.  
  152. #if _USE_MEMCMP_
  153. int memcmp(const void *cs, const void *ct, unsigned long bytes) {
  154. unsigned char *Dst = (unsigned char*)cs;
  155. unsigned char *Src = (unsigned char*)ct;
  156. int diff;
  157.  
  158. while((int)bytes-- > 0) {
  159. diff = *Dst++ - *Src++;
  160. if(diff) return diff;
  161. }
  162.  
  163. return 0;
  164. }
  165. #endif
  166.  
  167.  
  168. #if _USE_MEMSET_
  169. void *memset(void *dst, int c, unsigned long bytes) {
  170. unsigned char *Dst = (unsigned char*)dst;
  171. while((int)bytes-- > 0)
  172. *Dst++ = (unsigned char)c;
  173. return dst;
  174. }
  175. #endif
  176.  
  177.  
  178. #if _USE_ABS_
  179. int abs(int n) {
  180. return n>=0 ? n : -n;
  181. }
  182. #endif
  183.  
  184.  
  185. #if _USE_RAND_
  186. static unsigned int Rand1=0x1f2bcda3;
  187. int rand(void) {
  188. Rand1 = 1664525 * Rand1 + 1013904223; //from D.E. Knuth and H.W. Lewis
  189. return Rand1;
  190. }
  191.  
  192.  
  193. void srand(unsigned int seed) {
  194. Rand1 = seed;
  195. }
  196. #endif
  197.  
  198.  
  199. #if _USE_STRTOL_
  200. long strtol(const char *s, char **end, int base) {
  201. int i;
  202. unsigned long ch, value=0, neg=0;
  203.  
  204. if(s[0] == '-') {
  205. neg = 1;
  206. ++s;
  207. }
  208.  
  209. if(s[0] == '0' && s[1] == 'x') {
  210. base = 16;
  211. s += 2;
  212. }
  213.  
  214. for(i = 0; i <= 8; ++i) {
  215. ch = *s++;
  216. if('0' <= ch && ch <= '9')
  217. ch -= '0';
  218. else if('A' <= ch && ch <= 'Z')
  219. ch = ch - 'A' + 10;
  220. else if('a' <= ch && ch <= 'z')
  221. ch = ch - 'a' + 10;
  222. else
  223. break;
  224. value = value * base + ch;
  225. }
  226.  
  227. if(end) *end = (char*)s - 1;
  228. if(neg) value = -(int)value;
  229. return value;
  230. }
  231. #endif
  232.  
  233.  
  234. #if _USE_ATOI_
  235. int atoi(const char *s) {
  236. return strtol(s, NULL, 10);
  237. }
  238. #endif
  239.  
  240.  
  241. #if _USE_ITOA_
  242. char *itoa(int num, char *dst, int base) {
  243. int digit, negate=0, place;
  244. char c, text[20];
  245.  
  246. if(base == 10 && num < 0) {
  247. num = -num;
  248. negate = 1;
  249. }
  250.  
  251. text[16] = 0;
  252.  
  253. for(place = 15; place >= 0; --place) {
  254. digit = (unsigned int)num % (unsigned int)base;
  255.  
  256. if(num == 0 && place < 15 && base == 10 && negate) {
  257. c = '-';
  258. negate = 0;
  259. } else if(digit < 10)
  260. c = (char)('0' + digit);
  261. else
  262. c = (char)('a' + digit - 10);
  263. text[place] = c;
  264. num = (unsigned int)num / (unsigned int)base;
  265. if(num == 0 && negate == 0)
  266. break;
  267. }
  268. strcpy(dst, text + place);
  269. return dst;
  270. }
  271. #endif
  272.  
  273.  
  274. #if _USE_ISLOWER_
  275. int islower(int c) {
  276. return (c >= 'a' && c <= 'z');
  277. }
  278. #endif
  279.  
  280.  
  281. #if _USE_ISUPPER_
  282. int isupper(int c) {
  283. return (c >= 'A' && c <= 'Z');
  284. }
  285. #endif
  286.  
  287.  
  288. #if _USE_TOUPPER_
  289. int toupper(int c) {
  290. return (c & ~' ');
  291. }
  292. #endif
  293.  
  294.  
  295. #if _USE_TOLOWER_
  296. int tolower(int c) {
  297. return (c | ' ');
  298. }
  299. #endif
  300.  
  301. ////////////////////////////// small_libc.h /////////////////////////////////////
  302. #ifndef __SMALL_LIBC_H_
  303. #define __SMALL_LIBC_H_
  304.  
  305. char *strcpy(char *dst, const char *src);
  306. char *strncpy(char *dst, const char *src, int count);
  307. char *strcat(char *dst, const char *src);
  308. char *strncat(char *dst, const char *src, int count);
  309. int strcmp(const char *string1, const char *string2);
  310. int strncmp(const char *string1, const char *string2, int count);
  311. char *strstr(const char *string, const char *find);
  312. int strlen(const char *string);
  313. void *memcpy(void *dst, const void *src, unsigned long bytes);
  314. void *memmove(void *dst, const void *src, unsigned long bytes);
  315. int memcmp(const void *cs, const void *ct, unsigned long bytes);
  316. void *memset(void *dst, int c, unsigned long bytes);
  317. int abs(int n);
  318. int rand(void);
  319. void srand(unsigned int seed);
  320. long strtol(const char *s, char **end, int base);
  321. int atoi(const char *s);
  322. char *itoa(int num, char *dst, int base);
  323. int islower(int c);
  324. int isupper(int c);
  325. int toupper(int c);
  326. int tolower(int c);
  327.  
  328. #endif
  329.  
  330. ////////////////////////////// small_libc_conf.h /////////////////////////////////////
  331. #ifndef __SMALL_LIBC_CONF_H__
  332. #define __SMALL_LIBC_CONF_H__
  333.  
  334. #define _USE_STRCPY_ 1
  335. #define _USE_STRNCPY_ 1
  336. #define _USE_STRNCAT_ 1
  337. #define _USE_STRCMP_ 1
  338. #define _USE_STRNCMP_ 1
  339. #define _USE_STRSTR_ 1
  340. #define _USE_STRLEN_ 1
  341. #define _USE_MEMCPY_ 1
  342. #define _USE_MEMMOVE_ 1
  343. #define _USE_MEMCMP_ 1
  344. #define _USE_MEMSET_ 1
  345. #define _USE_ABS_ 1
  346. #define _USE_RAND_ 1
  347. #define _USE_SRAND_ 1
  348. #define _USE_STRTOL_ 1
  349. #define _USE_ATOI_ 1
  350. #define _USE_ITOA_ 1
  351. #define _USE_ISLOWER_ 1
  352. #define _USE_ISUPPER_ 1
  353. #define _USE_TOUPPER_ 1
  354. #define _USE_TOLOWER_ 1
  355.  
  356. #endif
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty