static size_t read_line()

in src/main.c [178:209]


static size_t read_line(char **buffer, size_t *capacity, FILE *input) {
  char *buf = *buffer;
  size_t cap = *capacity, pos = 0;

  if (cap < MINIMUM_CAPACITY) {
    cap = MINIMUM_CAPACITY;
  } else if (cap > MAXIMUM_CAPACITY) {
    return pos;
  }

  while (1) {
    buf = realloc(buf, cap);
    if (buf == NULL) {
      return pos;
    }
    *buffer = buf;
    *capacity = cap;

    if (fgets(buf + pos, cap - pos, input) == NULL) {
      break;
    }

    pos += strcspn(buf + pos, "\n");
    if (buf[pos] == '\n') {
      break;
    }

    cap *= 2;
  }

  return pos;
}