source: trunk/athena/lib/ares/ares__read_line.c @ 12097

Revision 12097, 2.0 KB checked in by ghudson, 26 years ago (diff)
Fix ares__read_line comment.
Line 
1/* Copyright 1998 by the Massachusetts Institute of Technology.
2 *
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose.  It is provided "as is"
13 * without express or implied warranty.
14 */
15
16static const char rcsid[] = "$Id: ares__read_line.c,v 1.2 1998-11-02 19:13:43 ghudson Exp $";
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include "ares.h"
22#include "ares_private.h"
23
24/* This is an internal function.  Its contract is to read a line from
25 * a file into a dynamically allocated buffer, zeroing the trailing
26 * newline if there is one.  The calling routine may call
27 * ares__read_line multiple times with the same buf and bufsize
28 * pointers; *buf will be reallocated and *bufsize adjusted as
29 * appropriate.  The initial value of *buf should be NULL.  After the
30 * calling routine is done reading lines, it should free *buf.
31 */
32int ares__read_line(FILE *fp, char **buf, int *bufsize)
33{
34  char *newbuf;
35  int offset = 0, len;
36
37  if (*buf == NULL)
38    {
39      *buf = malloc(128);
40      if (!*buf)
41        return ARES_ENOMEM;
42      *bufsize = 128;
43    }
44
45  while (1)
46    {
47      if (!fgets(*buf + offset, *bufsize - offset, fp))
48        return (offset != 0) ? 0 : (ferror(fp)) ? ARES_EFILE : ARES_EOF;
49      len = offset + strlen(*buf + offset);
50      if ((*buf)[len - 1] == '\n')
51        {
52          (*buf)[len - 1] = 0;
53          return ARES_SUCCESS;
54        }
55      offset = len;
56
57      /* Allocate more space. */
58      newbuf = realloc(*buf, *bufsize * 2);
59      if (!newbuf)
60        return ARES_ENOMEM;
61      *buf = newbuf;
62      *bufsize *= 2;
63    }
64}
Note: See TracBrowser for help on using the repository browser.