stdlib.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright (C) 2013 by John Cronin <jncronin@tysos.org>
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. * THE SOFTWARE.
  18. */
  19. #include <string.h>
  20. #include <stddef.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. int errno;
  24. void *memcpy(void *dest, const void *src, size_t n)
  25. {
  26. char *s = (char *)src;
  27. char *d = (char *)dest;
  28. while(n > 0)
  29. {
  30. *d++ = *s++;
  31. n--;
  32. }
  33. return dest;
  34. }
  35. void *memset(void *s, int c, size_t n)
  36. {
  37. char *dest = (char *)s;
  38. while(n > 0)
  39. {
  40. *dest++ = (char)c;
  41. n--;
  42. }
  43. return s;
  44. }
  45. void abort(void)
  46. {
  47. fputs("abort() called\n", stdout);
  48. fputs("abort() called\n", stderr);
  49. while(1);
  50. }
  51. char *strcpy(char *dest, const char *src)
  52. {
  53. char *d = dest;
  54. while(*src != 0)
  55. *d++ = *src++;
  56. *d = 0;
  57. return dest;
  58. }
  59. char *strcat(char *dest, const char *src)
  60. {
  61. char *d = dest;
  62. while(*d) d++;
  63. while(*src) *d++ = *src++;
  64. *d = 0;
  65. return dest;
  66. }
  67. char *strncpy(char *dest, const char *src, size_t n)
  68. {
  69. char *d = dest;
  70. while((*src != 0) && (n > 0))
  71. {
  72. *d++ = *src++;
  73. n--;
  74. }
  75. if(n > 0)
  76. *d = 0;
  77. return dest;
  78. }
  79. size_t strlen(const char *s)
  80. {
  81. size_t ret = 0;
  82. while(*s++ != 0) ret++;
  83. return ret;
  84. }
  85. int strcmp(const char *s1, const char *s2)
  86. {
  87. while(*s1 || *s2)
  88. {
  89. char s = *s1++ - *s2++;
  90. if(s != 0)
  91. return (int)s;
  92. }
  93. return 0;
  94. }
  95. int raise(int sig)
  96. {
  97. printf("ERROR: signal %i raised. Halted.\n", sig);
  98. while(1);
  99. return 0;
  100. }
  101. int tolower(int c)
  102. {
  103. if((c >= 'A') && (c <= 'Z'))
  104. return 'a' + (c - 'A');
  105. else
  106. return c;
  107. }
  108. int toupper(int c)
  109. {
  110. if((c >= 'a') && (c <= 'z'))
  111. return 'A' + (c - 'a');
  112. else
  113. return c;
  114. }
  115. char *strlwr(char *s)
  116. {
  117. size_t len = strlen(s);
  118. char *ret = (char *)malloc(len + 1);
  119. ret[len] = 0;
  120. for(size_t i = 0; i < len; i++)
  121. ret[i] = tolower(s[i]);
  122. return ret;
  123. }
  124. char *strupr(char *s)
  125. {
  126. size_t len = strlen(s);
  127. char *ret = (char *)malloc(len + 1);
  128. ret[len] = 0;
  129. for(size_t i = 0; i < len; i++)
  130. ret[i] = toupper(s[i]);
  131. return ret;
  132. }