2013-12-14 15:53:23 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/procfs/fs_procfsutil.c
|
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2013-12-14 15:53:23 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-12-14 15:53:23 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2013-12-14 15:53:23 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2013-12-14 15:53:23 +01:00
|
|
|
#include <sys/types.h>
|
2021-04-14 16:37:52 +02:00
|
|
|
#include <stdio.h>
|
2013-12-14 15:53:23 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <nuttx/fs/procfs.h>
|
|
|
|
|
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS)
|
|
|
|
|
2023-03-15 13:48:40 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Determines the size of an intermediate buffer in procfs_sprintf that must
|
|
|
|
* be large enough to handle the longest line generated by this logic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LINEBUF_SIZE 128
|
|
|
|
|
2013-12-14 15:53:23 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_memcpy
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* procfs/ file data may be read by the user with different user buffer
|
|
|
|
* sizes to receive the data. If the amount of data to be returned is
|
|
|
|
* large or if the callers receive buffer is small, then multiple read
|
|
|
|
* operations will be required.
|
|
|
|
*
|
|
|
|
* If multiple read operations are required, then each read operation will
|
|
|
|
* be identical accept that file position (f_pos) will be incremented with
|
|
|
|
* each read: f_pos must be incremented by the read method after each
|
|
|
|
* read operation to provide the 'offset' for the next read.
|
|
|
|
*
|
|
|
|
* procfs_memcpy() is a helper function. Each read() method should
|
|
|
|
* provide data in a local data buffer ('src' and 'srclen'). This
|
2020-12-08 17:29:58 +01:00
|
|
|
* will transfer the data to the user receive buffer ('dest' and
|
|
|
|
* 'destlen'), respecting both (1) the size of the destination buffer so
|
|
|
|
* that it will write beyond the user receiver and (1) the file position,
|
|
|
|
* 'offset'.
|
2013-12-14 15:53:23 +01:00
|
|
|
*
|
|
|
|
* This function will skip over data until the under of bytes specified
|
|
|
|
* by 'offset' have been skipped. Then it will transfer data from the
|
|
|
|
* the procfs/ 'src' buffer into the user receive buffer. No more than
|
|
|
|
* 'destlen' bytes will be transferred.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* src - The address of the intermediate procfs/ buffer containing the
|
|
|
|
* data to be returned.
|
|
|
|
* srclen - The number of bytes of data in the 'src' buffer
|
|
|
|
* dest - The address of the user's receive buffer.
|
|
|
|
* destlen - The size (in bytes) of the user's receive buffer.
|
|
|
|
* offset - On input, this is the number of bytes to skip before returning
|
|
|
|
* data; If bytes were skipped, this offset will be decremented.
|
|
|
|
* Data will not be transferred until this offset decrements to
|
|
|
|
* zero.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The number of bytes actually transferred into the user's receive buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
size_t procfs_memcpy(FAR const char *src, size_t srclen,
|
|
|
|
FAR char *dest, size_t destlen,
|
2024-08-25 22:37:15 +02:00
|
|
|
FAR off_t *offset)
|
2013-12-14 15:53:23 +01:00
|
|
|
{
|
|
|
|
size_t copysize;
|
|
|
|
size_t lnoffset;
|
|
|
|
|
|
|
|
/* Will this line take us past the offset? */
|
|
|
|
|
|
|
|
lnoffset = *offset;
|
|
|
|
if (srclen < lnoffset)
|
|
|
|
{
|
|
|
|
/* No... decrement the offset and return without doing anything */
|
|
|
|
|
|
|
|
*offset -= srclen;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the remaining offset */
|
|
|
|
|
|
|
|
srclen -= lnoffset;
|
2018-01-13 00:40:04 +01:00
|
|
|
src += lnoffset;
|
2013-12-14 15:53:23 +01:00
|
|
|
*offset = 0;
|
|
|
|
|
|
|
|
/* Copy the line into the user destination buffer */
|
|
|
|
|
|
|
|
copysize = MIN(srclen, destlen);
|
|
|
|
memcpy(dest, src, copysize);
|
|
|
|
return copysize;
|
|
|
|
}
|
|
|
|
|
2021-04-14 16:37:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_snprintf
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is same with snprintf, except return values.
|
|
|
|
* If buf has no enough space and output was truncated due to size limit,
|
2022-07-07 18:01:22 +02:00
|
|
|
* snprintf: return formatted string len.
|
2021-04-14 16:37:52 +02:00
|
|
|
* procfs_snprintf: return string len which has written to buf.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* Same with snprintf
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* See Description.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int procfs_snprintf(FAR char *buf, size_t size,
|
|
|
|
FAR const IPTR char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int n;
|
|
|
|
va_start(ap, format);
|
|
|
|
n = vsnprintf(buf, size, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return n < size - 1 ? n : size - 1;
|
|
|
|
}
|
|
|
|
|
2023-03-15 13:48:40 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: procfs_sprintf
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function used to continous format string and copy it to buffer.
|
|
|
|
* Every single string length must be smaller then LINEBUF_SIZE.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* buf - The address of the user's receive buffer.
|
|
|
|
* size - The size (in bytes) of the user's receive buffer.
|
|
|
|
* offset - On input, when *offset is larger the 0 , this is the
|
|
|
|
* number of bytes to skip before returning data; If bytes
|
|
|
|
* were skipped, this *offset will be decremented. when it
|
|
|
|
* decrements to a negative value, -*offset is the number of
|
|
|
|
* data copied to buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void procfs_sprintf(FAR char *buf, size_t size, FAR off_t *offset,
|
|
|
|
FAR const IPTR char *format, ...)
|
|
|
|
{
|
|
|
|
char linebuf[LINEBUF_SIZE];
|
|
|
|
size_t linesize;
|
|
|
|
size_t copysize;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
linesize = vsnprintf(linebuf, LINEBUF_SIZE, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
linesize = MIN(linesize, LINEBUF_SIZE - 1);
|
|
|
|
|
|
|
|
if (*offset > 0)
|
|
|
|
{
|
|
|
|
if (linesize < *offset)
|
|
|
|
{
|
|
|
|
*offset -= linesize;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
copysize = MIN(linesize - *offset, size);
|
|
|
|
memcpy(buf, linebuf + *offset, copysize);
|
|
|
|
*offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (size + *offset > 0)
|
|
|
|
{
|
|
|
|
copysize = MIN(linesize, size + *offset);
|
|
|
|
memcpy(buf - *offset, linebuf, copysize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
copysize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*offset -= copysize;
|
|
|
|
}
|
|
|
|
|
2013-12-14 15:53:23 +01:00
|
|
|
#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS */
|