1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* getdelim.c --- Implementation of replacement getdelim function.
4 Copyright (C) 1994, 1996, 1997, 1998, 2001, 2003, 2005, 2006, 2007, 2008 Free
5 Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3, or (at
10 your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 /* Ported from glibc by Simon Josefsson. */
33 # define SIZE_MAX ((size_t) -1)
36 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
40 # include "unlocked-io.h"
41 # define getc_maybe_unlocked(fp) getc(fp)
42 #elif !HAVE_FLOCKFILE || !HAVE_FUNLOCKFILE || !HAVE_DECL_GETC_UNLOCKED
45 # define flockfile(x) ((void) 0)
46 # define funlockfile(x) ((void) 0)
47 # define getc_maybe_unlocked(fp) getc(fp)
49 # define getc_maybe_unlocked(fp) getc_unlocked(fp)
52 /* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
53 NUL-terminate it). *LINEPTR is a pointer returned from malloc (or
54 NULL), pointing to *N characters of space. It is realloc'ed as
55 necessary. Returns the number of characters read (not including
56 the null terminator), or -1 on error or EOF. */
59 getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
64 if (lineptr == NULL || n == NULL || fp == NULL)
72 if (*lineptr == NULL || *n == 0)
76 new_lineptr = (char *) realloc (*lineptr, *n);
77 if (new_lineptr == NULL)
82 *lineptr = new_lineptr;
89 i = getc_maybe_unlocked (fp);
96 /* Make enough space for len+1 (for final NUL) bytes. */
97 if (cur_len + 1 >= *n)
100 SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
101 size_t needed = 2 * *n + 1; /* Be generous. */
104 if (needed_max < needed)
106 if (cur_len + 1 >= needed)
113 new_lineptr = (char *) realloc (*lineptr, needed);
114 if (new_lineptr == NULL)
120 *lineptr = new_lineptr;
124 (*lineptr)[cur_len] = i;
130 (*lineptr)[cur_len] = '\0';
131 result = cur_len ? cur_len : result;
134 funlockfile (fp); /* doesn't set errno */