libc.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /****************************************************************************
  2. * libs/libc/libc.h
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. #ifndef __LIBC_LIBC_H
  21. #define __LIBC_LIBC_H
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <nuttx/config.h>
  26. #include <sys/types.h>
  27. #include <stdbool.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <limits.h>
  31. #include <semaphore.h>
  32. #include <nuttx/kmalloc.h>
  33. #include <nuttx/streams.h>
  34. /****************************************************************************
  35. * Pre-processor Definitions
  36. ****************************************************************************/
  37. /* This configuration directory is used in environment variable processing
  38. * when we need to reference the user's home directory. There are no user
  39. * directories in NuttX so, by default, this always refers to the root
  40. * directory.
  41. */
  42. #ifndef CONFIG_LIB_HOMEDIR
  43. # define CONFIG_LIB_HOMEDIR "/"
  44. #endif
  45. /* If C std I/O buffering is not supported, then we don't need its semaphore
  46. * protection.
  47. */
  48. #ifdef CONFIG_STDIO_DISABLE_BUFFERING
  49. # define lib_sem_initialize(s)
  50. # define lib_take_semaphore(s)
  51. # define lib_give_semaphore(s)
  52. #endif
  53. /* The NuttX C library an be build in two modes: (1) as a standard, C-library
  54. * that can be used by normal, user-space applications, or (2) as a special,
  55. * kernel-mode C-library only used within the OS. If NuttX is not being
  56. * built as separated kernel- and user-space modules, then only the first
  57. * mode is supported.
  58. */
  59. #if !defined(CONFIG_BUILD_FLAT) && defined(__KERNEL__)
  60. /* Domain-specific allocations */
  61. # define lib_malloc(s) kmm_malloc(s)
  62. # define lib_zalloc(s) kmm_zalloc(s)
  63. # define lib_realloc(p,s) kmm_realloc(p,s)
  64. # define lib_memalign(p,s) kmm_memalign(p,s)
  65. # define lib_free(p) kmm_free(p)
  66. /* User-accessible allocations */
  67. # define lib_umalloc(s) kumm_malloc(s)
  68. # define lib_uzalloc(s) kumm_zalloc(s)
  69. # define lib_urealloc(p,s) kumm_realloc(p,s)
  70. # define lib_umemalign(p,s) kumm_memalign(p,s)
  71. # define lib_ufree(p) kumm_free(p)
  72. #else
  73. /* Domain-specific allocations */
  74. # define lib_malloc(s) malloc(s)
  75. # define lib_zalloc(s) zalloc(s)
  76. # define lib_realloc(p,s) realloc(p,s)
  77. # define lib_memalign(p,s) memalign(p,s)
  78. # define lib_free(p) free(p)
  79. /* User-accessible allocations */
  80. # define lib_umalloc(s) malloc(s)
  81. # define lib_uzalloc(s) zalloc(s)
  82. # define lib_urealloc(p,s) realloc(p,s)
  83. # define lib_umemalign(p,s) memalign(p,s)
  84. # define lib_ufree(p) free(p)
  85. #endif
  86. #define LIB_BUFLEN_UNKNOWN INT_MAX
  87. /****************************************************************************
  88. * Public Types
  89. ****************************************************************************/
  90. /****************************************************************************
  91. * Public Data
  92. ****************************************************************************/
  93. #undef EXTERN
  94. #if defined(__cplusplus)
  95. #define EXTERN extern "C"
  96. extern "C"
  97. {
  98. #else
  99. #define EXTERN extern
  100. #endif
  101. /****************************************************************************
  102. * Public Function Prototypes
  103. ****************************************************************************/
  104. /* Defined in lib_streamsem.c */
  105. #if CONFIG_NFILE_STREAMS > 0
  106. void stream_semtake(FAR struct streamlist *list);
  107. void stream_semgive(FAR struct streamlist *list);
  108. #endif
  109. /* Defined in lib_dtoa.c */
  110. #ifdef CONFIG_LIBC_FLOATINGPOINT
  111. FAR char *__dtoa(double d, int mode, int ndigits, FAR int *decpt,
  112. FAR int *sign, FAR char **rve);
  113. #endif
  114. /* Defined in lib_fopen.c */
  115. int lib_mode2oflags(FAR const char *mode);
  116. /* Defined in lib_libfwrite.c */
  117. ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream);
  118. /* Defined in lib_libfread.c */
  119. ssize_t lib_fread(FAR void *ptr, size_t count, FAR FILE *stream);
  120. /* Defined in lib_libfgets.c */
  121. FAR char *lib_fgets(FAR char *buf, size_t buflen, FILE *stream,
  122. bool keepnl, bool consume);
  123. /* Defined in lib_libfflush.c */
  124. ssize_t lib_fflush(FAR FILE *stream, bool bforce);
  125. /* Defined in lib_rdflush.c */
  126. int lib_rdflush(FAR FILE *stream);
  127. /* Defined in lib_wrflush.c */
  128. int lib_wrflush(FAR FILE *stream);
  129. /* Defined in lib_sem.c */
  130. #ifndef CONFIG_STDIO_DISABLE_BUFFERING
  131. void lib_sem_initialize(FAR struct file_struct *stream);
  132. void lib_take_semaphore(FAR struct file_struct *stream);
  133. void lib_give_semaphore(FAR struct file_struct *stream);
  134. #endif
  135. /* Defined in lib_libgetbase.c */
  136. int lib_getbase(FAR const char *nptr, FAR const char **endptr);
  137. /* Defined in lib_skipspace.c */
  138. void lib_skipspace(FAR const char **pptr);
  139. /* Defined in lib_isbasedigit.c */
  140. bool lib_isbasedigit(int ch, int base, FAR int *value);
  141. /* Defined in lib_checkbase.c */
  142. int lib_checkbase(int base, FAR const char **pptr);
  143. /* Defined in lib_expi.c */
  144. #ifdef CONFIG_LIBM
  145. float lib_expif(size_t n);
  146. double lib_expi(size_t n);
  147. #endif
  148. /* Defined in lib_libsqrtapprox.c */
  149. #ifdef CONFIG_LIBM
  150. float lib_sqrtapprox(float x);
  151. #endif
  152. /* Defined in lib_parsehostfile.c */
  153. #ifdef CONFIG_NETDB_HOSTFILE
  154. struct hostent;
  155. ssize_t lib_parse_hostfile(FAR FILE *stream, FAR struct hostent *host,
  156. FAR char *buf, size_t buflen);
  157. #endif
  158. #ifndef CONFIG_DISABLE_ENVIRON
  159. int lib_restoredir(void);
  160. #endif
  161. #undef EXTERN
  162. #if defined(__cplusplus)
  163. }
  164. #endif
  165. #endif /* __LIBC_LIBC_H */