dfs_posix.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * File : dfs_posix.h
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-05-27 Yi.qiu The first version.
  23. * 2010-07-18 Bernard add stat and statfs structure definitions.
  24. * 2011-05-16 Yi.qiu Change parameter name of rename, "new" is C++ key word.
  25. */
  26. #ifndef __DFS_POSIX_H__
  27. #define __DFS_POSIX_H__
  28. #include <dfs_file.h>
  29. #include <sys/time.h> /* for struct timeval */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. typedef struct
  34. {
  35. int fd; /* directory file */
  36. char buf[512];
  37. int num;
  38. int cur;
  39. } DIR;
  40. /* directory api*/
  41. int mkdir(const char *path, mode_t mode);
  42. DIR *opendir(const char *name);
  43. struct dirent *readdir(DIR *d);
  44. long telldir(DIR *d);
  45. void seekdir(DIR *d, off_t offset);
  46. void rewinddir(DIR *d);
  47. int closedir(DIR* d);
  48. /* file api*/
  49. int open(const char *file, int flags, int mode);
  50. int close(int d);
  51. #ifdef RT_USING_NEWLIB
  52. _READ_WRITE_RETURN_TYPE _EXFUN(read, (int __fd, void *__buf, size_t __nbyte));
  53. _READ_WRITE_RETURN_TYPE _EXFUN(write, (int __fd, const void *__buf, size_t __nbyte));
  54. #else
  55. int read(int fd, void *buf, size_t len);
  56. int write(int fd, const void *buf, size_t len);
  57. #endif
  58. off_t lseek(int fd, off_t offset, int whence);
  59. int rename(const char *from, const char *to);
  60. int unlink(const char *pathname);
  61. int stat(const char *file, struct stat *buf);
  62. int fstat(int fildes, struct stat *buf);
  63. int fsync(int fildes);
  64. int ioctl(int fildes, int cmd, void *data);
  65. /* directory api*/
  66. int rmdir(const char *path);
  67. int chdir(const char *path);
  68. char *getcwd(char *buf, size_t size);
  69. /* file system api */
  70. int statfs(const char *path, struct statfs *buf);
  71. int access(const char *path, int amode);
  72. int pipe(int fildes[2]);
  73. int mkfifo(const char *path, mode_t mode);
  74. /* poll and select */
  75. #define POLLIN (0x01)
  76. #define POLLRDNORM (0x01)
  77. #define POLLRDBAND (0x01)
  78. #define POLLPRI (0x01)
  79. #define POLLOUT (0x02)
  80. #define POLLWRNORM (0x02)
  81. #define POLLWRBAND (0x02)
  82. #define POLLERR (0x04)
  83. #define POLLHUP (0x08)
  84. #define POLLNVAL (0x10)
  85. #define POLLMASK_DEFAULT (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)
  86. typedef unsigned int nfds_t;
  87. struct pollfd
  88. {
  89. int fd;
  90. short events;
  91. short revents;
  92. };
  93. int poll(struct pollfd *fds, nfds_t nfds, int timeout);
  94. #ifdef RT_USING_LWIP
  95. /* we use lwIP's structure definitions. */
  96. #include <lwip/sockets.h>
  97. #else
  98. #ifndef FD_SET
  99. /* Get the total number of descriptors that we will have to support */
  100. #define FD_SETSIZE (12)
  101. /* We will use a 32-bit bitsets to represent the set of descriptors. How
  102. * many uint32_t's do we need to span all descriptors?
  103. */
  104. #if FD_SETSIZE <= 32
  105. # define __SELECT_NUINT32 1
  106. #elif FD_SETSIZE <= 64
  107. # define __SELECT_NUINT32 2
  108. #elif FD_SETSIZE <= 96
  109. # define __SELECT_NUINT32 3
  110. #elif FD_SETSIZE <= 128
  111. # define __SELECT_NUINT32 4
  112. #elif FD_SETSIZE <= 160
  113. # define __SELECT_NUINT32 5
  114. #elif FD_SETSIZE <= 192
  115. # define __SELECT_NUINT32 6
  116. #elif FD_SETSIZE <= 224
  117. # define __SELECT_NUINT32 7
  118. #elif FD_SETSIZE <= 256
  119. # define __SELECT_NUINT32 8
  120. #else
  121. # warning "Larger fd_set needed"
  122. #endif
  123. /* These macros map a file descriptor to an index and bit number */
  124. #define _FD_NDX(fd) ((fd) >> 5)
  125. #define _FD_BIT(fd) ((fd) & 0x1f)
  126. /* Standard helper macros */
  127. #define FD_CLR(fd,set) \
  128. ((((fd_set*)(set))->arr)[_FD_NDX(fd)] &= ~(1 << _FD_BIT(fd)))
  129. #define FD_SET(fd,set) \
  130. ((((fd_set*)(set))->arr)[_FD_NDX(fd)] |= (1 << _FD_BIT(fd)))
  131. #define FD_ISSET(fd,set) \
  132. (((((fd_set*)(set))->arr)[_FD_NDX(fd)] & (1 << _FD_BIT(fd))) != 0)
  133. #define FD_ZERO(set) \
  134. memset((set), 0, sizeof(fd_set))
  135. typedef struct
  136. {
  137. uint32_t arr[__SELECT_NUINT32];
  138. }fd_set;
  139. #endif
  140. #endif
  141. int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif