reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
ares__read_line.c
Go to the documentation of this file.
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
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include "ares.h"
21#include "ares_private.h"
22
23/* This is an internal function. Its contract is to read a line from
24 * a file into a dynamically allocated buffer, zeroing the trailing
25 * newline if there is one. The calling routine may call
26 * ares__read_line multiple times with the same buf and bufsize
27 * pointers; *buf will be reallocated and *bufsize adjusted as
28 * appropriate. The initial value of *buf should be NULL. After the
29 * calling routine is done reading lines, it should free *buf.
30 */
31int ares__read_line(FILE *fp, char **buf, int *bufsize)
32{
33 char *newbuf;
34 int offset = 0, len;
35
36 if (*buf == NULL)
37 {
38 *buf = malloc(128);
39 if (!*buf)
40 return ARES_ENOMEM;
41 *bufsize = 128;
42 }
43
44 while (1)
45 {
46 if (!fgets(*buf + offset, *bufsize - offset, fp))
47 return (offset != 0) ? 0 : (ferror(fp)) ? ARES_EFILE : ARES_EOF;
48 len = offset + (int)strlen(*buf + offset);
49 if ((*buf)[len - 1] == '\n')
50 {
51 (*buf)[len - 1] = 0;
52 return ARES_SUCCESS;
53 }
54 offset = len;
55
56 /* Allocate more space. */
57 newbuf = realloc(*buf, *bufsize * 2);
58 if (!newbuf)
59 return ARES_ENOMEM;
60 *buf = newbuf;
61 *bufsize *= 2;
62 }
63}
#define ARES_SUCCESS
Definition ares.h:79
#define ARES_ENOMEM
Definition ares.h:98
#define ARES_EOF
Definition ares.h:96
#define ARES_EFILE
Definition ares.h:97
int ares__read_line(FILE *fp, char **buf, int *bufsize)
void list()
Definition userAdmin.cxx:66