mirror of
https://gitlab.freedesktop.org/libbsd/libbsd.git
synced 2025-02-03 15:47:17 +01:00
Change fgetln to return the correct length value
Set len to 0 on error conditions to mimmic FreeBSD behaviour, and return the amount of read characters on success, instead of the allocated size by getline. Reported-by: Jief L. <jief1.l@gmail.com>
This commit is contained in:
parent
aefc6f441a
commit
deb9f56ceb
15
src/fgetln.c
15
src/fgetln.c
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2005 Hector Garcia Alvarez
|
* Copyright (C) 2005 Hector Garcia Alvarez
|
||||||
* Copyright (C) 2005, 2008 Guillem Jover
|
* Copyright (C) 2005, 2008, 2009 Guillem Jover
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@ -35,16 +35,17 @@ char *
|
|||||||
fgetln (FILE *stream, size_t *len)
|
fgetln (FILE *stream, size_t *len)
|
||||||
{
|
{
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
|
size_t line_len = 0;
|
||||||
ssize_t nread;
|
ssize_t nread;
|
||||||
|
|
||||||
nread = getline (&line, len, stream);
|
nread = getline(&line, &line_len, stream);
|
||||||
if (nread == -1)
|
if (nread == -1) {
|
||||||
|
*len = 0;
|
||||||
return NULL;
|
return NULL;
|
||||||
|
} else {
|
||||||
/* Get rid of the trailing \0, fgetln does not have it. */
|
*len = (size_t)nread;
|
||||||
(*len)--;
|
|
||||||
|
|
||||||
return line;
|
return line;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user