2N/A/* getdelim.c --- Implementation of replacement getdelim function.
2N/A Copyright (C) 1994, 1996, 1997, 1998, 2001, 2003, 2005, 2006, 2007, 2008,
2N/A 2009, 2010 Free Software Foundation, Inc.
2N/A
2N/A This program is free software; you can redistribute it and/or
2N/A modify it under the terms of the GNU General Public License as
2N/A published by the Free Software Foundation; either version 3, or (at
2N/A your option) any later version.
2N/A
2N/A This program is distributed in the hope that it will be useful, but
2N/A WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2N/A General Public License for more details.
2N/A
2N/A You should have received a copy of the GNU General Public License
2N/A along with this program; if not, write to the Free Software
2N/A Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
2N/A 02110-1301, USA. */
2N/A
2N/A/* Ported from glibc by Simon Josefsson. */
2N/A
2N/A#include <config.h>
2N/A
2N/A/* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
2N/A optimizes away the lineptr == NULL || n == NULL || fp == NULL tests below. */
2N/A#define _GL_ARG_NONNULL(params)
2N/A
2N/A#include <stdio.h>
2N/A
2N/A#include <limits.h>
2N/A#include <stdint.h>
2N/A#include <stdlib.h>
2N/A#include <errno.h>
2N/A
2N/A#ifndef SSIZE_MAX
2N/A# define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
2N/A#endif
2N/A
2N/A#if USE_UNLOCKED_IO
2N/A# include "unlocked-io.h"
2N/A# define getc_maybe_unlocked(fp) getc(fp)
2N/A#elif !HAVE_FLOCKFILE || !HAVE_FUNLOCKFILE || !HAVE_DECL_GETC_UNLOCKED
2N/A# undef flockfile
2N/A# undef funlockfile
2N/A# define flockfile(x) ((void) 0)
2N/A# define funlockfile(x) ((void) 0)
2N/A# define getc_maybe_unlocked(fp) getc(fp)
2N/A#else
2N/A# define getc_maybe_unlocked(fp) getc_unlocked(fp)
2N/A#endif
2N/A
2N/A/* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
2N/A NUL-terminate it). *LINEPTR is a pointer returned from malloc (or
2N/A NULL), pointing to *N characters of space. It is realloc'ed as
2N/A necessary. Returns the number of characters read (not including
2N/A the null terminator), or -1 on error or EOF. */
2N/A
2N/Assize_t
2N/Agetdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
2N/A{
2N/A ssize_t result;
2N/A size_t cur_len = 0;
2N/A
2N/A if (lineptr == NULL || n == NULL || fp == NULL)
2N/A {
2N/A errno = EINVAL;
2N/A return -1;
2N/A }
2N/A
2N/A flockfile (fp);
2N/A
2N/A if (*lineptr == NULL || *n == 0)
2N/A {
2N/A char *new_lineptr;
2N/A *n = 120;
2N/A new_lineptr = (char *) realloc (*lineptr, *n);
2N/A if (new_lineptr == NULL)
2N/A {
2N/A result = -1;
2N/A goto unlock_return;
2N/A }
2N/A *lineptr = new_lineptr;
2N/A }
2N/A
2N/A for (;;)
2N/A {
2N/A int i;
2N/A
2N/A i = getc_maybe_unlocked (fp);
2N/A if (i == EOF)
2N/A {
2N/A result = -1;
2N/A break;
2N/A }
2N/A
2N/A /* Make enough space for len+1 (for final NUL) bytes. */
2N/A if (cur_len + 1 >= *n)
2N/A {
2N/A size_t needed_max =
2N/A SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
2N/A size_t needed = 2 * *n + 1; /* Be generous. */
2N/A char *new_lineptr;
2N/A
2N/A if (needed_max < needed)
2N/A needed = needed_max;
2N/A if (cur_len + 1 >= needed)
2N/A {
2N/A result = -1;
2N/A errno = EOVERFLOW;
2N/A goto unlock_return;
2N/A }
2N/A
2N/A new_lineptr = (char *) realloc (*lineptr, needed);
2N/A if (new_lineptr == NULL)
2N/A {
2N/A result = -1;
2N/A goto unlock_return;
2N/A }
2N/A
2N/A *lineptr = new_lineptr;
2N/A *n = needed;
2N/A }
2N/A
2N/A (*lineptr)[cur_len] = i;
2N/A cur_len++;
2N/A
2N/A if (i == delimiter)
2N/A break;
2N/A }
2N/A (*lineptr)[cur_len] = '\0';
2N/A result = cur_len ? cur_len : result;
2N/A
2N/A unlock_return:
2N/A funlockfile (fp); /* doesn't set errno */
2N/A
2N/A return result;
2N/A}