Cross Reference: getline.c
xref
: /
lxc
/
src
/
include
/
getline.c
Home
History
Annotate
Line#
Navigate
Download
Search
only in
./
getline.c revision 1ba0013f21e1953ae4a59cdf61562fbe843077e9
2880
N/A
#
include
<
sys
/
types.h
>
2880
N/A
#
include
<
stdio.h
>
2880
N/A
#
include
<
stdlib.h
>
2880
N/A
#
include
<
string.h
>
2880
N/A
2880
N/A
/*
2880
N/A
* Emulate glibc getline() via BSD fgetln().
2880
N/A
* Note that outsize is not changed unless memory is allocated.
2880
N/A
*/
2880
N/A
ssize_t
2880
N/A
getline
(
char
**
outbuf
,
size_t
*
outsize
,
FILE
*
fp
)
2880
N/A
{
2880
N/A
size_t
len
;
2880
N/A
char
*
buf
;
2880
N/A
buf
=
fgetln
(
fp
, &
len
);
2880
N/A
2880
N/A
if
(
buf
==
NULL
)
2880
N/A
return
(-
1
);
2880
N/A
2880
N/A
/* Assumes realloc() accepts NULL for ptr (C99) */
2880
N/A
if
(*
outbuf
==
NULL
|| *
outsize
<
len
+
1
) {
2880
N/A
void
*
tmp
=
realloc
(*
outbuf
,
len
+
1
);
6033
N/A
if
(
tmp
==
NULL
)
2880
N/A
return
(-
1
);
4070
N/A
*
outbuf
=
tmp
;
2880
N/A
*
outsize
=
len
+
1
;
2880
N/A
}
2880
N/A
memcpy
(*
outbuf
,
buf
,
len
);
6033
N/A
(*
outbuf
)[
len
] =
'\0'
;
2880
N/A
return
(
len
);
2880
N/A
}
2880
N/A