clib.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright t lefering
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * These are the four essential freedoms with GNU GPL software:
  18. * 1: freedom to run the program, for any purpose
  19. * 2: freedom to study how the program works, and change it to make it do what you wish
  20. * 3: freedom to redistribute copies to help your Free Software friends
  21. * 4: freedom to distribute copies of your modified versions to your Free Software friends
  22. * , ,
  23. * / \
  24. * ((__-^^-,-^^-__))
  25. * `-_---' `---_-'
  26. * `--|o` 'o|--'
  27. * \ ` /
  28. * ): :(
  29. * :o_o:
  30. * "-"
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. unsigned long strlen(const char *str)
  36. {
  37. const char *p = (char *)0;
  38. unsigned long i = 0;
  39. if (str == (char *)0) {
  40. return (0);
  41. }
  42. p = str;
  43. while (*p) {
  44. i++;
  45. p++;
  46. }
  47. return (i);
  48. }
  49. char *strcpy(char *s1, const char *s2)
  50. {
  51. char *res = s1;
  52. while ((*s1++ = *s2++)) {;
  53. }
  54. return (res);
  55. }
  56. char *strcat(char *s, const char *t)
  57. {
  58. char *dest = s;
  59. s += strlen(s);
  60. for (;;) {
  61. if (!(*s = *t)) {
  62. break;
  63. }
  64. ++s;
  65. ++t;
  66. }
  67. return dest;
  68. }
  69. void *memset(void *dst, int s, size_t count)
  70. {
  71. char *a = dst;
  72. count++; /* this actually creates smaller code than using count-- */
  73. while (--count) {
  74. *a++ = s;
  75. }
  76. return dst;
  77. }
  78. void *memcpy(void *dst, const void *src, size_t n)
  79. {
  80. void *res = dst;
  81. unsigned char *c1 = NULL;
  82. unsigned char *c2 = NULL;
  83. c1 = (unsigned char *)dst;
  84. c2 = (unsigned char *)src;
  85. while (n--) {
  86. *c1++ = *c2++;
  87. }
  88. return (res);
  89. }
  90. /* reverse: reverse string s in place */
  91. static void reverse(char s[])
  92. {
  93. int i, j;
  94. char c;
  95. for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
  96. c = s[i];
  97. s[i] = s[j];
  98. s[j] = c;
  99. }
  100. return;
  101. }
  102. static char bs[32];
  103. char *intoa(int n)
  104. {
  105. int i, sign;
  106. memset(bs, 0, 32);
  107. if ((sign = n) < 0) /* record sign */
  108. n = -n; /* make n positive */
  109. i = 0;
  110. do { /* generate digits in reverse order */
  111. bs[i++] = n % 10 + '0'; /* get next digit */
  112. } while ((n /= 10) > 0); /* delete it */
  113. if (sign < 0)
  114. bs[i++] = '-';
  115. bs[i] = '\0';
  116. reverse(bs);
  117. return (bs);
  118. }
  119. int atoi(const char *s)
  120. {
  121. int v = 0;
  122. int sign = 1;
  123. int ret = 0;
  124. switch (*s) {
  125. case '-':
  126. sign = -1; /* fall through */
  127. case '+':
  128. ++s;
  129. }
  130. while ((unsigned int)((*s) - '0') < 10) {
  131. v = (v * 10) + ((*s) - '0');
  132. ++s;
  133. }
  134. if (sign < 0) {
  135. ret = -v;
  136. } else {
  137. ret = v;
  138. }
  139. return (ret);
  140. }