You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stdlib.h 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef STDLIB_H
  2. #define STDLIB_H
  3. FILE_LICENCE ( GPL2_OR_LATER );
  4. #include <stdint.h>
  5. #include <assert.h>
  6. #include <ctype.h>
  7. /*****************************************************************************
  8. *
  9. * Numeric parsing
  10. *
  11. ****************************************************************************
  12. */
  13. static inline int strtoul_base ( const char **pp, int base )
  14. {
  15. const char *p = *pp;
  16. while ( isspace ( *p ) )
  17. p++;
  18. if ( base == 0 ) {
  19. base = 10;
  20. if ( *p == '0' ) {
  21. p++;
  22. base = 8;
  23. if ( ( *p | 0x20 ) == 'x' ) {
  24. p++;
  25. base = 16;
  26. }
  27. }
  28. }
  29. *pp = p;
  30. return base;
  31. }
  32. static inline unsigned int strtoul_charval ( unsigned int charval )
  33. {
  34. if ( charval >= 'a' ) {
  35. charval = ( charval - 'a' + 10 );
  36. } else if ( charval >= 'A' ) {
  37. charval = ( charval - 'A' + 10 );
  38. } else if ( charval <= '9' ) {
  39. charval = ( charval - '0' );
  40. }
  41. return charval;
  42. }
  43. extern unsigned long strtoul ( const char *p, char **endp, int base );
  44. extern unsigned long long strtoull ( const char *p, char **endp, int base );
  45. /*****************************************************************************
  46. *
  47. * Memory allocation
  48. *
  49. ****************************************************************************
  50. */
  51. extern void * __malloc malloc ( size_t size );
  52. extern void * realloc ( void *old_ptr, size_t new_size );
  53. extern void free ( void *ptr );
  54. extern void * __malloc zalloc ( size_t len );
  55. /**
  56. * Allocate cleared memory
  57. *
  58. * @v nmemb Number of members
  59. * @v size Size of each member
  60. * @ret ptr Allocated memory
  61. *
  62. * Allocate memory as per malloc(), and zero it.
  63. *
  64. * This is implemented as a static inline, with the body of the
  65. * function in zalloc(), since in most cases @c nmemb will be 1 and
  66. * doing the multiply is just wasteful.
  67. */
  68. static inline void * __malloc calloc ( size_t nmemb, size_t size ) {
  69. return zalloc ( nmemb * size );
  70. }
  71. /*****************************************************************************
  72. *
  73. * Random number generation
  74. *
  75. ****************************************************************************
  76. */
  77. extern long int random ( void );
  78. extern void srandom ( unsigned int seed );
  79. static inline int rand ( void ) {
  80. return random();
  81. }
  82. static inline void srand ( unsigned int seed ) {
  83. srandom ( seed );
  84. }
  85. /*****************************************************************************
  86. *
  87. * Miscellaneous
  88. *
  89. ****************************************************************************
  90. */
  91. extern int system ( const char *command );
  92. extern __asmcall int main ( void );
  93. #endif /* STDLIB_H */