2004-06-14 08:32:07 +00:00
|
|
|
/* xreadline.c - fgets replacement function
|
|
|
|
* Copyright (C) 1999, 2004 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of either
|
2004-06-14 08:32:07 +00:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* - the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or both in parallel, as here.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
2004-06-14 08:32:07 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-04 19:49:40 +00:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2004-06-14 08:32:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Same as fgets() but if the provided buffer is too short a larger
|
|
|
|
one will be allocated. This is similar to getline. A line is
|
|
|
|
considered a byte stream ending in a LF.
|
|
|
|
|
|
|
|
If MAX_LENGTH is not NULL, it shall point to a value with the
|
2011-02-04 12:57:53 +01:00
|
|
|
maximum allowed allocation.
|
2004-06-14 08:32:07 +00:00
|
|
|
|
|
|
|
Returns the length of the line. EOF is indicated by a line of
|
2007-10-19 14:51:39 +00:00
|
|
|
length zero. A truncated line is indicated by setting the value at
|
2004-06-14 08:32:07 +00:00
|
|
|
MAX_LENGTH to 0. If the returned value is less then 0 not enough
|
|
|
|
memory was enable and ERRNO is set accordingly.
|
|
|
|
|
|
|
|
If a line has been truncated, the file pointer is moved forward to
|
2006-08-14 14:40:07 +00:00
|
|
|
the end of the line so that the next read starts with the next
|
|
|
|
line. Note that MAX_LENGTH must be re-initialzied in this case.
|
2004-06-14 08:32:07 +00:00
|
|
|
|
|
|
|
Note: The returned buffer is allocated with enough extra space to
|
|
|
|
append a CR,LF,Nul
|
|
|
|
*/
|
|
|
|
ssize_t
|
2011-02-04 12:57:53 +01:00
|
|
|
read_line (FILE *fp,
|
2004-06-14 08:32:07 +00:00
|
|
|
char **addr_of_buffer, size_t *length_of_buffer,
|
|
|
|
size_t *max_length)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
char *buffer = *addr_of_buffer;
|
|
|
|
size_t length = *length_of_buffer;
|
|
|
|
size_t nbytes = 0;
|
|
|
|
size_t maxlen = max_length? *max_length : 0;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
{ /* No buffer given - allocate a new one. */
|
|
|
|
length = 256;
|
|
|
|
buffer = xtrymalloc (length);
|
|
|
|
*addr_of_buffer = buffer;
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
*length_of_buffer = 0;
|
|
|
|
if (max_length)
|
|
|
|
*max_length = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*length_of_buffer = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
length -= 3; /* Reserve 3 bytes for CR,LF,EOL. */
|
|
|
|
p = buffer;
|
|
|
|
while ((c = getc (fp)) != EOF)
|
|
|
|
{
|
|
|
|
if (nbytes == length)
|
|
|
|
{ /* Enlarge the buffer. */
|
|
|
|
if (maxlen && length > maxlen) /* But not beyond our limit. */
|
|
|
|
{
|
|
|
|
/* Skip the rest of the line. */
|
|
|
|
while (c != '\n' && (c=getc (fp)) != EOF)
|
|
|
|
;
|
|
|
|
*p++ = '\n'; /* Always append a LF (we reserved some space). */
|
|
|
|
nbytes++;
|
|
|
|
if (max_length)
|
|
|
|
*max_length = 0; /* Indicate truncation. */
|
|
|
|
break; /* the while loop. */
|
|
|
|
}
|
|
|
|
length += 3; /* Adjust for the reserved bytes. */
|
|
|
|
length += length < 1024? 256 : 1024;
|
|
|
|
*addr_of_buffer = xtryrealloc (buffer, length);
|
|
|
|
if (!*addr_of_buffer)
|
|
|
|
{
|
|
|
|
int save_errno = errno;
|
2011-02-04 12:57:53 +01:00
|
|
|
xfree (buffer);
|
2009-06-24 14:03:09 +00:00
|
|
|
*length_of_buffer = 0;
|
|
|
|
if (max_length)
|
|
|
|
*max_length = 0;
|
2010-03-02 21:25:08 +00:00
|
|
|
gpg_err_set_errno (save_errno);
|
2004-06-14 08:32:07 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buffer = *addr_of_buffer;
|
|
|
|
*length_of_buffer = length;
|
2011-02-04 12:57:53 +01:00
|
|
|
length -= 3;
|
2004-06-14 08:32:07 +00:00
|
|
|
p = buffer + nbytes;
|
|
|
|
}
|
|
|
|
*p++ = c;
|
|
|
|
nbytes++;
|
|
|
|
if (c == '\n')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*p = 0; /* Make sure the line is a string. */
|
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|