|
| 1 | +/* getdelim.h --- Implementation of replacement getdelim/getline function. |
| 2 | + Copyright (C) 1994, 1996-1998, 2001, 2003, 2005-2025 Free Software |
| 3 | + Foundation, Inc. |
| 4 | +
|
| 5 | + This file is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU Lesser General Public License as |
| 7 | + published by the Free Software Foundation; either version 2.1 of the |
| 8 | + License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This file is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public License |
| 16 | + along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 | + |
| 18 | +/* Ported from glibc by Simon Josefsson. */ |
| 19 | + |
| 20 | +#ifndef GETDELIM_HEADER_H |
| 21 | +#define GETDELIM_HEADER_H |
| 22 | + |
| 23 | +#ifdef _WIN32 |
| 24 | + |
| 25 | +#include <stdio.h> |
| 26 | + |
| 27 | +#include <limits.h> |
| 28 | +#include <stdint.h> |
| 29 | +#include <stdlib.h> |
| 30 | +#include <errno.h> |
| 31 | + |
| 32 | +static inline void |
| 33 | +alloc_failed (void) |
| 34 | +{ |
| 35 | +#if defined _WIN32 && ! defined __CYGWIN__ |
| 36 | + /* Avoid errno problem without using the realloc module; see: |
| 37 | + https://lists.gnu.org/r/bug-gnulib/2016-08/msg00025.html */ |
| 38 | + errno = ENOMEM; |
| 39 | +#endif |
| 40 | +} |
| 41 | + |
| 42 | +/* Read up to (and including) a DELIMITER from FP into *LINEPTR (and |
| 43 | + NUL-terminate it). *LINEPTR is a pointer returned from malloc (or |
| 44 | + NULL), pointing to *N characters of space. It is realloc'ed as |
| 45 | + necessary. Returns the number of characters read (not including |
| 46 | + the null terminator), or -1 on error or EOF. */ |
| 47 | + |
| 48 | +static inline ssize_t |
| 49 | +getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp) |
| 50 | +{ |
| 51 | + ssize_t result; |
| 52 | + size_t cur_len = 0; |
| 53 | + |
| 54 | + if (lineptr == NULL || n == NULL || fp == NULL) |
| 55 | + { |
| 56 | + errno = EINVAL; |
| 57 | + return -1; |
| 58 | + } |
| 59 | + |
| 60 | + if (*lineptr == NULL || *n == 0) |
| 61 | + { |
| 62 | + char *new_lineptr; |
| 63 | + *n = 120; |
| 64 | + new_lineptr = (char *) realloc (*lineptr, *n); |
| 65 | + if (new_lineptr == NULL) |
| 66 | + { |
| 67 | + alloc_failed (); |
| 68 | + return -1; |
| 69 | + } |
| 70 | + *lineptr = new_lineptr; |
| 71 | + } |
| 72 | + |
| 73 | + for (;;) |
| 74 | + { |
| 75 | + int i; |
| 76 | + |
| 77 | + i = getc (fp); |
| 78 | + if (i == EOF) |
| 79 | + { |
| 80 | + result = -1; |
| 81 | + break; |
| 82 | + } |
| 83 | + |
| 84 | + /* Make enough space for len+1 (for final NUL) bytes. */ |
| 85 | + if (cur_len + 1 >= *n) |
| 86 | + { |
| 87 | + size_t needed_max = |
| 88 | + SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX; |
| 89 | + size_t needed = 2 * *n + 1; /* Be generous. */ |
| 90 | + char *new_lineptr; |
| 91 | + |
| 92 | + if (needed_max < needed) |
| 93 | + needed = needed_max; |
| 94 | + if (cur_len + 1 >= needed) |
| 95 | + { |
| 96 | + errno = EOVERFLOW; |
| 97 | + return -1; |
| 98 | + } |
| 99 | + |
| 100 | + new_lineptr = (char *) realloc (*lineptr, needed); |
| 101 | + if (new_lineptr == NULL) |
| 102 | + { |
| 103 | + alloc_failed (); |
| 104 | + return -1; |
| 105 | + } |
| 106 | + |
| 107 | + *lineptr = new_lineptr; |
| 108 | + *n = needed; |
| 109 | + } |
| 110 | + |
| 111 | + (*lineptr)[cur_len] = i; |
| 112 | + cur_len++; |
| 113 | + |
| 114 | + if (i == delimiter) |
| 115 | + break; |
| 116 | + } |
| 117 | + (*lineptr)[cur_len] = '\0'; |
| 118 | + result = cur_len ? cur_len : result; |
| 119 | + |
| 120 | + return result; |
| 121 | +} |
| 122 | + |
| 123 | +static inline ssize_t |
| 124 | +getline (char **lineptr, size_t *n, FILE *stream) |
| 125 | +{ |
| 126 | + return getdelim (lineptr, n, '\n', stream); |
| 127 | +} |
| 128 | + |
| 129 | +#endif |
| 130 | + |
| 131 | +#endif /* GETDELIM_HEADER_H */ |
0 commit comments