2006-10-24 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / common / xreadline.c
blob7e091e9ddbe1f6448c19f2aea77a0a15e9981bed
1 /* xreadline.c - fgets replacement function
2 * Copyright (C) 1999, 2004 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <errno.h>
27 #include "util.h"
30 /* Same as fgets() but if the provided buffer is too short a larger
31 one will be allocated. This is similar to getline. A line is
32 considered a byte stream ending in a LF.
34 If MAX_LENGTH is not NULL, it shall point to a value with the
35 maximum allowed allocation.
37 Returns the length of the line. EOF is indicated by a line of
38 length zero. A truncated line is indicated my setting the value at
39 MAX_LENGTH to 0. If the returned value is less then 0 not enough
40 memory was enable and ERRNO is set accordingly.
42 If a line has been truncated, the file pointer is moved forward to
43 the end of the line so that the next read starts with the next
44 line. Note that MAX_LENGTH must be re-initialzied in this case.
46 Note: The returned buffer is allocated with enough extra space to
47 append a CR,LF,Nul
49 ssize_t
50 read_line (FILE *fp,
51 char **addr_of_buffer, size_t *length_of_buffer,
52 size_t *max_length)
54 int c;
55 char *buffer = *addr_of_buffer;
56 size_t length = *length_of_buffer;
57 size_t nbytes = 0;
58 size_t maxlen = max_length? *max_length : 0;
59 char *p;
61 if (!buffer)
62 { /* No buffer given - allocate a new one. */
63 length = 256;
64 buffer = xtrymalloc (length);
65 *addr_of_buffer = buffer;
66 if (!buffer)
68 *length_of_buffer = 0;
69 if (max_length)
70 *max_length = 0;
71 return -1;
73 *length_of_buffer = length;
76 length -= 3; /* Reserve 3 bytes for CR,LF,EOL. */
77 p = buffer;
78 while ((c = getc (fp)) != EOF)
80 if (nbytes == length)
81 { /* Enlarge the buffer. */
82 if (maxlen && length > maxlen) /* But not beyond our limit. */
84 /* Skip the rest of the line. */
85 while (c != '\n' && (c=getc (fp)) != EOF)
87 *p++ = '\n'; /* Always append a LF (we reserved some space). */
88 nbytes++;
89 if (max_length)
90 *max_length = 0; /* Indicate truncation. */
91 break; /* the while loop. */
93 length += 3; /* Adjust for the reserved bytes. */
94 length += length < 1024? 256 : 1024;
95 *addr_of_buffer = xtryrealloc (buffer, length);
96 if (!*addr_of_buffer)
98 int save_errno = errno;
99 xfree (buffer);
100 *length_of_buffer = *max_length = 0;
101 errno = save_errno;
102 return -1;
104 buffer = *addr_of_buffer;
105 *length_of_buffer = length;
106 length -= 3;
107 p = buffer + nbytes;
109 *p++ = c;
110 nbytes++;
111 if (c == '\n')
112 break;
114 *p = 0; /* Make sure the line is a string. */
116 return nbytes;