grpc
third_party
cares
cares
src
lib
ares__read_line.c
Go to the documentation of this file.
1
2
/* Copyright 1998 by the Massachusetts Institute of Technology.
3
*
4
* Permission to use, copy, modify, and distribute this
5
* software and its documentation for any purpose and without
6
* fee is hereby granted, provided that the above copyright
7
* notice appear in all copies and that both that copyright
8
* notice and this permission notice appear in supporting
9
* documentation, and that the name of M.I.T. not be used in
10
* advertising or publicity pertaining to distribution of the
11
* software without specific, written prior permission.
12
* M.I.T. makes no representations about the suitability of
13
* this software for any purpose. It is provided "as is"
14
* without express or implied warranty.
15
*/
16
17
#include "
ares_setup.h
"
18
19
#include "
ares.h
"
20
#include "
ares_nowarn.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
*/
31
int
ares__read_line
(
FILE
*fp,
char
**
buf
,
size_t
*bufsize)
32
{
33
char
*newbuf;
34
size_t
offset
= 0;
35
size_t
len
;
36
37
if
(*
buf
== NULL)
38
{
39
*
buf
=
ares_malloc
(128);
40
if
(!*
buf
)
41
return
ARES_ENOMEM
;
42
*bufsize = 128;
43
}
44
45
for
(;;)
46
{
47
int
bytestoread =
aresx_uztosi
(*bufsize -
offset
);
48
49
if
(!fgets(*
buf
+
offset
, bytestoread, fp))
50
return
(
offset
!= 0) ? 0 : (ferror(fp)) ?
ARES_EFILE
:
ARES_EOF
;
51
len
=
offset
+ strlen(*
buf
+
offset
);
52
if
((*
buf
)[
len
- 1] ==
'\n'
)
53
{
54
(*buf)[
len
- 1] = 0;
55
break
;
56
}
57
offset
=
len
;
58
if
(
len
< *bufsize - 1)
59
continue
;
60
61
/* Allocate more space. */
62
newbuf =
ares_realloc
(*
buf
, *bufsize * 2);
63
if
(!newbuf)
64
{
65
ares_free
(*
buf
);
66
*
buf
= NULL;
67
return
ARES_ENOMEM
;
68
}
69
*
buf
= newbuf;
70
*bufsize *= 2;
71
}
72
return
ARES_SUCCESS
;
73
}
ARES_ENOMEM
#define ARES_ENOMEM
Definition:
ares.h:117
ares.h
buf
voidpf void * buf
Definition:
bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
aresx_uztosi
int aresx_uztosi(size_t uznum)
Definition:
ares_nowarn.c:83
ares__read_line
int ares__read_line(FILE *fp, char **buf, size_t *bufsize)
Definition:
ares__read_line.c:31
ares_malloc
void *(* ares_malloc)(size_t size)=default_malloc
Definition:
ares_library_init.c:58
ARES_SUCCESS
#define ARES_SUCCESS
Definition:
ares.h:98
ares_setup.h
ares_realloc
void *(* ares_realloc)(void *ptr, size_t size)=default_realloc
Definition:
ares_library_init.c:59
ARES_EFILE
#define ARES_EFILE
Definition:
ares.h:116
benchmark.FILE
FILE
Definition:
benchmark.py:21
ares_free
void(* ares_free)(void *ptr)=default_free
Definition:
ares_library_init.c:60
ares_private.h
len
int len
Definition:
abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
ARES_EOF
#define ARES_EOF
Definition:
ares.h:115
ares_nowarn.h
offset
voidpf uLong offset
Definition:
bloaty/third_party/zlib/contrib/minizip/ioapi.h:142
grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:43