Blame ares__read_line.c

Packit 514978
Packit 514978
/* Copyright 1998 by the Massachusetts Institute of Technology.
Packit 514978
 *
Packit 514978
 * Permission to use, copy, modify, and distribute this
Packit 514978
 * software and its documentation for any purpose and without
Packit 514978
 * fee is hereby granted, provided that the above copyright
Packit 514978
 * notice appear in all copies and that both that copyright
Packit 514978
 * notice and this permission notice appear in supporting
Packit 514978
 * documentation, and that the name of M.I.T. not be used in
Packit 514978
 * advertising or publicity pertaining to distribution of the
Packit 514978
 * software without specific, written prior permission.
Packit 514978
 * M.I.T. makes no representations about the suitability of
Packit 514978
 * this software for any purpose.  It is provided "as is"
Packit 514978
 * without express or implied warranty.
Packit 514978
 */
Packit 514978
Packit 514978
#include "ares_setup.h"
Packit 514978
Packit 514978
#include "ares.h"
Packit 514978
#include "ares_nowarn.h"
Packit 514978
#include "ares_private.h"
Packit 514978
Packit 514978
/* This is an internal function.  Its contract is to read a line from
Packit 514978
 * a file into a dynamically allocated buffer, zeroing the trailing
Packit 514978
 * newline if there is one.  The calling routine may call
Packit 514978
 * ares__read_line multiple times with the same buf and bufsize
Packit 514978
 * pointers; *buf will be reallocated and *bufsize adjusted as
Packit 514978
 * appropriate.  The initial value of *buf should be NULL.  After the
Packit 514978
 * calling routine is done reading lines, it should free *buf.
Packit 514978
 */
Packit 514978
int ares__read_line(FILE *fp, char **buf, size_t *bufsize)
Packit 514978
{
Packit 514978
  char *newbuf;
Packit 514978
  size_t offset = 0;
Packit 514978
  size_t len;
Packit 514978
Packit 514978
  if (*buf == NULL)
Packit 514978
    {
Packit 514978
      *buf = ares_malloc(128);
Packit 514978
      if (!*buf)
Packit 514978
        return ARES_ENOMEM;
Packit 514978
      *bufsize = 128;
Packit 514978
    }
Packit 514978
Packit 514978
  for (;;)
Packit 514978
    {
Packit 514978
      int bytestoread = aresx_uztosi(*bufsize - offset);
Packit 514978
Packit 514978
      if (!fgets(*buf + offset, bytestoread, fp))
Packit 514978
        return (offset != 0) ? 0 : (ferror(fp)) ? ARES_EFILE : ARES_EOF;
Packit 514978
      len = offset + strlen(*buf + offset);
Packit 514978
      if ((*buf)[len - 1] == '\n')
Packit 514978
        {
Packit 514978
          (*buf)[len - 1] = 0;
Packit 514978
          break;
Packit 514978
        }
Packit 514978
      offset = len;
Packit 514978
      if(len < *bufsize - 1)
Packit 514978
        continue;
Packit 514978
Packit 514978
      /* Allocate more space. */
Packit 514978
      newbuf = ares_realloc(*buf, *bufsize * 2);
Packit 514978
      if (!newbuf)
Packit 514978
        {
Packit 514978
          ares_free(*buf);
Packit 514978
          *buf = NULL;
Packit 514978
          return ARES_ENOMEM;
Packit 514978
        }
Packit 514978
      *buf = newbuf;
Packit 514978
      *bufsize *= 2;
Packit 514978
    }
Packit 514978
  return ARES_SUCCESS;
Packit 514978
}