leafnode  1.12.0
About: Leafnode is a store & forward NNTP proxy for small (dialup) sites.
  Fossies Dox: leafnode-1.12.0.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

getline.c
Go to the documentation of this file.
1/** getline.c - fetch a single line from a stdio stream, arbitrary length.
2 * Copyright (C) 2000 - 2002 Matthias Andree <matthias.andree@gmx.de>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or(at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16#undef _GNU_SOURCE
17#include <stdio.h>
18#include <sys/types.h>
19#include <string.h>
20#include "leafnode.h"
21#include "getline.h"
22#include "critmem.h"
23#ifdef WITH_DMALLOC
24#include <dmalloc.h>
25#endif
26
27ssize_t
28_getline(char *to, size_t size, FILE * stream)
29{
30 ssize_t i = 0;
31 int c;
32
33 if (1 > size)
34 return -1;
35
36 while (--size > 0 && ((c = getc(stream)) != EOF)) {
37 to[i++] = c;
38 if (c == '\n')
39 break;
40 }
41 to[i] = '\0';
42 if (ferror(stream))
43 return -1;
44 return i;
45}
46
47/** Clean-room reimplementation of getline, API compatible with glibc2.
48 * \return
49 * - >= 0 count of characters read
50 * - -1 for EOF/error
51 */
52ssize_t getline(char **pto, size_t * size, FILE * stream);
53ssize_t
54getline(char **pto, size_t * size, FILE * stream)
55{
56 ssize_t i = 0, cur = 0, off = 0;
57
58 if (!(*size) && !(*pto)) {
59 *size = 256;
60 *pto = critmalloc(*size, "fgetline");
61 }
62 while ((cur = _getline(*pto + off, *size - off, stream)) > 0) {
63 i += cur;
64 if ((*pto)[i - 1] == '\n')
65 return i;
66 off = i;
67 *size += *size;
68 *pto = critrealloc(*pto, *size, "fgetline");
69 }
70 if (!cur && i)
71 return i;
72 return -1;
73}
char * critrealloc(char *a, size_t size, const char *message)
Definition: critmem.c:79
char * critmalloc(size_t size, const char *message)
Definition: critmem.c:61
ssize_t _getline(char *to, size_t size, FILE *stream)
Definition: getline.c:28
ssize_t getline(char **pto, size_t *size, FILE *stream)
Definition: getline.c:54