stdlib.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet 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. * M2-Planet 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 M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <unistd.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. #define EXIT_FAILURE 1
  21. #define EXIT_SUCCESS 0
  22. void exit(int value);
  23. long _malloc_ptr;
  24. long _brk_ptr;
  25. void free(void* l)
  26. {
  27. return;
  28. }
  29. void* malloc(unsigned size)
  30. {
  31. if(NULL == _brk_ptr)
  32. {
  33. _brk_ptr = brk(0);
  34. _malloc_ptr = _brk_ptr;
  35. }
  36. if(_brk_ptr < _malloc_ptr + size)
  37. {
  38. _brk_ptr = brk(_malloc_ptr + size);
  39. if(-1 == _brk_ptr) return 0;
  40. }
  41. long old_malloc = _malloc_ptr;
  42. _malloc_ptr = _malloc_ptr + size;
  43. return old_malloc;
  44. }
  45. void* memset(void* ptr, int value, int num)
  46. {
  47. char* s;
  48. for(s = ptr; 0 < num; num = num - 1)
  49. {
  50. s[0] = value;
  51. s = s + 1;
  52. }
  53. return ptr;
  54. }
  55. void* calloc(int count, int size)
  56. {
  57. void* ret = malloc(count * size);
  58. if(NULL == ret) return NULL;
  59. memset(ret, 0, (count * size));
  60. return ret;
  61. }
  62. /* USED EXCLUSIVELY BY MKSTEMP */
  63. void __set_name(char* s, int i)
  64. {
  65. s[5] = '0' + (i % 10);
  66. i = i / 10;
  67. s[4] = '0' + (i % 10);
  68. i = i / 10;
  69. s[3] = '0' + (i % 10);
  70. i = i / 10;
  71. s[2] = '0' + (i % 10);
  72. i = i / 10;
  73. s[1] = '0' + (i % 10);
  74. i = i / 10;
  75. s[0] = '0' + i;
  76. }
  77. int mkstemp(char *template)
  78. {
  79. int i = 0;
  80. while(0 != template[i]) i = i + 1;
  81. i = i - 1;
  82. /* String MUST be more than 6 characters in length */
  83. if(i < 6) return -1;
  84. int count = 6;
  85. int c;
  86. while(count > 0)
  87. {
  88. c = template[i];
  89. /* last 6 chars must be X */
  90. if('X' != c) return -1;
  91. template[i] = '0';
  92. i = i - 1;
  93. count = count - 1;
  94. }
  95. int fd = -1;
  96. count = -1;
  97. /* open will return -17 or other values */
  98. while(0 > fd)
  99. {
  100. /* Just give up after the planet has blown up */
  101. if(9000 < count) return -1;
  102. /* Try upto 9000 unique filenames before stopping */
  103. count = count + 1;
  104. __set_name(template+i+1, count);
  105. /* Pray we can */
  106. fd = open(template, O_RDWR | O_CREAT | O_EXCL, 00600);
  107. }
  108. /* well that only took count many tries */
  109. return fd;
  110. }