1.1 --- a/fsaccess/input.c Thu Jan 11 19:24:44 2024 +0100
1.2 +++ b/fsaccess/input.c Thu Jan 11 19:25:29 2024 +0100
1.3 @@ -1,7 +1,7 @@
1.4 /*
1.5 * Elementary input utilities.
1.6 *
1.7 - * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
1.8 + * Copyright (C) 2022, 2024 Paul Boddie <paul@boddie.org.uk>
1.9 *
1.10 * This program is free software; you can redistribute it and/or
1.11 * modify it under the terms of the GNU General Public License as
1.12 @@ -19,74 +19,28 @@
1.13 * Boston, MA 02110-1301, USA
1.14 */
1.15
1.16 +#include <stdio.h>
1.17 #include <string.h>
1.18
1.19 #include "input.h"
1.20
1.21
1.22
1.23 -/* Read a line from a file into the given buffer. */
1.24 -
1.25 -char *read_line(FILE *fp, struct read_line_state *state)
1.26 -{
1.27 - size_t nread;
1.28 -
1.29 - do
1.30 - {
1.31 - do
1.32 - {
1.33 - /* Search for a newline character in any available text. */
1.34 +/* Read a line from a file into the given buffer, removing any newline. */
1.35
1.36 - if (state->end > state->start)
1.37 - {
1.38 - state->eolp = strchr(state->start, '\n');
1.39 -
1.40 - if (state->eolp != NULL)
1.41 - {
1.42 - *(state->eolp) = '\0';
1.43 - return state->eolp;
1.44 - }
1.45 - }
1.46 -
1.47 - /* Obtain more text if necessary. */
1.48 -
1.49 - nread = fread(state->end, sizeof(char), state->remaining, fp);
1.50 -
1.51 - /* Handle end of file condition. */
1.52 +char *read_line(FILE *fp, char *buffer, size_t bufsize)
1.53 +{
1.54 + char *last;
1.55
1.56 - if (!nread)
1.57 - {
1.58 - if (state->end > state->start)
1.59 - return state->end;
1.60 - else
1.61 - return NULL;
1.62 - }
1.63 + if (fgets(buffer, bufsize, fp) == NULL)
1.64 + return NULL;
1.65
1.66 - /* Zero-terminate the string for searching. */
1.67 -
1.68 - *(state->end + nread) = '\0';
1.69 -
1.70 - /* Advance the end of string and subtract remaining space. */
1.71 + last = buffer + strlen(buffer) - 1;
1.72
1.73 - state->end += nread;
1.74 - state->remaining -= nread;
1.75 - }
1.76 - while (state->remaining);
1.77 -
1.78 - /* Copy the remaining text to the start of the buffer. */
1.79 + if ((last >= buffer) && (*last == '\n'))
1.80 + *last = '\0';
1.81
1.82 - if (state->start > state->buffer)
1.83 - {
1.84 - strcpy(state->buffer, state->start);
1.85 -
1.86 - state->end -= (state->start - state->buffer);
1.87 - state->start = state->buffer;
1.88 - state->remaining = state->buffer_size - 1 - (state->end - state->buffer);
1.89 - }
1.90 - }
1.91 - while (state->remaining);
1.92 -
1.93 - return NULL;
1.94 + return buffer;
1.95 }
1.96
1.97 /* Parse the text in the given region, returning details of arguments. */