2019-11-13 01:58:50 +01:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* libs/libc/stdio/lib_getdelim.c
|
2019-11-13 01:58:50 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +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
|
2019-11-13 01:58:50 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-11-13 01:58:50 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +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.
|
2019-11-13 01:58:50 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "libc.h"
|
|
|
|
|
2019-11-13 02:43:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-11-13 18:45:26 +01:00
|
|
|
#define BUFSIZE_INIT 64
|
2019-11-13 16:01:52 +01:00
|
|
|
#define BUFSIZE_INCR 32
|
|
|
|
|
2019-11-13 01:58:50 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: getdelim()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The getdelim() function will read from stream until it encounters a
|
|
|
|
* character matching the delimiter character. The delimiter argument
|
|
|
|
* is an int, the value of which the application will ensure is a
|
|
|
|
* character representable as an unsigned char of equal value that
|
|
|
|
* terminates the read process. If the delimiter argument has any other
|
|
|
|
* value, the behavior is undefined.
|
|
|
|
*
|
|
|
|
* The application will ensure that *lineptr is a valid argument that
|
|
|
|
* could be passed to the free() function. If *n is non-zero, the
|
|
|
|
* application will ensure that *lineptr either points to an object of
|
|
|
|
* size at least *n bytes, or is a null pointer.
|
|
|
|
*
|
|
|
|
* If *lineptr is a null pointer or if the object pointed to by
|
|
|
|
* *lineptr is of insufficient size, an object will be allocated as if
|
|
|
|
* by malloc() or the object will be reallocated as if by realloc(),
|
|
|
|
* respectively, such that the object is large enough to hold the
|
|
|
|
* characters to be written to it, including the terminating NUL, and
|
2019-11-13 16:01:52 +01:00
|
|
|
* *n will be set to the new size. If the object was allocated, or if
|
2019-11-13 01:58:50 +01:00
|
|
|
* the reallocation operation moved the object, *lineptr will be
|
|
|
|
* updated to point to the new object or new location. The characters
|
|
|
|
* read, including any delimiter, will be stored in the object, and a
|
|
|
|
* terminating NUL added when the delimiter or end-of-file is encountered.
|
|
|
|
*
|
2019-11-13 18:45:26 +01:00
|
|
|
* Returned Value:
|
|
|
|
* Upon successful completion, the getline() and getdelim() functions will
|
|
|
|
* return the number of bytes written into the buffer, including the
|
|
|
|
* delimiter character if one was encountered before EOF, but excluding
|
|
|
|
* the terminating NUL character. If the end-of-file indicator for the
|
|
|
|
* stream is set, or if no characters were read and the stream is at
|
|
|
|
* end-of-file, the end-of-file indicator for the stream will be set and
|
|
|
|
* the function will return -1. If an error occurs, the error indicator for
|
|
|
|
* the stream will be set, and the function will return -1 and set errno to
|
|
|
|
* indicate the error.
|
|
|
|
*
|
2019-11-13 01:58:50 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t getdelim(FAR char **lineptr, size_t *n, int delimiter,
|
|
|
|
FAR FILE *stream)
|
|
|
|
{
|
|
|
|
FAR char *dest;
|
|
|
|
size_t bufsize;
|
2019-11-13 16:01:52 +01:00
|
|
|
size_t maxcopy;
|
2019-11-13 01:58:50 +01:00
|
|
|
ssize_t ncopied;
|
|
|
|
int ch;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Verify pointers */
|
|
|
|
|
|
|
|
if (lineptr == NULL || n == NULL || stream == NULL)
|
|
|
|
{
|
2021-11-17 19:10:30 +01:00
|
|
|
ret = EINVAL;
|
2019-11-13 01:58:50 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify the buffer size */
|
|
|
|
|
|
|
|
bufsize = *n;
|
|
|
|
if (bufsize == 0)
|
|
|
|
{
|
2019-11-13 16:01:52 +01:00
|
|
|
/* Pick an initial buffer size */
|
|
|
|
|
2019-11-13 18:45:26 +01:00
|
|
|
bufsize = BUFSIZE_INIT;
|
|
|
|
*n = BUFSIZE_INIT;
|
2019-11-13 16:01:52 +01:00
|
|
|
|
|
|
|
/* Free any mystery buffer. It will be reallocated below. */
|
|
|
|
|
|
|
|
if (*lineptr != NULL)
|
|
|
|
{
|
|
|
|
lib_free(*lineptr);
|
|
|
|
*lineptr = NULL;
|
|
|
|
}
|
2019-11-13 01:58:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If *lineptr is a NULL pointer, then allocate *lineptr with size *n */
|
|
|
|
|
|
|
|
dest = *lineptr;
|
|
|
|
if (dest == NULL)
|
|
|
|
{
|
2023-10-21 11:33:01 +02:00
|
|
|
dest = lib_malloc(bufsize);
|
2019-11-13 01:58:50 +01:00
|
|
|
if (dest == NULL)
|
|
|
|
{
|
2021-11-17 19:10:30 +01:00
|
|
|
ret = ENOMEM;
|
2019-11-13 01:58:50 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
*lineptr = dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transfer characters until either bufsize characters have been transfer
|
|
|
|
* or until the delimiter is encountered.
|
|
|
|
*/
|
|
|
|
|
2019-11-13 16:01:52 +01:00
|
|
|
ncopied = 0; /* No bytes have been transferred yet */
|
|
|
|
maxcopy = bufsize - 1; /* Reserve a byte for the NUL terminator */
|
2019-11-13 01:58:50 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2019-11-13 16:01:52 +01:00
|
|
|
/* If the object pointed to by *lineptr is of insufficient size, the
|
|
|
|
* object will be reallocated such that the object is large enough to
|
|
|
|
* hold the characters to be written to it, including the terminating
|
|
|
|
* NUL, and *n will be set to the new size.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ncopied >= maxcopy)
|
|
|
|
{
|
|
|
|
FAR char *newbuffer;
|
|
|
|
|
2019-11-13 21:01:25 +01:00
|
|
|
/* This function should fail with EOVERFLOW if ncopied exceeds
|
|
|
|
* SSIZE_MAX. However, I think we will have failed a memory
|
|
|
|
* allocation or crashed long before that could occur.
|
2019-11-13 18:45:26 +01:00
|
|
|
*/
|
|
|
|
|
2019-11-13 16:01:52 +01:00
|
|
|
bufsize += BUFSIZE_INCR;
|
2023-10-21 11:33:01 +02:00
|
|
|
newbuffer = lib_realloc(*lineptr, bufsize);
|
2019-11-13 16:01:52 +01:00
|
|
|
if (newbuffer == NULL)
|
|
|
|
{
|
2021-12-17 01:20:26 +01:00
|
|
|
ret = ENOMEM;
|
2019-11-13 16:01:52 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
*lineptr = newbuffer;
|
|
|
|
*n = bufsize;
|
|
|
|
dest = &newbuffer[ncopied];
|
|
|
|
maxcopy = bufsize - 1;
|
|
|
|
}
|
|
|
|
|
2019-11-13 01:58:50 +01:00
|
|
|
/* Get the next character and test for EOF */
|
|
|
|
|
|
|
|
ch = fgetc(stream);
|
|
|
|
if (ch == EOF)
|
|
|
|
{
|
2019-11-13 16:01:52 +01:00
|
|
|
/* errno is not set in this case */
|
|
|
|
|
|
|
|
return -1;
|
2019-11-13 01:58:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the character in the user buffer and increment the number of
|
|
|
|
* bytes transferred.
|
|
|
|
*/
|
|
|
|
|
|
|
|
*dest++ = ch;
|
|
|
|
ncopied++;
|
|
|
|
|
|
|
|
/* Terminate the loop when the delimiter is found or we have hit the
|
|
|
|
* end of the buffer (reserving a byte for the NUL terminator)
|
|
|
|
*/
|
|
|
|
}
|
2019-11-13 16:01:52 +01:00
|
|
|
while (ch != delimiter);
|
2019-11-13 01:58:50 +01:00
|
|
|
|
|
|
|
/* Add a NUL terminator character (but don't report this in the number of
|
|
|
|
* bytes transferred).
|
|
|
|
*/
|
|
|
|
|
|
|
|
*dest = '\0';
|
|
|
|
return ncopied;
|
|
|
|
|
|
|
|
errout:
|
2021-11-17 19:10:30 +01:00
|
|
|
set_errno(ret);
|
2019-11-13 01:58:50 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-02-01 13:22:25 +01:00
|
|
|
* Name: getline()
|
2019-11-13 01:58:50 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The getline() function will be equivalent to the getdelim() function
|
|
|
|
* with the delimiter character equal to the <newline> character.
|
|
|
|
*
|
|
|
|
* NOTE: Because of this functional definition, getline() will not work
|
|
|
|
* with on systems where multiple characters are used to denote the end
|
2019-11-13 21:01:25 +01:00
|
|
|
* of line such a CR-LF sequences. In that case, the CR would be
|
2019-11-13 01:58:50 +01:00
|
|
|
* transferred into the user buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t getline(FAR char **lineptr, size_t *n, FAR FILE *stream)
|
|
|
|
{
|
2023-02-22 14:02:21 +01:00
|
|
|
return getdelim(lineptr, n, '\n', stream);
|
2019-11-13 01:58:50 +01:00
|
|
|
}
|