stdlib.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef __STDLIB_H
  2. #define __STDLIB_H
  3. #include "asmc_types.h"
  4. void abort();
  5. int abs(int j);
  6. long int strtol( const char * s, char ** endptr, int base );
  7. void *malloc(size_t size);
  8. void free(void *ptr);
  9. #include "asmc.h"
  10. #include "errno.h"
  11. void *malloc(size_t size) {
  12. return __handles->malloc(size);
  13. }
  14. void *calloc(size_t num, size_t size) {
  15. return __handles->calloc(num, size);
  16. }
  17. void free(void *ptr) {
  18. __handles->free(ptr);
  19. }
  20. void *realloc(void *ptr, size_t new_size) {
  21. return __handles->realloc(ptr, new_size);
  22. }
  23. void exit(int return_code) {
  24. __return_code = return_code;
  25. longjmp(__return_jump_buf, 1);
  26. }
  27. void abort() {
  28. __return_code = -1;
  29. __aborted = 1;
  30. __dump_stacktrace();
  31. longjmp(__return_jump_buf, 1);
  32. }
  33. // STUB
  34. char *getenv(const char *name) {
  35. __unimplemented();
  36. return 0;
  37. }
  38. // STUB
  39. int setenv(const char *envname, const char *envval, int overwrite) {
  40. __unimplemented();
  41. errno = ENOTIMPL;
  42. return -1;
  43. }
  44. // STUB
  45. int mkstemp(char *template) {
  46. __unimplemented();
  47. errno = ENOTIMPL;
  48. return -1;
  49. }
  50. // From PDClib
  51. int abs( int j )
  52. {
  53. return ( j >= 0 ) ? j : -j;
  54. }
  55. #define RAND_MAX (1U << 31)
  56. unsigned _seed;
  57. // Very bad generator, but who cares
  58. int rand() {
  59. _seed = (1103515245 * _seed + 12345) % RAND_MAX;
  60. return _seed;
  61. }
  62. void srand(unsigned seed) {
  63. _seed = seed;
  64. }
  65. // In line of principle a better RNG is mandated, but again who cares
  66. long int random() {
  67. return rand();
  68. }
  69. void srandom(unsigned seed) {
  70. srand(seed);
  71. }
  72. #include "_qsort.h"
  73. #include "_strtox.h"
  74. #endif