dev_null.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /****************************************************************************
  2. * drivers/dev_null.c
  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. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <stdbool.h>
  26. #include <string.h>
  27. #include <poll.h>
  28. #include <errno.h>
  29. #include <nuttx/semaphore.h>
  30. #include <nuttx/fs/fs.h>
  31. #include <nuttx/drivers/drivers.h>
  32. /****************************************************************************
  33. * Private Function Prototypes
  34. ****************************************************************************/
  35. static ssize_t devnull_read(FAR struct file *filep, FAR char *buffer,
  36. size_t buflen);
  37. static ssize_t devnull_write(FAR struct file *filep, FAR const char *buffer,
  38. size_t buflen);
  39. static int devnull_poll(FAR struct file *filep, FAR struct pollfd *fds,
  40. bool setup);
  41. /****************************************************************************
  42. * Private Data
  43. ****************************************************************************/
  44. static const struct file_operations devnull_fops =
  45. {
  46. NULL, /* open */
  47. NULL, /* close */
  48. devnull_read, /* read */
  49. devnull_write, /* write */
  50. NULL, /* seek */
  51. NULL, /* ioctl */
  52. devnull_poll /* poll */
  53. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  54. , NULL /* unlink */
  55. #endif
  56. };
  57. /****************************************************************************
  58. * Private Functions
  59. ****************************************************************************/
  60. /****************************************************************************
  61. * Name: devnull_read
  62. ****************************************************************************/
  63. static ssize_t devnull_read(FAR struct file *filep, FAR char *buffer,
  64. size_t len)
  65. {
  66. return 0; /* Return EOF */
  67. }
  68. /****************************************************************************
  69. * Name: devnull_write
  70. ****************************************************************************/
  71. static ssize_t devnull_write(FAR struct file *filep, FAR const char *buffer,
  72. size_t len)
  73. {
  74. return len; /* Say that everything was written */
  75. }
  76. /****************************************************************************
  77. * Name: devnull_poll
  78. ****************************************************************************/
  79. static int devnull_poll(FAR struct file *filep, FAR struct pollfd *fds,
  80. bool setup)
  81. {
  82. if (setup)
  83. {
  84. fds->revents |= (fds->events & (POLLIN | POLLOUT));
  85. if (fds->revents != 0)
  86. {
  87. nxsem_post(fds->sem);
  88. }
  89. }
  90. return OK;
  91. }
  92. /****************************************************************************
  93. * Public Functions
  94. ****************************************************************************/
  95. /****************************************************************************
  96. * Name: devnull_register
  97. *
  98. * Description:
  99. * Register /dev/null
  100. *
  101. ****************************************************************************/
  102. void devnull_register(void)
  103. {
  104. register_driver("/dev/null", &devnull_fops, 0666, NULL);
  105. }