2006-07-22 17:37:10 +02:00
|
|
|
/* $Id$ */
|
2006-07-22 17:21:13 +02:00
|
|
|
|
2003-10-07 23:54:04 +02:00
|
|
|
/* Copyright 1998 by the Massachusetts Institute of Technology.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose and without
|
|
|
|
* fee is hereby granted, provided that the above copyright
|
|
|
|
* notice appear in all copies and that both that copyright
|
|
|
|
* notice and this permission notice appear in supporting
|
|
|
|
* documentation, and that the name of M.I.T. not be used in
|
|
|
|
* advertising or publicity pertaining to distribution of the
|
|
|
|
* software without specific, written prior permission.
|
|
|
|
* M.I.T. makes no representations about the suitability of
|
|
|
|
* this software for any purpose. It is provided "as is"
|
|
|
|
* without express or implied warranty.
|
|
|
|
*/
|
|
|
|
|
2009-11-02 12:55:53 +01:00
|
|
|
#include "ares_setup.h"
|
2003-10-07 23:54:04 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "ares.h"
|
|
|
|
#include "ares_private.h"
|
|
|
|
|
|
|
|
/* This is an internal function. Its contract is to read a line from
|
|
|
|
* a file into a dynamically allocated buffer, zeroing the trailing
|
|
|
|
* newline if there is one. The calling routine may call
|
|
|
|
* ares__read_line multiple times with the same buf and bufsize
|
|
|
|
* pointers; *buf will be reallocated and *bufsize adjusted as
|
|
|
|
* appropriate. The initial value of *buf should be NULL. After the
|
|
|
|
* calling routine is done reading lines, it should free *buf.
|
|
|
|
*/
|
2009-10-08 20:38:29 +02:00
|
|
|
int ares__read_line(FILE *fp, char **buf, size_t *bufsize)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
|
|
|
char *newbuf;
|
2004-02-26 13:45:29 +01:00
|
|
|
size_t offset = 0;
|
2004-02-23 08:46:59 +01:00
|
|
|
size_t len;
|
2003-10-07 23:54:04 +02:00
|
|
|
|
|
|
|
if (*buf == NULL)
|
|
|
|
{
|
|
|
|
*buf = malloc(128);
|
|
|
|
if (!*buf)
|
2004-10-06 09:50:18 +02:00
|
|
|
return ARES_ENOMEM;
|
2003-10-07 23:54:04 +02:00
|
|
|
*bufsize = 128;
|
|
|
|
}
|
|
|
|
|
2009-11-10 19:41:03 +01:00
|
|
|
for (;;)
|
2003-10-07 23:54:04 +02:00
|
|
|
{
|
2009-10-09 16:06:38 +02:00
|
|
|
if (!fgets(*buf + offset, (int)(*bufsize - offset), fp))
|
2004-10-06 09:50:18 +02:00
|
|
|
return (offset != 0) ? 0 : (ferror(fp)) ? ARES_EFILE : ARES_EOF;
|
2003-10-07 23:54:04 +02:00
|
|
|
len = offset + strlen(*buf + offset);
|
|
|
|
if ((*buf)[len - 1] == '\n')
|
2004-10-06 09:50:18 +02:00
|
|
|
{
|
|
|
|
(*buf)[len - 1] = 0;
|
2005-12-20 21:58:51 +01:00
|
|
|
break;
|
2004-10-06 09:50:18 +02:00
|
|
|
}
|
2003-10-07 23:54:04 +02:00
|
|
|
offset = len;
|
|
|
|
|
|
|
|
/* Allocate more space. */
|
|
|
|
newbuf = realloc(*buf, *bufsize * 2);
|
|
|
|
if (!newbuf)
|
2004-10-06 09:50:18 +02:00
|
|
|
return ARES_ENOMEM;
|
2003-10-07 23:54:04 +02:00
|
|
|
*buf = newbuf;
|
|
|
|
*bufsize *= 2;
|
|
|
|
}
|
2005-12-20 21:58:51 +01:00
|
|
|
return ARES_SUCCESS;
|
2003-10-07 23:54:04 +02:00
|
|
|
}
|