1/* getlin.c
2   Replacement for getline.
3
4   Copyright (C) 1992 Ian Lance Taylor
5
6   This file is part of Taylor UUCP.
7
8   This library is free software; you can redistribute it and/or
9   modify it under the terms of the GNU Library General Public License
10   as published by the Free Software Foundation; either version 2 of
11   the License, or (at your option) any later version.
12
13   This library is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   Library General Public License for more details.
17
18   You should have received a copy of the GNU Library General Public
19   License along with this library; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
21
22   The author of the program may be contacted at ian@airs.com.
23   */
24
25#include "uucp.h"
26
27/* Read a line from a file, returning the number of characters read.
28   This should really return ssize_t.  Returns -1 on error.  */
29
30#define CGETLINE_DEFAULT (63)
31
32int
33getline (pzline, pcline, e)
34     char **pzline;
35     size_t *pcline;
36     FILE *e;
37{
38  char *zput, *zend;
39  int bchar;
40
41  if (*pzline == NULL)
42    {
43      *pzline = (char *) malloc (CGETLINE_DEFAULT);
44      if (*pzline == NULL)
45	return -1;
46      *pcline = CGETLINE_DEFAULT;
47    }
48
49  zput = *pzline;
50  zend = *pzline + *pcline - 1;
51
52  while ((bchar = getc (e)) != EOF)
53    {
54      if (zput >= zend)
55	{
56	  size_t cnew;
57	  char *znew;
58
59	  cnew = *pcline * 2 + 1;
60	  znew = (char *) realloc ((pointer) *pzline, cnew);
61	  if (znew == NULL)
62	    return -1;
63	  zput = znew + *pcline - 1;
64	  zend = znew + cnew - 1;
65	  *pzline = znew;
66	  *pcline = cnew;
67	}
68
69      *zput++ = bchar;
70
71      if (bchar == '\n')
72	break;
73    }
74
75  if (zput == *pzline)
76    return -1;
77
78  *zput = '\0';
79  return zput - *pzline;
80}
81