1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * getline.c Copyright (C) 2003 Alexander Nedotsukov
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 /* GNU libc getline() compatibility */
29 getline(char** line
, size_t* size
, FILE* fp
)
31 static const size_t line_grow_by
= 80; /* in most texts line fits console */
35 if (line
== NULL
|| size
== NULL
|| fp
== NULL
) { /* illegal call */
39 if (*line
== NULL
&& *size
) { /* logically incorrect */
49 /* ensure bufer still large enough for ch and trailing null */
50 if ((*size
- i
) <= 1) {
51 *line
= (char*)realloc(*line
, *size
+= line_grow_by
);
57 *(*line
+ i
++) = (char)ch
;
64 return ferror(fp
) ? -1 : i
;