fs_procfsutil.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /****************************************************************************
  2. * fs/procfs/fs_procfsutil.c
  3. *
  4. * Copyright (C) 2013 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <string.h>
  41. #include <nuttx/fs/procfs.h>
  42. #if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS)
  43. /****************************************************************************
  44. * Pre-processor Definitions
  45. ****************************************************************************/
  46. #ifndef MIN
  47. # define MIN(a,b) ((a < b) ? a : b)
  48. #endif
  49. #ifndef MAX
  50. # define MAX(a,b) ((a > b) ? a : b)
  51. #endif
  52. /****************************************************************************
  53. * Public Functions
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Name: procfs_memcpy
  57. *
  58. * Description:
  59. * procfs/ file data may be read by the user with different user buffer
  60. * sizes to receive the data. If the amount of data to be returned is
  61. * large or if the callers receive buffer is small, then multiple read
  62. * operations will be required.
  63. *
  64. * If multiple read operations are required, then each read operation will
  65. * be identical accept that file position (f_pos) will be incremented with
  66. * each read: f_pos must be incremented by the read method after each
  67. * read operation to provide the 'offset' for the next read.
  68. *
  69. * procfs_memcpy() is a helper function. Each read() method should
  70. * provide data in a local data buffer ('src' and 'srclen'). This
  71. * will transfer the data to the user receive buffer ('dest' and 'destlen'),
  72. * respecting both (1) the size of the destination buffer so that it will
  73. * write beyond the user receiver and (1) the file position, 'offset'.
  74. *
  75. * This function will skip over data until the under of bytes specified
  76. * by 'offset' have been skipped. Then it will transfer data from the
  77. * the procfs/ 'src' buffer into the user receive buffer. No more than
  78. * 'destlen' bytes will be transferred.
  79. *
  80. * Input Parameters:
  81. * src - The address of the intermediate procfs/ buffer containing the
  82. * data to be returned.
  83. * srclen - The number of bytes of data in the 'src' buffer
  84. * dest - The address of the user's receive buffer.
  85. * destlen - The size (in bytes) of the user's receive buffer.
  86. * offset - On input, this is the number of bytes to skip before returning
  87. * data; If bytes were skipped, this offset will be decremented.
  88. * Data will not be transferred until this offset decrements to
  89. * zero.
  90. *
  91. * Returned Value:
  92. * The number of bytes actually transferred into the user's receive buffer.
  93. *
  94. ****************************************************************************/
  95. size_t procfs_memcpy(FAR const char *src, size_t srclen,
  96. FAR char *dest, size_t destlen,
  97. off_t *offset)
  98. {
  99. size_t copysize;
  100. size_t lnoffset;
  101. /* Will this line take us past the offset? */
  102. lnoffset = *offset;
  103. if (srclen < lnoffset)
  104. {
  105. /* No... decrement the offset and return without doing anything */
  106. *offset -= srclen;
  107. return 0;
  108. }
  109. /* Handle the remaining offset */
  110. srclen -= lnoffset;
  111. src += lnoffset;
  112. *offset = 0;
  113. /* Copy the line into the user destination buffer */
  114. copysize = MIN(srclen, destlen);
  115. memcpy(dest, src, copysize);
  116. return copysize;
  117. }
  118. #endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS */